mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-22 06:51:32 +03:00
91aee7fdeb
We add a new pytest flag `--accept` that will automatically write back yaml files with updated responses. This makes it much easier and less error-prone to update test cases when we expect output to change, or when authoring new tests. Second we make sure to test that we actually preserve the order of the selection set when returning results. This is a "SHOULD" part of the spec but seems pretty important and something that users will rely on. To support both of the above we use ruamel.yaml which preserves a certain amount of formatting and comments (so that --accept can work in a failry ergonomic way), as well as ordering (so that when we write yaml the order of keys has meaning that's preserved during parsing). Use ruamel.yaml everywhere for consistency (since both libraries have different quirks). Quirks of ruamel.yaml: - trailing whitespace in multiline strings in yaml files isn't written back out as we'd like: https://bitbucket.org/ruamel/yaml/issues/47/multiline-strings-being-changed-if-they - formatting is only sort of preserved; ruamel e.g. normalizes indentation. Normally the diff is pretty clean though, and you can always just check in portions of your test file after --accept fixup
88 lines
2.5 KiB
Python
88 lines
2.5 KiB
Python
import pytest
|
|
import ruamel.yaml as yaml
|
|
import json
|
|
import jsondiff
|
|
|
|
class TestInconsistentObjects():
|
|
|
|
get_inconsistent_metadata = {
|
|
"type": "get_inconsistent_metadata",
|
|
"args": {}
|
|
}
|
|
reload_metadata = {
|
|
"type": "reload_metadata",
|
|
"args": {}
|
|
}
|
|
drop_inconsistent_metadata = {
|
|
"type": "drop_inconsistent_metadata",
|
|
"args": {}
|
|
}
|
|
export_metadata = {
|
|
"type": "export_metadata",
|
|
"args": {}
|
|
}
|
|
|
|
def test_inconsistent_objects(self, hge_ctx):
|
|
with open(self.dir() + "/test.yaml") as c:
|
|
test = yaml.safe_load(c)
|
|
|
|
# setup
|
|
st_code, resp = hge_ctx.v1q(json.loads(json.dumps(test['setup'])))
|
|
assert st_code == 200, resp
|
|
|
|
# exec sql to cause inconsistentancy
|
|
sql_res = hge_ctx.sql(test['sql'])
|
|
|
|
# reload metadata
|
|
st_code, resp = hge_ctx.v1q(q=self.reload_metadata)
|
|
assert st_code == 200, resp
|
|
|
|
# fetch inconsistent objects
|
|
st_code, resp = hge_ctx.v1q(q=self.get_inconsistent_metadata)
|
|
assert st_code == 200, resp
|
|
incons_objs_test = test['inconsistent_objects']
|
|
incons_objs_resp = resp['inconsistent_objects']
|
|
|
|
assert resp['is_consistent'] == False, resp
|
|
assert incons_objs_resp == incons_objs_test, yaml.dump({
|
|
'response': resp,
|
|
'expected': incons_objs_test,
|
|
'diff': jsondiff.diff(incons_objs_test, resp)
|
|
})
|
|
|
|
# export metadata
|
|
st_code, export = hge_ctx.v1q(q=self.export_metadata)
|
|
assert st_code == 200, export
|
|
|
|
# apply metadata
|
|
st_code, resp = hge_ctx.v1q(
|
|
q={
|
|
"type": "replace_metadata",
|
|
"args": export
|
|
}
|
|
)
|
|
assert st_code == 400, resp
|
|
|
|
# drop inconsistent objects
|
|
st_code, resp = hge_ctx.v1q(q=self.drop_inconsistent_metadata)
|
|
assert st_code == 200, resp
|
|
|
|
# reload metadata
|
|
st_code, resp = hge_ctx.v1q(q=self.reload_metadata)
|
|
assert st_code == 200, resp
|
|
|
|
# fetch inconsistent objects
|
|
st_code, resp = hge_ctx.v1q(q=self.get_inconsistent_metadata)
|
|
assert st_code == 200, resp
|
|
|
|
assert resp['is_consistent'] == True, resp
|
|
assert len(resp['inconsistent_objects']) == 0, resp
|
|
|
|
# teardown
|
|
st_code, resp = hge_ctx.v1q(json.loads(json.dumps(test['teardown'])))
|
|
assert st_code == 200, resp
|
|
|
|
@classmethod
|
|
def dir(cls):
|
|
return 'queries/inconsistent_objects'
|