Repository: cogent3/cogent3
Pull request #1239 (2022-12-18 00:27:11)

MAINT: consistent behaviour of conversion func and log handling


Patch:
diff --git a/src/cogent3/app/data_store.py b/src/cogent3/app/data_store.py
index e3137f882..21def7208 100644
--- a/src/cogent3/app/data_store.py
+++ b/src/cogent3/app/data_store.py
@@ -365,8 +365,8 @@ class SingleReadDataStore(ReadOnlyDirectoryDataStore):  # pragma: no cover
 class ReadOnlyZippedDataStore(ReadOnlyDataStoreBase):  # pragma: no cover
     store_suffix = "zip"
 
-    def __init__(self):
-        super().__init__()
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
 
         from cogent3.util.warning import discontinued
 
diff --git a/src/cogent3/app/data_store_new.py b/src/cogent3/app/data_store_new.py
index b3b7007b5..33d17d9e6 100644
--- a/src/cogent3/app/data_store_new.py
+++ b/src/cogent3/app/data_store_new.py
@@ -574,6 +574,9 @@ def convert_tinydb_to_sqlite(source: Path, dest: Optional[Path] = None) -> DataS
             "You need to install tinydb to be able to migrate to new datastore."
         ) from e
 
+    from .io_new import write_db
+
+    source = Path(source)
     storage = CachingMiddleware(JSONStorage)
     tinydb = TinyDB(str(source), storage=storage)
     pattern = translate("*")
@@ -595,12 +598,12 @@ def convert_tinydb_to_sqlite(source: Path, dest: Optional[Path] = None) -> DataS
             f"Destination file {str(dest)} already exists. Delete or define new dest."
         )
     dstore = DataStoreSqlite(source=dest, mode=OVERWRITE)
-
+    writer = write_db(data_store=dstore)
     for id, data, is_completed in data_list:
-        if is_completed:
-            dstore.write(unique_id=id, data=data)
+        if id.endswith(".log"):
+            writer.data_store.write_log(unique_id=id, data=data)
         else:
-            dstore.write_not_completed(unique_id=id, data=data)
+            writer.main(data, identifier=id)
 
     if lock_id is not None or dstore._lock_id:
         cmnd = f"UPDATE state SET lock_pid =? WHERE state_id == 1"
diff --git a/src/cogent3/app/sqlite_data_store.py b/src/cogent3/app/sqlite_data_store.py
index f5a34e87e..0399c07e4 100644
--- a/src/cogent3/app/sqlite_data_store.py
+++ b/src/cogent3/app/sqlite_data_store.py
@@ -226,10 +226,11 @@ class DataStoreSqlite(DataStoreABC):
     @property
     def logs(self):
         """returns all log records"""
-        cmnd = self.db.execute(f"SELECT data, log_name FROM {_LOG_TABLE}")
+        cmnd = self.db.execute(f"SELECT log_name FROM {_LOG_TABLE}")
         return [
             DataMember(data_store=self, unique_id=Path(_LOG_TABLE) / r["log_name"])
             for r in cmnd.fetchall()
+            if r["log_name"]
         ]
 
     def _write(


Test patch:
diff --git a/tests/test_app/test_data_store_new.py b/tests/test_app/test_data_store_new.py
index 12bb5919f..0415d7fc1 100644
--- a/tests/test_app/test_data_store_new.py
+++ b/tests/test_app/test_data_store_new.py
@@ -233,11 +233,27 @@ def test_convert_tinydb_to_sqlite_error(tmp_dir):
     path = tmp_dir / "sample_locked.tinydb"
     shutil.copy(DATA_DIR / path.name, path)
     dest = tmp_dir / "data1.sqlitedb"
+    # create a file at dest path to trigger error
     dest.write_text("blah")
     with pytest.raises(IOError):
         _ = convert_tinydb_to_sqlite(path, dest=dest)
 
 
+@pytest.mark.parametrize(
+    "orig,num_logs",
+    (
+        (DATA_DIR / "sample_locked.tinydb", 0),
+        (DATA_DIR.parent.parent / "doc" / "data" / "demo-locked.tinydb", 1),
+    ),
+)
+def test_convert_tinydbs_to_sqlite(tmp_dir, orig, num_logs):
+    src = tmp_dir / orig.name
+    shutil.copy(orig, src)
+    dest = tmp_dir / "data1.sqlitedb"
+    got = convert_tinydb_to_sqlite(src, dest=dest)
+    assert len(got.logs) == num_logs
+
+
 def test_convert_directory_datastore(Sample_oldDirectoryDataStore, write_dir):
     new_dstore = convert_directory_datastore(
         Sample_oldDirectoryDataStore, write_dir, ".fasta"
diff --git a/tests/test_app/test_sqlite_data_store.py b/tests/test_app/test_sqlite_data_store.py
index dd2d9a116..932047f14 100644
--- a/tests/test_app/test_sqlite_data_store.py
+++ b/tests/test_app/test_sqlite_data_store.py
@@ -512,3 +512,7 @@ def test_lock_firsttime(full_dstore_sqlite):
     assert full_dstore_sqlite.locked
     full_dstore_sqlite.unlock()
     assert not full_dstore_sqlite.locked
+
+
+def test_db_without_logs(ro_sql_dstore):
+    assert len(ro_sql_dstore.logs) == 0