/
test_compiler.py
78 lines (67 loc) · 3.02 KB
/
test_compiler.py
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
# Copyright (c) 2021 The sqlalchemy-bigquery Authors
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import pytest
import sqlalchemy.exc
from conftest import setup_table
from conftest import sqlalchemy_1_4_or_higher
def test_constraints_are_ignored(faux_conn, metadata):
sqlalchemy.Table(
"ref", metadata, sqlalchemy.Column("id", sqlalchemy.Integer),
)
sqlalchemy.Table(
"some_table",
metadata,
sqlalchemy.Column("id", sqlalchemy.Integer, primary_key=True),
sqlalchemy.Column(
"ref_id", sqlalchemy.Integer, sqlalchemy.ForeignKey("ref.id")
),
sqlalchemy.UniqueConstraint("id", "ref_id", name="uix_1"),
)
metadata.create_all(faux_conn.engine)
assert " ".join(faux_conn.test_data["execute"][-1][0].strip().split()) == (
"CREATE TABLE `some_table`" " ( `id` INT64 NOT NULL, `ref_id` INT64 )"
)
def test_compile_column(faux_conn):
table = setup_table(faux_conn, "t", sqlalchemy.Column("c", sqlalchemy.Integer))
assert table.c.c.compile(faux_conn).string == "`c`"
def test_cant_compile_unnamed_column(faux_conn, metadata):
with pytest.raises(
sqlalchemy.exc.CompileError,
match="Cannot compile Column object until its 'name' is assigned.",
):
sqlalchemy.Column(sqlalchemy.Integer).compile(faux_conn)
@sqlalchemy_1_4_or_higher
def test_no_alias_for_known_tables(faux_conn, metadata):
# See: https://github.com/googleapis/python-bigquery-sqlalchemy/issues/353
table = setup_table(
faux_conn,
"table1",
metadata,
sqlalchemy.Column("foo", sqlalchemy.Integer),
sqlalchemy.Column("bar", sqlalchemy.ARRAY(sqlalchemy.Integer)),
)
F = sqlalchemy.func
q = sqlalchemy.select(table.c.foo).where(F.unnest(table.c.bar).column_valued() == 1)
expected_sql = (
"SELECT `table1`.`foo` \n"
"FROM `table1`, unnest(`table1`.`bar`) AS `anon_1` \n"
"WHERE `anon_1` = %(param_1:INT64)s"
)
found_sql = q.compile(faux_conn).string
assert found_sql == expected_sql