use std::collections::HashMap; use chrono::{SubsecRound, Utc}; use laboratory::{expect, SpecContext}; use sql_builder::{quote, SqlBuilder}; use sylvia_iot_broker::models::Model; use super::{super::common::device_route as common_test, TestState, STATE}; const TABLE_NAME: &'static str = "device_route"; const FIELDS: &'static [&'static str] = &[ "route_id", "unit_id", "unit_code", "application_id", "application_code", "device_id", "network_id", "network_code", "network_addr", "profile", "created_at", "modified_at", ]; pub fn after_each_fn(state: &mut HashMap<&'static str, TestState>) -> () { let state = state.get_mut(STATE).unwrap(); let runtime = state.runtime.as_ref().unwrap(); let conn = state.sqlite.as_ref().unwrap().get_connection(); let sql = SqlBuilder::delete_from(TABLE_NAME).sql().unwrap(); let _ = runtime.block_on(async { sqlx::query(sql.as_str()).execute(conn).await }); } /// Test table initialization. pub fn init(context: &mut SpecContext) -> Result<(), String> { let state = context.state.borrow(); let state = state.get(STATE).unwrap(); let runtime = state.runtime.as_ref().unwrap(); let model = state.sqlite.as_ref().unwrap().device_route(); let result = runtime.block_on(async { model.init().await }); expect(result.is_ok()).to_equal(true) } /// Test `get()`. pub fn get(context: &mut SpecContext) -> Result<(), String> { let state = context.state.borrow(); let state = state.get(STATE).unwrap(); let runtime = state.runtime.as_ref().unwrap(); let conn = state.sqlite.as_ref().unwrap().get_connection(); let model = state.sqlite.as_ref().unwrap().device_route(); let now = Utc::now().trunc_subsecs(3); let sql = match SqlBuilder::insert_into(TABLE_NAME) .fields(FIELDS) .values(&vec![ quote("route_id_get"), quote("unit_id_get"), quote("unit_code_get"), quote("application_id_get"), quote("application_code_get"), quote("device_id_get"), quote("network_id_get"), quote("network_code_get"), quote("network_addr_get"), quote("profile_get"), now.timestamp_millis().to_string(), now.timestamp_millis().to_string(), ]) .sql() { Err(e) => return Err(format!("sql() error: {}", e.to_string())), Ok(sql) => sql, }; if let Err(e) = runtime.block_on(async { sqlx::query(&sql).execute(conn).await }) { return Err(format!("insert_into() error: {}", e.to_string())); } match runtime.block_on(async { model.get("route_id_not_exist").await }) { Err(e) => return Err(format!("model.get() error: {}", e)), Ok(route) => match route { None => (), Some(_) => return Err(format!("should not get not-exist one")), }, }; let route = match runtime.block_on(async { model.get("route_id_get").await }) { Err(e) => return Err(format!("model.get() error: {}", e)), Ok(route) => match route { None => return Err("should get one".to_string()), Some(route) => route, }, }; expect(route.route_id).to_equal("route_id_get".to_string())?; expect(route.unit_id).to_equal("unit_id_get".to_string())?; expect(route.unit_code).to_equal("unit_code_get".to_string())?; expect(route.application_id).to_equal("application_id_get".to_string())?; expect(route.application_code).to_equal("application_code_get".to_string())?; expect(route.device_id).to_equal("device_id_get".to_string())?; expect(route.network_id).to_equal("network_id_get".to_string())?; expect(route.network_code).to_equal("network_code_get".to_string())?; expect(route.network_addr).to_equal("network_addr_get".to_string())?; expect(route.profile).to_equal("profile_get".to_string())?; expect(route.created_at).to_equal(now)?; expect(route.modified_at).to_equal(now) } /// Test `add()`. pub fn add(context: &mut SpecContext) -> Result<(), String> { let state = context.state.borrow(); let state = state.get(STATE).unwrap(); let runtime = state.runtime.as_ref().unwrap(); let model = state.sqlite.as_ref().unwrap().device_route(); common_test::add(runtime, model) } /// Test `add()` with duplicate key. pub fn add_dup(context: &mut SpecContext) -> Result<(), String> { let state = context.state.borrow(); let state = state.get(STATE).unwrap(); let runtime = state.runtime.as_ref().unwrap(); let model = state.sqlite.as_ref().unwrap().device_route(); common_test::add_dup(runtime, model) } /// Test `add_bulk()`. pub fn add_bulk(context: &mut SpecContext) -> Result<(), String> { let state = context.state.borrow(); let state = state.get(STATE).unwrap(); let runtime = state.runtime.as_ref().unwrap(); let model = state.sqlite.as_ref().unwrap().device_route(); common_test::add_bulk(runtime, model) } /// Test `del()` by specifying a route ID. pub fn del_by_route_id(context: &mut SpecContext) -> Result<(), String> { let state = context.state.borrow(); let state = state.get(STATE).unwrap(); let runtime = state.runtime.as_ref().unwrap(); let model = state.sqlite.as_ref().unwrap().device_route(); common_test::del_by_route_id(runtime, model) } /// Test `del()` twice. pub fn del_twice(context: &mut SpecContext) -> Result<(), String> { let state = context.state.borrow(); let state = state.get(STATE).unwrap(); let runtime = state.runtime.as_ref().unwrap(); let model = state.sqlite.as_ref().unwrap().device_route(); common_test::del_twice(runtime, model) } /// Test `del()` by specifying a unit ID. pub fn del_by_unit_id(context: &mut SpecContext) -> Result<(), String> { let state = context.state.borrow(); let state = state.get(STATE).unwrap(); let runtime = state.runtime.as_ref().unwrap(); let model = state.sqlite.as_ref().unwrap().device_route(); common_test::del_by_unit_id(runtime, model) } /// Test `del()` by specifying a pair of unit ID and route ID. pub fn del_by_unit_route(context: &mut SpecContext) -> Result<(), String> { let state = context.state.borrow(); let state = state.get(STATE).unwrap(); let runtime = state.runtime.as_ref().unwrap(); let model = state.sqlite.as_ref().unwrap().device_route(); common_test::del_by_unit_route(runtime, model) } /// Test `del()` by specifying an application ID. pub fn del_by_application_id(context: &mut SpecContext) -> Result<(), String> { let state = context.state.borrow(); let state = state.get(STATE).unwrap(); let runtime = state.runtime.as_ref().unwrap(); let model = state.sqlite.as_ref().unwrap().device_route(); common_test::del_by_application_id(runtime, model) } /// Test `del()` by specifying a network ID. pub fn del_by_network_id(context: &mut SpecContext) -> Result<(), String> { let state = context.state.borrow(); let state = state.get(STATE).unwrap(); let runtime = state.runtime.as_ref().unwrap(); let model = state.sqlite.as_ref().unwrap().device_route(); common_test::del_by_network_id(runtime, model) } /// Test `del()` by specifying a device ID. pub fn del_by_device_id(context: &mut SpecContext) -> Result<(), String> { let state = context.state.borrow(); let state = state.get(STATE).unwrap(); let runtime = state.runtime.as_ref().unwrap(); let model = state.sqlite.as_ref().unwrap().device_route(); common_test::del_by_device_id(runtime, model) } /// Test `del()` by specifying network addresses. pub fn del_by_network_addrs(context: &mut SpecContext) -> Result<(), String> { let state = context.state.borrow(); let state = state.get(STATE).unwrap(); let runtime = state.runtime.as_ref().unwrap(); let model = state.sqlite.as_ref().unwrap().device_route(); common_test::del_by_network_addrs(runtime, model) } /// Test `update()`. pub fn update(context: &mut SpecContext) -> Result<(), String> { let state = context.state.borrow(); let state = state.get(STATE).unwrap(); let runtime = state.runtime.as_ref().unwrap(); let model = state.sqlite.as_ref().unwrap().device_route(); common_test::update(runtime, model) } /// Test `update()` with a non-exist condition. pub fn update_not_exist(context: &mut SpecContext) -> Result<(), String> { let state = context.state.borrow(); let state = state.get(STATE).unwrap(); let runtime = state.runtime.as_ref().unwrap(); let model = state.sqlite.as_ref().unwrap().device_route(); common_test::update_not_exist(runtime, model) } /// Test `update()` with invalid update content. pub fn update_invalid(context: &mut SpecContext) -> Result<(), String> { let state = context.state.borrow(); let state = state.get(STATE).unwrap(); let runtime = state.runtime.as_ref().unwrap(); let model = state.sqlite.as_ref().unwrap().device_route(); common_test::update_invalid(runtime, model) } /// Test `count()`. pub fn count(context: &mut SpecContext) -> Result<(), String> { let state = context.state.borrow(); let state = state.get(STATE).unwrap(); let runtime = state.runtime.as_ref().unwrap(); let model = state.sqlite.as_ref().unwrap().device_route(); common_test::count(runtime, model) } /// Test `list()`. pub fn list(context: &mut SpecContext) -> Result<(), String> { let state = context.state.borrow(); let state = state.get(STATE).unwrap(); let runtime = state.runtime.as_ref().unwrap(); let model = state.sqlite.as_ref().unwrap().device_route(); common_test::list(runtime, model) } /// Test `list()` with sorting. pub fn list_sort(context: &mut SpecContext) -> Result<(), String> { let state = context.state.borrow(); let state = state.get(STATE).unwrap(); let runtime = state.runtime.as_ref().unwrap(); let model = state.sqlite.as_ref().unwrap().device_route(); common_test::list_sort(runtime, model) } /// Test `list()` with offset/limit. pub fn list_offset_limit(context: &mut SpecContext) -> Result<(), String> { let state = context.state.borrow(); let state = state.get(STATE).unwrap(); let runtime = state.runtime.as_ref().unwrap(); let model = state.sqlite.as_ref().unwrap().device_route(); common_test::list_offset_limit(runtime, model) } /// Test `list()` with cursors. pub fn list_cursor(context: &mut SpecContext) -> Result<(), String> { let state = context.state.borrow(); let state = state.get(STATE).unwrap(); let runtime = state.runtime.as_ref().unwrap(); let model = state.sqlite.as_ref().unwrap().device_route(); common_test::list_cursor(runtime, model) }