Skip to content

Commit

Permalink
Temp mock push
Browse files Browse the repository at this point in the history
  • Loading branch information
williamwoldum committed Nov 20, 2023
1 parent 4bb7a4c commit 25f4aff
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 35 deletions.
1 change: 0 additions & 1 deletion src/api/ecdar_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,6 @@ impl EcdarApiAuth for ConcreteEcdarApi {

/// Implementation of the EcdarBackend trait, which is used to ensure backwards compatability with the Reveaal engine.
#[tonic::async_trait]
#[automock]
impl EcdarBackend for ConcreteEcdarApi {
async fn get_user_token(
&self,
Expand Down
1 change: 1 addition & 0 deletions src/api/reveaal_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::api::server::server::{
SimulationStepResponse, UserTokenResponse,
};
use async_trait::async_trait;
use mockall::automock;

Check warning on line 8 in src/api/reveaal_context.rs

View workflow job for this annotation

GitHub Actions / Clippy lint and check

unused import: `mockall::automock`

warning: unused import: `mockall::automock` --> src/api/reveaal_context.rs:8:5 | 8 | use mockall::automock; | ^^^^^^^^^^^^^^^^^

Check warning on line 8 in src/api/reveaal_context.rs

View workflow job for this annotation

GitHub Actions / Clippy lint and check

unused import: `mockall::automock`

warning: unused import: `mockall::automock` --> src/api/reveaal_context.rs:8:5 | 8 | use mockall::automock; | ^^^^^^^^^^^^^^^^^
use std::env;
use tonic::transport::Channel;
use tonic::{Request, Response, Status};
Expand Down
2 changes: 1 addition & 1 deletion src/database/user_context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::database::database_context::DatabaseContextTrait;
use crate::database::entity_context::EntityContextTrait;
use crate::entities::user;
use mockall::automock;
use mockall::{automock, mock};

Check warning on line 4 in src/database/user_context.rs

View workflow job for this annotation

GitHub Actions / Clippy lint and check

unused imports: `automock`, `mock`

warning: unused imports: `automock`, `mock` --> src/database/user_context.rs:4:15 | 4 | use mockall::{automock, mock}; | ^^^^^^^^ ^^^^
use sea_orm::prelude::async_trait::async_trait;
use sea_orm::ActiveValue::{Set, Unchanged};
use sea_orm::{ActiveModelTrait, ColumnTrait, DbErr, EntityTrait, QueryFilter};
Expand Down
50 changes: 34 additions & 16 deletions src/tests/api/ecdar_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,27 @@ mod ecdar_api {
api::server::server::ecdar_api_server::EcdarApi, entities::session::Model as Session,
entities::user::Model as User,
};
use mockall::{mock, predicate};
use std::str::FromStr;
use std::sync::Arc;
use async_trait::async_trait;
use futures::SinkExt;

use crate::api::server::server::ecdar_backend_server::EcdarBackend;
use crate::tests::api::helpers::get_reset_concrete_ecdar_api;
use crate::database::entity_context::EntityContextTrait;
use crate::database::user_context::MockUserContextTrait;
use crate::tests::api::helpers::get_mock_concrete_ecdar_api;
use tonic::{metadata, Request};

#[tokio::test]
async fn delete_user_nonexistent_user_returns_err() {
let api = get_reset_concrete_ecdar_api().await;
mock! {
C {}
#[async_trait]
impl EntityContextTrait<user::Model> for C {
async fn delete(&self, entity_id: i32) -> Result<user::Model, DbErr>;
}
}

let mut delete_request = Request::new({});

Expand All @@ -29,14 +40,21 @@ mod ecdar_api {
.metadata_mut()
.insert("uid", metadata::MetadataValue::from_str("1").unwrap());


let mut mock_user_context = MockC::new();

mock_user_context.expect_delete().

let api = get_mock_concrete_ecdar_api(Arc::new(mock_user_context)).await;

Check failure on line 48 in src/tests/api/ecdar_api.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

expected identifier, found keyword `let`

Check failure on line 48 in src/tests/api/ecdar_api.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

expected one of `(`, `.`, `::`, `;`, `?`, `}`, or an operator, found `api`

let delete_response = api.delete_user(delete_request).await;

assert!(delete_response.is_err());
}

#[tokio::test]
async fn delete_user_existing_user_returns_ok() {
let api = get_reset_concrete_ecdar_api().await;
let api = get_mock_concrete_ecdar_api().await;

let _ = api
.user_context
Expand All @@ -63,7 +81,7 @@ mod ecdar_api {

#[tokio::test]
async fn create_user_nonexistent_user_returns_ok() {
let api = get_reset_concrete_ecdar_api().await;
let api = get_mock_concrete_ecdar_api().await;

let create_user_request = Request::new(CreateUserRequest {
email: "[email protected]".to_string(),
Expand All @@ -77,7 +95,7 @@ mod ecdar_api {

#[tokio::test]
async fn create_user_nonexistent_user_inserts_user() {
let api = get_reset_concrete_ecdar_api().await;
let api = get_mock_concrete_ecdar_api().await;

let username = "newuser".to_string();

Expand All @@ -99,7 +117,7 @@ mod ecdar_api {

#[tokio::test]
async fn test_create_user_duplicate_email_returns_error() {
let api = get_reset_concrete_ecdar_api().await;
let api = get_mock_concrete_ecdar_api().await;

let _ = api
.user_context
Expand All @@ -123,7 +141,7 @@ mod ecdar_api {

#[tokio::test]
async fn test_create_user_invalid_email_returns_error() {
let api = get_reset_concrete_ecdar_api().await;
let api = get_mock_concrete_ecdar_api().await;

let create_user_request = Request::new(CreateUserRequest {
email: "invalid-email".to_string(),
Expand All @@ -137,7 +155,7 @@ mod ecdar_api {

#[tokio::test]
async fn test_create_user_duplicate_username_returns_error() {
let api = get_reset_concrete_ecdar_api().await;
let api = get_mock_concrete_ecdar_api().await;

let _ = api
.user_context
Expand All @@ -161,7 +179,7 @@ mod ecdar_api {

#[tokio::test]
async fn test_create_user_invalid_username_returns_error() {
let api = get_reset_concrete_ecdar_api().await;
let api = get_mock_concrete_ecdar_api().await;

let create_user_request = Request::new(CreateUserRequest {
email: "[email protected]".to_string(),
Expand All @@ -175,7 +193,7 @@ mod ecdar_api {

#[tokio::test]
async fn test_create_user_valid_request_returns_ok() {
let api = get_reset_concrete_ecdar_api().await;
let api = get_mock_concrete_ecdar_api().await;

let create_user_request = Request::new(CreateUserRequest {
email: "[email protected]".to_string(),
Expand All @@ -189,7 +207,7 @@ mod ecdar_api {

#[tokio::test]
async fn update_user_returns_ok() {
let api = get_reset_concrete_ecdar_api().await;
let api = get_mock_concrete_ecdar_api().await;

let user = User {
id: 1,
Expand Down Expand Up @@ -217,7 +235,7 @@ mod ecdar_api {

#[tokio::test]
async fn update_user_non_existant_user_returns_err() {
let api = get_reset_concrete_ecdar_api().await;
let api = get_mock_concrete_ecdar_api().await;

let mut update_user_request = Request::new(UpdateUserRequest {
email: Some("new_test@test".to_string()),
Expand All @@ -236,7 +254,7 @@ mod ecdar_api {

#[tokio::test]
async fn update_user_single_field_returns_ok() {
let api = get_reset_concrete_ecdar_api().await;
let api = get_mock_concrete_ecdar_api().await;

let user = User {
id: 1,
Expand Down Expand Up @@ -264,7 +282,7 @@ mod ecdar_api {

#[tokio::test]
async fn handle_session_updated_session_contains_correct_fields() {
let api = get_reset_concrete_ecdar_api().await;
let api = get_mock_concrete_ecdar_api().await;

let mut get_auth_token_request = Request::new(GetAuthTokenRequest {
user_credentials: Some(UserCredentials {
Expand Down Expand Up @@ -334,7 +352,7 @@ mod ecdar_api {

#[tokio::test]
async fn handle_session_no_session_exists_creates_session() {
let api = get_reset_concrete_ecdar_api().await;
let api = get_mock_concrete_ecdar_api().await;

let mut get_auth_token_request = Request::new(GetAuthTokenRequest {
user_credentials: Some(UserCredentials {
Expand Down Expand Up @@ -375,7 +393,7 @@ mod ecdar_api {

#[tokio::test]
async fn handle_session_update_non_existing_session_returns_err() {
let api = get_reset_concrete_ecdar_api(Arc::new(MockEcdarBackend)).await;
let api = get_mock_concrete_ecdar_api(Arc::new(MockEcdarBackend)).await;

let mut get_auth_token_request = Request::new(GetAuthTokenRequest {
user_credentials: Some(UserCredentials {
Expand Down
28 changes: 11 additions & 17 deletions src/tests/api/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#![cfg(test)]

use crate::api::ecdar_api::ConcreteEcdarApi;
use crate::api::reveaal_context::MockReveaalContext;

Check failure on line 4 in src/tests/api/helpers.rs

View workflow job for this annotation

GitHub Actions / Clippy lint and check

unresolved import `crate::api::reveaal_context::MockReveaalContext`

error[E0432]: unresolved import `crate::api::reveaal_context::MockReveaalContext` --> src/tests/api/helpers.rs:4:5 | 4 | use crate::api::reveaal_context::MockReveaalContext; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------------------ | | | | | help: a similar name exists in the module: `ReveaalContext` | no `MockReveaalContext` in `api::reveaal_context`
use crate::api::server::server::ecdar_backend_server::EcdarBackend;
use crate::api::server::server::{
QueryRequest, QueryResponse, SimulationStartRequest, SimulationStepRequest,
SimulationStepResponse, UserTokenResponse,
};
use crate::database::access_context::AccessContext;
use crate::database::database_context::{
DatabaseContextTrait, PostgresDatabaseContext, SQLiteDatabaseContext,
Expand All @@ -15,35 +12,32 @@ use crate::database::in_use_context::InUseContext;
use crate::database::model_context::ModelContext;
use crate::database::query_context::QueryContext;
use crate::database::session_context::SessionContext;
use crate::database::user_context::UserContext;
use dotenv::dotenv;
use crate::database::user_context::{MockUserContextTrait, UserContext};
use sea_orm::{ConnectionTrait, Database, DbBackend};
use std::env;
use std::sync::Arc;
use tonic::{Request, Response, Status};

pub async fn get_reset_concrete_ecdar_api(
mock_ecdar_backend: Arc<dyn EcdarBackend>,
pub async fn get_mock_concrete_ecdar_api(
mock_user_context: Arc<MockUserContextTrait>,
) -> ConcreteEcdarApi {
dotenv().ok();

let url = env::var("TEST_DATABASE_URL").expect("TEST_DATABASE_URL must be set to run tests.");
let db = Database::connect(&url).await.unwrap();
let url = env::var("DATABASE_URL").expect("Expected DATABASE_URL to be set.");
let db = Database::connect(&url).await?;
let db_context: Arc<dyn DatabaseContextTrait> = match db.get_database_backend() {
DbBackend::Sqlite => Arc::new(SQLiteDatabaseContext::new(&url).await.unwrap()),
DbBackend::Postgres => Arc::new(PostgresDatabaseContext::new(&url).await.unwrap()),
DbBackend::Sqlite => Arc::new(SQLiteDatabaseContext::new(&url).await?),
DbBackend::Postgres => Arc::new(PostgresDatabaseContext::new(&url).await?),
_ => panic!("Database protocol not supported"),
};

db_context.reset().await.unwrap();
let user_context = mock_user_context.clone();

let model_context = Arc::new(ModelContext::new(db_context.clone()));
let user_context = Arc::new(UserContext::new(db_context.clone()));
let access_context = Arc::new(AccessContext::new(db_context.clone()));
let query_context = Arc::new(QueryContext::new(db_context.clone()));
let session_context = Arc::new(SessionContext::new(db_context.clone()));
let in_use_context = Arc::new(InUseContext::new(db_context.clone()));

let mock_ecdar_backend = Arc::new(MockReveaalContext::new());

ConcreteEcdarApi::new(
model_context,
user_context,
Expand Down

0 comments on commit 25f4aff

Please sign in to comment.