Skip to content

Commit 43779ff

Browse files
SamirTalwarhasura-bot
authored andcommitted
server/tests-py: Fix test_inconsistent_meta.py for split databases.
This test did not work when splitting the metadata and source backends. Fixed mostly by running the relevant SQL using `source_backend.engine`, but I also took the time to clean it up a little, and broke up _test.yaml_ into 3 files. PR-URL: hasura/graphql-engine-mono#6957 GitOrigin-RevId: bbca60a8906caba2d0cffd834b3b8595fca058fd
1 parent 7a1dc37 commit 43779ff

File tree

5 files changed

+99
-101
lines changed

5 files changed

+99
-101
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
- definition:
2+
schema: public
3+
name: article
4+
name: table article in source default
5+
reason: 'Inconsistent object: no such table/view exists in source: "article"'
6+
type: table
7+
- definition:
8+
using:
9+
foreign_key_constraint_on:
10+
column: author_id
11+
table:
12+
schema: public
13+
name: article
14+
name: articles
15+
source: default
16+
comment:
17+
table:
18+
schema: public
19+
name: author
20+
name: array_relation articles in table author in source default
21+
reason: >-
22+
Inconsistent object: in table "author": in relationship "articles": table "article" does not exist in source: default
23+
type: array_relation
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
type: bulk
2+
args:
3+
# tables
4+
- type: run_sql
5+
args:
6+
sql: |
7+
create table author(
8+
id serial primary key,
9+
name text unique
10+
);
11+
create table article(
12+
id serial primary key,
13+
title text not null,
14+
content text,
15+
author_id integer not null references author(id)
16+
);
17+
- type: track_table
18+
args:
19+
schema: public
20+
name: author
21+
- type: track_table
22+
args:
23+
schema: public
24+
name: article
25+
26+
#Object relationship
27+
- type: create_object_relationship
28+
args:
29+
table: article
30+
name: author
31+
using:
32+
foreign_key_constraint_on: author_id
33+
34+
#Array relationship
35+
- type: create_array_relationship
36+
args:
37+
table: author
38+
name: articles
39+
using:
40+
foreign_key_constraint_on:
41+
table: article
42+
column: author_id
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
type: bulk
2+
args:
3+
- type: run_sql
4+
args:
5+
sql: |
6+
drop table author

server/tests-py/queries/inconsistent_objects/test.yaml

Lines changed: 0 additions & 81 deletions
This file was deleted.

server/tests-py/test_inconsistent_meta.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,56 @@
1-
import pytest
21
import json
32
import jsondiff
43
from ruamel.yaml import YAML
54

65
yaml=YAML(typ='safe', pure=True)
76

7+
def load_yaml(path):
8+
with open(path) as f:
9+
return json.loads(json.dumps(yaml.load(f)))
10+
811
class TestInconsistentObjects():
912

1013
reload_metadata = {
1114
"type": "reload_metadata",
12-
"args": {}
15+
"args": {},
1316
}
1417
drop_inconsistent_metadata = {
1518
"type": "drop_inconsistent_metadata",
16-
"args": {}
19+
"args": {},
1720
}
1821
export_metadata = {
1922
"type": "export_metadata",
20-
"args": {}
23+
"args": {},
2124
}
2225

23-
def test_inconsistent_objects(self, hge_ctx):
24-
with open(self.dir() + "/test.yaml") as c:
25-
test = yaml.load(c)
26+
def test_inconsistent_objects(self, hge_ctx, source_backend):
27+
setup = load_yaml(self.dir() + "/setup.yaml")
28+
expected_inconsistent_objects = load_yaml(self.dir() + "/expectation.yaml")
29+
teardown = load_yaml(self.dir() + "/teardown.yaml")
2630

27-
# setup
28-
resp = hge_ctx.v1q(json.loads(json.dumps(test['setup'])))
31+
hge_ctx.v1q(setup)
2932

3033
try:
3134
# exec sql to cause inconsistentancy
32-
hge_ctx.sql(test['sql'])
35+
# TODO: remove once parallelization work is completed
36+
# `source_backend` will no longer be optional
37+
if source_backend:
38+
with source_backend.engine.connect() as connection:
39+
connection.execute('drop table article')
40+
else:
41+
# this only works when the metadata database and the source database are the same
42+
hge_ctx.sql('drop table article')
3343

3444
# reload metadata
3545
resp = hge_ctx.v1q(q=self.reload_metadata)
3646
# check inconsistent objects
37-
incons_objs_test = test['inconsistent_objects']
38-
incons_objs_resp = resp['inconsistent_objects']
47+
actual_inconsistent_objects = resp['inconsistent_objects']
3948

4049
assert resp['is_consistent'] == False, resp
41-
assert incons_objs_resp == incons_objs_test, yaml.dump({
42-
'response': incons_objs_resp,
43-
'expected': incons_objs_test,
44-
'diff': jsondiff.diff(incons_objs_test, incons_objs_resp)
50+
assert actual_inconsistent_objects == expected_inconsistent_objects, yaml.dump({
51+
'response': actual_inconsistent_objects,
52+
'expected': expected_inconsistent_objects,
53+
'diff': jsondiff.diff(expected_inconsistent_objects, actual_inconsistent_objects)
4554
})
4655

4756
# export metadata
@@ -51,9 +60,9 @@ def test_inconsistent_objects(self, hge_ctx):
5160
hge_ctx.v1q(
5261
q={
5362
"type": "replace_metadata",
54-
"args": export
63+
"args": export,
5564
},
56-
expected_status_code = 400
65+
expected_status_code = 400,
5766
)
5867

5968
finally:
@@ -65,8 +74,7 @@ def test_inconsistent_objects(self, hge_ctx):
6574
# check inconsistent objects
6675
assert resp['is_consistent'] == True, resp
6776

68-
# teardown
69-
hge_ctx.v1q(json.loads(json.dumps(test['teardown'])))
77+
hge_ctx.v1q(teardown)
7078

7179
@classmethod
7280
def dir(cls):

0 commit comments

Comments
 (0)