瀏覽代碼

Add macro to derive test name for test databases.

Kestrel 1 月之前
父節點
當前提交
7601050ca2

+ 1 - 7
microrm/Cargo.toml

@@ -34,10 +34,4 @@ test-log = "0.2.15"
 
 serde = { version = "1.0", features = ["derive"] }
 clap = { version = "4", features = ["derive"] }
-# criterion = "0.5"
-# rand = "0.8.5"
-# stats_alloc = "0.1.10"
-
-# [[bench]]
-# name = "simple_in_memory"
-# harness = false
+stdext = { version = "0.3" }

+ 17 - 3
microrm/tests/common/mod.rs

@@ -1,10 +1,24 @@
 use microrm::prelude::*;
 
-pub fn open_test_db<DB: Database>(identifier: &'static str) -> DB {
+pub fn open_test_db_helper<DB: Database>(identifier: &'static str) -> DB {
     let path = format!(
-        "{tmpdir}/microrm-test-{identifier}.db",
-        tmpdir = std::env!("CARGO_TARGET_TMPDIR")
+        "{tmpdir}/{identifier}.db",
+        tmpdir = std::env!("CARGO_TARGET_TMPDIR"),
     );
+    let path = path.replace("::", "_");
     let _ = std::fs::remove_file(path.as_str());
     DB::open_path(path).expect("couldn't open database file")
 }
+
+mod macros {
+    macro_rules! open_test_db {
+        () => {{
+            use stdext::function_name;
+            $crate::common::open_test_db_helper(function_name!())
+        }};
+    }
+
+    pub(crate) use open_test_db;
+}
+
+pub(crate) use macros::open_test_db;

+ 6 - 6
microrm/tests/derive.rs

@@ -31,7 +31,7 @@ fn open_test() {
 
 #[test]
 fn check_for_inserted() {
-    let db = common::open_test_db::<PeopleDB>("derive_check_for_inserted");
+    let db: PeopleDB = common::open_test_db!();
     let name_string = "name_here".to_string();
     // check that it isn't in the database before we insert it
     assert!(db.people.keyed(&name_string).get().ok().flatten().is_none());
@@ -49,7 +49,7 @@ fn check_for_inserted() {
 
 #[test]
 fn check_relation_query_construction() {
-    let db = common::open_test_db::<PeopleDB>("derive_check_relation_query_construction");
+    let db: PeopleDB = common::open_test_db!();
 
     let name_string = "name_here".to_string();
     db.people
@@ -75,7 +75,7 @@ fn check_relation_query_construction() {
 
 #[test]
 fn check_relation_insertion() {
-    let db = common::open_test_db::<PeopleDB>("derive_check_relation_insertion");
+    let db: PeopleDB = common::open_test_db!();
 
     let name_string = "name_here".to_string();
     db.people
@@ -101,7 +101,7 @@ fn check_relation_insertion() {
 
 #[test]
 fn delete_test() {
-    let db = common::open_test_db::<PeopleDB>("derive_delete_test");
+    let db: PeopleDB = common::open_test_db!();
 
     let id = db
         .people
@@ -124,7 +124,7 @@ fn delete_test() {
 
 #[test]
 fn remove_test() {
-    let db = common::open_test_db::<PeopleDB>("derive_remove_test");
+    let db: PeopleDB = common::open_test_db!();
 
     let id = db
         .people
@@ -151,7 +151,7 @@ fn remove_test() {
 
 #[test]
 fn update_test() {
-    let db = common::open_test_db::<PeopleDB>("derive_update_test");
+    let db: PeopleDB = common::open_test_db!();
 
     let mut stored = db
         .people

+ 1 - 1
microrm/tests/fkey.rs

@@ -23,7 +23,7 @@ struct PeopleDB {
 
 #[test]
 fn simple_query() {
-    let db = common::open_test_db::<PeopleDB>("fkey_simple_query");
+    let db: PeopleDB = common::open_test_db!(); // ::<PeopleDB>("fkey_simple_query");
 
     let pid = db
         .parents

+ 2 - 2
microrm/tests/index.rs

@@ -22,12 +22,12 @@ struct TWDB {
 
 #[test]
 fn db_construction() {
-    let _db = common::open_test_db::<TWDB>("index_db_construction");
+    let _db: TWDB = common::open_test_db!();
 }
 
 #[test]
 fn lookup() {
-    let db = common::open_test_db::<TWDB>("index_lookup");
+    let db: TWDB = common::open_test_db!();
 
     let e1 = db
         .entries

+ 2 - 2
microrm/tests/injective.rs

@@ -36,12 +36,12 @@ struct ABDB {
 
 #[test]
 fn schema_creation() {
-    let _db = common::open_test_db::<ABDB>("injective_schema_creation");
+    let _db: ABDB = common::open_test_db!();
 }
 
 #[test]
 fn single_connection() {
-    let db = common::open_test_db::<ABDB>("injective_single_connection");
+    let db: ABDB = common::open_test_db!();
 
     let a1 = db
         .authors

+ 1 - 1
microrm/tests/join.rs

@@ -29,7 +29,7 @@ struct JoinDB {
 
 #[test]
 fn simple_ops() {
-    let db = common::open_test_db::<JoinDB>("join_simple_ops");
+    let db: JoinDB = common::open_test_db!();
 
     let b1id = db
         .bases

+ 2 - 2
microrm/tests/mutual.rs

@@ -31,12 +31,12 @@ struct ReceiptDB {
 
 #[test]
 fn check_schema_creation() {
-    let _db = common::open_test_db::<ReceiptDB>("mutual_check_schema_creation");
+    let _db: ReceiptDB = common::open_test_db!();
 }
 
 #[test]
 fn simple_operations() {
-    let db = common::open_test_db::<ReceiptDB>("mutual_simple_operations");
+    let db: ReceiptDB = common::open_test_db!();
 
     let ca = db
         .customers

+ 1 - 1
microrm/tests/query_construct.rs

@@ -17,7 +17,7 @@ struct KVDB {
 
 #[test]
 fn repeated_uniqueness() {
-    let db = common::open_test_db::<KVDB>("repeated_uniqueness");
+    let db: KVDB = common::open_test_db!();
 
     db.entries
         .first()

+ 3 - 3
microrm/tests/query_equiv.rs

@@ -27,7 +27,7 @@ struct ItemDB {
 
 #[test]
 fn single_item() {
-    let db = common::open_test_db::<ItemDB>("query_equiv_single_item");
+    let db: ItemDB = common::open_test_db!();
 
     db.single_items
         .insert(SingleItem {
@@ -72,7 +72,7 @@ fn single_item() {
 
 #[test]
 fn double_item() {
-    let db = common::open_test_db::<ItemDB>("query_equiv_double_item");
+    let db: ItemDB = common::open_test_db!();
 
     db.double_items
         .insert(DoubleItem {
@@ -119,7 +119,7 @@ fn double_item() {
 
 #[test]
 fn with_test() {
-    let db = common::open_test_db::<ItemDB>("query_equiv_with");
+    let db: ItemDB = common::open_test_db!();
 
     db.single_items
         .insert(SingleItem {