Skip to content

Commit

Permalink
fix bug when starting with new db file (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekorman authored Jul 10, 2024
1 parent fe10de0 commit 0c6645c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion affine/engine.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import pickle
from abc import ABC, abstractmethod
from collections import defaultdict
Expand Down Expand Up @@ -91,7 +92,7 @@ def __init__(
) -> None: # maybe add option to the init for ANN algo
self.path = path
self.records: dict[str, list[Collection]] = defaultdict(list)
if self.path is not None:
if self.path is not None and os.path.exists(self.path):
with open(self.path, "rb") as f:
self.records = pickle.load(f)
self.collection_id_counter: dict[str, int] = defaultdict(
Expand Down
11 changes: 11 additions & 0 deletions tests/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ def test_local_engine(data: list[Collection]):
assert db.insert(Product(name="Banana", price=2.0)) == 2


def test_local_engine_persistence(data: list[Collection], tmp_path):
db = LocalEngine(tmp_path / "db.affine")

assert len(db.query(Person.objects())) == 0
for rec in data:
db.insert(rec)

db2 = LocalEngine(tmp_path / "db.affine")
assert len(db2.query(Person.objects())) == 2


def test_local_engine_save_load(data: list[Collection], tmp_path):
db = LocalEngine()

Expand Down

0 comments on commit 0c6645c

Please sign in to comment.