Uncomment db2 tests

Julia created

Change summary

crates/db2/src/db2.rs | 274 ++++++++++++++++++++++----------------------
crates/db2/src/kvp.rs |  58 ++++----
2 files changed, 168 insertions(+), 164 deletions(-)

Detailed changes

crates/db2/src/db2.rs 🔗

@@ -190,138 +190,142 @@ where
         .detach()
 }
 
-// #[cfg(test)]
-// mod tests {
-//     use std::thread;
-
-//     use sqlez::domain::Domain;
-//     use sqlez_macros::sql;
-//     use tempdir::TempDir;
-
-//     use crate::open_db;
-
-//     // Test bad migration panics
-//     #[gpui::test]
-//     #[should_panic]
-//     async fn test_bad_migration_panics() {
-//         enum BadDB {}
-
-//         impl Domain for BadDB {
-//             fn name() -> &'static str {
-//                 "db_tests"
-//             }
-
-//             fn migrations() -> &'static [&'static str] {
-//                 &[
-//                     sql!(CREATE TABLE test(value);),
-//                     // failure because test already exists
-//                     sql!(CREATE TABLE test(value);),
-//                 ]
-//             }
-//         }
-
-//         let tempdir = TempDir::new("DbTests").unwrap();
-//         let _bad_db = open_db::<BadDB>(tempdir.path(), &util::channel::ReleaseChannel::Dev).await;
-//     }
-
-//     /// Test that DB exists but corrupted (causing recreate)
-//     #[gpui::test]
-//     async fn test_db_corruption() {
-//         enum CorruptedDB {}
-
-//         impl Domain for CorruptedDB {
-//             fn name() -> &'static str {
-//                 "db_tests"
-//             }
-
-//             fn migrations() -> &'static [&'static str] {
-//                 &[sql!(CREATE TABLE test(value);)]
-//             }
-//         }
-
-//         enum GoodDB {}
-
-//         impl Domain for GoodDB {
-//             fn name() -> &'static str {
-//                 "db_tests" //Notice same name
-//             }
-
-//             fn migrations() -> &'static [&'static str] {
-//                 &[sql!(CREATE TABLE test2(value);)] //But different migration
-//             }
-//         }
-
-//         let tempdir = TempDir::new("DbTests").unwrap();
-//         {
-//             let corrupt_db =
-//                 open_db::<CorruptedDB>(tempdir.path(), &util::channel::ReleaseChannel::Dev).await;
-//             assert!(corrupt_db.persistent());
-//         }
-
-//         let good_db = open_db::<GoodDB>(tempdir.path(), &util::channel::ReleaseChannel::Dev).await;
-//         assert!(
-//             good_db.select_row::<usize>("SELECT * FROM test2").unwrap()()
-//                 .unwrap()
-//                 .is_none()
-//         );
-//     }
-
-//     /// Test that DB exists but corrupted (causing recreate)
-//     #[gpui::test(iterations = 30)]
-//     async fn test_simultaneous_db_corruption() {
-//         enum CorruptedDB {}
-
-//         impl Domain for CorruptedDB {
-//             fn name() -> &'static str {
-//                 "db_tests"
-//             }
-
-//             fn migrations() -> &'static [&'static str] {
-//                 &[sql!(CREATE TABLE test(value);)]
-//             }
-//         }
-
-//         enum GoodDB {}
-
-//         impl Domain for GoodDB {
-//             fn name() -> &'static str {
-//                 "db_tests" //Notice same name
-//             }
-
-//             fn migrations() -> &'static [&'static str] {
-//                 &[sql!(CREATE TABLE test2(value);)] //But different migration
-//             }
-//         }
-
-//         let tempdir = TempDir::new("DbTests").unwrap();
-//         {
-//             // Setup the bad database
-//             let corrupt_db =
-//                 open_db::<CorruptedDB>(tempdir.path(), &util::channel::ReleaseChannel::Dev).await;
-//             assert!(corrupt_db.persistent());
-//         }
-
-//         // Try to connect to it a bunch of times at once
-//         let mut guards = vec![];
-//         for _ in 0..10 {
-//             let tmp_path = tempdir.path().to_path_buf();
-//             let guard = thread::spawn(move || {
-//                 let good_db = smol::block_on(open_db::<GoodDB>(
-//                     tmp_path.as_path(),
-//                     &util::channel::ReleaseChannel::Dev,
-//                 ));
-//                 assert!(
-//                     good_db.select_row::<usize>("SELECT * FROM test2").unwrap()()
-//                         .unwrap()
-//                         .is_none()
-//                 );
-//             });
-
-//             guards.push(guard);
-//         }
-
-//         for guard in guards.into_iter() {
-//             assert!(guard.join().is_ok());
-//         }
-//     }
-// }
+#[cfg(test)]
+mod tests {
+    use std::thread;
+
+    use sqlez::domain::Domain;
+    use sqlez_macros::sql;
+    use tempdir::TempDir;
+
+    use crate::open_db;
+
+    // Test bad migration panics
+    #[gpui2::test]
+    #[should_panic]
+    async fn test_bad_migration_panics() {
+        enum BadDB {}
+
+        impl Domain for BadDB {
+            fn name() -> &'static str {
+                "db_tests"
+            }
+
+            fn migrations() -> &'static [&'static str] {
+                &[
+                    sql!(CREATE TABLE test(value);),
+                    // failure because test already exists
+                    sql!(CREATE TABLE test(value);),
+                ]
+            }
+        }
+
+        let tempdir = TempDir::new("DbTests").unwrap();
+        let _bad_db = open_db::<BadDB>(tempdir.path(), &util::channel::ReleaseChannel::Dev).await;
+    }
+
+    /// Test that DB exists but corrupted (causing recreate)
+    #[gpui2::test]
+    async fn test_db_corruption(cx: &mut gpui2::TestAppContext) {
+        cx.executor().allow_parking();
+
+        enum CorruptedDB {}
+
+        impl Domain for CorruptedDB {
+            fn name() -> &'static str {
+                "db_tests"
+            }
+
+            fn migrations() -> &'static [&'static str] {
+                &[sql!(CREATE TABLE test(value);)]
+            }
+        }
+
+        enum GoodDB {}
+
+        impl Domain for GoodDB {
+            fn name() -> &'static str {
+                "db_tests" //Notice same name
+            }
+
+            fn migrations() -> &'static [&'static str] {
+                &[sql!(CREATE TABLE test2(value);)] //But different migration
+            }
+        }
+
+        let tempdir = TempDir::new("DbTests").unwrap();
+        {
+            let corrupt_db =
+                open_db::<CorruptedDB>(tempdir.path(), &util::channel::ReleaseChannel::Dev).await;
+            assert!(corrupt_db.persistent());
+        }
+
+        let good_db = open_db::<GoodDB>(tempdir.path(), &util::channel::ReleaseChannel::Dev).await;
+        assert!(
+            good_db.select_row::<usize>("SELECT * FROM test2").unwrap()()
+                .unwrap()
+                .is_none()
+        );
+    }
+
+    /// Test that DB exists but corrupted (causing recreate)
+    #[gpui2::test(iterations = 30)]
+    async fn test_simultaneous_db_corruption(cx: &mut gpui2::TestAppContext) {
+        cx.executor().allow_parking();
+
+        enum CorruptedDB {}
+
+        impl Domain for CorruptedDB {
+            fn name() -> &'static str {
+                "db_tests"
+            }
+
+            fn migrations() -> &'static [&'static str] {
+                &[sql!(CREATE TABLE test(value);)]
+            }
+        }
+
+        enum GoodDB {}
+
+        impl Domain for GoodDB {
+            fn name() -> &'static str {
+                "db_tests" //Notice same name
+            }
+
+            fn migrations() -> &'static [&'static str] {
+                &[sql!(CREATE TABLE test2(value);)] //But different migration
+            }
+        }
+
+        let tempdir = TempDir::new("DbTests").unwrap();
+        {
+            // Setup the bad database
+            let corrupt_db =
+                open_db::<CorruptedDB>(tempdir.path(), &util::channel::ReleaseChannel::Dev).await;
+            assert!(corrupt_db.persistent());
+        }
+
+        // Try to connect to it a bunch of times at once
+        let mut guards = vec![];
+        for _ in 0..10 {
+            let tmp_path = tempdir.path().to_path_buf();
+            let guard = thread::spawn(move || {
+                let good_db = smol::block_on(open_db::<GoodDB>(
+                    tmp_path.as_path(),
+                    &util::channel::ReleaseChannel::Dev,
+                ));
+                assert!(
+                    good_db.select_row::<usize>("SELECT * FROM test2").unwrap()()
+                        .unwrap()
+                        .is_none()
+                );
+            });
+
+            guards.push(guard);
+        }
+
+        for guard in guards.into_iter() {
+            assert!(guard.join().is_ok());
+        }
+    }
+}

