Skip to content

Commit

Permalink
removed magic methods. updated tests.py for version 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
patx committed Jan 13, 2025
1 parent a09a9b7 commit 9d7b598
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 37 deletions.
25 changes: 0 additions & 25 deletions pickledb.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,31 +65,6 @@ def _load(self):
self.db = {}
print("Database created")

def __setitem__(self, key, value):
key = str(key) if not isinstance(key, str) else key
self.set(key, value)

def __getitem__(self, key):
key = str(key) if not isinstance(key, str) else key
result = self.get(key)
return result

def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, traceback):
self.save()

def __contains__(self, key):
key = str(key) if not isinstance(key, str) else key
return key in self.db

def __iter__(self):
return iter(self.db)

def __len__(self):
return len(self.db)

def save(self):
"""
Save the database to the file using an atomic save.
Expand Down
17 changes: 5 additions & 12 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TestPickleDB(unittest.TestCase):
def setUp(self):
"""Set up a PickleDB instance with a real file."""
self.test_file = "test_pickledb.json"
self.db = PickleDB(self.test_file, auto_dump=False)
self.db = PickleDB(self.test_file)

def tearDown(self):
"""Clean up after tests."""
Expand Down Expand Up @@ -48,7 +48,7 @@ def test_stress_operation(self):

# Measure dump performance
start_time = time.time()
self.db.dump()
self.db.save()
dump_time = time.time() - start_time
print(f"Dumped {num_docs} key-value pairs to disk in {dump_time:.2f} seconds")

Expand Down Expand Up @@ -91,24 +91,17 @@ def test_all_keys(self):
def test_dump_and_reload(self):
"""Test dumping the database to disk and reloading it."""
self.db.set("key1", "value1")
self.db.dump()
reloaded_db = PickleDB(self.test_file, auto_dump=False)
self.db.save()
reloaded_db = PickleDB(self.test_file)
self.assertEqual(reloaded_db.get("key1"), "value1")

def test_invalid_file_loading(self):
"""Test initializing a database with a corrupt file."""
with open(self.test_file, 'w') as f:
f.write("corrupt data")
db = PickleDB(self.test_file, auto_dump=False)
db = PickleDB(self.test_file)
self.assertEqual(db.all(), [])

def test_auto_dump(self):
"""Test the auto-dump functionality."""
db = PickleDB(self.test_file, auto_dump=True)
db.set("key1", "value1")
reloaded_db = PickleDB(self.test_file, auto_dump=False)
self.assertEqual(reloaded_db.get("key1"), "value1")

def test_set_non_string_key(self):
"""Test setting a non-string key."""
self.db.set(123, "value123")
Expand Down

0 comments on commit 9d7b598

Please sign in to comment.