Skip to content

Commit

Permalink
add draft for media api test
Browse files Browse the repository at this point in the history
  • Loading branch information
thebino committed Aug 17, 2023
1 parent 33ec6bb commit 62b124c
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 65 deletions.
61 changes: 26 additions & 35 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ anyhow = { version = "1.0.72" }

chrono = { version = "0.4.26", features = ["serde"] }

hyper = { version = "0.14", features = ["full"] }

mockall = { version = "0.11.4" }

rstest = { version = "0.18.1" }
Expand All @@ -58,6 +60,7 @@ serde = "1.0.183"
serde_json = { version = "1.0.104", features = ["raw_value"] }

tokio = { version = "1.30.0", features = ["full"] }
tower = { version = "0.4.13", features = ["util"] }
tower-http = { version = "0.4.3", features = ["fs", "tracing", "trace", "cors"] }
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.17", features = ["registry", "fmt", "std", "json"] }
Expand Down Expand Up @@ -110,13 +113,13 @@ tracing-appender.workspace = true
futures = "0.3.25"
futures-channel = "0.3.25"
futures-util = "0.3.25"
rumqttc = "0.21.0"

rumqttc = "0.22.0"

tokio.workspace = true
tokio-stream = { version = "0.1.11", features = ["net"] }
tokio-util = { version = "0.7.4", features = ["rt"] }

tower = { version = "0.4.13", features = ["util"] }
tower-http.workspace = true


Expand Down
6 changes: 5 additions & 1 deletion crates/media/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ doctest = false
[dependencies]
# serialization
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }

# Router
axum = { workspace = true }
tower-http.workspace = true
tower-http = { workspace = true }

# persistency
sea-orm = { workspace = true }
Expand All @@ -30,3 +31,6 @@ log = "0.4.19"
# testing
mockall = { workspace = true }
rstest = { workspace = true }
tokio = { workspace = true }
tower = { workspace = true, features = ["util"] }
hyper = { workspace = true, features = ["full"] }
37 changes: 37 additions & 0 deletions crates/media/src/api/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,40 @@ impl MediaApi {
.layer(tower_http::trace::TraceLayer::new_for_http())
}
}

#[cfg(test)]
mod tests {
use std::sync::Arc;

use crate::repository::{MediaRepositoryState, MediaRepository};

use super::*;
use axum::{body::Body, http::{Request, StatusCode}};
use rstest::rstest;

#[rstest]
#[case("/?name=Wonder")]
#[tokio::test]
async fn get_media_success(#[case] uri: &'static str) {
// given
let repo: MediaRepositoryState = Arc::new(MediaRepository::new().await);
let api: Router<MediaRepositoryState> = MediaApi::routes().with_state(repo);

// when
/*
TODO: find replacement for `oneshot`
let response = api::oneshot(
Request::builder()
.uri("/media")
.method("GET")
.body(Body::empty())
.unwrap()
).await.unwrap;
let body = hyper::body::to_bytes(response.into_body()).await.unwrap();
let body = serde_json::from_slice(&body).unwrap();
// then
assert_eq!(response.status(), StatusCode::NOT_FOUND);
*/
}
}
62 changes: 35 additions & 27 deletions crates/media/src/repository/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

use std::sync::Arc;

use axum::async_trait;
use mockall::predicate::*;
use sea_orm::DatabaseConnection;
Expand All @@ -23,11 +25,13 @@ use crate::data::error::DataAccessError;
use crate::data::media_item::MediaItem;
use crate::data::open_db_conn;

struct MediaRepository {
pub struct MediaRepository {
db_url: &'static str,

Check warning on line 29 in crates/media/src/repository/mod.rs

View workflow job for this annotation

GitHub Actions / Test Suite

fields `db_url` and `db` are never read

Check warning on line 29 in crates/media/src/repository/mod.rs

View workflow job for this annotation

GitHub Actions / Check

fields `db_url` and `db` are never read
db: DatabaseConnection,
}

pub(crate) type MediaRepositoryState = Arc<MediaRepository>;

Check warning on line 33 in crates/media/src/repository/mod.rs

View workflow job for this annotation

GitHub Actions / Test Suite

type alias `MediaRepositoryState` is never used

Check warning on line 33 in crates/media/src/repository/mod.rs

View workflow job for this annotation

GitHub Actions / Check

type alias `MediaRepositoryState` is never used

/// MockPhotosRepositoryTrait is created by automock macro
#[cfg_attr(test, mockall::automock)]
#[async_trait]
Expand All @@ -39,8 +43,12 @@ trait MediaRepositoryTrait {
}

impl MediaRepository {
// async fn new(&self) {
// }
pub(crate) async fn new() -> Self {

Check warning on line 46 in crates/media/src/repository/mod.rs

View workflow job for this annotation

GitHub Actions / Test Suite

associated function `new` is never used

Check warning on line 46 in crates/media/src/repository/mod.rs

View workflow job for this annotation

GitHub Actions / Check

associated function `new` is never used
Self {
db_url: "",
db: DatabaseConnection::Disconnected
}
}
}

#[async_trait]
Expand Down Expand Up @@ -73,34 +81,34 @@ mod tests {
let schema = Schema::new(DbBackend::Sqlite);

// Derive from Entity
let stmt: TableCreateStatement = schema.create_table_from_entity(MyEntity);
// let stmt: TableCreateStatement = schema.create_table_from_entity(MyEntity);

// Or setup manually
assert_eq!(
stmt.build(SqliteQueryBuilder),
Table::create()
.table(MyEntity)
.col(
ColumnDef::new(MyEntity::Column::Id)
.integer()
.not_null()
)
//...
.build(SqliteQueryBuilder)
);
// assert_eq!(
// stmt.build(SqliteQueryBuilder),
// Table::create()
// .table(MyEntity)
// .col(
// ColumnDef::new(MyEntity::Column::Id)
// .integer()
// .not_null()
// )
// //...
// .build(SqliteQueryBuilder)
// );

// Execute create table statement
let result = db
.execute(db.get_database_backend().build(&stmt))
.await;
// // Execute create table statement
// let result = db
// .execute(db.get_database_backend().build(&stmt))
// .await;
}

#[rstest]
#[case("/?name=Wonder", "Wonder%")] // Verify that % is appended to the filter
async fn get_media_items_for_user_success(#[case] uri: &'static str, #[case] expected_filter: &'static str) {
// #[rstest]
// #[case("/?name=Wonder", "Wonder%")] // Verify that % is appended to the filter
// async fn get_media_items_for_user_success(#[case] uri: &'static str, #[case] expected_filter: &'static str) {

let mut repo_mock = MockMediaRepositoryTrait::new("sqlite::memory:");
setup_schema(&db).await?;
testcase(&db).await?;
}
// let mut repo_mock = MockMediaRepositoryTrait::new("sqlite::memory:");
// setup_schema(&db).await?;
// testcase(&db).await?;
// }
}

0 comments on commit 62b124c

Please sign in to comment.