-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(todos): better examples and ussage of core module
- Loading branch information
Showing
8 changed files
with
159 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use async_trait::async_trait; | ||
|
||
use super::model::{Todo as TodoModel, TodoRequest}; | ||
use crate::core::db; | ||
use crate::core::db::model::DatabaseModel; | ||
use crate::core::factory::ModelFactory; | ||
use fake::{faker::internet, Fake}; | ||
|
||
pub struct Todo; | ||
|
||
#[async_trait] | ||
impl ModelFactory<TodoModel, TodoRequest> for Todo { | ||
fn build() -> TodoRequest { | ||
TodoRequest { | ||
description: "".to_string(), | ||
done: false, | ||
} | ||
} | ||
|
||
async fn create( | ||
request: Option<TodoRequest>, | ||
conn: db::PgPool, | ||
) -> Result<TodoModel, db::Error> { | ||
let obj_build = request.unwrap_or_else(Self::build); | ||
TodoModel::create(obj_build, &conn).await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod model; | ||
pub mod factory; | ||
pub mod model; | ||
mod routes; | ||
|
||
pub use model::*; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
extern crate planet_express as src; | ||
|
||
mod common; | ||
mod todos; | ||
mod users; | ||
|
||
use actix_web::test; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use super::super::common; | ||
use actix_web::http; | ||
use actix_web::test; | ||
use src::user::model::User; | ||
use src::user::routes::AuthenticationResponse; | ||
|
||
#[actix_rt::test] | ||
async fn test_auth_viewer_create() { | ||
let (mut srv, db_conn, test_name) = common::setup().await; | ||
let obj = planet_express::user::UserFactory::build(); | ||
let req = test::TestRequest::post() | ||
.uri("/v1/viewer/create") | ||
.set_json(&obj) | ||
.to_request(); | ||
let res: AuthenticationResponse = test::read_response_json(&mut srv, req).await; | ||
assert_eq!(res.user.email, obj.email); | ||
|
||
// Test user exists error | ||
let req = test::TestRequest::post() | ||
.uri("/v1/viewer/create") | ||
.set_json(&obj) | ||
.to_request(); | ||
|
||
// Test user already exists | ||
let mut res = test::call_service(&mut srv, req).await; | ||
|
||
assert_eq!(res.status(), http::StatusCode::CONFLICT); | ||
common::teardown(db_conn, test_name).await; | ||
} | ||
|
||
#[actix_rt::test] | ||
async fn test_auth_viewer_authenticate() { | ||
let (mut srv, db_conn, test_name) = common::setup().await; | ||
let obj = planet_express::user::UserFactory::create(db_conn.clone()).await; | ||
let req = test::TestRequest::post() | ||
.uri("/v1/viewer/authenticate") | ||
.set_json(&obj) | ||
.to_request(); | ||
|
||
let res: AuthenticationResponse = test::read_response_json(&mut srv, req).await; | ||
assert_eq!(res.user.email, obj.email.clone()); | ||
|
||
let req = test::TestRequest::get() | ||
.uri("/v1/viewer") | ||
.header("Authorization", format!("Bearer {}", res.token)) | ||
.to_request(); | ||
let res: User = test::read_response_json(&mut srv, req).await; | ||
assert_eq!(res.email, obj.email.clone()); | ||
common::teardown(db_conn, test_name).await; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod model; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use super::super::common; | ||
use src::core::db::model::DatabaseModel; | ||
use src::core::factory::ModelFactory; | ||
use src::todo::factory; | ||
use src::todo::model; | ||
|
||
#[actix_rt::test] | ||
async fn test_model_create() { | ||
let (_srv, db_conn, test_name) = common::setup().await; | ||
let obj = factory::Todo::build(); | ||
|
||
let record = model::Todo::create(obj.clone(), &db_conn).await.unwrap(); | ||
|
||
assert_eq!(obj.description, record.description); | ||
common::teardown(db_conn, test_name).await; | ||
} | ||
|
||
#[actix_rt::test] | ||
async fn test_model_get_by_id() { | ||
let (_srv, db_conn, test_name) = common::setup().await; | ||
let obj = factory::Todo::create(None, db_conn.clone()).await.unwrap(); | ||
let record = model::Todo::get(obj.id, &db_conn).await.unwrap(); | ||
|
||
assert_eq!(obj.description, record.description); | ||
common::teardown(db_conn, test_name).await; | ||
} |