From 974b6836b31426d8d66ff233a2e1e71130e7946a Mon Sep 17 00:00:00 2001 From: Pavlo Myroniuk Date: Tue, 24 Dec 2024 21:37:07 +0200 Subject: [PATCH 1/5] refactor(back): improve sqlx tests: use `#[sqlx::test]` instad of `#[tokio::test]`; --- dataans/Cargo.lock | 13 ------ dataans/src-tauri/Cargo.toml | 4 -- dataans/src-tauri/src/dataans/db/sqlite.rs | 48 +++++++++++----------- 3 files changed, 23 insertions(+), 42 deletions(-) diff --git a/dataans/Cargo.lock b/dataans/Cargo.lock index d43a5de..77e057e 100644 --- a/dataans/Cargo.lock +++ b/dataans/Cargo.lock @@ -1050,7 +1050,6 @@ dependencies = [ "tauri-plugin-shell", "thiserror 2.0.7", "time", - "tokio", "toml 0.8.2", "tracing", "tracing-subscriber", @@ -5752,21 +5751,9 @@ dependencies = [ "mio", "pin-project-lite", "socket2", - "tokio-macros", "windows-sys 0.52.0", ] -[[package]] -name = "tokio-macros" -version = "2.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.90", -] - [[package]] name = "tokio-stream" version = "0.1.17" diff --git a/dataans/src-tauri/Cargo.toml b/dataans/src-tauri/Cargo.toml index 754cdcb..3f0d524 100644 --- a/dataans/src-tauri/Cargo.toml +++ b/dataans/src-tauri/Cargo.toml @@ -42,10 +42,6 @@ syntect = { version = "5.2", default-features = false, features = [ serde_json = "1" thiserror = "2.0" -[dev-dependencies] -tokio = { version = "1.42", features = ["rt", "macros"] } - - [features] # This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!! custom-protocol = ["tauri/custom-protocol"] diff --git a/dataans/src-tauri/src/dataans/db/sqlite.rs b/dataans/src-tauri/src/dataans/db/sqlite.rs index e1569d8..b43633e 100644 --- a/dataans/src-tauri/src/dataans/db/sqlite.rs +++ b/dataans/src-tauri/src/dataans/db/sqlite.rs @@ -307,27 +307,15 @@ impl Db for SqliteDb { #[cfg(test)] mod tests { - use std::str::FromStr; - + use sqlx::SqlitePool; use time::OffsetDateTime; use uuid::Uuid; use super::*; - fn pool() -> SqlitePool { - use crate::dataans::SqlitePoolOptions; - - SqlitePoolOptions::new() - .max_connections(4) - .min_connections(1) - .acquire_timeout(std::time::Duration::from_secs(5)) - .connect_lazy("sqlite:///home/pavlo-myroniuk/.local/share/com.tbt.dataans/db/dataans.sqlite") - .expect("can not connect to sqlite db") - } - - #[tokio::test] - async fn space_crud() { - let db = SqliteDb::new(pool()); + #[sqlx::test] + async fn space_crud(pool: SqlitePool) { + let db = SqliteDb::new(pool); let file_id = Uuid::new_v4(); let file = File { @@ -336,10 +324,19 @@ mod tests { path: "/home/tbt/cat-01.jpg".into(), }; - //------ - db.add_file(&file).await.unwrap(); + let new_avatar_id = Uuid::new_v4(); + let new_avatar = File { + id: new_avatar_id, + name: "cat-2.jpg".into(), + path: "/home/tbt/cat-02.jpg".into(), + }; + + db.add_file(&new_avatar).await.unwrap(); + + //------ + let id = Uuid::new_v4(); let created_at = OffsetDateTime::now_utc(); let space = Space { @@ -357,7 +354,7 @@ mod tests { let updated_space = Space { id, name: "TheBestTvarynka".into(), - avatar_id: Uuid::from_str("620b74b0-05d7-4170-911f-6eeea7b15c44").unwrap(), + avatar_id: new_avatar_id, created_at, }; db.update_space(&updated_space).await.unwrap(); @@ -373,11 +370,12 @@ mod tests { //------ db.remove_file(file_id).await.unwrap(); + db.remove_file(new_avatar_id).await.unwrap(); } - #[tokio::test] - async fn file_crud() { - let db = SqliteDb::new(pool()); + #[sqlx::test] + async fn file_crud(pool: SqlitePool) { + let db = SqliteDb::new(pool); let id = Uuid::new_v4(); let file = File { @@ -407,9 +405,9 @@ mod tests { assert!(files.iter().find(|file| file.id == id).is_none()); } - #[tokio::test] - async fn note_crud() { - let db = SqliteDb::new(pool()); + #[sqlx::test] + async fn note_crud(pool: SqlitePool) { + let db = SqliteDb::new(pool); let file_id = Uuid::new_v4(); let file = File { From 3b0a5c54b79a5e105bafe96cdf6ba505590a8dde Mon Sep 17 00:00:00 2001 From: Pavlo Myroniuk Date: Tue, 24 Dec 2024 21:45:32 +0200 Subject: [PATCH 2/5] chore: add `.sqlx` dir with queries metadata; --- ...aff019b95105c32e7d67996915015f6a03df78593b83.json | 12 ++++++++++++ ...283e890171c312b4677e658a3a0090f295d49e5fca2a.json | 12 ++++++++++++ ...8b165745fbc5bebf268bf969e50835e6b3abb924746d.json | 12 ++++++++++++ ...d544e5ef646ee8d26678116157161dc45b66d17b0257.json | 12 ++++++++++++ ...e92f48e144c81e1063a32bd2d4098806bde0c1606fa1.json | 12 ++++++++++++ ...f51e6314357081272bde811c22b87fc1a3f1e360b682.json | 12 ++++++++++++ ...2cfebb74dd8f02330a4b75b0f1fbe11bd0d5d1652a31.json | 12 ++++++++++++ ...1721f9b3cac0b816f93f44dbbd9ec9f75e43891f7b52.json | 12 ++++++++++++ ...51c01d29a6d69517263b1f6f04081ce02bb0556d92de.json | 12 ++++++++++++ 9 files changed, 108 insertions(+) create mode 100644 dataans/.sqlx/query-0d384091c7bc449afb3faff019b95105c32e7d67996915015f6a03df78593b83.json create mode 100644 dataans/.sqlx/query-3111f54fb6717a0b7725283e890171c312b4677e658a3a0090f295d49e5fca2a.json create mode 100644 dataans/.sqlx/query-616bd75ab7def23b34128b165745fbc5bebf268bf969e50835e6b3abb924746d.json create mode 100644 dataans/.sqlx/query-716cb462910879842922d544e5ef646ee8d26678116157161dc45b66d17b0257.json create mode 100644 dataans/.sqlx/query-885bf7cd9672e3c19be6e92f48e144c81e1063a32bd2d4098806bde0c1606fa1.json create mode 100644 dataans/.sqlx/query-a13e83655503b60ce890f51e6314357081272bde811c22b87fc1a3f1e360b682.json create mode 100644 dataans/.sqlx/query-b7163fca3578daeaa4692cfebb74dd8f02330a4b75b0f1fbe11bd0d5d1652a31.json create mode 100644 dataans/.sqlx/query-db8fe09c75473c4a4efe1721f9b3cac0b816f93f44dbbd9ec9f75e43891f7b52.json create mode 100644 dataans/.sqlx/query-fbce94c09b46d54d59d751c01d29a6d69517263b1f6f04081ce02bb0556d92de.json diff --git a/dataans/.sqlx/query-0d384091c7bc449afb3faff019b95105c32e7d67996915015f6a03df78593b83.json b/dataans/.sqlx/query-0d384091c7bc449afb3faff019b95105c32e7d67996915015f6a03df78593b83.json new file mode 100644 index 0000000..eba118e --- /dev/null +++ b/dataans/.sqlx/query-0d384091c7bc449afb3faff019b95105c32e7d67996915015f6a03df78593b83.json @@ -0,0 +1,12 @@ +{ + "db_name": "SQLite", + "query": "DELETE FROM spaces WHERE id = ?1", + "describe": { + "columns": [], + "parameters": { + "Right": 1 + }, + "nullable": [] + }, + "hash": "0d384091c7bc449afb3faff019b95105c32e7d67996915015f6a03df78593b83" +} diff --git a/dataans/.sqlx/query-3111f54fb6717a0b7725283e890171c312b4677e658a3a0090f295d49e5fca2a.json b/dataans/.sqlx/query-3111f54fb6717a0b7725283e890171c312b4677e658a3a0090f295d49e5fca2a.json new file mode 100644 index 0000000..620b6e2 --- /dev/null +++ b/dataans/.sqlx/query-3111f54fb6717a0b7725283e890171c312b4677e658a3a0090f295d49e5fca2a.json @@ -0,0 +1,12 @@ +{ + "db_name": "SQLite", + "query": "DELETE FROM files WHERE id = ?1", + "describe": { + "columns": [], + "parameters": { + "Right": 1 + }, + "nullable": [] + }, + "hash": "3111f54fb6717a0b7725283e890171c312b4677e658a3a0090f295d49e5fca2a" +} diff --git a/dataans/.sqlx/query-616bd75ab7def23b34128b165745fbc5bebf268bf969e50835e6b3abb924746d.json b/dataans/.sqlx/query-616bd75ab7def23b34128b165745fbc5bebf268bf969e50835e6b3abb924746d.json new file mode 100644 index 0000000..80737c7 --- /dev/null +++ b/dataans/.sqlx/query-616bd75ab7def23b34128b165745fbc5bebf268bf969e50835e6b3abb924746d.json @@ -0,0 +1,12 @@ +{ + "db_name": "SQLite", + "query": "UPDATE files SET name = ?1, path = ?2 WHERE id = ?3", + "describe": { + "columns": [], + "parameters": { + "Right": 3 + }, + "nullable": [] + }, + "hash": "616bd75ab7def23b34128b165745fbc5bebf268bf969e50835e6b3abb924746d" +} diff --git a/dataans/.sqlx/query-716cb462910879842922d544e5ef646ee8d26678116157161dc45b66d17b0257.json b/dataans/.sqlx/query-716cb462910879842922d544e5ef646ee8d26678116157161dc45b66d17b0257.json new file mode 100644 index 0000000..030a622 --- /dev/null +++ b/dataans/.sqlx/query-716cb462910879842922d544e5ef646ee8d26678116157161dc45b66d17b0257.json @@ -0,0 +1,12 @@ +{ + "db_name": "SQLite", + "query": "INSERT INTO files (id, name, path) VALUES (?1, ?2, ?3)", + "describe": { + "columns": [], + "parameters": { + "Right": 3 + }, + "nullable": [] + }, + "hash": "716cb462910879842922d544e5ef646ee8d26678116157161dc45b66d17b0257" +} diff --git a/dataans/.sqlx/query-885bf7cd9672e3c19be6e92f48e144c81e1063a32bd2d4098806bde0c1606fa1.json b/dataans/.sqlx/query-885bf7cd9672e3c19be6e92f48e144c81e1063a32bd2d4098806bde0c1606fa1.json new file mode 100644 index 0000000..d706090 --- /dev/null +++ b/dataans/.sqlx/query-885bf7cd9672e3c19be6e92f48e144c81e1063a32bd2d4098806bde0c1606fa1.json @@ -0,0 +1,12 @@ +{ + "db_name": "SQLite", + "query": "UPDATE notes SET text = ?1 WHERE id = ?2", + "describe": { + "columns": [], + "parameters": { + "Right": 2 + }, + "nullable": [] + }, + "hash": "885bf7cd9672e3c19be6e92f48e144c81e1063a32bd2d4098806bde0c1606fa1" +} diff --git a/dataans/.sqlx/query-a13e83655503b60ce890f51e6314357081272bde811c22b87fc1a3f1e360b682.json b/dataans/.sqlx/query-a13e83655503b60ce890f51e6314357081272bde811c22b87fc1a3f1e360b682.json new file mode 100644 index 0000000..24a20f4 --- /dev/null +++ b/dataans/.sqlx/query-a13e83655503b60ce890f51e6314357081272bde811c22b87fc1a3f1e360b682.json @@ -0,0 +1,12 @@ +{ + "db_name": "SQLite", + "query": "DELETE FROM notes WHERE id = ?1", + "describe": { + "columns": [], + "parameters": { + "Right": 1 + }, + "nullable": [] + }, + "hash": "a13e83655503b60ce890f51e6314357081272bde811c22b87fc1a3f1e360b682" +} diff --git a/dataans/.sqlx/query-b7163fca3578daeaa4692cfebb74dd8f02330a4b75b0f1fbe11bd0d5d1652a31.json b/dataans/.sqlx/query-b7163fca3578daeaa4692cfebb74dd8f02330a4b75b0f1fbe11bd0d5d1652a31.json new file mode 100644 index 0000000..ff97438 --- /dev/null +++ b/dataans/.sqlx/query-b7163fca3578daeaa4692cfebb74dd8f02330a4b75b0f1fbe11bd0d5d1652a31.json @@ -0,0 +1,12 @@ +{ + "db_name": "SQLite", + "query": "UPDATE spaces SET name = ?1, avatar_id = ?2 WHERE id = ?3", + "describe": { + "columns": [], + "parameters": { + "Right": 3 + }, + "nullable": [] + }, + "hash": "b7163fca3578daeaa4692cfebb74dd8f02330a4b75b0f1fbe11bd0d5d1652a31" +} diff --git a/dataans/.sqlx/query-db8fe09c75473c4a4efe1721f9b3cac0b816f93f44dbbd9ec9f75e43891f7b52.json b/dataans/.sqlx/query-db8fe09c75473c4a4efe1721f9b3cac0b816f93f44dbbd9ec9f75e43891f7b52.json new file mode 100644 index 0000000..f0f3dc0 --- /dev/null +++ b/dataans/.sqlx/query-db8fe09c75473c4a4efe1721f9b3cac0b816f93f44dbbd9ec9f75e43891f7b52.json @@ -0,0 +1,12 @@ +{ + "db_name": "SQLite", + "query": "INSERT INTO spaces (id, name, avatar_id, created_at) VALUES (?1, ?2, ?3, ?4)", + "describe": { + "columns": [], + "parameters": { + "Right": 4 + }, + "nullable": [] + }, + "hash": "db8fe09c75473c4a4efe1721f9b3cac0b816f93f44dbbd9ec9f75e43891f7b52" +} diff --git a/dataans/.sqlx/query-fbce94c09b46d54d59d751c01d29a6d69517263b1f6f04081ce02bb0556d92de.json b/dataans/.sqlx/query-fbce94c09b46d54d59d751c01d29a6d69517263b1f6f04081ce02bb0556d92de.json new file mode 100644 index 0000000..0d64de7 --- /dev/null +++ b/dataans/.sqlx/query-fbce94c09b46d54d59d751c01d29a6d69517263b1f6f04081ce02bb0556d92de.json @@ -0,0 +1,12 @@ +{ + "db_name": "SQLite", + "query": "INSERT INTO notes (id, text, created_at, space_id) VALUES (?1, ?2, ?3, ?4)", + "describe": { + "columns": [], + "parameters": { + "Right": 4 + }, + "nullable": [] + }, + "hash": "fbce94c09b46d54d59d751c01d29a6d69517263b1f6f04081ce02bb0556d92de" +} From 9f9bdf60759cd873ec3d63a9e003df25e378d3d0 Mon Sep 17 00:00:00 2001 From: Pavlo Myroniuk Date: Tue, 24 Dec 2024 21:46:32 +0200 Subject: [PATCH 3/5] feat(ci): run tests on CI; --- .github/workflows/check-pr.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/check-pr.yml b/.github/workflows/check-pr.yml index 3192f3f..a94b8c6 100644 --- a/.github/workflows/check-pr.yml +++ b/.github/workflows/check-pr.yml @@ -25,8 +25,7 @@ jobs: run: 'cd dataans && cargo +nightly fmt --all -- --check' - name: 'Clippy' - run: 'cd dataans && cargo clippy --all-targets --all -- -D warnings' + run: 'cd dataans && cargo clippy --all-targets --workspace -- -D warnings' - # We do not have tests yet. Uncomment when we have. - # - name: 'Tests' - # run: 'cargo test' \ No newline at end of file + - name: 'Tests' + run: 'cd dataans && cargo test --workspace' \ No newline at end of file From cb8838f92c0bfd5a68e94e21cd682f3034eeef51 Mon Sep 17 00:00:00 2001 From: Pavlo Myroniuk Date: Tue, 24 Dec 2024 21:54:03 +0200 Subject: [PATCH 4/5] refactor(back): fix clippy warnings; --- dataans/src-tauri/src/dataans/db/sqlite.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dataans/src-tauri/src/dataans/db/sqlite.rs b/dataans/src-tauri/src/dataans/db/sqlite.rs index b43633e..20ad367 100644 --- a/dataans/src-tauri/src/dataans/db/sqlite.rs +++ b/dataans/src-tauri/src/dataans/db/sqlite.rs @@ -343,7 +343,7 @@ mod tests { id, name: "Tbt".into(), avatar_id: file_id, - created_at: created_at.clone(), + created_at, }; db.create_space(&space).await.unwrap(); @@ -365,7 +365,7 @@ mod tests { db.remove_space(id).await.unwrap(); let spaces = db.spaces().await.unwrap(); - assert!(spaces.iter().find(|space| space.id == id).is_none()); + assert!(!spaces.iter().any(|space| space.id == id)); //------ @@ -402,7 +402,7 @@ mod tests { db.remove_file(id).await.unwrap(); let files = db.files().await.unwrap(); - assert!(files.iter().find(|file| file.id == id).is_none()); + assert!(!files.iter().any(|file| file.id == id)); } #[sqlx::test] @@ -436,7 +436,7 @@ mod tests { id, text: "some text 1".into(), space_id, - created_at: created_at.clone(), + created_at, }; db.create_note(¬e).await.unwrap(); @@ -458,7 +458,7 @@ mod tests { db.remove_note(id).await.unwrap(); let notes = db.space_notes(space_id).await.unwrap(); - assert!(notes.iter().find(|note| note.id == id).is_none()); + assert!(!notes.iter().any(|note| note.id == id)); //------ From 7c550fa3a89a5737231078fcd3f656f62a242b04 Mon Sep 17 00:00:00 2001 From: Pavlo Myroniuk Date: Tue, 24 Dec 2024 22:01:23 +0200 Subject: [PATCH 5/5] feat(ci): run tests on CI (main branch); --- .github/workflows/github-actions.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/github-actions.yml b/.github/workflows/github-actions.yml index 2c88f07..3f0dd3e 100644 --- a/.github/workflows/github-actions.yml +++ b/.github/workflows/github-actions.yml @@ -25,11 +25,10 @@ jobs: run: 'cd dataans && cargo +nightly fmt --all -- --check' - name: 'Clippy' - run: 'cd dataans && cargo clippy --all-targets --all -- -D warnings' + run: 'cd dataans && cargo clippy --all-targets --workspace -- -D warnings' - # We do not have tests yet. Uncomment when we have. - # - name: 'Tests' - # run: 'cargo test' + - name: 'Tests' + run: 'cd dataans && cargo test --workspace' # Some day I'll set up cross platform (matrix) app build with proper artifacts. # But currently I don't need it :)