Skip to content

Commit af759c6

Browse files
committed
Tests: Add test cases discovered from mutation testing
Thanks https://github.com/sixty-north/cosmic-ray/ !
1 parent 119d71a commit af759c6

File tree

6 files changed

+53
-7
lines changed

6 files changed

+53
-7
lines changed

tests/test_middlewares.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ def test_caching_write_many(storage):
4646

4747

4848
def test_caching_flush(storage):
49+
# Write contents
50+
for _ in range(CachingMiddleware.WRITE_CACHE_SIZE - 1):
51+
storage.write(element)
52+
53+
# Not yet flushed...
54+
assert storage.memory is None
55+
56+
storage.write(element)
57+
58+
# Verify contents: Cache should be emptied and written to storage
59+
assert storage.memory
60+
61+
62+
def test_caching_flush_manually(storage):
4963
# Write contents
5064
storage.write(element)
5165

tests/test_queries.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def test_eq():
2121

2222
def test_ne():
2323
query = Query().value != 1
24+
assert query({'value': 0})
2425
assert query({'value': 2})
2526
assert not query({'value': 1})
2627
assert hash(query)
@@ -35,6 +36,7 @@ def test_lt():
3536
query = Query().value < 1
3637
assert query({'value': 0})
3738
assert not query({'value': 1})
39+
assert not query({'value': 2})
3840
assert hash(query)
3941

4042

tests/test_storages.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ def test_create_dirs():
8787
db_file = os.path.join(db_dir, 'db.json')
8888
break
8989

90+
with pytest.raises(OSError):
91+
JSONStorage(db_file)
92+
9093
JSONStorage(db_file, create_dirs=True).close()
9194
assert os.path.exists(db_file)
9295

@@ -130,3 +133,22 @@ class MyStorage(Storage):
130133

131134
with pytest.raises(TypeError):
132135
MyStorage()
136+
137+
138+
def test_custom_with_exception():
139+
class MyStorage(Storage):
140+
def read(self):
141+
pass
142+
143+
def write(self, data):
144+
pass
145+
146+
def __init__(self):
147+
raise ValueError()
148+
149+
def close(self):
150+
raise RuntimeError()
151+
152+
with pytest.raises(ValueError):
153+
with TinyDB(storage=MyStorage) as db:
154+
pass

tests/test_tinydb.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,10 +435,14 @@ def test_non_default_table():
435435
assert set(['non-default']) == db.tables()
436436

437437
db.purge_tables()
438+
default_table = TinyDB.DEFAULT_TABLE
439+
438440
TinyDB.DEFAULT_TABLE = 'non-default'
439441
db = TinyDB(storage=MemoryStorage)
440442
assert set(['non-default']) == db.tables()
441443

444+
TinyDB.DEFAULT_TABLE = default_table
445+
442446

443447
def test_purge_table():
444448
db = TinyDB(storage=MemoryStorage)
@@ -454,6 +458,7 @@ def test_purge_table():
454458

455459
db.purge_table(table_name)
456460
assert set([TinyDB.DEFAULT_TABLE]) == db.tables()
461+
assert table_name not in db._table_cache
457462

458463
db.purge_table('non-existent-table-name')
459464
assert set([TinyDB.DEFAULT_TABLE]) == db.tables()
@@ -481,9 +486,13 @@ def test_query_cache():
481486

482487
results = db.search(query)
483488
assert len(results) == 1
484-
# Now, modify the result ist
485-
results.extend([1])
486489

490+
# Modify the db instance to not return any results when
491+
# bypassing the query cache
492+
db._table_cache[TinyDB.DEFAULT_TABLE]._read = lambda: {}
493+
494+
# Make sure we got an independent copy of the result list
495+
results.extend([1])
487496
assert db.search(query) == [{'name': 'foo', 'value': 42}]
488497

489498

tinydb/database.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ def __init__(self, *args, **kwargs):
7979
table = kwargs.pop('default_table', self.DEFAULT_TABLE)
8080

8181
# Prepare the storage
82-
self._opened = False
83-
8482
#: :type: Storage
8583
self._storage = storage(*args, **kwargs)
8684

@@ -157,7 +155,7 @@ def __enter__(self):
157155
return self
158156

159157
def __exit__(self, *args):
160-
if self._opened is True:
158+
if self._opened:
161159
self.close()
162160

163161
def __getattr__(self, name):

tinydb/storages.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
import json
1616

1717

18-
def touch(fname, times=None, create_dirs=False):
18+
def touch(fname, create_dirs):
1919
if create_dirs:
2020
base_dir = os.path.dirname(fname)
2121
if not os.path.exists(base_dir):
2222
os.makedirs(base_dir)
23+
2324
with open(fname, 'a'):
24-
os.utime(fname, times)
25+
os.utime(fname, None)
2526

2627

2728
class Storage(with_metaclass(ABCMeta, object)):

0 commit comments

Comments
 (0)