-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #212 from singnet/imp/issue-203-review-unit-and-in…
…tegration-tests [#203] Review AtomDB tests
- Loading branch information
Showing
8 changed files
with
1,387 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from hyperon_das_atomdb.adapters.ram_only import InMemoryDB | ||
from tests.unit.fixtures import in_memory_db # noqa: F401 | ||
from tests.unit.test_database import _check_handle | ||
|
||
|
||
class TestRamOnlyExtra: | ||
def test__build_atom_type_key_hash(self, in_memory_db): # noqa: F811 | ||
db: InMemoryDB = in_memory_db | ||
hash = db._build_atom_type_key_hash("A") | ||
assert _check_handle(hash) | ||
assert hash == "2c832bdcd9d74bf961205676d861540a" | ||
|
||
def test__delete_atom_type(self, in_memory_db): # noqa: F811 | ||
db: InMemoryDB = in_memory_db | ||
node = db.add_node({"name": "A", "type": "A"}) | ||
assert len(db.all_named_types) == 1 | ||
assert node["named_type"] in db.all_named_types | ||
db._delete_atom_type("A") | ||
assert len(db.all_named_types) == 0 | ||
assert node["named_type"] not in db.all_named_types | ||
|
||
def test__update_atom_indexes(self, in_memory_db): # noqa: F811 | ||
db: InMemoryDB = in_memory_db | ||
node = db.add_node({"name": "A", "type": "A"}) | ||
db._update_atom_indexes([node]) | ||
assert len(db.all_named_types) == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from unittest import mock | ||
|
||
import pytest | ||
|
||
from hyperon_das_atomdb.adapters.redis_mongo_db import MongoDBIndex, RedisMongoDB, _HashableDocument | ||
from tests.unit.fixtures import redis_mongo_db # noqa: F401 | ||
|
||
|
||
class TestRedisMongoExtra: | ||
def test_hashable_document_str(self, redis_mongo_db): # noqa: F811 | ||
db = redis_mongo_db | ||
node = db._build_node({"type": "A", "name": "A"}) | ||
hashable = _HashableDocument(node) | ||
str_hashable = str(hashable) | ||
assert isinstance(str_hashable, str) | ||
assert hashable | ||
assert str(node) == str_hashable | ||
|
||
@pytest.mark.parametrize( | ||
"params", | ||
[ | ||
{"atom_type": "A", "fields": []}, | ||
{"atom_type": "A", "fields": None}, | ||
], | ||
) | ||
def test_index_create_exceptions(self, params, request): | ||
db = request.getfixturevalue("redis_mongo_db") | ||
mi = MongoDBIndex(db.mongo_db) | ||
with pytest.raises(ValueError): | ||
mi.create(**params) | ||
|
||
@mock.patch( | ||
"hyperon_das_atomdb.adapters.redis_mongo_db.MongoClient", return_value=mock.MagicMock() | ||
) | ||
@mock.patch("hyperon_das_atomdb.adapters.redis_mongo_db.Redis", return_value=mock.MagicMock()) | ||
@mock.patch( | ||
"hyperon_das_atomdb.adapters.redis_mongo_db.RedisCluster", return_value=mock.MagicMock() | ||
) | ||
def test_create_db_connection_mongo(self, mock_mongo, mock_redis, mock_redis_cluster): | ||
RedisMongoDB(mongo_tls_ca_file="/tmp/mock", redis_password="12", redis_username="A") | ||
RedisMongoDB(redis_cluster=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
from unittest import mock | ||
|
||
import mongomock | ||
import pytest | ||
|
||
from hyperon_das_atomdb.adapters.ram_only import InMemoryDB | ||
from hyperon_das_atomdb.adapters.redis_mongo_db import MongoCollectionNames, RedisMongoDB | ||
|
||
|
||
class MockRedis: | ||
def __init__(self, cache=dict()): | ||
self.cache = cache | ||
|
||
def get(self, key): | ||
if key in self.cache: | ||
return self.cache[key] | ||
return None | ||
|
||
def set(self, key, value, *args, **kwargs): | ||
if self.cache: | ||
self.cache[key] = value | ||
return "OK" | ||
return None | ||
|
||
def hget(self, hash, key): | ||
if hash in self.cache: | ||
if key in self.cache[hash]: | ||
return self.cache[hash][key] | ||
return None | ||
|
||
def hset(self, hash, key, value, *args, **kwargs): | ||
if self.cache: | ||
self.cache[hash][key] = value | ||
return 1 | ||
return None | ||
|
||
def exists(self, key): | ||
if key in self.cache: | ||
return 1 | ||
return 0 | ||
|
||
def cache_overwrite(self, cache=dict()): | ||
self.cache = cache | ||
|
||
def sadd(self, key, *members): | ||
if key not in self.cache: | ||
self.cache[key] = set() | ||
before_count = len(self.cache[key]) | ||
self.cache[key].update(members) | ||
after_count = len(self.cache[key]) | ||
return after_count - before_count | ||
|
||
def smembers(self, key): | ||
if key in self.cache: | ||
return self.cache[key] | ||
return set() | ||
|
||
def flushall(self): | ||
self.cache.clear() | ||
|
||
def delete(self, *keys): | ||
deleted_count = 0 | ||
for key in keys: | ||
if key in self.cache: | ||
del self.cache[key] | ||
deleted_count += 1 | ||
return deleted_count | ||
|
||
def getdel(self, key): | ||
value = self.cache.get(key) | ||
if key in self.cache: | ||
del self.cache[key] | ||
return value | ||
|
||
def srem(self, key, *members): | ||
if key not in self.cache: | ||
return 0 | ||
initial_count = len(self.cache[key]) | ||
self.cache[key].difference_update(members) | ||
removed_count = initial_count - len(self.cache[key]) | ||
return removed_count | ||
|
||
def sscan(self, name, cursor=0, match=None, count=None): | ||
key = name | ||
if key not in self.cache: | ||
return (0, []) | ||
|
||
elements = list(self.cache[key]) | ||
if match: | ||
elements = [e for e in elements if match in e] | ||
start = cursor | ||
end = min(start + (count if count else len(elements)), len(elements)) | ||
new_cursor = end if end < len(elements) else 0 | ||
|
||
return (new_cursor, elements[start:end]) | ||
|
||
|
||
@pytest.fixture | ||
def redis_mongo_db(): | ||
mongo_db = mongomock.MongoClient().db | ||
redis_db = MockRedis() | ||
with mock.patch( | ||
"hyperon_das_atomdb.adapters.redis_mongo_db.RedisMongoDB._connection_mongo_db", | ||
return_value=mongo_db, | ||
), mock.patch( | ||
"hyperon_das_atomdb.adapters.redis_mongo_db.RedisMongoDB._connection_redis", | ||
return_value=redis_db, | ||
): | ||
db = RedisMongoDB() | ||
db.mongo_atoms_collection = mongo_db.collection | ||
db.mongo_types_collection = mongo_db.collection | ||
|
||
db.all_mongo_collections = [ | ||
(MongoCollectionNames.ATOMS, db.mongo_atoms_collection), | ||
(MongoCollectionNames.ATOM_TYPES, db.mongo_types_collection), | ||
] | ||
db.mongo_bulk_insertion_buffer = { | ||
MongoCollectionNames.ATOMS: tuple([db.mongo_atoms_collection, set()]), | ||
MongoCollectionNames.ATOM_TYPES: tuple([db.mongo_types_collection, set()]), | ||
} | ||
|
||
yield db | ||
|
||
|
||
@pytest.fixture | ||
def in_memory_db(): | ||
db = InMemoryDB() | ||
yield db |
Oops, something went wrong.