2019-04-17 19:29:39 +03:00
|
|
|
import json
|
2019-07-22 15:47:13 +03:00
|
|
|
import jsondiff
|
2022-01-17 10:39:59 +03:00
|
|
|
from ruamel.yaml import YAML
|
|
|
|
|
|
|
|
yaml=YAML(typ='safe', pure=True)
|
2019-04-17 19:29:39 +03:00
|
|
|
|
2022-11-23 17:12:42 +03:00
|
|
|
def load_yaml(path):
|
|
|
|
with open(path) as f:
|
|
|
|
return json.loads(json.dumps(yaml.load(f)))
|
|
|
|
|
2019-04-17 19:29:39 +03:00
|
|
|
class TestInconsistentObjects():
|
|
|
|
|
|
|
|
reload_metadata = {
|
|
|
|
"type": "reload_metadata",
|
2022-11-23 17:12:42 +03:00
|
|
|
"args": {},
|
2019-04-17 19:29:39 +03:00
|
|
|
}
|
|
|
|
drop_inconsistent_metadata = {
|
|
|
|
"type": "drop_inconsistent_metadata",
|
2022-11-23 17:12:42 +03:00
|
|
|
"args": {},
|
2019-04-17 19:29:39 +03:00
|
|
|
}
|
|
|
|
export_metadata = {
|
|
|
|
"type": "export_metadata",
|
2022-11-23 17:12:42 +03:00
|
|
|
"args": {},
|
2019-04-17 19:29:39 +03:00
|
|
|
}
|
|
|
|
|
2022-11-23 17:12:42 +03:00
|
|
|
def test_inconsistent_objects(self, hge_ctx, source_backend):
|
|
|
|
setup = load_yaml(self.dir() + "/setup.yaml")
|
|
|
|
expected_inconsistent_objects = load_yaml(self.dir() + "/expectation.yaml")
|
|
|
|
teardown = load_yaml(self.dir() + "/teardown.yaml")
|
2019-04-17 19:29:39 +03:00
|
|
|
|
2022-11-23 17:12:42 +03:00
|
|
|
hge_ctx.v1q(setup)
|
2019-04-17 19:29:39 +03:00
|
|
|
|
2021-05-21 05:46:58 +03:00
|
|
|
try:
|
|
|
|
# exec sql to cause inconsistentancy
|
2022-11-23 17:12:42 +03:00
|
|
|
# TODO: remove once parallelization work is completed
|
|
|
|
# `source_backend` will no longer be optional
|
|
|
|
if source_backend:
|
|
|
|
with source_backend.engine.connect() as connection:
|
|
|
|
connection.execute('drop table article')
|
|
|
|
else:
|
|
|
|
# this only works when the metadata database and the source database are the same
|
|
|
|
hge_ctx.sql('drop table article')
|
2021-05-21 05:46:58 +03:00
|
|
|
|
|
|
|
# reload metadata
|
2022-07-05 21:00:08 +03:00
|
|
|
resp = hge_ctx.v1q(q=self.reload_metadata)
|
2022-03-03 16:33:43 +03:00
|
|
|
# check inconsistent objects
|
2022-11-23 17:12:42 +03:00
|
|
|
actual_inconsistent_objects = resp['inconsistent_objects']
|
2021-05-21 05:46:58 +03:00
|
|
|
|
|
|
|
assert resp['is_consistent'] == False, resp
|
2022-11-23 17:12:42 +03:00
|
|
|
assert actual_inconsistent_objects == expected_inconsistent_objects, yaml.dump({
|
|
|
|
'response': actual_inconsistent_objects,
|
|
|
|
'expected': expected_inconsistent_objects,
|
|
|
|
'diff': jsondiff.diff(expected_inconsistent_objects, actual_inconsistent_objects)
|
2021-05-21 05:46:58 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
# export metadata
|
2022-07-05 21:00:08 +03:00
|
|
|
export = hge_ctx.v1q(q=self.export_metadata)
|
2021-05-21 05:46:58 +03:00
|
|
|
|
|
|
|
# apply metadata
|
2022-07-05 21:00:08 +03:00
|
|
|
hge_ctx.v1q(
|
2021-05-21 05:46:58 +03:00
|
|
|
q={
|
|
|
|
"type": "replace_metadata",
|
2022-11-23 17:12:42 +03:00
|
|
|
"args": export,
|
2022-07-05 21:00:08 +03:00
|
|
|
},
|
2022-11-23 17:12:42 +03:00
|
|
|
expected_status_code = 400,
|
2021-05-21 05:46:58 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
finally:
|
|
|
|
# drop inconsistent objects
|
2022-07-05 21:00:08 +03:00
|
|
|
hge_ctx.v1q(q=self.drop_inconsistent_metadata)
|
2021-05-21 05:46:58 +03:00
|
|
|
|
|
|
|
# reload metadata
|
2022-07-05 21:00:08 +03:00
|
|
|
resp = hge_ctx.v1q(q=self.reload_metadata)
|
2022-03-03 16:33:43 +03:00
|
|
|
# check inconsistent objects
|
2021-05-21 05:46:58 +03:00
|
|
|
assert resp['is_consistent'] == True, resp
|
|
|
|
|
2022-11-23 17:12:42 +03:00
|
|
|
hge_ctx.v1q(teardown)
|
2019-04-17 19:29:39 +03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def dir(cls):
|
|
|
|
return 'queries/inconsistent_objects'
|