-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathtest_dbschemelaoder.py
More file actions
168 lines (137 loc) · 3.71 KB
/
test_dbschemelaoder.py
File metadata and controls
168 lines (137 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import sys
from copy import deepcopy
from misc.codegen.lib import dbscheme
from misc.codegen.loaders.dbschemeloader import iterload
from misc.codegen.test.utils import *
@pytest.fixture
def load(tmp_path):
file = tmp_path / "test.dbscheme"
def ret(yml):
write(file, yml)
return list(iterload(file))
return ret
def test_load_empty(load):
assert load("") == []
def test_load_one_empty_table(load):
assert (
load(
"""
test_foos();
"""
)
== [dbscheme.Table(name="test_foos", columns=[])]
)
def test_load_table_with_keyset(load):
assert (
load(
"""
#keyset[x, y,z]
test_foos();
"""
)
== [
dbscheme.Table(
name="test_foos", columns=[], keyset=dbscheme.KeySet(["x", "y", "z"])
)
]
)
expected_columns = [
("int foo: int ref", dbscheme.Column(schema_name="foo", type="int", binding=False)),
(
" int bar : int ref",
dbscheme.Column(schema_name="bar", type="int", binding=False),
),
(
"str baz_: str ref",
dbscheme.Column(schema_name="baz", type="str", binding=False),
),
("int x: @foo ref", dbscheme.Column(schema_name="x", type="@foo", binding=False)),
("int y: @foo", dbscheme.Column(schema_name="y", type="@foo", binding=True)),
("unique int z: @foo", dbscheme.Column(schema_name="z", type="@foo", binding=True)),
]
@pytest.mark.parametrize("column,expected", expected_columns)
def test_load_table_with_column(load, column, expected):
assert (
load(
f"""
foos(
{column}
);
"""
)
== [dbscheme.Table(name="foos", columns=[deepcopy(expected)])]
)
def test_load_table_with_multiple_columns(load):
columns = ",\n".join(c for c, _ in expected_columns)
expected = [deepcopy(e) for _, e in expected_columns]
assert (
load(
f"""
foos(
{columns}
);
"""
)
== [dbscheme.Table(name="foos", columns=expected)]
)
def test_load_table_with_multiple_columns_and_dir(load):
columns = ",\n".join(c for c, _ in expected_columns)
expected = [deepcopy(e) for _, e in expected_columns]
assert (
load(
f"""
foos( //dir=foo/bar/baz
{columns}
);
"""
)
== [
dbscheme.Table(
name="foos", columns=expected, dir=pathlib.Path("foo/bar/baz")
)
]
)
def test_load_multiple_table_with_columns(load):
tables = [f"table{i}({col});" for i, (col, _) in enumerate(expected_columns)]
expected = [
dbscheme.Table(name=f"table{i}", columns=[deepcopy(e)])
for i, (_, e) in enumerate(expected_columns)
]
assert load("\n".join(tables)) == expected
def test_union(load):
assert load("@foo = @bar | @baz | @bla;") == [
dbscheme.Union(lhs="@foo", rhs=["@bar", "@baz", "@bla"]),
]
def test_table_and_union(load):
assert (
load(
"""
foos();
@foo = @bar | @baz | @bla;"""
)
== [
dbscheme.Table(name="foos", columns=[]),
dbscheme.Union(lhs="@foo", rhs=["@bar", "@baz", "@bla"]),
]
)
def test_comments_ignored(load):
assert (
load(
"""
// fake_table();
foos(/* x */unique /*y*/int/*
z
*/ id/* */: /* * */ @bar/*,
int ignored: int ref*/);
@foo = @bar | @baz | @bla; // | @xxx"""
)
== [
dbscheme.Table(
name="foos",
columns=[dbscheme.Column(schema_name="id", type="@bar", binding=True)],
),
dbscheme.Union(lhs="@foo", rhs=["@bar", "@baz", "@bla"]),
]
)
if __name__ == "__main__":
sys.exit(pytest.main([__file__] + sys.argv[1:]))