-
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.
Added integration tests for the crud functions on all three DB types
- Loading branch information
Showing
9 changed files
with
268 additions
and
3 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod todo; | ||
|
||
pub use todo::Todo; |
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,35 @@ | ||
use miniorm::Entity; | ||
use sqlx::FromRow; | ||
|
||
#[derive(Debug, Clone, Eq, PartialEq, FromRow, Entity, Hash)] | ||
#[cfg_attr(feature = "axum", derive(serde::Serialize, serde::Deserialize))] | ||
pub struct Todo { | ||
#[column(TEXT NOT NULL)] | ||
description: String, | ||
|
||
#[column(BOOLEAN NOT NULL DEFAULT false)] | ||
done: bool, | ||
} | ||
|
||
impl Todo { | ||
pub fn new(description: impl AsRef<str>) -> Self { | ||
let description = description.as_ref().to_string(); | ||
let done = false; | ||
Todo { description, done } | ||
} | ||
|
||
#[allow(unused)] | ||
pub fn is_done(&self) -> bool { | ||
self.done | ||
} | ||
|
||
#[allow(unused)] | ||
pub fn description(&self) -> &str { | ||
&self.description | ||
} | ||
|
||
#[allow(unused)] | ||
pub fn mark_as_done(&mut self) { | ||
self.done = true; | ||
} | ||
} |
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,158 @@ | ||
mod common; | ||
|
||
#[macro_export] | ||
macro_rules! test_todo_crud { | ||
($new_store: expr) => { | ||
#[cfg_attr(not(feature = "integration_tests"), ignore)] | ||
#[serial] | ||
#[tokio::test] | ||
async fn create() { | ||
#[allow(unused_mut)] | ||
let mut store = $new_store; | ||
let todo = store.create(Todo::new("checkout miniorm")).await.unwrap(); | ||
assert_eq!(todo.id(), 1); | ||
assert_eq!(todo.description(), "checkout miniorm"); | ||
assert!(!todo.is_done()); | ||
} | ||
|
||
#[cfg_attr(not(feature = "integration_tests"), ignore)] | ||
#[serial] | ||
#[tokio::test] | ||
async fn read() { | ||
#[allow(unused_mut)] | ||
let mut store = $new_store; | ||
let todo = store.create(Todo::new("checkout miniorm")).await.unwrap(); | ||
assert_eq!(todo, store.read(todo.id()).await.unwrap()); | ||
} | ||
|
||
#[cfg_attr(not(feature = "integration_tests"), ignore)] | ||
#[serial] | ||
#[tokio::test] | ||
async fn list() { | ||
#[allow(unused_mut)] | ||
let mut store = $new_store; | ||
let todo1 = store.create(Todo::new("todo1")).await.unwrap(); | ||
let todo2 = store.create(Todo::new("todo2")).await.unwrap(); | ||
let todo3 = store.create(Todo::new("todo3")).await.unwrap(); | ||
|
||
let all_todos = store.list().await.unwrap(); | ||
assert_eq!(all_todos, [todo1, todo2, todo3]); | ||
} | ||
|
||
#[cfg_attr(not(feature = "integration_tests"), ignore)] | ||
#[serial] | ||
#[tokio::test] | ||
async fn update() { | ||
#[allow(unused_mut)] | ||
let mut store = $new_store; | ||
let mut todo = store.create(Todo::new("checkout miniorm")).await.unwrap(); | ||
let id = todo.id(); | ||
|
||
assert!(!store.read(id).await.unwrap().is_done()); | ||
|
||
todo.mark_as_done(); | ||
assert!(!store.read(id).await.unwrap().is_done()); | ||
|
||
store.update(todo).await.unwrap(); | ||
assert!(store.read(id).await.unwrap().is_done()); | ||
} | ||
|
||
#[cfg_attr(not(feature = "integration_tests"), ignore)] | ||
#[serial] | ||
#[tokio::test] | ||
async fn delete() { | ||
#[allow(unused_mut)] | ||
let mut store = $new_store; | ||
let todo = store.create(Todo::new("checkout miniorm")).await.unwrap(); | ||
|
||
store.delete(todo.id()).await.unwrap(); | ||
|
||
assert!(matches!( | ||
store.delete(todo.id()).await, | ||
Err(sqlx::Error::RowNotFound) | ||
)); | ||
|
||
assert!(matches!( | ||
store.read(todo.id()).await, | ||
Err(sqlx::Error::RowNotFound) | ||
)); | ||
} | ||
|
||
#[cfg_attr(not(feature = "integration_tests"), ignore)] | ||
#[serial] | ||
#[tokio::test] | ||
async fn delete_all() { | ||
#[allow(unused_mut)] | ||
let mut store = $new_store; | ||
let todo1 = store.create(Todo::new("todo1")).await.unwrap(); | ||
let todo2 = store.create(Todo::new("todo2")).await.unwrap(); | ||
let todo3 = store.create(Todo::new("todo3")).await.unwrap(); | ||
|
||
let all_todos = store.list().await.unwrap(); | ||
assert_eq!(all_todos, [todo1, todo2, todo3]); | ||
|
||
let result = store.delete_all().await.unwrap(); | ||
assert_eq!(result, 3); | ||
assert!(store.list().await.unwrap().is_empty()); | ||
} | ||
}; | ||
} | ||
|
||
mod test_crud { | ||
|
||
mod test_mysql { | ||
use crate::common::Todo; | ||
use miniorm::prelude::*; | ||
use serial_test::serial; | ||
use sqlx::{MySql, MySqlPool}; | ||
use std::error::Error; | ||
|
||
pub async fn get_store() -> Result<Store<MySql, Todo>, Box<dyn Error>> { | ||
dotenv::dotenv()?; | ||
let url = std::env::var("MYSQL_URL").expect("missing MYSQL_URL env"); | ||
let db = MySqlPool::connect(&url).await?; | ||
let store = miniorm::Store::new(db); | ||
store.recreate_table().await?; | ||
Ok(store) | ||
} | ||
|
||
test_todo_crud!(get_store().await.unwrap()); | ||
} | ||
|
||
mod test_pgstore { | ||
use crate::common::Todo; | ||
use miniorm::prelude::*; | ||
use serial_test::serial; | ||
use sqlx::{PgPool, Postgres}; | ||
use std::error::Error; | ||
|
||
pub async fn get_store() -> Result<Store<Postgres, Todo>, Box<dyn Error>> { | ||
dotenv::dotenv()?; | ||
let url = std::env::var("POSTGRES_URL").expect("missing POSTGRES_URL env"); | ||
let db = PgPool::connect(&url).await?; | ||
let store = miniorm::Store::new(db); | ||
store.recreate_table().await?; | ||
Ok(store) | ||
} | ||
|
||
test_todo_crud!(get_store().await.unwrap()); | ||
} | ||
|
||
mod test_sqlitestore { | ||
use crate::common::Todo; | ||
use miniorm::prelude::*; | ||
use serial_test::serial; | ||
use sqlx::{Sqlite, SqlitePool}; | ||
use std::error::Error; | ||
|
||
async fn get_store() -> Result<Store<Sqlite, Todo>, Box<dyn Error>> { | ||
let url = ":memory:"; | ||
let connection = SqlitePool::connect(url).await?; | ||
let store = Store::new(connection); | ||
store.recreate_table().await?; | ||
Ok(store) | ||
} | ||
|
||
test_todo_crud!(get_store().await.unwrap()); | ||
} | ||
} |