def test_json2obj_fail_invalid_entity(use_testdb): pd = PonyDump(testdb) with pytest.raises(ValueError) as excinfo: pd.json2obj({'_entity': 'wtfisthat', 'id': 1}) assert str(excinfo.value) == 'Unknown entity "wtfisthat"'
def test_json2obj_fail_unknown_attr(use_testdb): pd = PonyDump(testdb) with pytest.raises(ValueError) as excinfo: # test4 ссылается не на тот PDTest4, который прописан в дампе самого PDTest4 pd.json2obj({'_entity': 'pdtest4', 'id': 1, 'wtfisthat': 'that', 'andthat': 'that'}) x = 'Unknown attributes in dump of model "pdtest4": ' assert str(excinfo.value) in (x + 'wtfisthat, andthat', x + 'andthat, wtfisthat')
def test_json2obj_fail_inconsistent_optional_value(use_testdb): pd = PonyDump(testdb) pd.json2obj({'_entity': 'pdtest4', 'id': 1, 'test1': (1, 2, 3)}) with pytest.raises(ValueError) as excinfo: # test4 ссылается не на тот PDTest4, который прописан в дампе самого PDTest4 pd.json2obj({'_entity': 'pdtest1', 'k1': 1, 'k2': 2, 'k3': 3, 'test4': 7}) assert str(excinfo.value) == 'Inconsistent dump: attribute "test4" of "pdtest1(1, 2, 3)" conflicts with reverse data'
def test_json2obj_fail_inconsistent_set(use_testdb): pd = PonyDump(testdb) pd.json2obj({'_entity': 'many1', 'id': 1, 'many2': [2]}) with pytest.raises(ValueError) as excinfo: # many1 не должен быть пуст, так как есть связь pd.json2obj({'_entity': 'many2', 'id': 2, 'many1': []}) assert str(excinfo.value) == 'Inconsistent dump: attribute "many1" of "many2(2,)" conflicts with reverse data'
def test_json2obj_fail_inconsistent_optional_count_2(use_testdb): pd = PonyDump(testdb) pd.json2obj({'_entity': 'pdtest4', 'id': 1, 'test1': (1, 2, 3)}) pd.json2obj({'_entity': 'pdtest4', 'id': 2, 'test1': [1, 2, 3]}) with pytest.raises(ValueError) as excinfo: # То же самое, что и в предыдущем тесте, но тут какой-нибудь test4 прописан pd.json2obj({'_entity': 'pdtest1', 'k1': 1, 'k2': 2, 'k3': 3, 'test4': 1}) assert str(excinfo.value) == 'Inconsistent dump: attribute "test4" of "pdtest1(1, 2, 3)" has multiple values in reverse data'
def test_json2obj_fail_inconsistent_optional_count(use_testdb): pd = PonyDump(testdb) pd.json2obj({'_entity': 'pdtest4', 'id': 1, 'test1': (1, 2, 3)}) pd.json2obj({'_entity': 'pdtest4', 'id': 2, 'test1': [1, 2, 3]}) with pytest.raises(ValueError) as excinfo: # test4 — Optional, а на один и тот же PDTest1 один ссылаются аж два PDTest2, так не бывает pd.json2obj({'_entity': 'pdtest1', 'k1': 1, 'k2': 2, 'k3': 3}) assert str(excinfo.value) == 'Inconsistent dump: attribute "test4" of "pdtest1(1, 2, 3)" has multiple values in reverse data'
def test_json2obj_fail_inconsistent_set(use_testdb): pd = PonyDump(testdb) pd.json2obj({'_entity': 'many1', 'id': 1, 'many2': [2]}) with pytest.raises(ValueError) as excinfo: # many1 не должен быть пуст, так как есть связь pd.json2obj({'_entity': 'many2', 'id': 2, 'many1': []}) assert str( excinfo.value ) == 'Inconsistent dump: attribute "many1" of "many2(2,)" conflicts with reverse data'
def test_json2obj_fail_inconsistent_optional_count(use_testdb): pd = PonyDump(testdb) pd.json2obj({'_entity': 'pdtest4', 'id': 1, 'test1': (1, 2, 3)}) pd.json2obj({'_entity': 'pdtest4', 'id': 2, 'test1': [1, 2, 3]}) with pytest.raises(ValueError) as excinfo: # test4 — Optional, а на один и тот же PDTest1 один ссылаются аж два PDTest2, так не бывает pd.json2obj({'_entity': 'pdtest1', 'k1': 1, 'k2': 2, 'k3': 3}) assert str( excinfo.value ) == 'Inconsistent dump: attribute "test4" of "pdtest1(1, 2, 3)" has multiple values in reverse data'
def test_json2obj_fail_unknown_attr(use_testdb): pd = PonyDump(testdb) with pytest.raises(ValueError) as excinfo: # test4 ссылается не на тот PDTest4, который прописан в дампе самого PDTest4 pd.json2obj({ '_entity': 'pdtest4', 'id': 1, 'wtfisthat': 'that', 'andthat': 'that' }) x = 'Unknown attributes in dump of model "pdtest4": ' assert str(excinfo.value) in (x + 'wtfisthat, andthat', x + 'andthat, wtfisthat')
def test_json2obj_fail_inconsistent_optional_value(use_testdb): pd = PonyDump(testdb) pd.json2obj({'_entity': 'pdtest4', 'id': 1, 'test1': (1, 2, 3)}) with pytest.raises(ValueError) as excinfo: # test4 ссылается не на тот PDTest4, который прописан в дампе самого PDTest4 pd.json2obj({ '_entity': 'pdtest1', 'k1': 1, 'k2': 2, 'k3': 3, 'test4': 7 }) assert str( excinfo.value ) == 'Inconsistent dump: attribute "test4" of "pdtest1(1, 2, 3)" conflicts with reverse data'
def test_json2obj_fail_inconsistent_optional_count_2(use_testdb): pd = PonyDump(testdb) pd.json2obj({'_entity': 'pdtest4', 'id': 1, 'test1': (1, 2, 3)}) pd.json2obj({'_entity': 'pdtest4', 'id': 2, 'test1': [1, 2, 3]}) with pytest.raises(ValueError) as excinfo: # То же самое, что и в предыдущем тесте, но тут какой-нибудь test4 прописан pd.json2obj({ '_entity': 'pdtest1', 'k1': 1, 'k2': 2, 'k3': 3, 'test4': 1 }) assert str( excinfo.value ) == 'Inconsistent dump: attribute "test4" of "pdtest1(1, 2, 3)" has multiple values in reverse data'
def test_json2obj_ok_datetime_obj(use_testdb): pd = PonyDump(testdb) result = pd.json2obj({ "_entity": "pdtest3", "foo_bool": True, "foo_float": 0.30000000000000004, "foo_int": -4, "foo_longstr": "longstr", "foo_string": "foo\\u0000😊bar", "foo_uuid": "8e8cdc11-0785-43a8-8203-66c148c3f57c", "id": 1, "test1": [1, 2, 3], "foo_datetime": datetime(1970, 1, 1, 2, 0, 3, 999987), }) assert result['obj'].foo_datetime == datetime(1970, 1, 1, 2, 0, 3, 999987)
def test_json2obj_ok_uuid_obj(use_testdb): pd = PonyDump(testdb) u = uuid.uuid4() result = pd.json2obj({ "_entity": "pdtest3", "foo_bool": True, "foo_datetime": "2017-06-01T01:02:03.999987Z", "foo_float": 0.30000000000000004, "foo_int": -4, "foo_longstr": "longstr", "foo_string": "foo\\u0000😊bar", "id": 1, "test1": [1, 2, 3], "foo_uuid": u, }) assert result['obj'].foo_uuid == u
def test_json2obj_ok_entity_case_insensitive(use_testdb): pd = PonyDump(testdb) result = pd.json2obj({'_entity': 'mAnY1', 'id': 7, "many2": []}) assert isinstance(result['obj'], Many1) assert result['obj'].id == 7