@@ -48,5 +48,107 @@ def test_union_has_first_case_marked():
4848 assert [c .type for c in u .rhs ] == rhs
4949
5050
51+ # load tests
52+ @pytest .fixture
53+ def load (tmp_path ):
54+ file = tmp_path / "test.dbscheme"
55+
56+ def ret (yml ):
57+ write (file , yml )
58+ return list (dbscheme .iterload (file ))
59+
60+ return ret
61+
62+
63+ def test_load_empty (load ):
64+ assert load ("" ) == []
65+
66+
67+ def test_load_one_empty_table (load ):
68+ assert load ("""
69+ test_foos();
70+ """ ) == [
71+ dbscheme .Table (name = "test_foos" , columns = [])
72+ ]
73+
74+
75+ def test_load_table_with_keyset (load ):
76+ assert load ("""
77+ #keyset[x, y,z]
78+ test_foos();
79+ """ ) == [
80+ dbscheme .Table (name = "test_foos" , columns = [], keyset = dbscheme .KeySet (["x" , "y" , "z" ]))
81+ ]
82+
83+
84+ expected_columns = [
85+ ("int foo: int ref" , dbscheme .Column (schema_name = "foo" , type = "int" , binding = False )),
86+ (" int bar : int ref" , dbscheme .Column (schema_name = "bar" , type = "int" , binding = False )),
87+ ("str baz_: str ref" , dbscheme .Column (schema_name = "baz" , type = "str" , binding = False )),
88+ ("int x: @foo ref" , dbscheme .Column (schema_name = "x" , type = "@foo" , binding = False )),
89+ ("int y: @foo" , dbscheme .Column (schema_name = "y" , type = "@foo" , binding = True )),
90+ ("unique int z: @foo" , dbscheme .Column (schema_name = "z" , type = "@foo" , binding = True )),
91+ ]
92+
93+
94+ @pytest .mark .parametrize ("column,expected" , expected_columns )
95+ def test_load_table_with_column (load , column , expected ):
96+ assert load (f"""
97+ foos(
98+ { column }
99+ );
100+ """ ) == [
101+ dbscheme .Table (name = "foos" , columns = [deepcopy (expected )])
102+ ]
103+
104+
105+ def test_load_table_with_multiple_columns (load ):
106+ columns = ",\n " .join (c for c , _ in expected_columns )
107+ expected = [deepcopy (e ) for _ , e in expected_columns ]
108+ assert load (f"""
109+ foos(
110+ { columns }
111+ );
112+ """ ) == [
113+ dbscheme .Table (name = "foos" , columns = expected )
114+ ]
115+
116+
117+ def test_load_multiple_table_with_columns (load ):
118+ tables = [f"table{ i } ({ col } );" for i , (col , _ ) in enumerate (expected_columns )]
119+ expected = [dbscheme .Table (name = f"table{ i } " , columns = [deepcopy (e )]) for i , (_ , e ) in enumerate (expected_columns )]
120+ assert load ("\n " .join (tables )) == expected
121+
122+
123+ def test_union (load ):
124+ assert load ("@foo = @bar | @baz | @bla;" ) == [
125+ dbscheme .Union (lhs = "@foo" , rhs = ["@bar" , "@baz" , "@bla" ]),
126+ ]
127+
128+
129+ def test_table_and_union (load ):
130+ assert load ("""
131+ foos();
132+
133+ @foo = @bar | @baz | @bla;""" ) == [
134+ dbscheme .Table (name = "foos" , columns = []),
135+ dbscheme .Union (lhs = "@foo" , rhs = ["@bar" , "@baz" , "@bla" ]),
136+ ]
137+
138+
139+ def test_comments_ignored (load ):
140+ assert load ("""
141+ // fake_table();
142+ foos(/* x */unique /*y*/int/*
143+ z
144+ */ id/* */: /* * */ @bar/*,
145+ int ignored: int ref*/);
146+
147+ @foo = @bar | @baz | @bla; // | @xxx""" ) == [
148+ dbscheme .Table (name = "foos" , columns = [dbscheme .Column (schema_name = "id" , type = "@bar" , binding = True )]),
149+ dbscheme .Union (lhs = "@foo" , rhs = ["@bar" , "@baz" , "@bla" ]),
150+ ]
151+
152+
51153if __name__ == '__main__' :
52154 sys .exit (pytest .main ())
0 commit comments