crates/db2/src/kvp.rs 🔗

@@ -31,32 +31,32 @@ impl KeyValueStore {
     }
 }
 
-// #[cfg(test)]
-// mod tests {
-//     use crate::kvp::KeyValueStore;
-
-//     #[gpui::test]
-//     async fn test_kvp() {
-//         let db = KeyValueStore(crate::open_test_db("test_kvp").await);
-
-//         assert_eq!(db.read_kvp("key-1").unwrap(), None);
-
-//         db.write_kvp("key-1".to_string(), "one".to_string())
-//             .await
-//             .unwrap();
-//         assert_eq!(db.read_kvp("key-1").unwrap(), Some("one".to_string()));
-
-//         db.write_kvp("key-1".to_string(), "one-2".to_string())
-//             .await
-//             .unwrap();
-//         assert_eq!(db.read_kvp("key-1").unwrap(), Some("one-2".to_string()));
-
-//         db.write_kvp("key-2".to_string(), "two".to_string())
-//             .await
-//             .unwrap();
-//         assert_eq!(db.read_kvp("key-2").unwrap(), Some("two".to_string()));
-
-//         db.delete_kvp("key-1".to_string()).await.unwrap();
-//         assert_eq!(db.read_kvp("key-1").unwrap(), None);
-//     }
-// }
+#[cfg(test)]
+mod tests {
+    use crate::kvp::KeyValueStore;
+
+    #[gpui2::test]
+    async fn test_kvp() {
+        let db = KeyValueStore(crate::open_test_db("test_kvp").await);
+
+        assert_eq!(db.read_kvp("key-1").unwrap(), None);
+
+        db.write_kvp("key-1".to_string(), "one".to_string())
+            .await
+            .unwrap();
+        assert_eq!(db.read_kvp("key-1").unwrap(), Some("one".to_string()));
+
+        db.write_kvp("key-1".to_string(), "one-2".to_string())
+            .await
+            .unwrap();
+        assert_eq!(db.read_kvp("key-1").unwrap(), Some("one-2".to_string()));
+
+        db.write_kvp("key-2".to_string(), "two".to_string())
+            .await
+            .unwrap();
+        assert_eq!(db.read_kvp("key-2").unwrap(), Some("two".to_string()));
+
+        db.delete_kvp("key-1".to_string()).await.unwrap();
+        assert_eq!(db.read_kvp("key-1").unwrap(), None);
+    }
+}