Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
williamwoldum committed Nov 19, 2023
1 parent 6ade12f commit b8d8571
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 20 deletions.
9 changes: 7 additions & 2 deletions migration/src/m20231012_094242_create_query_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ impl MigrationTrait for Migration {
)
.col(ColumnDef::new(Query::String).string().not_null())
.col(ColumnDef::new(Query::Result).json())
.col(ColumnDef::new(Query::Outdated).boolean().not_null().default(true))
.col(
ColumnDef::new(Query::Outdated)
.boolean()
.not_null()
.default(true),
)
.col(ColumnDef::new(Query::ModelId).integer().not_null())
.foreign_key(
ForeignKey::create()
Expand All @@ -49,5 +54,5 @@ enum Query {
String,
Result,
ModelId,
Outdated
Outdated,
}
1 change: 0 additions & 1 deletion src/api/ecdar_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ async fn handle_session(

session.access_token = access_token.clone();
session.refresh_token = refresh_token.clone();
session.updated_at = Local::now().naive_local();

match session_context.update(session).await {
Ok(_) => (),
Expand Down
4 changes: 2 additions & 2 deletions src/database/session_context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use chrono::Utc;
use chrono::{Local, Utc};
use sea_orm::prelude::async_trait::async_trait;
use sea_orm::ActiveValue::{Set, Unchanged};
use sea_orm::{ActiveModelTrait, ColumnTrait, DbErr, EntityTrait, QueryFilter};
Expand Down Expand Up @@ -118,7 +118,7 @@ impl EntityContextTrait<session::Model> for SessionContext {
refresh_token: Set(entity.refresh_token),
access_token: Set(entity.access_token),
user_id: Unchanged(entity.user_id),
updated_at: Set(Default::default()),
updated_at: Set(Local::now().naive_local()),
}
.update(&self.db_context.get_connection())
.await
Expand Down
22 changes: 12 additions & 10 deletions src/tests/api/ecdar_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ mod ecdar_api {
use std::str::FromStr;

use crate::tests::api::helpers::get_reset_concrete_ecdar_api;
use chrono::Local;
use tonic::{metadata, Request};

#[tokio::test]
Expand Down Expand Up @@ -292,7 +291,7 @@ mod ecdar_api {
id: Default::default(),
refresh_token: "test_refresh_token".to_string(),
access_token: "test_access_token".to_string(),
updated_at: Local::now().naive_local(),
updated_at: Default::default(),
user_id: 1,
})
.await
Expand All @@ -318,12 +317,15 @@ mod ecdar_api {
};

let updated_session = api.session_context.get_by_id(1).await.unwrap().unwrap();
assert!(dbg!(updated_session != old_session));
assert!(dbg!(updated_session.refresh_token) == dbg!(expected_session.refresh_token));
assert!(dbg!(updated_session.access_token) == dbg!(expected_session.access_token));
assert!(dbg!(updated_session.updated_at) > dbg!(old_session.updated_at));
assert!(dbg!(updated_session.user_id) == dbg!(expected_session.user_id));
assert!(dbg!(updated_session.id) == dbg!(expected_session.id));
assert_ne!(updated_session, old_session);
assert_eq!(
updated_session.refresh_token,
expected_session.refresh_token
);
assert_eq!(updated_session.access_token, expected_session.access_token);
assert!(updated_session.updated_at > old_session.updated_at);
assert_eq!(updated_session.user_id, expected_session.user_id);
assert_eq!(updated_session.id, expected_session.id);
}

#[tokio::test]
Expand Down Expand Up @@ -394,7 +396,7 @@ mod ecdar_api {

api.user_context.create(user.clone()).await.unwrap();

assert!(dbg!(handle_session(
assert!(handle_session(
api.session_context.clone(),
&get_auth_token_request,
is_new_session,
Expand All @@ -403,6 +405,6 @@ mod ecdar_api {
user.id.to_string(),
)
.await
.is_err()));
.is_err());
}
}
40 changes: 35 additions & 5 deletions src/tests/database/session_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod database_tests {
to_active_models,
};

use crate::database::session_context::SessionContextTrait;
use chrono::{Duration, Utc};

async fn seed_db() -> (SessionContext, session::Model, user::Model, model::Model) {
Expand Down Expand Up @@ -187,16 +188,19 @@ mod database_tests {
.unwrap();

//A session has nothing to update
let new_session = session::Model { ..session };
let mut new_session = session::Model { ..session };

let updated_session = session_context.update(new_session.clone()).await.unwrap();
let mut updated_session = session_context.update(new_session.clone()).await.unwrap();

let fetched_session = session::Entity::find_by_id(updated_session.id)
.one(&session_context.db_context.get_connection())
.await
.unwrap()
.unwrap();

new_session.updated_at = fetched_session.updated_at;
updated_session.updated_at = fetched_session.updated_at;

assert_eq!(new_session, updated_session);
assert_eq!(updated_session, fetched_session);
}
Expand All @@ -219,8 +223,8 @@ mod database_tests {
}

#[tokio::test]
async fn update_does_not_modify_updated_at_test() {
let (session_context, session, _, _) = seed_db().await;
async fn update_does_modifies_updated_at_automatically_test() {
let (session_context, mut session, _, _) = seed_db().await;
session::Entity::insert(session.clone().into_active_model())
.exec(&session_context.db_context.get_connection())
.await
Expand All @@ -235,12 +239,16 @@ mod database_tests {
.await
.unwrap();

assert!(session.updated_at < res.updated_at);

session.updated_at = res.updated_at;

assert_eq!(session, res);
}

#[tokio::test]
async fn update_does_not_modify_user_id_test() {
let (session_context, session, _, _) = seed_db().await;
let (session_context, mut session, _, _) = seed_db().await;
session::Entity::insert(session.clone().into_active_model())
.exec(&session_context.db_context.get_connection())
.await
Expand All @@ -255,6 +263,8 @@ mod database_tests {
.await
.unwrap();

session.updated_at = res.updated_at;

assert_eq!(session, res);
}

Expand Down Expand Up @@ -331,4 +341,24 @@ mod database_tests {
DbErr::RecordNotFound(_)
));
}

#[tokio::test]
async fn get_by_refresh_token_test() {
let (session_context, session, _, _) = seed_db().await;

session::Entity::insert(session.clone().into_active_model())
.exec(&session_context.db_context.get_connection())
.await
.unwrap();

let fetched_session = session_context
.get_by_refresh_token(session.refresh_token.clone())
.await
.unwrap();

assert_eq!(
fetched_session.unwrap().refresh_token,
session.refresh_token
);
}
}
34 changes: 34 additions & 0 deletions src/tests/database/user_context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(test)]
mod database_tests {
use crate::database::user_context::UserContextTrait;
use crate::tests::database::helpers::*;
use crate::{
database::{
Expand Down Expand Up @@ -500,4 +501,37 @@ mod database_tests {
DbErr::RecordNotFound(_)
));
}

#[tokio::test]
async fn get_by_username_test() {
let (user_context, user) = seed_db().await;

user::Entity::insert(user.clone().into_active_model())
.exec(&user_context.db_context.get_connection())
.await
.unwrap();

// Fetches the user created using the 'get_by_username' function
let fetched_user = user_context
.get_by_username(user.username.clone())
.await
.unwrap();

// Assert if the fetched user is the same as the created user
assert_eq!(fetched_user.unwrap().username, user.username);
}

#[tokio::test]
async fn get_by_email_test() {
let (user_context, user) = seed_db().await;

user::Entity::insert(user.clone().into_active_model())
.exec(&user_context.db_context.get_connection())
.await
.unwrap();

let fetched_user = user_context.get_by_email(user.email.clone()).await.unwrap();

assert_eq!(fetched_user.unwrap().email, user.email);
}
}

0 comments on commit b8d8571

Please sign in to comment.