Upgrade to Python 3.8 + duplicate check when adding in sdp

This commit is contained in:
2019-11-06 16:01:58 +01:00
parent 0d2adf1b6c
commit b818c992ec
6 changed files with 244 additions and 39 deletions
+13 -9
View File
@@ -39,6 +39,18 @@ def null():
def b(operator, left, right):
return BinaryNode([], operator, left, right)
def compare_ast(left, right):
left_as_string = ast.dump(left)
left_as_string = left_as_string.replace(", ctx=Load()", "")
left_as_string = left_as_string.replace(", kind=None", "")
right_as_string = right if isinstance(right, str) else ast.dump(right)
right_as_string = right_as_string.replace(", ctx=Load()", "")
right_as_string = right_as_string.replace(", kind=None", "")
return left_as_string == right_as_string
def test_i_can_tokenize():
source = "+*-/{}[]() ,;:.?\n\n\r\r\r\nidentifier_0\t \t10.15 10 'string\n' \"another string\"="
@@ -213,19 +225,11 @@ def test_i_can_parse_def_concept(text, expected_name, expected_expr):
assert isinstance(tree, DefConceptNode)
assert tree.name == expected_name
if isinstance(tree.body, PythonNode):
assert ast.dump(tree.body.ast) == ast.dump(expected_expr)
assert compare_ast(tree.body.ast, expected_expr)
else:
assert tree.body == expected_expr
def compare_ast(left, right):
left_as_string = ast.dump(left)
left_as_string = left_as_string.replace(", ctx=Load()", "")
right_as_string = right if isinstance(right, str) else ast.dump(right)
right_as_string = right_as_string.replace(", ctx=Load()", "")
return left_as_string == right_as_string
def test_i_can_parse_complex_def_concept_statement():
+3 -3
View File
@@ -6,7 +6,7 @@ from os import path
import shutil
from core.concept import Concept, ConceptParts
from core.sheerka import Sheerka
from core.sheerka import Sheerka, ExecutionContext
from parsers.DefaultParser import DefConceptNode, DefaultParser
from parsers.PythonParser import PythonParser
@@ -72,7 +72,7 @@ def test_i_can_add_a_concept():
concept = get_concept()
sheerka = Sheerka()
sheerka.initialize(root_folder)
res = sheerka.add_concept(concept)
res = sheerka.add_concept(ExecutionContext("xxx"), concept)
concept_found = res.value
assert res.status
@@ -91,7 +91,7 @@ def test_i_can_add_a_concept():
assert all_props == ["a", "b"]
assert concept_found.key == "__var__0 + __var__1"
assert concept_found.id == "100"
assert concept_found.id == "1001"
# def test_i_cannot_add_the_same_concept_twice():
# concept1 = DefConceptNode(name="concept")
+114 -4
View File
@@ -3,7 +3,8 @@ import hashlib
import pytest
import os
from os import path
from sdp.sheerkaDataProvider import SheerkaDataProvider, Event, SheerkaDataProviderError
from sdp.sheerkaDataProvider import SheerkaDataProvider, Event, SheerkaDataProviderError, \
SheerkaDataProviderDuplicateKeyError
from datetime import date, datetime
import shutil
import json
@@ -106,6 +107,49 @@ class ObjDumpJson:
self.key = as_dict["key"]
class ObjWithDigestNoKey:
def __init__(self, a, b):
self.a = a
self.b = b
def __hash__(self):
return hash((self.a, self.b))
def __eq__(self, obj):
return isinstance(obj, ObjNoKey) and \
self.a == obj.a and \
self.b == obj.b
def __repr__(self):
return f"ObjNoKey({self.a}, {self.b})"
def get_digest(self):
return str(self.a) + str(self.b)
class ObjWithDigestWithKey:
def __init__(self, a, b):
self.a = a
self.b = b
def __hash__(self):
return hash((self.a, self.b))
def __eq__(self, obj):
return isinstance(obj, ObjNoKey) and \
self.a == obj.a and \
self.b == obj.b
def __repr__(self):
return f"ObjNoKey({self.a}, {self.b})"
def get_key(self):
return self.a
def get_digest(self):
return str(self.a) + str(self.b)
@pytest.fixture(autouse=True)
def init_test():
if path.exists(tests_root):
@@ -408,6 +452,72 @@ def test_i_can_add_string_using_auto_generated_key():
assert key3 == "1"
def test_i_cannot_add_the_same_digest_twice_in_the_same_entry():
"""
If get_digest() is implemented, checks for duplicates
:return:
"""
sdp = SheerkaDataProvider(".sheerka")
with pytest.raises(SheerkaDataProviderDuplicateKeyError) as error:
sdp.add(evt_digest, "entry", ObjWithDigestNoKey("a", "b"))
sdp.add(evt_digest, "entry", ObjWithDigestNoKey("a", "b"))
assert error.value.obj.get_digest() == ObjWithDigestNoKey("a", "b").get_digest()
assert error.value.key == "entry"
assert error.value.args[0] == "duplicate key"
def test_i_cannot_add_the_same_digest_twice_in_the_same_entry2():
"""
If get_digest() is implemented, checks for duplicates in list when no key
:return:
"""
sdp = SheerkaDataProvider(".sheerka")
with pytest.raises(SheerkaDataProviderDuplicateKeyError) as error:
sdp.add(evt_digest, "entry", ObjWithDigestNoKey("a", "b"))
sdp.add(evt_digest, "entry", ObjWithDigestNoKey("a", "c"))
sdp.add(evt_digest, "entry", ObjWithDigestNoKey("a", "b"))
assert error.value.obj.get_digest() == ObjWithDigestNoKey("a", "b").get_digest()
assert error.value.key == "entry"
assert error.value.args[0] == "duplicate key"
def test_i_cannot_add_the_same_digest_twice_in_the_same_entry3():
"""
If get_digest() is implemented, checks for duplicates when the key is provided
:return:
"""
sdp = SheerkaDataProvider(".sheerka")
with pytest.raises(SheerkaDataProviderDuplicateKeyError) as error:
sdp.add(evt_digest, "entry", ObjWithDigestWithKey("a", "b"))
sdp.add(evt_digest, "entry", ObjWithDigestWithKey("a", "b"))
assert error.value.obj.get_digest() == ObjWithDigestWithKey("a", "b").get_digest()
assert error.value.key == "entry.a"
assert error.value.args[0] == "duplicate key"
def test_i_cannot_add_the_same_digest_twice_in_the_same_entry4():
"""
If get_digest() is implemented, checks for duplicates in list when the key is provided
:return:
"""
sdp = SheerkaDataProvider(".sheerka")
with pytest.raises(SheerkaDataProviderDuplicateKeyError) as error:
sdp.add(evt_digest, "entry", ObjWithDigestWithKey("a", "b"))
sdp.add(evt_digest, "entry", ObjWithDigestWithKey("a", "c"))
sdp.add(evt_digest, "entry", ObjWithDigestWithKey("a", "b"))
assert error.value.obj.get_digest() == ObjWithDigestWithKey("a", "b").get_digest()
assert error.value.key == "entry.a"
assert error.value.args[0] == "duplicate key"
def test_i_can_get_and_set_key():
sdp = SheerkaDataProvider(".sheerka")
key_file = path.join(sdp.root, SheerkaDataProvider.KeysFile)
@@ -499,12 +609,12 @@ def test_i_can_set_using_reference():
entry, key = sdp.set(evt_digest, "entry", ObjWithKey(2, "foo"), use_ref=True)
state = sdp.load_state(sdp.get_snapshot())
assert state.data == {"entry": {"2": '##REF##:9b14e03847d73c640f54ea9b46ba62b19e5451ecd300428a225be012ad9f25f9'}}
assert state.data == {"entry": {"2": '##REF##:95b5cbab545dded0b90b57a3d15a157b9a559fb586ee2f8d6ccbc6d2491f1268'}}
assert entry == "entry"
assert key == "2"
assert path.exists(sdp.get_obj_path(SheerkaDataProvider.ObjectsFolder,
"9b14e03847d73c640f54ea9b46ba62b19e5451ecd300428a225be012ad9f25f9"))
"95b5cbab545dded0b90b57a3d15a157b9a559fb586ee2f8d6ccbc6d2491f1268"))
# sanity check, make sure that I can load back
loaded = sdp.get(entry, key)
@@ -873,7 +983,7 @@ def test_i_can_modify_a_ref():
state = sdp.load_state(sdp.get_snapshot())
assert state.data == {"entry": {
"key1": ObjWithKey("key1", "foo"),
"key2": "##REF##:eb297e98710dd17244bb0e38eb9f1bf72cba692a8f8d94e9eb2d898e130cac8b"}}
"key2": "##REF##:d70b0247311645ed18d275337cbcf79ad186d995236cdc8ad4fcfc708085bd3d"}}
assert entry == "entry"
assert key == "key2"