From d54d6cdcee78849d8cc79260cfe8dd7c31ef39c9 Mon Sep 17 00:00:00 2001 From: Markus Siemens Date: Fri, 17 May 2019 20:12:27 +0200 Subject: [PATCH] Tests: Skip incompatible tests when `ujson` is installed Closes #262 --- tests/test_storages.py | 8 ++++++++ tests/test_tinydb.py | 27 +++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/tests/test_storages.py b/tests/test_storages.py index bf2fde83..021f77b7 100644 --- a/tests/test_storages.py +++ b/tests/test_storages.py @@ -13,6 +13,13 @@ random.seed() +try: + import ujson as json +except ImportError: + HAS_UJSON = False +else: + HAS_UJSON = True + doc = {'none': [None, None], 'int': 42, 'float': 3.1415899999999999, 'list': ['LITE', 'RES_ACID', 'SUS_DEXT'], 'dict': {'hp': 13, 'sp': 5}, @@ -30,6 +37,7 @@ def test_json(tmpdir): storage.close() +@pytest.mark.skipif(HAS_UJSON, reason="not compatible with ujson") def test_json_kwargs(tmpdir): db_file = tmpdir.join('test.db') db = TinyDB(str(db_file), sort_keys=True, indent=4, separators=(',', ': ')) diff --git a/tests/test_tinydb.py b/tests/test_tinydb.py index 43b41cf5..e2c650b0 100644 --- a/tests/test_tinydb.py +++ b/tests/test_tinydb.py @@ -4,10 +4,17 @@ import pytest -from tinydb import TinyDB, where +from tinydb import TinyDB, where, Query from tinydb.middlewares import Middleware, CachingMiddleware from tinydb.storages import MemoryStorage +try: + import ujson as json +except ImportError: + HAS_UJSON = False +else: + HAS_UJSON = True + def test_purge(db): db.purge() @@ -539,6 +546,7 @@ def test_insert_string(tmpdir): _db.insert({'int': 3}) # Does not fail +@pytest.mark.skipif(HAS_UJSON, reason="not compatible with ujson") def test_insert_invalid_dict(tmpdir): path = str(tmpdir.join('db.json')) @@ -547,7 +555,7 @@ def test_insert_invalid_dict(tmpdir): _db.insert_multiple(data) with pytest.raises(TypeError): - _db.insert({'int': {'bark'}}) # Fails + _db.insert({'int': _db}) # Fails assert data == _db.all() @@ -780,6 +788,21 @@ def test_repr(tmpdir): repr(TinyDB(path))) +def test_delete(tmpdir): + path = str(tmpdir.join('db.json')) + + db = TinyDB(path, ensure_ascii=False) + q = Query() + db.insert({'network': {'id': '114', 'name': 'ok', 'rpc': 'dac', + 'ticker': 'mkay'}}) + assert db.search(q.network.id == '114') == [ + {'network': {'id': '114', 'name': 'ok', 'rpc': 'dac', + 'ticker': 'mkay'}} + ] + db.remove(q.network.id == '114') + assert db.search(q.network.id == '114') == [] + + def test_insert_multiple_with_single_dict(db): with pytest.raises(ValueError): d = {'first': 'John', 'last': 'smith'}