Skip to content

Commit

Permalink
Refactor area model
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Aug 16, 2024
1 parent 03bbbaf commit 18d3762
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 84 deletions.
8 changes: 4 additions & 4 deletions src/area/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ mod test {
#[test]
async fn patch_should_return_401_if_unauthorized() -> Result<()> {
let state = mock_state().await;
state.area_repo.insert(&Map::new()).await?;
Area::insert(&Map::new(), &state.conn)?;
let app = test::init_service(
App::new()
.app_data(Data::new(state.pool))
Expand All @@ -221,7 +221,7 @@ mod test {
let url_alias = "test";
let mut tags = Map::new();
tags.insert("url_alias".into(), Value::String(url_alias.into()));
state.area_repo.insert(&tags).await?;
Area::insert(&tags, &state.conn)?;
let app = test::init_service(
App::new()
.app_data(Data::new(state.pool))
Expand Down Expand Up @@ -265,7 +265,7 @@ mod test {
let url_alias = "test";
let mut tags = Map::new();
tags.insert("url_alias".into(), Value::String(url_alias.into()));
state.area_repo.insert(&tags).await?;
Area::insert(&tags, &state.conn)?;
let app = test::init_service(
App::new()
.app_data(Data::new(state.pool))
Expand All @@ -291,7 +291,7 @@ mod test {
let mut tags = Map::new();
tags.insert("url_alias".into(), Value::String(url_alias.into()));
tags.insert("geo_json".into(), phuket_geo_json());
state.area_repo.insert(&tags).await?;
Area::insert(&tags, &state.conn)?;

let area_element = state
.element_repo
Expand Down
67 changes: 14 additions & 53 deletions src/area/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,6 @@ impl AreaRepo {
Self { pool: pool.clone() }
}

#[cfg(test)]
pub async fn insert(&self, tags: &Map<String, Value>) -> Result<Area> {
let tags = tags.clone();
self.pool
.get()
.await?
.interact(move |conn| Area::insert(&tags, conn))
.await?
}

pub async fn select_all(&self, limit: Option<i64>) -> Result<Vec<Area>> {
self.pool
.get()
Expand Down Expand Up @@ -75,16 +65,6 @@ impl AreaRepo {
.await?
}

#[cfg(test)]
pub async fn patch_tags(&self, id: i64, tags: &Map<String, Value>) -> Result<Area> {
let tags = tags.clone();
self.pool
.get()
.await?
.interact(move |conn| Area::_patch_tags(id, &tags, conn))
.await?
}

#[cfg(test)]
pub async fn set_updated_at(&self, id: i64, updated_at: &OffsetDateTime) -> Result<Area> {
let updated_at = updated_at.clone();
Expand All @@ -94,19 +74,6 @@ impl AreaRepo {
.interact(move |conn| Area::_set_updated_at(id, &updated_at, conn))
.await?
}

#[cfg(test)]
pub async fn set_deleted_at(
&self,
id: i64,
deleted_at: Option<OffsetDateTime>,
) -> Result<Area> {
self.pool
.get()
.await?
.interact(move |conn| Area::_set_deleted_at(id, deleted_at, conn))
.await?
}
}

const TABLE: &str = "area";
Expand Down Expand Up @@ -447,7 +414,7 @@ impl Area {
mod test {
use crate::{
area::Area,
test::{mock_state, mock_tags},
test::{mock_conn, mock_state, mock_tags},
Result,
};
use serde_json::{json, Map};
Expand Down Expand Up @@ -508,23 +475,20 @@ mod test {

#[test]
async fn select_by_id() -> Result<()> {
let state = mock_state().await;
let area = state.area_repo.insert(&Map::new()).await?;
assert_eq!(area, state.area_repo.select_by_id(area.id).await?.unwrap());
let conn = mock_conn();
let area = Area::insert(&Map::new(), &conn)?;
assert_eq!(area, Area::select_by_id(area.id, &conn)?.unwrap());
Ok(())
}

#[test]
async fn select_by_url_alias() -> Result<()> {
let state = mock_state().await;
let conn = mock_conn();
let url_alias = json!("url_alias_value");
let mut tags = Map::new();
tags.insert("url_alias".into(), url_alias.clone());
state.area_repo.insert(&tags).await?;
let area = state
.area_repo
.select_by_url_alias(url_alias.as_str().unwrap())
.await?;
Area::insert(&tags, &conn)?;
let area = Area::select_by_url_alias(url_alias.as_str().unwrap(), &conn)?;
assert!(area.is_some());
let area = area.unwrap();
assert_eq!(url_alias, area.tags["url_alias"]);
Expand All @@ -533,32 +497,29 @@ mod test {

#[test]
async fn patch_tags() -> Result<()> {
let state = mock_state().await;
let conn = mock_conn();
let tag_1_name = "tag_1_name";
let tag_1_value = json!("tag_1_value");
let tag_2_name = "tag_2_name";
let tag_2_value = json!("tag_2_value");
let mut tags = Map::new();
tags.insert(tag_1_name.into(), tag_1_value.clone());
let area = state.area_repo.insert(&tags).await?;
let area = Area::insert(&tags, &conn)?;
assert_eq!(tag_1_value, area.tags[tag_1_name]);
tags.insert(tag_2_name.into(), tag_2_value.clone());
let area = state.area_repo.patch_tags(area.id, &tags).await?;
let area = Area::_patch_tags(area.id, &tags, &conn)?;
assert_eq!(tag_1_value, area.tags[tag_1_name]);
assert_eq!(tag_2_value, area.tags[tag_2_name]);
Ok(())
}

#[test]
async fn set_deleted_at() -> Result<()> {
let state = mock_state().await;
let area = state.area_repo.insert(&Map::new()).await?;
let area = state
.area_repo
.set_deleted_at(area.id, Some(OffsetDateTime::now_utc()))
.await?;
let conn = mock_conn();
let area = Area::insert(&Map::new(), &conn)?;
let area = Area::_set_deleted_at(area.id, Some(OffsetDateTime::now_utc()), &conn)?;
assert!(area.deleted_at.is_some());
let area = state.area_repo.set_deleted_at(area.id, None).await?;
let area = Area::_set_deleted_at(area.id, None, &conn)?;
assert!(area.deleted_at.is_none());
Ok(())
}
Expand Down
10 changes: 5 additions & 5 deletions src/area/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ mod tests {
let state = mock_state().await;
let mut tags = Map::new();
tags.insert("url_alias".into(), "test".into());
state.area_repo.insert(&tags).await?;
Area::insert(&tags, &state.conn)?;
let app = test::init_service(
App::new()
.app_data(Data::new(AreaRepo::new(&state.pool)))
Expand All @@ -143,9 +143,9 @@ mod tests {
let state = mock_state().await;
let mut tags = Map::new();
tags.insert("url_alias".into(), "test".into());
state.area_repo.insert(&tags).await?;
state.area_repo.insert(&tags).await?;
state.area_repo.insert(&tags).await?;
Area::insert(&tags, &state.conn)?;
Area::insert(&tags, &state.conn)?;
Area::insert(&tags, &state.conn)?;
let app = test::init_service(
App::new()
.app_data(Data::new(AreaRepo::new(&state.pool)))
Expand All @@ -164,7 +164,7 @@ mod tests {
let area_url_alias = "test";
let mut tags = Map::new();
tags.insert("url_alias".into(), Value::String(area_url_alias.into()));
state.area_repo.insert(&tags).await?;
Area::insert(&tags, &state.conn)?;
let app = test::init_service(
App::new()
.app_data(Data::new(AreaRepo::new(&state.pool)))
Expand Down
13 changes: 7 additions & 6 deletions src/area/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub async fn get_by_id(id: Path<String>, pool: Data<Arc<Pool>>) -> Result<Json<G

#[cfg(test)]
mod test {
use crate::area::Area;
use crate::element::ElementRepo;
use crate::error::{self, ApiError};
use crate::test::mock_state;
Expand Down Expand Up @@ -147,7 +148,7 @@ mod test {
#[test]
async fn get_not_empty_array() -> Result<()> {
let state = mock_state().await;
let area = state.area_repo.insert(&Map::new()).await?;
let area = Area::insert(&Map::new(), &state.conn)?;
let app = test::init_service(
App::new()
.app_data(Data::new(state.area_repo))
Expand All @@ -165,9 +166,9 @@ mod test {
#[test]
async fn get_with_limit() -> Result<()> {
let state = mock_state().await;
let area_1 = state.area_repo.insert(&Map::new()).await?;
let area_2 = state.area_repo.insert(&Map::new()).await?;
let _area_3 = state.area_repo.insert(&Map::new()).await?;
let area_1 = Area::insert(&Map::new(), &state.conn)?;
let area_2 = Area::insert(&Map::new(), &state.conn)?;
let _area_3 = Area::insert(&Map::new(), &state.conn)?;
let app = test::init_service(
App::new()
.app_data(Data::new(state.area_repo))
Expand All @@ -185,12 +186,12 @@ mod test {
#[test]
async fn get_updated_since() -> Result<()> {
let state = mock_state().await;
let area_1 = state.area_repo.insert(&Map::new()).await?;
let area_1 = Area::insert(&Map::new(), &state.conn)?;
state
.area_repo
.set_updated_at(area_1.id, &datetime!(2022-01-05 00:00 UTC))
.await?;
let area_2 = state.area_repo.insert(&Map::new()).await?;
let area_2 = Area::insert(&Map::new(), &state.conn)?;
let area_2 = state
.area_repo
.set_updated_at(area_2.id, &datetime!(2022-02-05 00:00 UTC))
Expand Down
2 changes: 1 addition & 1 deletion src/command/generate_reports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ mod test {
let state = mock_state().await;
let mut area_tags = Map::new();
area_tags.insert("url_alias".into(), json!("test"));
state.area_repo.insert(&area_tags).await?;
Area::insert(&area_tags, &state.conn)?;
for _ in 1..100 {
state
.report_repo
Expand Down
18 changes: 9 additions & 9 deletions src/report/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ const fn mapper() -> fn(&Row) -> rusqlite::Result<Report> {

#[cfg(test)]
mod test {
use crate::{test::mock_state, Result};
use crate::{area::Area, test::mock_state, Result};
use serde_json::Map;
use std::ops::Add;
use time::{macros::datetime, Duration, OffsetDateTime};
Expand All @@ -415,7 +415,7 @@ mod test {
let state = mock_state().await;
let mut area_tags = Map::new();
area_tags.insert("url_alias".into(), "test".into());
state.area_repo.insert(&area_tags).await?;
Area::insert(&area_tags, &state.conn)?;
state
.report_repo
.insert(1, &OffsetDateTime::now_utc().date(), &Map::new())
Expand All @@ -433,7 +433,7 @@ mod test {
let state = mock_state().await;
let mut area_tags = Map::new();
area_tags.insert("url_alias".into(), "test".into());
state.area_repo.insert(&area_tags).await?;
Area::insert(&area_tags, &state.conn)?;
state
.report_repo
.insert(1, &OffsetDateTime::now_utc().date(), &Map::new())
Expand All @@ -459,7 +459,7 @@ mod test {
let state = mock_state().await;
let mut area_tags = Map::new();
area_tags.insert("url_alias".into(), "test".into());
state.area_repo.insert(&area_tags).await?;
Area::insert(&area_tags, &state.conn)?;
let report_1 = state
.report_repo
.insert(1, &OffsetDateTime::now_utc().date(), &Map::new())
Expand Down Expand Up @@ -500,7 +500,7 @@ mod test {
let state = mock_state().await;
let mut area_tags = Map::new();
area_tags.insert("url_alias".into(), "test".into());
state.area_repo.insert(&area_tags).await?;
Area::insert(&area_tags, &state.conn)?;
state
.report_repo
.insert(1, &OffsetDateTime::now_utc().date(), &Map::new())
Expand All @@ -514,7 +514,7 @@ mod test {
let state = mock_state().await;
let mut area_tags = Map::new();
area_tags.insert("url_alias".into(), "test".into());
let area = state.area_repo.insert(&area_tags).await?;
let area = Area::insert(&area_tags, &state.conn)?;
state
.report_repo
.insert(
Expand Down Expand Up @@ -543,7 +543,7 @@ mod test {
let state = mock_state().await;
let mut area_tags = Map::new();
area_tags.insert("url_alias".into(), "test".into());
state.area_repo.insert(&area_tags).await?;
Area::insert(&area_tags, &state.conn)?;
let tag_1_name = "foo";
let tag_1_value = "bar";
let tag_2_name = "qwerty";
Expand Down Expand Up @@ -571,7 +571,7 @@ mod test {
let state = mock_state().await;
let mut area_tags = Map::new();
area_tags.insert("url_alias".into(), "test".into());
state.area_repo.insert(&area_tags).await?;
Area::insert(&area_tags, &state.conn)?;
let report = state
.report_repo
.insert(1, &OffsetDateTime::now_utc().date(), &Map::new())
Expand Down Expand Up @@ -599,7 +599,7 @@ mod test {
let state = mock_state().await;
let mut area_tags = Map::new();
area_tags.insert("url_alias".into(), "test".into());
state.area_repo.insert(&area_tags).await?;
Area::insert(&area_tags, &state.conn)?;
let report = state
.report_repo
.insert(1, &OffsetDateTime::now_utc().date(), &Map::new())
Expand Down
7 changes: 4 additions & 3 deletions src/report/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ pub async fn get_by_id(id: Path<i64>, repo: Data<ReportRepo>) -> Result<Json<Get

#[cfg(test)]
mod test {
use crate::area::Area;
use crate::report::v2::GetItem;
use crate::test::mock_state;
use crate::Result;
Expand Down Expand Up @@ -190,7 +191,7 @@ mod test {
let state = mock_state().await;
let mut area_tags = Map::new();
area_tags.insert("url_alias".into(), "test".into());
state.area_repo.insert(&area_tags).await?;
Area::insert(&area_tags, &state.conn)?;
state
.report_repo
.insert(1, &OffsetDateTime::now_utc().date(), &Map::new())
Expand All @@ -212,7 +213,7 @@ mod test {
let state = mock_state().await;
let mut area_tags = Map::new();
area_tags.insert("url_alias".into(), "test".into());
state.area_repo.insert(&area_tags).await?;
Area::insert(&area_tags, &state.conn)?;
state
.report_repo
.insert(1, &date!(2023 - 05 - 06), &Map::new())
Expand Down Expand Up @@ -242,7 +243,7 @@ mod test {
let state = mock_state().await;
let mut area_tags = Map::new();
area_tags.insert("url_alias".into(), "test".into());
state.area_repo.insert(&area_tags).await?;
Area::insert(&area_tags, &state.conn)?;
let report_1 = state
.report_repo
.insert(1, &OffsetDateTime::now_utc().date(), &Map::new())
Expand Down
Loading

0 comments on commit 18d3762

Please sign in to comment.