ybelkada HF Staff commited on
Commit
9c4e2c9
·
verified ·
1 Parent(s): 1bbc0b5

Harbor format: 871 coding_baseline tasks (part 33)

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted-inventory.json +242 -0
  2. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/__init__.py +2 -0
  3. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/clickhouse/__init__.py +0 -0
  4. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/clickhouse/test_array.py +104 -0
  5. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/clickhouse/test_condition.py +72 -0
  6. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/clickhouse/test_dates_and_times.py +70 -0
  7. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/clickhouse/test_nullable_arg.py +24 -0
  8. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/clickhouse/test_search_string.py +41 -0
  9. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/clickhouse/test_type_conversion.py +176 -0
  10. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/dialects/__init__.py +0 -0
  11. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/dialects/test_clickhouse.py +146 -0
  12. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/dialects/test_mssql.py +81 -0
  13. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/dialects/test_mysql.py +91 -0
  14. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/dialects/test_oracle.py +51 -0
  15. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/dialects/test_postgresql.py +262 -0
  16. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/dialects/test_snowflake.py +58 -0
  17. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/dialects/test_sqllite.py +35 -0
  18. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/dialects/test_vertica.py +93 -0
  19. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_aggregate.py +122 -0
  20. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_analytic_queries.py +610 -0
  21. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_create.py +210 -0
  22. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_criterions.py +873 -0
  23. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_custom_functions.py +49 -0
  24. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_data_types.py +15 -0
  25. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_date_math.py +179 -0
  26. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_deletes.py +97 -0
  27. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_drop.py +49 -0
  28. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_formats.py +68 -0
  29. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_functions.py +847 -0
  30. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_groupby_modifiers.py +168 -0
  31. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_immutability.py +99 -0
  32. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_inserts.py +885 -0
  33. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_internals.py +35 -0
  34. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_joins.py +1168 -0
  35. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_negation.py +31 -0
  36. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_parameter.py +206 -0
  37. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_pseudocolumns.py +51 -0
  38. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_query.py +242 -0
  39. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_selects.py +1331 -0
  40. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_tables.py +186 -0
  41. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_terms.py +82 -0
  42. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_tuples.py +162 -0
  43. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_updates.py +162 -0
  44. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_utils.py +24 -0
  45. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pyproject.toml +22 -0
  46. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/setup.cfg +16 -0
  47. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/setup.py +81 -0
  48. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/tox.ini +16 -0
  49. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/validation/canaries/first-hunk-only.sh +23 -0
  50. tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/validation/canaries/original-bug.sh +3 -0
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted-inventory.json ADDED
@@ -0,0 +1,242 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "directory_roots": [
3
+ "pypika/tests"
4
+ ],
5
+ "files": {
6
+ "pypika/tests/__init__.py": {
7
+ "bytes": 58,
8
+ "mode": 420,
9
+ "sha256": "7d41f44b87688bdf162e6d5cbe7b517cfba468f96eaa421f02718c17eb486a18"
10
+ },
11
+ "pypika/tests/clickhouse/__init__.py": {
12
+ "bytes": 0,
13
+ "mode": 420,
14
+ "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
15
+ },
16
+ "pypika/tests/clickhouse/test_array.py": {
17
+ "bytes": 2942,
18
+ "mode": 420,
19
+ "sha256": "0f0a5b4c3daf3ddce2944b6119795e80d50b74e4687ef2e34399e5d21d3d48ae"
20
+ },
21
+ "pypika/tests/clickhouse/test_condition.py": {
22
+ "bytes": 2533,
23
+ "mode": 420,
24
+ "sha256": "c666976d082b1d12cd57e791d1a525183246335efad96a369b58871a6f1e4fa1"
25
+ },
26
+ "pypika/tests/clickhouse/test_dates_and_times.py": {
27
+ "bytes": 2508,
28
+ "mode": 420,
29
+ "sha256": "cb8ea84363e35ad0a00b52a8ec20cb38f2348f4cba28d026a4c32c14fd1fc56f"
30
+ },
31
+ "pypika/tests/clickhouse/test_nullable_arg.py": {
32
+ "bytes": 672,
33
+ "mode": 420,
34
+ "sha256": "883c5161ebeb09544ce124d20a096d248bd12a0404ad77c84637be75d6738529"
35
+ },
36
+ "pypika/tests/clickhouse/test_search_string.py": {
37
+ "bytes": 1189,
38
+ "mode": 420,
39
+ "sha256": "d5f0ed6a51cf550bdd4b261fa2e98084184dd1cbb08f5dbb8b91278b4c8f3bfd"
40
+ },
41
+ "pypika/tests/clickhouse/test_type_conversion.py": {
42
+ "bytes": 4445,
43
+ "mode": 420,
44
+ "sha256": "7e6045b64433a8947346cfb34ac30279349f7a2e2081a2815212c8cb7d0235ab"
45
+ },
46
+ "pypika/tests/dialects/__init__.py": {
47
+ "bytes": 0,
48
+ "mode": 420,
49
+ "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
50
+ },
51
+ "pypika/tests/dialects/test_clickhouse.py": {
52
+ "bytes": 5747,
53
+ "mode": 420,
54
+ "sha256": "2272538d6eeb6dfb01f9803eed328b4cd4efc8d255b6a8ab3c4e25bee74d73fa"
55
+ },
56
+ "pypika/tests/dialects/test_mssql.py": {
57
+ "bytes": 3152,
58
+ "mode": 420,
59
+ "sha256": "0cd9b2ee6498caa9fa75a51ead30803057fb23deb2ac706d396d88329e0e91d7"
60
+ },
61
+ "pypika/tests/dialects/test_mysql.py": {
62
+ "bytes": 2984,
63
+ "mode": 420,
64
+ "sha256": "a5f4550dd14bf4820e5d85cf06b6c7c79286e41d2d3c794f639579d20fffab92"
65
+ },
66
+ "pypika/tests/dialects/test_oracle.py": {
67
+ "bytes": 1906,
68
+ "mode": 420,
69
+ "sha256": "67f80eb40cc9b4dabcbad355e8be6d83c54a64cbee809182ef57fa9f48d2ebc6"
70
+ },
71
+ "pypika/tests/dialects/test_postgresql.py": {
72
+ "bytes": 9532,
73
+ "mode": 420,
74
+ "sha256": "548ee7267ca87c9e1ae82ca89a4e5bd96c7392a55cc07996f2b105f911ffa6cf"
75
+ },
76
+ "pypika/tests/dialects/test_snowflake.py": {
77
+ "bytes": 2159,
78
+ "mode": 420,
79
+ "sha256": "e567b9dab0a5366bd11a0ef162c3a3bb492280b78bc4b397a23070563dc84468"
80
+ },
81
+ "pypika/tests/dialects/test_sqllite.py": {
82
+ "bytes": 1243,
83
+ "mode": 420,
84
+ "sha256": "118fed7e575415f276bda390a119080bd6a767aee3d9c953e11a8c219f0f017c"
85
+ },
86
+ "pypika/tests/dialects/test_vertica.py": {
87
+ "bytes": 3578,
88
+ "mode": 420,
89
+ "sha256": "33a027e6735ca03b7afd11394905c4193e48a15dda3efbe47cdcd85fcabf840c"
90
+ },
91
+ "pypika/tests/test_aggregate.py": {
92
+ "bytes": 3946,
93
+ "mode": 420,
94
+ "sha256": "5b0fb549de3bb50ad88ae1d75824f12f7ab4a5f1102b06a70c7b2ebae33bea6d"
95
+ },
96
+ "pypika/tests/test_analytic_queries.py": {
97
+ "bytes": 20611,
98
+ "mode": 420,
99
+ "sha256": "8546b7a296c8e212c349c4baff95f93934b3120f74625b3945ade3b438e3a3ed"
100
+ },
101
+ "pypika/tests/test_create.py": {
102
+ "bytes": 9088,
103
+ "mode": 420,
104
+ "sha256": "6c57302da52ca1044be0afdafe677d5de245e44873dff1386c9a1ccb2625cc82"
105
+ },
106
+ "pypika/tests/test_criterions.py": {
107
+ "bytes": 32014,
108
+ "mode": 420,
109
+ "sha256": "78539ccca82f9a02fad0f5a39041ac343e3a142ce52368cd4f6ada489c52516a"
110
+ },
111
+ "pypika/tests/test_custom_functions.py": {
112
+ "bytes": 1710,
113
+ "mode": 420,
114
+ "sha256": "bfa18bc328a3577965db7cc75eb94439f0cfd95a3775d9b1cc8af1062bb202cb"
115
+ },
116
+ "pypika/tests/test_data_types.py": {
117
+ "bytes": 403,
118
+ "mode": 420,
119
+ "sha256": "c1be6ecc621a80dd91d35b1427bdfc8b1b33bd926515198e626d6fb5e1878c8b"
120
+ },
121
+ "pypika/tests/test_date_math.py": {
122
+ "bytes": 5544,
123
+ "mode": 420,
124
+ "sha256": "85aa54efe54fe19639f2dc2a5750647e3337ac2681768bc3fe327841f1634d9e"
125
+ },
126
+ "pypika/tests/test_deletes.py": {
127
+ "bytes": 3204,
128
+ "mode": 420,
129
+ "sha256": "ccde6df760d76d5d499ce828f6b0e2ad3eebcb2a5553c1890f481b9160b8d51a"
130
+ },
131
+ "pypika/tests/test_drop.py": {
132
+ "bytes": 1556,
133
+ "mode": 420,
134
+ "sha256": "a13dffa59ef8657398d5498c18977bfd702635e336ea9966b1a86137afca7926"
135
+ },
136
+ "pypika/tests/test_formats.py": {
137
+ "bytes": 1948,
138
+ "mode": 420,
139
+ "sha256": "240458a404bb119322778893b94a12be0f98f914acd701d2b4be73eb9be96f75"
140
+ },
141
+ "pypika/tests/test_functions.py": {
142
+ "bytes": 32769,
143
+ "mode": 420,
144
+ "sha256": "3ceaac7791bd27e50f44e1b4a1451d1e09afe6a8011052caea838e5124582154"
145
+ },
146
+ "pypika/tests/test_groupby_modifiers.py": {
147
+ "bytes": 5227,
148
+ "mode": 420,
149
+ "sha256": "9db4497660f08e3352a9a02c34889ad1cd6bab3aebaeaa485e84390ad71c84c5"
150
+ },
151
+ "pypika/tests/test_immutability.py": {
152
+ "bytes": 3954,
153
+ "mode": 420,
154
+ "sha256": "af537ced19f7274951cc91697eae748ef6c9cd20a8d8e492d93dc85b06f8fb50"
155
+ },
156
+ "pypika/tests/test_inserts.py": {
157
+ "bytes": 32549,
158
+ "mode": 420,
159
+ "sha256": "652e9f7f0f425e9acc0f70e2851ae29310b378262ea30369ea0113ef428f210f"
160
+ },
161
+ "pypika/tests/test_internals.py": {
162
+ "bytes": 828,
163
+ "mode": 420,
164
+ "sha256": "4374c2632563346d00ca4de6af397a022ef3f5022e808c8711a3f207a3a21609"
165
+ },
166
+ "pypika/tests/test_joins.py": {
167
+ "bytes": 43895,
168
+ "mode": 420,
169
+ "sha256": "53f224815c10393382443f020cb20e313420764495a8fd30f7ea2f34fd52935d"
170
+ },
171
+ "pypika/tests/test_negation.py": {
172
+ "bytes": 763,
173
+ "mode": 420,
174
+ "sha256": "6be105e8f39b32fb23e6ac7c7158232ee78111455d4f35638c8af60781138c79"
175
+ },
176
+ "pypika/tests/test_parameter.py": {
177
+ "bytes": 7382,
178
+ "mode": 420,
179
+ "sha256": "aaf2a3e79c8267bdc60e218445bc2a2254b97cf37b643a44d6f1a8141cbb5104"
180
+ },
181
+ "pypika/tests/test_pseudocolumns.py": {
182
+ "bytes": 1609,
183
+ "mode": 420,
184
+ "sha256": "b7c69f7aca41323c0e14a6a9e58697399cd7860610f07291afc6e8af26079ca2"
185
+ },
186
+ "pypika/tests/test_query.py": {
187
+ "bytes": 9025,
188
+ "mode": 420,
189
+ "sha256": "2da9d717673912a84e86e6c0d3955dbd8f2fa7b41b7ed9a6f78089dfa28f9444"
190
+ },
191
+ "pypika/tests/test_selects.py": {
192
+ "bytes": 50757,
193
+ "mode": 420,
194
+ "sha256": "5ac1b7ad3248d64b1e3865a72c763873514ae5cd6e03356b913e531c0c336986"
195
+ },
196
+ "pypika/tests/test_tables.py": {
197
+ "bytes": 6568,
198
+ "mode": 420,
199
+ "sha256": "384c8e843ba56374ee92cd7b7e2fd3f89247b899e23cd9e1f6663d751ad5d884"
200
+ },
201
+ "pypika/tests/test_terms.py": {
202
+ "bytes": 3215,
203
+ "mode": 420,
204
+ "sha256": "8598513180ddcd28cc61582618d85bd570280978b07603044dae81b2eb099f77"
205
+ },
206
+ "pypika/tests/test_tuples.py": {
207
+ "bytes": 6033,
208
+ "mode": 420,
209
+ "sha256": "863942196a93e8939be0d03c49cfeb42a35503ad42f9f3d6c730260112811087"
210
+ },
211
+ "pypika/tests/test_updates.py": {
212
+ "bytes": 5689,
213
+ "mode": 420,
214
+ "sha256": "0f0f2521bb8d1f5d782f8fabaea5fd1d82090cde045ba87fad65770db6c75d2f"
215
+ },
216
+ "pypika/tests/test_utils.py": {
217
+ "bytes": 834,
218
+ "mode": 420,
219
+ "sha256": "5db6e90df5daaa70b9f35f1243db69908da1f27887f63466fc27ce20c7e14892"
220
+ },
221
+ "pyproject.toml": {
222
+ "bytes": 390,
223
+ "mode": 420,
224
+ "sha256": "037fedacd5be5d078f398265ff97e1677e25214ad8839d17073a439695ee7a91"
225
+ },
226
+ "setup.cfg": {
227
+ "bytes": 317,
228
+ "mode": 420,
229
+ "sha256": "c84a31de586e8385e741127b004fc1f19ebae6463cdf0b3f243991db35cdeaea"
230
+ },
231
+ "setup.py": {
232
+ "bytes": 2638,
233
+ "mode": 420,
234
+ "sha256": "f8bad274588a22e65764d8cf9fb2be5aa6fcfaf3bf861d71e5f751105898845a"
235
+ },
236
+ "tox.ini": {
237
+ "bytes": 257,
238
+ "mode": 420,
239
+ "sha256": "c2d7205d0ac93e9c9e80dc5d24f0a826a6781f587a33ea722ea621f9e2ccd0ea"
240
+ }
241
+ }
242
+ }
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/__init__.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ __author__ = "Timothy Heys"
2
+ __email__ = "theys@kayak.com"
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/clickhouse/__init__.py ADDED
File without changes
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/clickhouse/test_array.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from parameterized import parameterized
4
+
5
+ from pypika import Field
6
+ from pypika.clickhouse.array import (
7
+ Array,
8
+ Empty,
9
+ HasAny,
10
+ Length,
11
+ NotEmpty,
12
+ )
13
+ from pypika.clickhouse.type_conversion import (
14
+ ToFixedString,
15
+ ToInt64,
16
+ )
17
+
18
+
19
+ class TestArray(unittest.TestCase):
20
+ @parameterized.expand(
21
+ [
22
+ (
23
+ "['ridley', 'scott', 'jimi', 'hendrix']",
24
+ Array(["ridley", "scott", "jimi", "hendrix"]),
25
+ ),
26
+ (
27
+ "[1, 2, 3, 4]",
28
+ Array([1, 2, 3, 4]),
29
+ ),
30
+ (
31
+ "[toInt64('1'),toInt64('2'),toInt64('3'),toInt64('4')]",
32
+ Array(["1", "2", "3", "4"], ToInt64),
33
+ ),
34
+ (
35
+ "[toFixedString('mogwai',10),toFixedString('mono',10),toFixedString('bonobo',10)] arr",
36
+ Array(["mogwai", "mono", "bonobo"], ToFixedString, {"length": 10}).as_('arr'),
37
+ ),
38
+ ]
39
+ )
40
+ def test_get_sql(self, expected: str, array: Array):
41
+ self.assertEqual(expected, array.get_sql())
42
+
43
+
44
+ class TestHasAny(unittest.TestCase):
45
+ @parameterized.expand(
46
+ [
47
+ (
48
+ 'hasAny("mental_abilities","physical_abilities")',
49
+ HasAny(Field("mental_abilities"), Field("physical_abilities")),
50
+ ),
51
+ ("hasAny([1, 2, 3, 4],[3])", HasAny(Array([1, 2, 3, 4]), Array([3]))),
52
+ (
53
+ "hasAny(\"bands\",[toFixedString('port-royal',20),toFixedString('hammock',20)])",
54
+ HasAny(
55
+ Field("bands"),
56
+ Array(["port-royal", "hammock"], ToFixedString, {"length": 20}),
57
+ ),
58
+ ),
59
+ ]
60
+ )
61
+ def test_get_sql(self, expected: str, func: HasAny):
62
+ self.assertEqual(expected, func.get_sql())
63
+
64
+
65
+ class TestLength(unittest.TestCase):
66
+ @parameterized.expand(
67
+ [
68
+ (
69
+ 'length("tags")',
70
+ Length(Field("tags")),
71
+ ),
72
+ ("length([1, 2, 3])", Length(Array([1, 2, 3]))),
73
+ ]
74
+ )
75
+ def test_get_sql(self, expected: str, func: Length):
76
+ self.assertEqual(expected, func.get_sql())
77
+
78
+
79
+ class TestEmpty(unittest.TestCase):
80
+ @parameterized.expand(
81
+ [
82
+ (
83
+ 'empty("tags")',
84
+ Empty(Field("tags")),
85
+ ),
86
+ ("empty([1, 2, 3])", Empty(Array([1, 2, 3]))),
87
+ ]
88
+ )
89
+ def test_get_sql(self, expected: str, func: Empty):
90
+ self.assertEqual(expected, func.get_sql())
91
+
92
+
93
+ class TestNotEmpty(unittest.TestCase):
94
+ @parameterized.expand(
95
+ [
96
+ (
97
+ 'notEmpty("tags")',
98
+ NotEmpty(Field("tags")),
99
+ ),
100
+ ("notEmpty([1, 2, 3])", NotEmpty(Array([1, 2, 3]))),
101
+ ]
102
+ )
103
+ def test_get_sql(self, expected: str, func: NotEmpty):
104
+ self.assertEqual(expected, func.get_sql())
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/clickhouse/test_condition.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from parameterized import parameterized
4
+
5
+ from pypika import Field
6
+ from pypika.clickhouse.condition import If, MultiIf
7
+ from pypika.clickhouse.type_conversion import ToFixedString
8
+
9
+
10
+ class TestIfCondition(unittest.TestCase):
11
+ @parameterized.expand(
12
+ [
13
+ (
14
+ If(
15
+ Field("filmmaker").isnull(),
16
+ ToFixedString("Tarantino", 20),
17
+ Field("filmmaker"),
18
+ ),
19
+ "if(filmmaker IS NULL,toFixedString('Tarantino',20),filmmaker)",
20
+ ),
21
+ (
22
+ If(
23
+ Field("created") == Field("updated"),
24
+ ToFixedString("yes", 3),
25
+ ToFixedString("no", 3),
26
+ ),
27
+ "if(created=updated,toFixedString('yes',3),toFixedString('no',3))",
28
+ ),
29
+ ]
30
+ )
31
+ def test_get_sql(self, func, expected):
32
+ self.assertEqual(func.get_sql(), expected)
33
+
34
+
35
+ class TestMultiIfCondition(unittest.TestCase):
36
+ @parameterized.expand(
37
+ [
38
+ (
39
+ MultiIf(
40
+ Field("filmmaker").isnull(),
41
+ ToFixedString("Tarantino", 20),
42
+ Field("filmmaker") == ToFixedString("undefined", 20),
43
+ ToFixedString("Tarantino", 20),
44
+ Field("filmmaker"),
45
+ ),
46
+ "multiIf(filmmaker IS NULL,"
47
+ "toFixedString('Tarantino',20),"
48
+ "filmmaker=toFixedString('undefined',20),"
49
+ "toFixedString('Tarantino',20),filmmaker)",
50
+ ),
51
+ (
52
+ MultiIf(
53
+ Field("color") == ToFixedString("black", 20),
54
+ ToFixedString("dark", 20),
55
+ Field("color") == ToFixedString("grey", 20),
56
+ ToFixedString("dark", 20),
57
+ Field("color") == ToFixedString("white", 20),
58
+ ToFixedString("light", 20),
59
+ ToFixedString("undefined", 20),
60
+ ),
61
+ "multiIf(color=toFixedString('black',20),"
62
+ "toFixedString('dark',20),"
63
+ "color=toFixedString('grey',20),"
64
+ "toFixedString('dark',20),"
65
+ "color=toFixedString('white',20),"
66
+ "toFixedString('light',20),"
67
+ "toFixedString('undefined',20))",
68
+ ),
69
+ ]
70
+ )
71
+ def test_get_sql(self, func, expected):
72
+ self.assertEqual(func.get_sql(), expected)
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/clickhouse/test_dates_and_times.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from parameterized import parameterized
4
+
5
+ from pypika import Field
6
+ from pypika.clickhouse.dates_and_times import (
7
+ AddDays,
8
+ AddHours,
9
+ AddMinutes,
10
+ AddMonths,
11
+ AddQuarters,
12
+ AddSeconds,
13
+ AddWeeks,
14
+ AddYears,
15
+ FormatDateTime,
16
+ SubtractDays,
17
+ SubtractHours,
18
+ SubtractMinutes,
19
+ SubtractMonths,
20
+ SubtractQuarters,
21
+ SubtractSeconds,
22
+ SubtractWeeks,
23
+ SubtractYears,
24
+ ToYYYYMM,
25
+ )
26
+
27
+
28
+ class TestFormatDateTime(unittest.TestCase):
29
+ @parameterized.expand(
30
+ [
31
+ (FormatDateTime(Field("created"), "%V"), "formatDateTime(created,'%V')"),
32
+ (
33
+ FormatDateTime(Field("created"), "%Y-%m-%d"),
34
+ "formatDateTime(created,'%Y-%m-%d')",
35
+ ),
36
+ ]
37
+ )
38
+ def test_get_sql(self, func, expected):
39
+ self.assertEqual(func.get_sql(), expected)
40
+
41
+
42
+ class TestAddSubtractDt(unittest.TestCase):
43
+ @parameterized.expand(
44
+ [
45
+ (AddYears(Field("created"), 1), "addYears(created,1)"),
46
+ (AddMonths(Field("created"), 2), "addMonths(created,2)"),
47
+ (AddWeeks(Field("created"), 3), "addWeeks(created,3)"),
48
+ (AddDays(Field("created"), 4), "addDays(created,4)"),
49
+ (AddHours(Field("created"), 5), "addHours(created,5)"),
50
+ (AddMinutes(Field("created"), 6), "addMinutes(created,6)"),
51
+ (AddSeconds(Field("created"), 7), "addSeconds(created,7)"),
52
+ (AddQuarters(Field("created"), 8), "addQuarters(created,8)"),
53
+ (SubtractYears(Field("created"), 9), "subtractYears(created,9)"),
54
+ (SubtractMonths(Field("created"), 10), "subtractMonths(created,10)"),
55
+ (SubtractWeeks(Field("created"), 11), "subtractWeeks(created,11)"),
56
+ (SubtractDays(Field("created"), 12), "subtractDays(created,12)"),
57
+ (SubtractHours(Field("created"), 13), "subtractHours(created,13)"),
58
+ (SubtractHours(Field("created"), 13), "subtractHours(created,13)"),
59
+ (SubtractMinutes(Field("created"), 14), "subtractMinutes(created,14)"),
60
+ (SubtractSeconds(Field("created"), 15), "subtractSeconds(created,15)"),
61
+ (SubtractQuarters(Field("created"), 16), "subtractQuarters(created,16)"),
62
+ ]
63
+ )
64
+ def test_get_sql(self, func, expected):
65
+ self.assertEqual(func.get_sql(), expected)
66
+
67
+
68
+ class TestToYYYYMM(unittest.TestCase):
69
+ def test_get_sql(self):
70
+ self.assertEqual(ToYYYYMM(Field("created").get_sql()), "toYYYYMM(created)")
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/clickhouse/test_nullable_arg.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from parameterized import parameterized
4
+
5
+ from pypika import Field
6
+ from pypika.clickhouse.nullable_arg import IfNull
7
+ from pypika.clickhouse.type_conversion import ToFixedString
8
+
9
+
10
+ class TestSearchString(unittest.TestCase):
11
+ @parameterized.expand(
12
+ [
13
+ (
14
+ IfNull(Field("name"), Field("login")),
15
+ "ifNull(name,login)",
16
+ ),
17
+ (
18
+ IfNull(Field("builder"), ToFixedString("pypika", 100)),
19
+ "ifNull(builder,toFixedString('pypika',100))",
20
+ ),
21
+ ]
22
+ )
23
+ def test_get_sql(self, func, expected):
24
+ self.assertEqual(func.get_sql(), expected)
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/clickhouse/test_search_string.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from parameterized import parameterized
4
+
5
+ from pypika import Field
6
+ from pypika.clickhouse.search_string import (
7
+ Match,
8
+ Like,
9
+ NotLike,
10
+ MultiSearchAny,
11
+ MultiMatchAny,
12
+ )
13
+
14
+
15
+ class TestSearchString(unittest.TestCase):
16
+ @parameterized.expand(
17
+ [
18
+ (Match(Field("name"), "twheys"), "match(toString(\"name\"),'twheys')"),
19
+ (Like(Field("name"), "twheys"), "like(toString(\"name\"),'twheys')"),
20
+ (NotLike(Field("name"), "twheys"), "notLike(toString(\"name\"),'twheys')"),
21
+ ]
22
+ )
23
+ def test_search_string(self, func, expected):
24
+ self.assertEqual(func, expected)
25
+
26
+
27
+ class TestMultiSearch(unittest.TestCase):
28
+ @parameterized.expand(
29
+ [
30
+ (
31
+ MultiSearchAny(Field("name"), ["sarah", "connor"]),
32
+ "multiSearchAny(toString(\"name\"),['sarah','connor'])",
33
+ ),
34
+ (
35
+ MultiMatchAny(Field("name"), ["sarah", "connor"]),
36
+ "multiMatchAny(toString(\"name\"),['sarah','connor'])",
37
+ ),
38
+ ]
39
+ )
40
+ def test_multi_search_string(self, func, expected):
41
+ self.assertEqual(func.get_sql(), expected)
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/clickhouse/test_type_conversion.py ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from parameterized import parameterized
4
+
5
+ from pypika import ClickHouseQuery, Field, Table
6
+ from pypika.clickhouse.type_conversion import (
7
+ ToDate,
8
+ ToDateTime,
9
+ ToFixedString,
10
+ ToFloat32,
11
+ ToFloat64,
12
+ ToInt16,
13
+ ToInt32,
14
+ ToInt64,
15
+ ToInt8,
16
+ ToString,
17
+ ToUInt16,
18
+ ToUInt32,
19
+ ToUInt64,
20
+ ToUInt8,
21
+ )
22
+
23
+
24
+ class TestBasicTypeConverters(unittest.TestCase):
25
+ @parameterized.expand(
26
+ [
27
+ (
28
+ 'toString("field_name")',
29
+ ToString(Field("field_name")),
30
+ ),
31
+ (
32
+ 'toInt8("field_name")',
33
+ ToInt8(Field("field_name")),
34
+ ),
35
+ (
36
+ 'toInt16("field_name")',
37
+ ToInt16(Field("field_name")),
38
+ ),
39
+ (
40
+ 'toInt32("field_name")',
41
+ ToInt32(Field("field_name")),
42
+ ),
43
+ (
44
+ 'toInt64("field_name")',
45
+ ToInt64(Field("field_name")),
46
+ ),
47
+ (
48
+ 'toUInt8("field_name")',
49
+ ToUInt8(Field("field_name")),
50
+ ),
51
+ (
52
+ 'toUInt16("field_name")',
53
+ ToUInt16(Field("field_name")),
54
+ ),
55
+ (
56
+ 'toUInt32("field_name")',
57
+ ToUInt32(Field("field_name")),
58
+ ),
59
+ (
60
+ 'toUInt64("field_name")',
61
+ ToUInt64(Field("field_name")),
62
+ ),
63
+ (
64
+ 'toFloat32("field_name")',
65
+ ToFloat32(Field("field_name")),
66
+ ),
67
+ (
68
+ 'toFloat64("field_name")',
69
+ ToFloat64(Field("field_name")),
70
+ ),
71
+ (
72
+ 'toFloat64("field_name")',
73
+ ToFloat64(Field("field_name")),
74
+ ),
75
+ (
76
+ 'toDate("field_name")',
77
+ ToDate(Field("field_name")),
78
+ ),
79
+ (
80
+ 'toDateTime("field_name")',
81
+ ToDateTime(Field("field_name")),
82
+ ),
83
+ (
84
+ 'toFixedString("field_name",100)',
85
+ ToFixedString(Field("field_name"), 100),
86
+ ),
87
+ ]
88
+ )
89
+ def test_basic_types_field(self, expected, func):
90
+ self.assertEqual(func, expected)
91
+
92
+ @parameterized.expand(
93
+ [
94
+ (
95
+ "toString('100')",
96
+ ToString("100"),
97
+ ),
98
+ (
99
+ "toInt8('100')",
100
+ ToInt8("100"),
101
+ ),
102
+ (
103
+ "toInt16('100')",
104
+ ToInt16("100"),
105
+ ),
106
+ (
107
+ "toInt32('100')",
108
+ ToInt32("100"),
109
+ ),
110
+ (
111
+ "toInt64('100')",
112
+ ToInt64("100"),
113
+ ),
114
+ (
115
+ "toUInt8('100')",
116
+ ToUInt8("100"),
117
+ ),
118
+ (
119
+ "toUInt16('100')",
120
+ ToUInt16("100"),
121
+ ),
122
+ (
123
+ "toUInt32('100')",
124
+ ToUInt32("100"),
125
+ ),
126
+ (
127
+ "toUInt64('100')",
128
+ ToUInt64("100"),
129
+ ),
130
+ (
131
+ "toFloat32('100')",
132
+ ToFloat32("100"),
133
+ ),
134
+ (
135
+ "toFloat64('100')",
136
+ ToFloat64("100"),
137
+ ),
138
+ (
139
+ "toFloat64('100')",
140
+ ToFloat64("100"),
141
+ ),
142
+ (
143
+ "toDate('100')",
144
+ ToDate("100"),
145
+ ),
146
+ (
147
+ "toDateTime('100')",
148
+ ToDateTime("100"),
149
+ ),
150
+ (
151
+ "toFixedString('100',100)",
152
+ ToFixedString("100", 100),
153
+ ),
154
+ ]
155
+ )
156
+ def test_basic_types_value(self, expected, func):
157
+ self.assertEqual(func, expected)
158
+
159
+
160
+ class TestToFixedString(unittest.TestCase):
161
+ def test_get_sql_with_table(self):
162
+ table = Table("example")
163
+ query = (
164
+ ClickHouseQuery.from_(table)
165
+ .select(
166
+ table.name,
167
+ )
168
+ .where(
169
+ table.name == ToFixedString("name", 50),
170
+ )
171
+ )
172
+
173
+ self.assertEqual(
174
+ 'SELECT "name" FROM "example" WHERE "name"=toFixedString(\'name\',50)',
175
+ query.get_sql(),
176
+ )
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/dialects/__init__.py ADDED
File without changes
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/dialects/test_clickhouse.py ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from unittest import TestCase
2
+
3
+ from pypika import (
4
+ ClickHouseQuery,
5
+ Database,
6
+ Table,
7
+ )
8
+
9
+
10
+ class ClickHouseQueryTests(TestCase):
11
+ def test_use_AS_keyword_for_alias(self):
12
+ t = Table('abc')
13
+ query = ClickHouseQuery.from_(t).select(t.foo.as_('f1'), t.bar.as_('f2'))
14
+ self.assertEqual(str(query), 'SELECT "foo" AS "f1","bar" AS "f2" FROM "abc"')
15
+
16
+ def test_use_SAMPLE_keyword(self):
17
+ t = Table('abc')
18
+ query = ClickHouseQuery.from_(t).select(t.foo).sample(10)
19
+ self.assertEqual(str(query), 'SELECT "foo" FROM "abc" SAMPLE 10')
20
+
21
+ def test_use_SAMPLE_with_offset_keyword(self):
22
+ t = Table('abc')
23
+ query = ClickHouseQuery.from_(t).select(t.foo).sample(10, 5)
24
+ self.assertEqual(str(query), 'SELECT "foo" FROM "abc" SAMPLE 10 OFFSET 5')
25
+
26
+ def test_use_FINAL_keyword(self):
27
+ t = Table('abc')
28
+ query = ClickHouseQuery.from_(t).select(t.foo).final()
29
+ self.assertEqual(str(query), 'SELECT "foo" FROM "abc" FINAL')
30
+
31
+
32
+ class ClickHouseDeleteTests(TestCase):
33
+ table_abc = Table("abc")
34
+
35
+ def test_omit_where(self):
36
+ q = ClickHouseQuery.from_("abc").delete()
37
+
38
+ self.assertEqual('ALTER TABLE "abc" DELETE', str(q))
39
+
40
+ def test_omit_where__table_schema(self):
41
+ q = ClickHouseQuery.from_(Table("abc", "schema1")).delete()
42
+
43
+ self.assertEqual('ALTER TABLE "schema1"."abc" DELETE', str(q))
44
+
45
+ def test_where_field_equals(self):
46
+ q1 = ClickHouseQuery.from_(self.table_abc).where(self.table_abc.foo == self.table_abc.bar).delete()
47
+ q2 = ClickHouseQuery.from_(self.table_abc).where(self.table_abc.foo.eq(self.table_abc.bar)).delete()
48
+
49
+ self.assertEqual('ALTER TABLE "abc" DELETE WHERE "foo"="bar"', str(q1))
50
+ self.assertEqual('ALTER TABLE "abc" DELETE WHERE "foo"="bar"', str(q2))
51
+
52
+
53
+ class ClickHouseUpdateTests(TestCase):
54
+ table_abc = Table("abc")
55
+
56
+ def test_update(self):
57
+ q = ClickHouseQuery.update(self.table_abc).where(self.table_abc.foo == 0).set("foo", "bar")
58
+
59
+ self.assertEqual('ALTER TABLE "abc" UPDATE "foo"=\'bar\' WHERE "foo"=0', str(q))
60
+
61
+
62
+ class ClickHouseDropQuery(TestCase):
63
+ table_abc = Table("abc")
64
+ database_xyz = Database("mydb")
65
+ cluster_name = "mycluster"
66
+
67
+ def test_drop_database(self):
68
+ q1 = ClickHouseQuery.drop_database(self.database_xyz)
69
+ q2 = ClickHouseQuery.drop_database(self.database_xyz).on_cluster(self.cluster_name)
70
+ q3 = ClickHouseQuery.drop_database(self.database_xyz).if_exists().on_cluster(self.cluster_name)
71
+
72
+ self.assertEqual('DROP DATABASE "mydb"', str(q1))
73
+ self.assertEqual('DROP DATABASE "mydb" ON CLUSTER "mycluster"', str(q2))
74
+ self.assertEqual('DROP DATABASE IF EXISTS "mydb" ON CLUSTER "mycluster"', str(q3))
75
+
76
+ def test_drop_table(self):
77
+ q1 = ClickHouseQuery.drop_table(self.table_abc)
78
+ q2 = ClickHouseQuery.drop_table(self.table_abc).on_cluster(self.cluster_name)
79
+ q3 = ClickHouseQuery.drop_table(self.table_abc).if_exists().on_cluster(self.cluster_name)
80
+
81
+ self.assertEqual('DROP TABLE "abc"', str(q1))
82
+ self.assertEqual('DROP TABLE "abc" ON CLUSTER "mycluster"', str(q2))
83
+ self.assertEqual('DROP TABLE IF EXISTS "abc" ON CLUSTER "mycluster"', str(q3))
84
+
85
+ def test_drop_dictionary(self):
86
+ q1 = ClickHouseQuery.drop_dictionary("dict")
87
+ q2 = ClickHouseQuery.drop_dictionary("dict").on_cluster(self.cluster_name)
88
+ q3 = ClickHouseQuery.drop_dictionary("dict").if_exists().on_cluster(self.cluster_name)
89
+
90
+ self.assertEqual('DROP DICTIONARY "dict"', str(q1))
91
+ self.assertEqual('DROP DICTIONARY "dict"', str(q2)) # NO CLUSTER
92
+ self.assertEqual('DROP DICTIONARY IF EXISTS "dict"', str(q3)) # NO CLUSTER
93
+
94
+ def test_drop_other(self):
95
+ q1 = ClickHouseQuery.drop_quota("myquota")
96
+ q2 = ClickHouseQuery.drop_user("myuser")
97
+ q3 = ClickHouseQuery.drop_view("myview")
98
+
99
+ self.assertEqual('DROP QUOTA "myquota"', str(q1))
100
+ self.assertEqual('DROP USER "myuser"', str(q2))
101
+ self.assertEqual('DROP VIEW "myview"', str(q3))
102
+
103
+
104
+ class DistinctOnTests(TestCase):
105
+ table_abc = Table("abc")
106
+
107
+ def test_distinct_on(self):
108
+ q = ClickHouseQuery.from_(self.table_abc).distinct_on("lname", self.table_abc.fname).select("lname", "id")
109
+
110
+ self.assertEqual('''SELECT DISTINCT ON("lname","fname") "lname","id" FROM "abc"''', str(q))
111
+
112
+
113
+ class LimitByTests(TestCase):
114
+ table_abc = Table("abc")
115
+
116
+ def test_limit_by(self):
117
+ q = ClickHouseQuery.from_(self.table_abc).limit_by(1, "a", self.table_abc.b).select("a", "b", "c")
118
+
119
+ self.assertEqual('''SELECT "a","b","c" FROM "abc" LIMIT 1 BY ("a","b")''', str(q))
120
+
121
+ def test_limit_offset_by(self):
122
+ q = ClickHouseQuery.from_(self.table_abc).limit_offset_by(1, 2, "a", self.table_abc.b).select("a", "b", "c")
123
+
124
+ self.assertEqual('''SELECT "a","b","c" FROM "abc" LIMIT 1 OFFSET 2 BY ("a","b")''', str(q))
125
+
126
+ def test_limit_offset0_by(self):
127
+ q = ClickHouseQuery.from_(self.table_abc).limit_offset_by(1, 0, "a", self.table_abc.b).select("a", "b", "c")
128
+
129
+ self.assertEqual('''SELECT "a","b","c" FROM "abc" LIMIT 1 BY ("a","b")''', str(q))
130
+
131
+ def test_rename_table(self):
132
+ table_join = Table("join")
133
+
134
+ q = (
135
+ ClickHouseQuery.from_(self.table_abc)
136
+ .join(table_join)
137
+ .using("a")
138
+ .limit_by(1, self.table_abc.a, table_join.a)
139
+ .select(self.table_abc.b, table_join.b)
140
+ )
141
+ q = q.replace_table(self.table_abc, Table("xyz"))
142
+
143
+ self.assertEqual(
144
+ '''SELECT "xyz"."b","join"."b" FROM "xyz" JOIN "join" USING ("a") LIMIT 1 BY ("xyz"."a","join"."a")''',
145
+ str(q),
146
+ )
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/dialects/test_mssql.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from pypika import Table
4
+ from pypika.analytics import Count
5
+ from pypika.dialects import MSSQLQuery
6
+ from pypika.utils import QueryException
7
+
8
+
9
+ class SelectTests(unittest.TestCase):
10
+ def test_normal_select(self):
11
+ q = MSSQLQuery.from_("abc").select("def")
12
+
13
+ self.assertEqual('SELECT "def" FROM "abc"', str(q))
14
+
15
+ def test_distinct_select(self):
16
+ q = MSSQLQuery.from_("abc").select("def").distinct()
17
+
18
+ self.assertEqual('SELECT DISTINCT "def" FROM "abc"', str(q))
19
+
20
+ def test_top_distinct_select(self):
21
+ q = MSSQLQuery.from_("abc").select("def").top(10).distinct()
22
+
23
+ self.assertEqual('SELECT DISTINCT TOP (10) "def" FROM "abc"', str(q))
24
+
25
+ def test_top_select(self):
26
+ q = MSSQLQuery.from_("abc").select("def").top(10)
27
+
28
+ self.assertEqual('SELECT TOP (10) "def" FROM "abc"', str(q))
29
+
30
+ def test_top_with_ties_select(self):
31
+ q = MSSQLQuery.from_("abc").select("def").top(10, with_ties=True)
32
+
33
+ self.assertEqual('SELECT TOP (10) WITH TIES "def" FROM "abc"', str(q))
34
+
35
+ def test_top_percent_select(self):
36
+ q = MSSQLQuery.from_("abc").select("def").top(10, percent=True)
37
+
38
+ self.assertEqual('SELECT TOP (10) PERCENT "def" FROM "abc"', str(q))
39
+
40
+ def test_top_percent_invalid_range(self):
41
+ for invalid_top in [-1, 101]:
42
+ with self.assertRaisesRegex(
43
+ QueryException, "TOP value must be between 0 and 100 when `percent` is specified"
44
+ ):
45
+ MSSQLQuery.from_("abc").select("def").top(invalid_top, percent=True)
46
+
47
+ def test_top_select_non_int(self):
48
+ with self.assertRaisesRegex(QueryException, "TOP value must be an integer"):
49
+ MSSQLQuery.from_("abc").select("def").top("a")
50
+
51
+ def test_limit(self):
52
+ q = MSSQLQuery.from_("abc").select("def").orderby("def").limit(10)
53
+
54
+ self.assertEqual('SELECT "def" FROM "abc" ORDER BY "def" OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY', str(q))
55
+
56
+ def test_offset(self):
57
+ q = MSSQLQuery.from_("abc").select("def").orderby("def").offset(10)
58
+
59
+ self.assertEqual('SELECT "def" FROM "abc" ORDER BY "def" OFFSET 10 ROWS', str(q))
60
+
61
+ def test_limit_with_offset(self):
62
+ q = MSSQLQuery.from_("abc").select("def").orderby("def").limit(10).offset(10)
63
+
64
+ self.assertEqual('SELECT "def" FROM "abc" ORDER BY "def" OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY', str(q))
65
+
66
+ def test_groupby_alias_False_does_not_group_by_alias_with_standard_query(self):
67
+ t = Table('table1')
68
+ col = t.abc.as_('a')
69
+ q = MSSQLQuery.from_(t).select(col, Count('*')).groupby(col)
70
+
71
+ self.assertEqual('SELECT "abc" "a",COUNT(\'*\') FROM "table1" GROUP BY "abc"', str(q))
72
+
73
+ def test_groupby_alias_False_does_not_group_by_alias_when_subqueries_are_present(self):
74
+ t = Table('table1')
75
+ subquery = MSSQLQuery.from_(t).select(t.abc)
76
+ col = subquery.abc.as_('a')
77
+ q = MSSQLQuery.from_(subquery).select(col, Count('*')).groupby(col)
78
+
79
+ self.assertEqual(
80
+ 'SELECT "sq0"."abc" "a",COUNT(\'*\') FROM (SELECT "abc" FROM "table1") "sq0" GROUP BY "sq0"."abc"', str(q)
81
+ )
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/dialects/test_mysql.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from pypika import MySQLQuery, QueryException, Table, Column
4
+
5
+
6
+ class SelectTests(unittest.TestCase):
7
+ table_abc = Table("abc")
8
+
9
+ def test_normal_select(self):
10
+ q = MySQLQuery.from_("abc").select("def")
11
+
12
+ self.assertEqual("SELECT `def` FROM `abc`", str(q))
13
+
14
+ def test_distinct_select(self):
15
+ q = MySQLQuery.from_("abc").select("def").distinct()
16
+
17
+ self.assertEqual("SELECT DISTINCT `def` FROM `abc`", str(q))
18
+
19
+ def test_modifier_select(self):
20
+ q = MySQLQuery.from_("abc").select("def").select("ghi").modifier("SQL_CALC_FOUND_ROWS")
21
+
22
+ self.assertEqual("SELECT SQL_CALC_FOUND_ROWS `def`,`ghi` FROM `abc`", str(q))
23
+
24
+ def test_multiple_modifier_select(self):
25
+ q = MySQLQuery.from_("abc").select("def").modifier("HIGH_PRIORITY").modifier("SQL_CALC_FOUND_ROWS")
26
+
27
+ self.assertEqual("SELECT HIGH_PRIORITY SQL_CALC_FOUND_ROWS `def` FROM `abc`", str(q))
28
+
29
+
30
+ class UpdateTests(unittest.TestCase):
31
+ table_abc = Table("abc")
32
+
33
+ def test_update(self):
34
+ q = MySQLQuery.into("abc").insert(1, [1, "a", True])
35
+
36
+ self.assertEqual("INSERT INTO `abc` VALUES (1,[1,'a',true])", str(q))
37
+
38
+ def test_on_duplicate_key_ignore_update(self):
39
+ q = MySQLQuery.into("abc").insert(1, [1, "a", True]).on_duplicate_key_ignore()
40
+
41
+ self.assertEqual("INSERT INTO `abc` VALUES (1,[1,'a',true]) ON DUPLICATE KEY IGNORE", str(q))
42
+
43
+ def test_on_duplicate_key_update_update(self):
44
+ q = MySQLQuery.into("abc").insert(1, [1, "a", True]).on_duplicate_key_update(self.table_abc.a, 'b')
45
+
46
+ self.assertEqual("INSERT INTO `abc` VALUES (1,[1,'a',true]) ON DUPLICATE KEY UPDATE `a`='b'", str(q))
47
+
48
+ def test_conflict_handlers_update(self):
49
+ with self.assertRaises(QueryException):
50
+ (
51
+ MySQLQuery.into("abc")
52
+ .insert(1, [1, "a", True])
53
+ .on_duplicate_key_ignore()
54
+ .on_duplicate_key_update(self.table_abc.a, 'b')
55
+ )
56
+
57
+
58
+ class LoadCSVTests(unittest.TestCase):
59
+ table_abc = Table("abc")
60
+
61
+ def test_load_from_file(self):
62
+ q1 = MySQLQuery.load("/path/to/file").into("abc")
63
+
64
+ q2 = MySQLQuery.load("/path/to/file").into(self.table_abc)
65
+
66
+ self.assertEqual(
67
+ "LOAD DATA LOCAL INFILE '/path/to/file' INTO TABLE `abc` FIELDS TERMINATED BY ','",
68
+ str(q1),
69
+ )
70
+ self.assertEqual(
71
+ "LOAD DATA LOCAL INFILE '/path/to/file' INTO TABLE `abc` FIELDS TERMINATED BY ','",
72
+ str(q2),
73
+ )
74
+
75
+
76
+ class TableTests(unittest.TestCase):
77
+ table_abc = Table("abc")
78
+
79
+ def test_create_table(self):
80
+ q = MySQLQuery.create_table(self.table_abc).columns(Column("id", "INT"))
81
+ self.assertEqual(
82
+ 'CREATE TABLE `abc` (`id` INT)',
83
+ str(q),
84
+ )
85
+
86
+ def test_drop_table(self):
87
+ q = MySQLQuery.drop_table(self.table_abc)
88
+ self.assertEqual(
89
+ 'DROP TABLE `abc`',
90
+ str(q),
91
+ )
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/dialects/test_oracle.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from pypika import OracleQuery, Table
4
+ from pypika.analytics import Count
5
+
6
+
7
+ class SelectTests(unittest.TestCase):
8
+ def test_groupby_alias_False_does_not_group_by_alias_with_standard_query(self):
9
+ t = Table('table1')
10
+ col = t.abc.as_('a')
11
+ q = OracleQuery.from_(t).select(col, Count('*')).groupby(col)
12
+
13
+ self.assertEqual('SELECT abc a,COUNT(\'*\') FROM table1 GROUP BY abc', str(q))
14
+
15
+ def test_groupby_alias_False_does_not_group_by_alias_when_subqueries_are_present(self):
16
+ t = Table('table1')
17
+ subquery = OracleQuery.from_(t).select(t.abc)
18
+ col = subquery.abc.as_('a')
19
+ q = OracleQuery.from_(subquery).select(col, Count('*')).groupby(col)
20
+
21
+ self.assertEqual('SELECT sq0.abc a,COUNT(\'*\') FROM (SELECT abc FROM table1) sq0 GROUP BY sq0.abc', str(q))
22
+
23
+ def test_limit_query(self):
24
+ t = Table('table1')
25
+ limit = 5
26
+ q = OracleQuery.from_(t).select(t.test).limit(limit)
27
+
28
+ self.assertEqual(f'SELECT test FROM table1 FETCH NEXT {limit} ROWS ONLY', str(q))
29
+
30
+ def test_offset_query(self):
31
+ t = Table('table1')
32
+ offset = 5
33
+ q = OracleQuery.from_(t).select(t.test).offset(offset)
34
+
35
+ self.assertEqual(f'SELECT test FROM table1 OFFSET {offset} ROWS', str(q))
36
+
37
+ def test_limit_offset_query(self):
38
+ t = Table('table1')
39
+ limit = 5
40
+ offset = 5
41
+ q = OracleQuery.from_(t).select(t.test).limit(limit).offset(offset)
42
+
43
+ self.assertEqual(f'SELECT test FROM table1 OFFSET {offset} ROWS FETCH NEXT {limit} ROWS ONLY', str(q))
44
+
45
+ def test_fetch_next_method_deprecated(self):
46
+ with self.assertWarns(DeprecationWarning):
47
+ t = Table('table1')
48
+ limit = 5
49
+ q = OracleQuery.from_(t).select(t.test).fetch_next(limit)
50
+
51
+ self.assertEqual(f'SELECT test FROM table1 FETCH NEXT {limit} ROWS ONLY', str(q))
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/dialects/test_postgresql.py ADDED
@@ -0,0 +1,262 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+ from collections import OrderedDict
3
+
4
+ from pypika import (
5
+ Array,
6
+ Field,
7
+ JSON,
8
+ QueryException,
9
+ Table,
10
+ )
11
+ from pypika.dialects import PostgreSQLQuery
12
+
13
+
14
+ class InsertTests(unittest.TestCase):
15
+ table_abc = Table("abc")
16
+
17
+ def test_array_keyword(self):
18
+ q = PostgreSQLQuery.into(self.table_abc).insert(1, [1, "a", True])
19
+
20
+ self.assertEqual("INSERT INTO \"abc\" VALUES (1,ARRAY[1,'a',true])", str(q))
21
+
22
+
23
+ class JSONObjectTests(unittest.TestCase):
24
+ def test_alias_set_correctly(self):
25
+ table = Table('jsonb_table')
26
+ q = PostgreSQLQuery.from_('abc').select(table.value.get_text_value('a').as_('name'))
27
+
28
+ self.assertEqual('''SELECT "value"->>'a' "name" FROM "abc"''', str(q))
29
+
30
+ def test_json_value_from_dict(self):
31
+ q = PostgreSQLQuery.select(JSON({"a": "foo"}))
32
+
33
+ self.assertEqual('SELECT \'{"a":"foo"}\'', str(q))
34
+
35
+ def test_json_value_from_array_num(self):
36
+ q = PostgreSQLQuery.select(JSON([1, 2, 3]))
37
+
38
+ self.assertEqual("SELECT '[1,2,3]'", str(q))
39
+
40
+ def test_json_value_from_array_str(self):
41
+ q = PostgreSQLQuery.select(JSON(["a", "b", "c"]))
42
+
43
+ self.assertEqual('SELECT \'["a","b","c"]\'', str(q))
44
+
45
+ def test_json_value_from_dict_recursive(self):
46
+ q = PostgreSQLQuery.select(JSON({"a": "z", "b": {"c": "foo"}, "d": 1}))
47
+
48
+ # gotta split this one up to avoid the indeterminate order
49
+ sql = str(q)
50
+ start, end = 9, -2
51
+ self.assertEqual("SELECT '{}'", sql[:start] + sql[end:])
52
+
53
+ members_set = set(sql[start:end].split(","))
54
+ self.assertSetEqual({'"a":"z"', '"b":{"c":"foo"}', '"d":1'}, members_set)
55
+
56
+
57
+ class JSONOperatorsTests(unittest.TestCase):
58
+ # reference https://www.postgresql.org/docs/9.5/functions-json.html
59
+ table_abc = Table("abc")
60
+
61
+ def test_get_json_value_by_key(self):
62
+ q = PostgreSQLQuery.from_(self.table_abc).select("*").where(self.table_abc.json.get_json_value("dates"))
63
+
64
+ self.assertEqual('SELECT * FROM "abc" WHERE "json"->\'dates\'', str(q))
65
+
66
+ def test_get_json_value_by_index(self):
67
+ q = PostgreSQLQuery.from_(self.table_abc).select("*").where(self.table_abc.json.get_json_value(1))
68
+
69
+ self.assertEqual('SELECT * FROM "abc" WHERE "json"->1', str(q))
70
+
71
+ def test_get_text_value_by_key(self):
72
+ q = PostgreSQLQuery.from_(self.table_abc).select("*").where(self.table_abc.json.get_text_value("dates"))
73
+
74
+ self.assertEqual('SELECT * FROM "abc" WHERE "json"->>\'dates\'', str(q))
75
+
76
+ def test_get_text_value_by_index(self):
77
+ q = PostgreSQLQuery.from_(self.table_abc).select("*").where(self.table_abc.json.get_text_value(1))
78
+
79
+ self.assertEqual('SELECT * FROM "abc" WHERE "json"->>1', str(q))
80
+
81
+ def test_get_path_json_value(self):
82
+ q = PostgreSQLQuery.from_(self.table_abc).select("*").where(self.table_abc.json.get_path_json_value("{a,b}"))
83
+
84
+ self.assertEqual('SELECT * FROM "abc" WHERE "json"#>\'{a,b}\'', str(q))
85
+
86
+ def test_get_path_text_value(self):
87
+ q = PostgreSQLQuery.from_(self.table_abc).select("*").where(self.table_abc.json.get_path_text_value("{a,b}"))
88
+
89
+ self.assertEqual('SELECT * FROM "abc" WHERE "json"#>>\'{a,b}\'', str(q))
90
+
91
+
92
+ class JSONBOperatorsTests(unittest.TestCase):
93
+ # reference https://www.postgresql.org/docs/9.5/functions-json.html
94
+ table_abc = Table("abc")
95
+
96
+ def test_json_contains_for_json(self):
97
+ q = PostgreSQLQuery.select(JSON({"a": 1, "b": 2}).contains({"a": 1}))
98
+
99
+ # gotta split this one up to avoid the indeterminate order
100
+ sql = str(q)
101
+ start, end = 9, -13
102
+ self.assertEqual("SELECT '{}'@>'{\"a\":1}'", sql[:start] + sql[end:])
103
+
104
+ members_set = set(sql[start:end].split(","))
105
+ self.assertSetEqual({'"a":1', '"b":2'}, members_set)
106
+
107
+ def test_json_contains_for_field(self):
108
+ q = (
109
+ PostgreSQLQuery.from_(self.table_abc)
110
+ .select("*")
111
+ .where(self.table_abc.json.contains({"dates": "2018-07-10 - 2018-07-17"}))
112
+ )
113
+
114
+ self.assertEqual(
115
+ "SELECT * " 'FROM "abc" ' 'WHERE "json"@>\'{"dates":"2018-07-10 - 2018-07-17"}\'',
116
+ str(q),
117
+ )
118
+
119
+ def test_json_contained_by_using_str_arg(self):
120
+ q = (
121
+ PostgreSQLQuery.from_(self.table_abc)
122
+ .select("*")
123
+ .where(
124
+ self.table_abc.json.contained_by(
125
+ OrderedDict(
126
+ [
127
+ ("dates", "2018-07-10 - 2018-07-17"),
128
+ ("imported", "8"),
129
+ ]
130
+ )
131
+ )
132
+ )
133
+ )
134
+ self.assertEqual(
135
+ 'SELECT * FROM "abc" ' 'WHERE "json"<@\'{"dates":"2018-07-10 - 2018-07-17","imported":"8"}\'',
136
+ str(q),
137
+ )
138
+
139
+ def test_json_contained_by_using_list_arg(self):
140
+ q = (
141
+ PostgreSQLQuery.from_(self.table_abc)
142
+ .select("*")
143
+ .where(self.table_abc.json.contained_by(["One", "Two", "Three"]))
144
+ )
145
+
146
+ self.assertEqual('SELECT * FROM "abc" WHERE "json"<@\'["One","Two","Three"]\'', str(q))
147
+
148
+ def test_json_contained_by_with_complex_criterion(self):
149
+ q = (
150
+ PostgreSQLQuery.from_(self.table_abc)
151
+ .select("*")
152
+ .where(self.table_abc.json.contained_by(["One", "Two", "Three"]) & (self.table_abc.id == 26))
153
+ )
154
+
155
+ self.assertEqual(
156
+ 'SELECT * FROM "abc" WHERE "json"<@\'["One","Two","Three"]\' AND "id"=26',
157
+ str(q),
158
+ )
159
+
160
+ def test_json_has_key(self):
161
+ q = PostgreSQLQuery.from_(self.table_abc).select("*").where(self.table_abc.json.has_key("dates"))
162
+
163
+ self.assertEqual('SELECT * FROM "abc" WHERE "json"?\'dates\'', str(q))
164
+
165
+ def test_json_has_keys(self):
166
+ q = PostgreSQLQuery.from_(self.table_abc).select("*").where(self.table_abc.json.has_keys(["dates", "imported"]))
167
+
168
+ self.assertEqual("SELECT * FROM \"abc\" WHERE \"json\"?&ARRAY['dates','imported']", str(q))
169
+
170
+ def test_json_has_any_keys(self):
171
+ q = (
172
+ PostgreSQLQuery.from_(self.table_abc)
173
+ .select("*")
174
+ .where(self.table_abc.json.has_any_keys(["dates", "imported"]))
175
+ )
176
+
177
+ self.assertEqual("SELECT * FROM \"abc\" WHERE \"json\"?|ARRAY['dates','imported']", str(q))
178
+
179
+ def test_subnet_contains_inet(self):
180
+ q = (
181
+ PostgreSQLQuery.from_(self.table_abc)
182
+ .select(self.table_abc.a.lshift(2))
183
+ .where(self.table_abc.cidr >> "1.1.1.1")
184
+ )
185
+
186
+ self.assertEqual("SELECT \"a\"<<2 FROM \"abc\" WHERE \"cidr\">>'1.1.1.1'", str(q))
187
+
188
+
189
+ class DistinctOnTests(unittest.TestCase):
190
+ table_abc = Table("abc")
191
+
192
+ def test_distinct_on(self):
193
+ q = PostgreSQLQuery.from_(self.table_abc).distinct_on("lname", self.table_abc.fname).select("lname", "id")
194
+
195
+ self.assertEqual('''SELECT DISTINCT ON("lname","fname") "lname","id" FROM "abc"''', str(q))
196
+
197
+
198
+ class ArrayTests(unittest.TestCase):
199
+ def test_array_syntax(self):
200
+ tb = Table("tb")
201
+ q = PostgreSQLQuery.from_(tb).select(Array(1, "a", ["b", 2, 3]))
202
+
203
+ self.assertEqual(str(q), "SELECT ARRAY[1,'a',ARRAY['b',2,3]] FROM \"tb\"")
204
+
205
+ def test_render_alias_in_array_sql(self):
206
+ tb = Table("tb")
207
+
208
+ q = PostgreSQLQuery.from_(tb).select(Array(tb.col).as_("different_name"))
209
+ self.assertEqual(str(q), 'SELECT ARRAY["col"] "different_name" FROM "tb"')
210
+
211
+
212
+ class ReturningClauseTests(unittest.TestCase):
213
+ @classmethod
214
+ def setUpClass(cls) -> None:
215
+ super().setUpClass()
216
+ cls.table_abc = Table('abc')
217
+
218
+ def test_returning_from_missing_table_raises_queryexception(self):
219
+ field_from_diff_table = Field('xyz', table=Table('other'))
220
+
221
+ with self.assertRaisesRegex(QueryException, "You can't return from other tables"):
222
+ (
223
+ PostgreSQLQuery.from_(self.table_abc)
224
+ .where(self.table_abc.foo == self.table_abc.bar)
225
+ .delete()
226
+ .returning(field_from_diff_table)
227
+ )
228
+
229
+ def test_queryexception_if_returning_used_on_invalid_query(self):
230
+ with self.assertRaisesRegex(QueryException, "Returning can't be used in this query"):
231
+ PostgreSQLQuery.from_(self.table_abc).select('abc').returning('abc')
232
+
233
+ def test_no_queryexception_if_returning_used_on_valid_query_type(self):
234
+ # No exceptions for insert, update and delete queries
235
+ with self.subTest('DELETE'):
236
+ PostgreSQLQuery.from_(self.table_abc).where(self.table_abc.foo == self.table_abc.bar).delete().returning(
237
+ "id"
238
+ )
239
+ with self.subTest('UPDATE'):
240
+ PostgreSQLQuery.update(self.table_abc).where(self.table_abc.foo == 0).set("foo", "bar").returning("id")
241
+ with self.subTest('INSERT'):
242
+ PostgreSQLQuery.into(self.table_abc).insert('abc').returning('abc')
243
+
244
+ def test_return_field_from_join_table(self):
245
+ new_table = Table('xyz')
246
+ q = (
247
+ PostgreSQLQuery.update(self.table_abc)
248
+ .join(new_table)
249
+ .on(new_table.id == self.table_abc.xyz)
250
+ .where(self.table_abc.foo == 0)
251
+ .set("foo", "bar")
252
+ .returning(new_table.a)
253
+ )
254
+
255
+ self.assertEqual(
256
+ 'UPDATE "abc" '
257
+ 'JOIN "xyz" ON "xyz"."id"="abc"."xyz" '
258
+ 'SET "foo"=\'bar\' '
259
+ 'WHERE "abc"."foo"=0 '
260
+ 'RETURNING "xyz"."a"',
261
+ str(q),
262
+ )
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/dialects/test_snowflake.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from pypika import (
4
+ Tables,
5
+ functions as fn,
6
+ Column,
7
+ )
8
+ from pypika.dialects import SnowflakeQuery
9
+
10
+
11
+ class QuoteTests(unittest.TestCase):
12
+ table_abc, table_efg = Tables("abc", "efg")
13
+
14
+ def test_use_double_quotes_on_alias_but_not_on_terms(self):
15
+ q = SnowflakeQuery.from_(self.table_abc).select(self.table_abc.a.as_("bar"))
16
+
17
+ self.assertEqual('SELECT a "bar" FROM abc', q.get_sql(with_namespace=True))
18
+
19
+ def test_use_double_quotes_on_alias_but_not_on_terms_with_joins(self):
20
+ foo = self.table_abc.as_("foo")
21
+ bar = self.table_efg.as_("bar")
22
+
23
+ q = SnowflakeQuery.from_(foo).join(bar).on(foo.fk == bar.id).select(foo.a, bar.b)
24
+
25
+ self.assertEqual(
26
+ "SELECT foo.a,bar.b " 'FROM abc "foo" ' 'JOIN efg "bar" ' "ON foo.fk=bar.id",
27
+ q.get_sql(with_namespace=True),
28
+ )
29
+
30
+ def test_use_double_quotes_on_alias_but_not_on_terms(self):
31
+ idx = self.table_abc.index.as_("idx")
32
+ val = fn.Sum(self.table_abc.value).as_("val")
33
+ q = SnowflakeQuery.from_(self.table_abc).select(idx, val).groupby(idx).orderby(idx)
34
+
35
+ self.assertEqual(
36
+ 'SELECT index "idx",SUM(value) "val" ' "FROM abc " 'GROUP BY "idx" ' 'ORDER BY "idx"',
37
+ q.get_sql(with_namespace=True),
38
+ )
39
+
40
+ def test_dont_use_double_quotes_on_joining_queries(self):
41
+ foo = self.table_abc
42
+ bar = self.table_efg
43
+ q1 = SnowflakeQuery.from_(foo).select(foo.b)
44
+ q2 = SnowflakeQuery.from_(bar).select(bar.b)
45
+ q = SnowflakeQuery.from_(q1).join(q2).on(q1.b == q2.b).select("*")
46
+
47
+ self.assertEqual(
48
+ "SELECT * " 'FROM (SELECT b FROM abc) sq0 ' 'JOIN (SELECT b FROM efg) sq1 ' "ON sq0.b=sq1.b",
49
+ q.get_sql(),
50
+ )
51
+
52
+ def test_dont_use_double_quotes_on_create_queries(self):
53
+ q = SnowflakeQuery.create_table(self.table_abc).columns(Column("id", "INT"))
54
+ self.assertEqual("CREATE TABLE abc (id INT)", q.get_sql())
55
+
56
+ def test_dont_use_double_quotes_on_drop_queries(self):
57
+ q = SnowflakeQuery.drop_table(self.table_abc)
58
+ self.assertEqual("DROP TABLE abc", q.get_sql())
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/dialects/test_sqllite.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from pypika import Table
4
+ from pypika.dialects import SQLLiteQuery
5
+
6
+
7
+ class SelectTests(unittest.TestCase):
8
+ table_abc = Table("abc")
9
+
10
+ def test_bool_true_as_one(self):
11
+ q = SQLLiteQuery.from_("abc").select(True)
12
+
13
+ self.assertEqual('SELECT 1 FROM "abc"', str(q))
14
+
15
+ def test_bool_false_as_zero(self):
16
+ q = SQLLiteQuery.from_("abc").select(False)
17
+
18
+ self.assertEqual('SELECT 0 FROM "abc"', str(q))
19
+
20
+
21
+ class ReplaceTests(unittest.TestCase):
22
+ def test_normal_replace(self):
23
+ query = SQLLiteQuery.into("abc").replace("v1", "v2", "v3")
24
+ expected_output = "REPLACE INTO \"abc\" VALUES ('v1','v2','v3')"
25
+ self.assertEqual(expected_output, str(query))
26
+
27
+ def test_replace_subquery(self):
28
+ query = SQLLiteQuery.into("abc").replace(SQLLiteQuery.from_("def").select("f1", "f2"))
29
+ expected_output = 'REPLACE INTO "abc" VALUES ((SELECT "f1","f2" FROM "def"))'
30
+ self.assertEqual(expected_output, str(query))
31
+
32
+ def test_insert_or_replace(self):
33
+ query = SQLLiteQuery.into("abc").insert_or_replace("v1", "v2", "v3")
34
+ expected_output = "INSERT OR REPLACE INTO \"abc\" VALUES ('v1','v2','v3')"
35
+ self.assertEqual(expected_output, str(query))
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/dialects/test_vertica.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from pypika import Columns, Table, Tables, VerticaQuery
4
+
5
+
6
+ class VerticaQueryTests(unittest.TestCase):
7
+ table_abc = Table("abc")
8
+
9
+ def test_select_query_with_hint(self):
10
+ q = VerticaQuery.from_("abc").select("*").hint("test_hint")
11
+
12
+ self.assertEqual('SELECT /*+label(test_hint)*/ * FROM "abc"', str(q))
13
+
14
+ def test_insert_query_with_hint(self):
15
+ query = VerticaQuery.into(self.table_abc).insert(1).hint("test_hint")
16
+
17
+ self.assertEqual('INSERT /*+label(test_hint)*/ INTO "abc" VALUES (1)', str(query))
18
+
19
+ def test_update_query_with_hint(self):
20
+ q = VerticaQuery.update(self.table_abc).set("foo", "bar").hint("test_hint")
21
+
22
+ self.assertEqual('UPDATE /*+label(test_hint)*/ "abc" SET "foo"=\'bar\'', str(q))
23
+
24
+ def test_delete_query_with_hint(self):
25
+ q = VerticaQuery.from_("abc").delete().hint("test_hint")
26
+
27
+ self.assertEqual('DELETE /*+label(test_hint)*/ FROM "abc"', str(q))
28
+
29
+
30
+ class CopyCSVTests(unittest.TestCase):
31
+ table_abc = Table("abc")
32
+
33
+ def test_copy_from_file(self):
34
+ with self.subTest("with table as string"):
35
+ q = VerticaQuery.from_file("/path/to/file").copy_("abc")
36
+
37
+ self.assertEqual(
38
+ "COPY \"abc\" FROM LOCAL '/path/to/file' PARSER fcsvparser(header=false)",
39
+ str(q),
40
+ )
41
+
42
+ with self.subTest("with table"):
43
+ q = VerticaQuery.from_file("/path/to/file").copy_(self.table_abc)
44
+
45
+ self.assertEqual(
46
+ "COPY \"abc\" FROM LOCAL '/path/to/file' PARSER fcsvparser(header=false)",
47
+ str(q),
48
+ )
49
+
50
+
51
+ class CreateTemporaryTableTests(unittest.TestCase):
52
+ new_table, existing_table = Tables("abc", "efg")
53
+ foo, bar = Columns(("a", "INT"), ("b", "VARCHAR(100)"))
54
+ select = VerticaQuery.from_(existing_table).select(existing_table.foo, existing_table.bar)
55
+
56
+ def test_create_local_temporary_table(self):
57
+ with self.subTest("with columns"):
58
+ q = VerticaQuery.create_table(self.new_table).temporary().local().columns(self.foo, self.bar)
59
+
60
+ self.assertEqual('CREATE LOCAL TEMPORARY TABLE "abc" ("a" INT,"b" VARCHAR(100))', str(q))
61
+
62
+ with self.subTest("with select"):
63
+ q = VerticaQuery.create_table(self.new_table).temporary().local().as_select(self.select)
64
+
65
+ self.assertEqual(
66
+ 'CREATE LOCAL TEMPORARY TABLE "abc" AS (SELECT "foo","bar" FROM "efg")',
67
+ str(q),
68
+ )
69
+
70
+ def test_create_local_table_without_temporary_raises_error(self):
71
+ with self.assertRaises(AttributeError):
72
+ VerticaQuery.create_table(self.new_table).local()
73
+
74
+ def test_create_temporary_table_preserve_rows(self):
75
+ with self.subTest("with columns"):
76
+ q = VerticaQuery.create_table(self.new_table).temporary().preserve_rows().columns(self.foo, self.bar)
77
+
78
+ self.assertEqual(
79
+ 'CREATE TEMPORARY TABLE "abc" ("a" INT,"b" VARCHAR(100)) ON COMMIT PRESERVE ROWS',
80
+ str(q),
81
+ )
82
+
83
+ with self.subTest("with select"):
84
+ q = VerticaQuery.create_table(self.new_table).temporary().preserve_rows().as_select(self.select)
85
+
86
+ self.assertEqual(
87
+ 'CREATE TEMPORARY TABLE "abc" ON COMMIT PRESERVE ROWS AS (SELECT "foo","bar" FROM "efg")',
88
+ str(q),
89
+ )
90
+
91
+ def test_create_table_preserve_rows_without_temporary_raises_error(self):
92
+ with self.assertRaises(AttributeError):
93
+ VerticaQuery.create_table(self.new_table).preserve_rows()
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_aggregate.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from pypika import (
4
+ Case,
5
+ Field,
6
+ Table,
7
+ functions as fn,
8
+ )
9
+ from pypika.terms import (
10
+ Negative,
11
+ ValueWrapper,
12
+ )
13
+
14
+
15
+ class IsAggregateTests(unittest.TestCase):
16
+ def test__field_is_not_aggregate(self):
17
+ v = Field("foo")
18
+ self.assertFalse(v.is_aggregate)
19
+
20
+ def test__constant_is_aggregate_none(self):
21
+ v = ValueWrapper(100)
22
+ self.assertIsNone(v.is_aggregate)
23
+
24
+ def test__constant_arithmetic_is_aggregate_none(self):
25
+ v = ValueWrapper(100) + ValueWrapper(100)
26
+ self.assertIsNone(v.is_aggregate)
27
+
28
+ def test__field_arithmetic_is_not_aggregate(self):
29
+ v = Field("foo") + Field("bar")
30
+ self.assertFalse(v.is_aggregate)
31
+
32
+ def test__field_arithmetic_constant_is_not_aggregate(self):
33
+ v = Field("foo") + 1
34
+ self.assertFalse(v.is_aggregate)
35
+
36
+ def test__agg_func_is_aggregate(self):
37
+ v = fn.Sum(Field("foo"))
38
+ self.assertTrue(v.is_aggregate)
39
+
40
+ def test__negative_agg_func_is_aggregate(self):
41
+ v = Negative(fn.Sum(Field("foo")))
42
+ self.assertTrue(v.is_aggregate)
43
+
44
+ def test__agg_func_arithmetic_is_aggregate(self):
45
+ v = fn.Sum(Field("foo")) / fn.Sum(Field("foo"))
46
+ self.assertTrue(v.is_aggregate)
47
+
48
+ def test__mixed_func_arithmetic_is_not_aggregate(self):
49
+ v = Field("foo") / fn.Sum(Field("foo"))
50
+ self.assertFalse(v.is_aggregate)
51
+
52
+ def test__func_arithmetic_constant_is_not_aggregate(self):
53
+ v = 1 / fn.Sum(Field("foo"))
54
+ self.assertTrue(v.is_aggregate)
55
+
56
+ def test__agg_case_criterion_is_aggregate(self):
57
+ v = Case().when(fn.Sum(Field("foo")) > 666, 'More than 666').else_('Less than 666')
58
+
59
+ self.assertTrue(v.is_aggregate)
60
+
61
+ def test__agg_case_is_aggregate(self):
62
+ v = (
63
+ Case()
64
+ .when(Field("foo") == 1, fn.Sum(Field("bar")))
65
+ .when(Field("foo") == 2, fn.Sum(Field("fiz")))
66
+ .else_(fn.Sum(Field("fiz")))
67
+ )
68
+
69
+ self.assertTrue(v.is_aggregate)
70
+
71
+ def test__mixed_case_is_not_aggregate(self):
72
+ v = Case().when(Field("foo") == 1, fn.Sum(Field("bar"))).when(Field("foo") == 2, Field("fiz"))
73
+
74
+ self.assertFalse(v.is_aggregate)
75
+
76
+ def test__case_mixed_else_is_not_aggregate(self):
77
+ v = (
78
+ Case()
79
+ .when(Field("foo") == 1, fn.Sum(Field("bar")))
80
+ .when(Field("foo") == 2, fn.Sum(Field("fiz")))
81
+ .else_(Field("fiz"))
82
+ )
83
+
84
+ self.assertFalse(v.is_aggregate)
85
+
86
+ def test__case_mixed_constant_is_not_aggregate(self):
87
+ v = Case().when(Field("foo") == 1, fn.Sum(Field("bar"))).when(Field("foo") == 2, fn.Sum(Field("fiz"))).else_(1)
88
+
89
+ self.assertTrue(v.is_aggregate)
90
+
91
+ def test__case_with_field_is_not_aggregate(self):
92
+ v = Case().when(Field("foo") == 1, 1).when(Field("foo") == 2, 2).else_(3)
93
+
94
+ self.assertFalse(v.is_aggregate)
95
+
96
+ def test__case_with_single_aggregate_field_in_one_criterion_is_aggregate(self):
97
+ v = Case().when(Field("foo") == 1, 1).when(fn.Sum(Field("foo")) == 2, 2).else_(3)
98
+
99
+ self.assertTrue(v.is_aggregate)
100
+
101
+ def test__non_aggregate_function_with_aggregated_arg(self):
102
+ t = Table("abc")
103
+ expr = fn.Sqrt(fn.Sum(t.a))
104
+
105
+ self.assertTrue(expr.is_aggregate)
106
+
107
+ def test_complicated(self):
108
+ t = Table("abc")
109
+ is_placebo = t.campaign_extra_info == "placebo"
110
+
111
+ pixel_mobile_search = Case().when(is_placebo, t.action_fb_pixel_search + t.action_fb_mobile_search)
112
+ unique_impressions = Case().when(is_placebo, t.unique_impressions)
113
+
114
+ v = fn.Sum(pixel_mobile_search) / fn.Sum(unique_impressions) - 1.96 * fn.Sqrt(
115
+ 1
116
+ / fn.Sum(unique_impressions)
117
+ * fn.Sum(pixel_mobile_search)
118
+ / fn.Sum(unique_impressions)
119
+ * (1 - fn.Sum(pixel_mobile_search) / fn.Sum(unique_impressions))
120
+ )
121
+
122
+ self.assertTrue(v.is_aggregate)
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_analytic_queries.py ADDED
@@ -0,0 +1,610 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from pypika import Criterion, JoinType, Order, Query, Tables, analytics as an
4
+ from pypika.analytics import Lag, Lead
5
+
6
+ __author__ = "Timothy Heys"
7
+ __email__ = "theys@kayak.com"
8
+
9
+
10
+ class RankTests(unittest.TestCase):
11
+ table_abc, table_efg = Tables("abc", "efg")
12
+
13
+ def test_rank(self):
14
+ expr = an.Rank().over(self.table_abc.foo).orderby(self.table_abc.date)
15
+
16
+ q = Query.from_(self.table_abc).select(expr)
17
+
18
+ self.assertEqual(
19
+ "SELECT " "RANK() " 'OVER(PARTITION BY "foo" ORDER BY "date") ' 'FROM "abc"',
20
+ str(q),
21
+ )
22
+
23
+ def test_dense_rank(self):
24
+ expr = an.DenseRank().over(self.table_abc.foo).orderby(self.table_abc.date)
25
+
26
+ q = Query.from_(self.table_abc).select(expr)
27
+
28
+ self.assertEqual(
29
+ "SELECT " "DENSE_RANK() " 'OVER(PARTITION BY "foo" ORDER BY "date") ' 'FROM "abc"',
30
+ str(q),
31
+ )
32
+
33
+ def test_row_number(self):
34
+ expr = an.RowNumber().over(self.table_abc.foo).orderby(self.table_abc.date)
35
+
36
+ q = Query.from_(self.table_abc).select(expr)
37
+
38
+ self.assertEqual(
39
+ "SELECT " "ROW_NUMBER() " 'OVER(PARTITION BY "foo" ORDER BY "date") ' 'FROM "abc"',
40
+ str(q),
41
+ )
42
+
43
+ def test_rank_with_alias(self):
44
+ expr = an.Rank().over(self.table_abc.foo).orderby(self.table_abc.date).as_("rank")
45
+
46
+ q = Query.from_(self.table_abc).select(expr)
47
+
48
+ self.assertEqual(
49
+ "SELECT " "RANK() " 'OVER(PARTITION BY "foo" ORDER BY "date") "rank" ' 'FROM "abc"',
50
+ str(q),
51
+ )
52
+
53
+ def test_multiple_partitions(self):
54
+ expr = an.Rank().orderby(self.table_abc.date).over(self.table_abc.foo, self.table_abc.bar)
55
+
56
+ q = Query.from_(self.table_abc).select(expr)
57
+
58
+ self.assertEqual(
59
+ "SELECT " "RANK() " 'OVER(PARTITION BY "foo","bar" ORDER BY "date") ' 'FROM "abc"',
60
+ str(q),
61
+ )
62
+
63
+ def test_ntile_no_partition_or_order_invalid_sql(self):
64
+ expr = an.NTile(5)
65
+
66
+ q = Query.from_(self.table_abc).select(expr)
67
+
68
+ self.assertEqual("SELECT " "NTILE(5) " 'FROM "abc"', str(q))
69
+
70
+ def test_ntile_with_partition(self):
71
+ expr = an.NTile(5).over(self.table_abc.foo)
72
+
73
+ q = Query.from_(self.table_abc).select(expr)
74
+
75
+ self.assertEqual("SELECT " "NTILE(5) " 'OVER(PARTITION BY "foo") ' 'FROM "abc"', str(q))
76
+
77
+ def test_ntile_with_order(self):
78
+ expr = an.NTile(5).orderby(self.table_abc.date)
79
+
80
+ q = Query.from_(self.table_abc).select(expr)
81
+
82
+ self.assertEqual("SELECT " "NTILE(5) " 'OVER(ORDER BY "date") ' 'FROM "abc"', str(q))
83
+
84
+ def test_ntile_with_partition_and_order(self):
85
+ expr = an.NTile(5).over(self.table_abc.foo).orderby(self.table_abc.date)
86
+
87
+ q = Query.from_(self.table_abc).select(expr)
88
+
89
+ self.assertEqual(
90
+ "SELECT " "NTILE(5) " 'OVER(PARTITION BY "foo" ORDER BY "date") ' 'FROM "abc"',
91
+ str(q),
92
+ )
93
+
94
+ def test_first_value(self):
95
+ expr = an.FirstValue(self.table_abc.fizz).over(self.table_abc.foo).orderby(self.table_abc.date)
96
+
97
+ q = Query.from_(self.table_abc).select(expr)
98
+
99
+ self.assertEqual(
100
+ "SELECT " 'FIRST_VALUE("fizz") ' 'OVER(PARTITION BY "foo" ORDER BY "date") ' 'FROM "abc"',
101
+ str(q),
102
+ )
103
+
104
+ def test_first_value_ignore_nulls(self):
105
+ expr = an.FirstValue(self.table_abc.fizz).over(self.table_abc.foo).orderby(self.table_abc.date).ignore_nulls()
106
+
107
+ q = Query.from_(self.table_abc).select(expr)
108
+
109
+ self.assertEqual(
110
+ "SELECT " 'FIRST_VALUE("fizz" IGNORE NULLS) ' 'OVER(PARTITION BY "foo" ORDER BY "date") ' 'FROM "abc"',
111
+ str(q),
112
+ )
113
+
114
+ def test_first_value_ignore_nulls_first(self):
115
+ expr = an.FirstValue(self.table_abc.fizz).ignore_nulls().over(self.table_abc.foo).orderby(self.table_abc.date)
116
+
117
+ q = Query.from_(self.table_abc).select(expr)
118
+
119
+ self.assertEqual(
120
+ "SELECT " 'FIRST_VALUE("fizz" IGNORE NULLS) ' 'OVER(PARTITION BY "foo" ORDER BY "date") ' 'FROM "abc"',
121
+ str(q),
122
+ )
123
+
124
+ def test_first_value_multi_argument(self):
125
+ expr = (
126
+ an.FirstValue(self.table_abc.fizz, self.table_abc.buzz)
127
+ .over(self.table_abc.foo)
128
+ .orderby(self.table_abc.date)
129
+ )
130
+
131
+ q = Query.from_(self.table_abc).select(expr)
132
+
133
+ self.assertEqual(
134
+ "SELECT " 'FIRST_VALUE("fizz","buzz") ' 'OVER(PARTITION BY "foo" ORDER BY "date") ' 'FROM "abc"',
135
+ str(q),
136
+ )
137
+
138
+ def test_last_value(self):
139
+ expr = an.LastValue(self.table_abc.fizz).over(self.table_abc.foo).orderby(self.table_abc.date)
140
+
141
+ q = Query.from_(self.table_abc).select(expr)
142
+
143
+ self.assertEqual(
144
+ "SELECT " 'LAST_VALUE("fizz") ' 'OVER(PARTITION BY "foo" ORDER BY "date") ' 'FROM "abc"',
145
+ str(q),
146
+ )
147
+
148
+ def test_filter(self):
149
+ expr = (
150
+ an.LastValue(self.table_abc.fizz)
151
+ .filter(Criterion.all([self.table_abc.bar == True]))
152
+ .over(self.table_abc.foo)
153
+ .orderby(self.table_abc.date)
154
+ )
155
+
156
+ q = Query.from_(self.table_abc).select(expr)
157
+
158
+ self.assertEqual(
159
+ "SELECT "
160
+ 'LAST_VALUE("fizz") '
161
+ 'FILTER(WHERE "bar"=true) '
162
+ 'OVER(PARTITION BY "foo" ORDER BY "date") '
163
+ 'FROM "abc"',
164
+ str(q),
165
+ )
166
+
167
+ def test_filter_quote_table_in_filter(self):
168
+ expr = (
169
+ an.LastValue(self.table_efg.fizz)
170
+ .filter(self.table_efg.filed.eq('yes'))
171
+ .over(self.table_efg.foo)
172
+ .orderby(self.table_efg.date)
173
+ )
174
+
175
+ q = (
176
+ Query.from_(self.table_abc)
177
+ .inner_join(self.table_efg)
178
+ .on(self.table_abc.id.eq(self.table_efg.id))
179
+ .select(expr)
180
+ )
181
+
182
+ self.assertEqual(
183
+ 'SELECT '
184
+ 'LAST_VALUE("efg"."fizz") '
185
+ 'FILTER(WHERE "efg"."filed"=\'yes\') '
186
+ 'OVER(PARTITION BY "efg"."foo" ORDER BY "efg"."date") '
187
+ 'FROM "abc" '
188
+ 'JOIN "efg" ON "abc"."id"="efg"."id"',
189
+ str(q),
190
+ )
191
+
192
+ def test_orderby_asc(self):
193
+ expr = an.LastValue(self.table_abc.fizz).over(self.table_abc.foo).orderby(self.table_abc.date, order=Order.asc)
194
+
195
+ q = Query.from_(self.table_abc).select(expr)
196
+
197
+ self.assertEqual(
198
+ "SELECT " 'LAST_VALUE("fizz") ' 'OVER(PARTITION BY "foo" ORDER BY "date" ASC) ' 'FROM "abc"',
199
+ str(q),
200
+ )
201
+
202
+ def test_orderby_desc(self):
203
+ expr = an.LastValue(self.table_abc.fizz).over(self.table_abc.foo).orderby(self.table_abc.date, order=Order.desc)
204
+
205
+ q = Query.from_(self.table_abc).select(expr)
206
+
207
+ self.assertEqual(
208
+ "SELECT " 'LAST_VALUE("fizz") ' 'OVER(PARTITION BY "foo" ORDER BY "date" DESC) ' 'FROM "abc"',
209
+ str(q),
210
+ )
211
+
212
+ def test_last_value_multi_argument(self):
213
+ expr = (
214
+ an.LastValue(self.table_abc.fizz, self.table_abc.buzz).over(self.table_abc.foo).orderby(self.table_abc.date)
215
+ )
216
+
217
+ q = Query.from_(self.table_abc).select(expr)
218
+
219
+ self.assertEqual(
220
+ "SELECT " 'LAST_VALUE("fizz","buzz") ' 'OVER(PARTITION BY "foo" ORDER BY "date") ' 'FROM "abc"',
221
+ str(q),
222
+ )
223
+
224
+ def test_last_value_ignore_nulls(self):
225
+ expr = an.LastValue(self.table_abc.fizz).over(self.table_abc.foo).orderby(self.table_abc.date).ignore_nulls()
226
+
227
+ q = Query.from_(self.table_abc).select(expr)
228
+
229
+ self.assertEqual(
230
+ "SELECT " 'LAST_VALUE("fizz" IGNORE NULLS) ' 'OVER(PARTITION BY "foo" ORDER BY "date") ' 'FROM "abc"',
231
+ str(q),
232
+ )
233
+
234
+ def test_median(self):
235
+ expr = an.Median(self.table_abc.fizz).over(self.table_abc.foo, self.table_abc.bar).orderby(self.table_abc.date)
236
+
237
+ q = Query.from_(self.table_abc).select(expr)
238
+
239
+ self.assertEqual(
240
+ "SELECT " 'MEDIAN("fizz") ' 'OVER(PARTITION BY "foo","bar" ORDER BY "date") ' 'FROM "abc"',
241
+ str(q),
242
+ )
243
+
244
+ def test_avg(self):
245
+ expr = an.Avg(self.table_abc.fizz).over(self.table_abc.foo, self.table_abc.bar).orderby(self.table_abc.date)
246
+
247
+ q = Query.from_(self.table_abc).select(expr)
248
+
249
+ self.assertEqual(
250
+ "SELECT " 'AVG("fizz") ' 'OVER(PARTITION BY "foo","bar" ORDER BY "date") ' 'FROM "abc"',
251
+ str(q),
252
+ )
253
+
254
+ def test_stddev(self):
255
+ expr = an.StdDev(self.table_abc.fizz).over(self.table_abc.foo, self.table_abc.bar).orderby(self.table_abc.date)
256
+
257
+ q = Query.from_(self.table_abc).select(expr)
258
+
259
+ self.assertEqual(
260
+ "SELECT " 'STDDEV("fizz") ' 'OVER(PARTITION BY "foo","bar" ORDER BY "date") ' 'FROM "abc"',
261
+ str(q),
262
+ )
263
+
264
+ def test_stddev_pop(self):
265
+ expr = (
266
+ an.StdDevPop(self.table_abc.fizz).over(self.table_abc.foo, self.table_abc.bar).orderby(self.table_abc.date)
267
+ )
268
+
269
+ q = Query.from_(self.table_abc).select(expr)
270
+
271
+ self.assertEqual(
272
+ "SELECT " 'STDDEV_POP("fizz") ' 'OVER(PARTITION BY "foo","bar" ORDER BY "date") ' 'FROM "abc"',
273
+ str(q),
274
+ )
275
+
276
+ def test_stddev_samp(self):
277
+ expr = (
278
+ an.StdDevSamp(self.table_abc.fizz).over(self.table_abc.foo, self.table_abc.bar).orderby(self.table_abc.date)
279
+ )
280
+
281
+ q = Query.from_(self.table_abc).select(expr)
282
+
283
+ self.assertEqual(
284
+ "SELECT " 'STDDEV_SAMP("fizz") ' 'OVER(PARTITION BY "foo","bar" ORDER BY "date") ' 'FROM "abc"',
285
+ str(q),
286
+ )
287
+
288
+ def test_variance(self):
289
+ expr = (
290
+ an.Variance(self.table_abc.fizz).over(self.table_abc.foo, self.table_abc.bar).orderby(self.table_abc.date)
291
+ )
292
+
293
+ q = Query.from_(self.table_abc).select(expr)
294
+
295
+ self.assertEqual(
296
+ "SELECT " 'VARIANCE("fizz") ' 'OVER(PARTITION BY "foo","bar" ORDER BY "date") ' 'FROM "abc"',
297
+ str(q),
298
+ )
299
+
300
+ def test_var_pop(self):
301
+ expr = an.VarPop(self.table_abc.fizz).over(self.table_abc.foo, self.table_abc.bar).orderby(self.table_abc.date)
302
+
303
+ q = Query.from_(self.table_abc).select(expr)
304
+
305
+ self.assertEqual(
306
+ "SELECT " 'VAR_POP("fizz") ' 'OVER(PARTITION BY "foo","bar" ORDER BY "date") ' 'FROM "abc"',
307
+ str(q),
308
+ )
309
+
310
+ def test_var_samp(self):
311
+ expr = an.VarSamp(self.table_abc.fizz).over(self.table_abc.foo, self.table_abc.bar).orderby(self.table_abc.date)
312
+
313
+ q = Query.from_(self.table_abc).select(expr)
314
+
315
+ self.assertEqual(
316
+ "SELECT " 'VAR_SAMP("fizz") ' 'OVER(PARTITION BY "foo","bar" ORDER BY "date") ' 'FROM "abc"',
317
+ str(q),
318
+ )
319
+
320
+ def test_table_prefixes_used_in_analytic_functions(self):
321
+ expr = an.Rank().over(self.table_abc.foo).orderby(self.table_efg.date)
322
+
323
+ query = (
324
+ Query.from_(self.table_abc)
325
+ .join(self.table_efg, how=JoinType.left)
326
+ .on(self.table_abc.foo == self.table_efg.bar)
327
+ .select("*", expr)
328
+ )
329
+
330
+ self.assertEqual(
331
+ 'SELECT *,RANK() OVER(PARTITION BY "abc"."foo" ORDER BY "efg"."date") '
332
+ 'FROM "abc" LEFT JOIN "efg" ON "abc"."foo"="efg"."bar"',
333
+ str(query),
334
+ )
335
+
336
+ def test_sum_rows_unbounded_preceeding(self):
337
+ expr = an.Sum(self.table_abc.fizz).over(self.table_abc.foo).orderby(self.table_abc.date).rows(an.Preceding())
338
+
339
+ q = Query.from_(self.table_abc).select(expr)
340
+
341
+ self.assertEqual(
342
+ "SELECT "
343
+ 'SUM("fizz") '
344
+ "OVER("
345
+ 'PARTITION BY "foo" ORDER BY "date" '
346
+ "ROWS UNBOUNDED PRECEDING"
347
+ ") "
348
+ 'FROM "abc"',
349
+ str(q),
350
+ )
351
+
352
+ def test_max_rows_x_preceeding(self):
353
+ expr = an.Max(self.table_abc.fizz).over(self.table_abc.foo).orderby(self.table_abc.date).rows(an.Preceding(5))
354
+
355
+ q = Query.from_(self.table_abc).select(expr)
356
+
357
+ self.assertEqual(
358
+ "SELECT " 'MAX("fizz") ' "OVER(" 'PARTITION BY "foo" ORDER BY "date" ' "ROWS 5 PRECEDING" ") " 'FROM "abc"',
359
+ str(q),
360
+ )
361
+
362
+ def test_min_rows_current_row(self):
363
+ expr = an.Min(self.table_abc.fizz).over(self.table_abc.foo).orderby(self.table_abc.date).rows(an.CURRENT_ROW)
364
+
365
+ q = Query.from_(self.table_abc).select(expr)
366
+
367
+ self.assertEqual(
368
+ "SELECT " 'MIN("fizz") ' "OVER(" 'PARTITION BY "foo" ORDER BY "date" ' "ROWS CURRENT ROW" ") " 'FROM "abc"',
369
+ str(q),
370
+ )
371
+
372
+ def test_varpop_range_unbounded_preceeding(self):
373
+ expr = (
374
+ an.VarPop(self.table_abc.fizz).over(self.table_abc.foo).orderby(self.table_abc.date).range(an.Preceding())
375
+ )
376
+
377
+ q = Query.from_(self.table_abc).select(expr)
378
+
379
+ self.assertEqual(
380
+ "SELECT "
381
+ 'VAR_POP("fizz") '
382
+ "OVER("
383
+ 'PARTITION BY "foo" ORDER BY "date" '
384
+ "RANGE UNBOUNDED PRECEDING"
385
+ ") "
386
+ 'FROM "abc"',
387
+ str(q),
388
+ )
389
+
390
+ def test_max_range_x_preceeding(self):
391
+ expr = an.Max(self.table_abc.fizz).over(self.table_abc.foo).orderby(self.table_abc.date).range(an.Preceding(5))
392
+
393
+ q = Query.from_(self.table_abc).select(expr)
394
+
395
+ self.assertEqual(
396
+ "SELECT "
397
+ 'MAX("fizz") '
398
+ "OVER("
399
+ 'PARTITION BY "foo" ORDER BY "date" '
400
+ "RANGE 5 PRECEDING"
401
+ ") "
402
+ 'FROM "abc"',
403
+ str(q),
404
+ )
405
+
406
+ def test_min_range_current_row(self):
407
+ expr = an.Min(self.table_abc.fizz).over(self.table_abc.foo).orderby(self.table_abc.date).range(an.CURRENT_ROW)
408
+
409
+ q = Query.from_(self.table_abc).select(expr)
410
+
411
+ self.assertEqual(
412
+ "SELECT "
413
+ 'MIN("fizz") '
414
+ "OVER("
415
+ 'PARTITION BY "foo" ORDER BY "date" '
416
+ "RANGE CURRENT ROW"
417
+ ") "
418
+ 'FROM "abc"',
419
+ str(q),
420
+ )
421
+
422
+ def test_variance_rows_between_unbounded_preceeding_unbounded_following(self):
423
+ expr = (
424
+ an.Variance(self.table_abc.fizz)
425
+ .over(self.table_abc.foo)
426
+ .orderby(self.table_abc.date)
427
+ .rows(an.Preceding(), an.Following())
428
+ )
429
+
430
+ q = Query.from_(self.table_abc).select(expr)
431
+
432
+ self.assertEqual(
433
+ "SELECT "
434
+ 'VARIANCE("fizz") '
435
+ "OVER("
436
+ 'PARTITION BY "foo" ORDER BY "date" '
437
+ "ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING"
438
+ ") "
439
+ 'FROM "abc"',
440
+ str(q),
441
+ )
442
+
443
+ def test_first_value_range_between_x_preceeding_unbounded_following(self):
444
+ expr = (
445
+ an.FirstValue(self.table_abc.fizz)
446
+ .over(self.table_abc.foo)
447
+ .orderby(self.table_abc.date)
448
+ .range(an.Preceding(3), an.Following())
449
+ )
450
+
451
+ q = Query.from_(self.table_abc).select(expr)
452
+
453
+ self.assertEqual(
454
+ "SELECT "
455
+ 'FIRST_VALUE("fizz") '
456
+ "OVER("
457
+ 'PARTITION BY "foo" ORDER BY "date" '
458
+ "RANGE BETWEEN 3 PRECEDING AND UNBOUNDED FOLLOWING"
459
+ ") "
460
+ 'FROM "abc"',
461
+ str(q),
462
+ )
463
+
464
+ def test_varpop_rows_between_unbounded_preceeding_x_following(self):
465
+ expr = (
466
+ an.VarPop(self.table_abc.fizz)
467
+ .over(self.table_abc.foo)
468
+ .orderby(self.table_abc.date)
469
+ .rows(an.Preceding(), an.Following(6))
470
+ )
471
+
472
+ q = Query.from_(self.table_abc).select(expr)
473
+
474
+ self.assertEqual(
475
+ "SELECT "
476
+ 'VAR_POP("fizz") '
477
+ "OVER("
478
+ 'PARTITION BY "foo" ORDER BY "date" '
479
+ "ROWS BETWEEN UNBOUNDED PRECEDING AND 6 FOLLOWING"
480
+ ") "
481
+ 'FROM "abc"',
482
+ str(q),
483
+ )
484
+
485
+ def test_count_range_between_unbounded_preceeding_current_row(self):
486
+ expr = (
487
+ an.Count(self.table_abc.fizz)
488
+ .over(self.table_abc.foo)
489
+ .orderby(self.table_abc.date)
490
+ .range(an.Preceding(), an.CURRENT_ROW)
491
+ )
492
+
493
+ q = Query.from_(self.table_abc).select(expr)
494
+
495
+ self.assertEqual(
496
+ "SELECT "
497
+ 'COUNT("fizz") '
498
+ "OVER("
499
+ 'PARTITION BY "foo" ORDER BY "date" '
500
+ "RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW"
501
+ ") "
502
+ 'FROM "abc"',
503
+ str(q),
504
+ )
505
+
506
+ def test_last_value_rows_between_current_row_unbounded_following(self):
507
+ expr = (
508
+ an.LastValue(self.table_abc.fizz)
509
+ .over(self.table_abc.foo)
510
+ .orderby(self.table_abc.date)
511
+ .rows(an.CURRENT_ROW, an.Following())
512
+ )
513
+
514
+ q = Query.from_(self.table_abc).select(expr)
515
+
516
+ self.assertEqual(
517
+ "SELECT "
518
+ 'LAST_VALUE("fizz") '
519
+ "OVER("
520
+ 'PARTITION BY "foo" ORDER BY "date" '
521
+ "ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING"
522
+ ") "
523
+ 'FROM "abc"',
524
+ str(q),
525
+ )
526
+
527
+ def test_last_value_rows_between_current_row_unbounded_following_ignore_nulls(self):
528
+ expr = (
529
+ an.LastValue(self.table_abc.fizz)
530
+ .over(self.table_abc.foo)
531
+ .orderby(self.table_abc.date)
532
+ .ignore_nulls()
533
+ .rows(an.CURRENT_ROW, an.Following(8))
534
+ )
535
+
536
+ q = Query.from_(self.table_abc).select(expr)
537
+
538
+ self.assertEqual(
539
+ "SELECT "
540
+ 'LAST_VALUE("fizz" IGNORE NULLS) '
541
+ "OVER("
542
+ 'PARTITION BY "foo" ORDER BY "date" '
543
+ "ROWS BETWEEN CURRENT ROW AND 8 FOLLOWING"
544
+ ") "
545
+ 'FROM "abc"',
546
+ str(q),
547
+ )
548
+
549
+ def test_empty_over(self):
550
+ query = Query.from_(self.table_abc).select(an.Sum(self.table_abc.fizz).over())
551
+
552
+ self.assertEqual("SELECT " 'SUM("fizz") OVER() ' 'FROM "abc"', str(query))
553
+
554
+ def test_rows_called_twice_raises_attribute_error(self):
555
+ with self.assertRaises(AttributeError):
556
+ an.Sum(self.table_abc.fizz).over(self.table_abc.foo).orderby(self.table_abc.date).rows(an.Preceding()).rows(
557
+ an.Preceding()
558
+ )
559
+
560
+ def test_range_called_twice_raises_attribute_error(self):
561
+ with self.assertRaises(AttributeError):
562
+ an.Sum(self.table_abc.fizz).over(self.table_abc.foo).orderby(self.table_abc.date).range(
563
+ an.Preceding()
564
+ ).range(an.Preceding())
565
+
566
+ def test_rows_then_range_raises_attribute_error(self):
567
+ with self.assertRaises(AttributeError):
568
+ an.Sum(self.table_abc.fizz).over(self.table_abc.foo).orderby(self.table_abc.date).rows(
569
+ an.Preceding()
570
+ ).range(an.Preceding())
571
+
572
+ def test_range_then_rows_raises_attribute_error(self):
573
+ with self.assertRaises(AttributeError):
574
+ an.Sum(self.table_abc.fizz).over(self.table_abc.foo).orderby(self.table_abc.date).range(
575
+ an.Preceding()
576
+ ).rows(an.Preceding())
577
+
578
+ def test_lag_generates_correct_sql(self):
579
+ with self.subTest('without partition by'):
580
+ q = Query.from_(self.table_abc).select(
581
+ Lag(self.table_abc.date, 1, '2000-01-01').orderby(self.table_abc.date)
582
+ )
583
+
584
+ self.assertEqual('SELECT LAG("date",1,\'2000-01-01\') OVER(ORDER BY "date") FROM "abc"', str(q))
585
+
586
+ with self.subTest('with partition by'):
587
+ q = Query.from_(self.table_abc).select(
588
+ Lag(self.table_abc.date, 1, '2000-01-01').over(self.table_abc.foo).orderby(self.table_abc.date)
589
+ )
590
+
591
+ self.assertEqual(
592
+ 'SELECT LAG("date",1,\'2000-01-01\') OVER(PARTITION BY "foo" ORDER BY "date") FROM "abc"', str(q)
593
+ )
594
+
595
+ def test_lead_generates_correct_sql(self):
596
+ with self.subTest('without partition by'):
597
+ q = Query.from_(self.table_abc).select(
598
+ Lead(self.table_abc.date, 1, '2000-01-01').orderby(self.table_abc.date)
599
+ )
600
+
601
+ self.assertEqual('SELECT LEAD("date",1,\'2000-01-01\') OVER(ORDER BY "date") FROM "abc"', str(q))
602
+
603
+ with self.subTest('with partition by'):
604
+ q = Query.from_(self.table_abc).select(
605
+ Lead(self.table_abc.date, 1, '2000-01-01').over(self.table_abc.foo).orderby(self.table_abc.date)
606
+ )
607
+
608
+ self.assertEqual(
609
+ 'SELECT LEAD("date",1,\'2000-01-01\') OVER(PARTITION BY "foo" ORDER BY "date") FROM "abc"', str(q)
610
+ )
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_create.py ADDED
@@ -0,0 +1,210 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from pypika import Column, Columns, Query, Tables, Table
4
+ from pypika.terms import ValueWrapper, Index
5
+ from pypika.enums import ReferenceOption
6
+
7
+
8
+ class CreateTableTests(unittest.TestCase):
9
+ new_table, existing_table = Tables("abc", "efg")
10
+ foo, bar = Columns(("a", "INT"), ("b", "VARCHAR(100)"))
11
+
12
+ def test_create_table_with_columns(self):
13
+ with self.subTest("with nullable"):
14
+ a = Column("a", "INT", True)
15
+ b = Column("b", "VARCHAR(100)", False)
16
+ q = Query.create_table(self.new_table).columns(a, b)
17
+
18
+ self.assertEqual('CREATE TABLE "abc" ("a" INT NULL,"b" VARCHAR(100) NOT NULL)', str(q))
19
+
20
+ with self.subTest("with defaults"):
21
+ a = Column("a", "INT", default=ValueWrapper(42))
22
+ b = Column("b", "VARCHAR(100)", default=ValueWrapper("foo"))
23
+ q = Query.create_table(self.new_table).columns(a, b)
24
+
25
+ self.assertEqual('CREATE TABLE "abc" ("a" INT DEFAULT 42,"b" VARCHAR(100) DEFAULT \'foo\')', str(q))
26
+
27
+ with self.subTest("with unwrapped defaults"):
28
+ a = Column("a", "INT", default=42)
29
+ b = Column("b", "VARCHAR(100)", default="foo")
30
+ q = Query.create_table(self.new_table).columns(a, b)
31
+
32
+ self.assertEqual('CREATE TABLE "abc" ("a" INT DEFAULT 42,"b" VARCHAR(100) DEFAULT \'foo\')', str(q))
33
+
34
+ with self.subTest("with period for"):
35
+ a = Column("id", "INT")
36
+ b = Column("valid_from", "DATETIME")
37
+ c = Column("valid_to", "DATETIME")
38
+ q = Query.create_table(self.new_table).columns(a, b, c).period_for('valid_period', b, c)
39
+
40
+ self.assertEqual(
41
+ 'CREATE TABLE "abc" ('
42
+ '"id" INT,'
43
+ '"valid_from" DATETIME,'
44
+ '"valid_to" DATETIME,'
45
+ 'PERIOD FOR "valid_period" ("valid_from","valid_to"))',
46
+ str(q),
47
+ )
48
+
49
+ with self.subTest("without temporary keyword"):
50
+ q = Query.create_table(self.new_table).columns(self.foo, self.bar)
51
+
52
+ self.assertEqual('CREATE TABLE "abc" ("a" INT,"b" VARCHAR(100))', str(q))
53
+
54
+ with self.subTest("with temporary keyword"):
55
+ q = Query.create_table(self.new_table).temporary().columns(self.foo, self.bar)
56
+
57
+ self.assertEqual('CREATE TEMPORARY TABLE "abc" ("a" INT,"b" VARCHAR(100))', str(q))
58
+
59
+ with self.subTest("with primary key"):
60
+ q = Query.create_table(self.new_table).columns(self.foo, self.bar).primary_key(self.foo, self.bar)
61
+
62
+ self.assertEqual('CREATE TABLE "abc" ("a" INT,"b" VARCHAR(100),PRIMARY KEY ("a","b"))', str(q))
63
+
64
+ with self.subTest("with simple foreign key"):
65
+ cref, dref = Columns(("c", "INT"), ("d", "VARCHAR(100)"))
66
+ q = (
67
+ Query.create_table(self.new_table)
68
+ .columns(self.foo, self.bar)
69
+ .foreign_key([self.foo, self.bar], self.existing_table, [cref, dref])
70
+ )
71
+
72
+ self.assertEqual(
73
+ 'CREATE TABLE "abc" ('
74
+ '"a" INT,'
75
+ '"b" VARCHAR(100),'
76
+ 'FOREIGN KEY ("a","b") REFERENCES "efg" ("c","d"))',
77
+ str(q),
78
+ )
79
+
80
+ with self.subTest("with foreign key reference options"):
81
+ cref, dref = Columns(("c", "INT"), ("d", "VARCHAR(100)"))
82
+ q = (
83
+ Query.create_table(self.new_table)
84
+ .columns(self.foo, self.bar)
85
+ .foreign_key(
86
+ [self.foo, self.bar],
87
+ self.existing_table,
88
+ [cref, dref],
89
+ on_delete=ReferenceOption.cascade,
90
+ on_update=ReferenceOption.restrict,
91
+ )
92
+ )
93
+
94
+ self.assertEqual(
95
+ 'CREATE TABLE "abc" ('
96
+ '"a" INT,'
97
+ '"b" VARCHAR(100),'
98
+ 'FOREIGN KEY ("a","b") REFERENCES "efg" ("c","d") ON DELETE CASCADE ON UPDATE RESTRICT)',
99
+ str(q),
100
+ )
101
+
102
+ with self.subTest("with unique keys"):
103
+ q = (
104
+ Query.create_table(self.new_table)
105
+ .columns(self.foo, self.bar)
106
+ .unique(self.foo, self.bar)
107
+ .unique(self.foo)
108
+ )
109
+
110
+ self.assertEqual('CREATE TABLE "abc" ("a" INT,"b" VARCHAR(100),UNIQUE ("a","b"),UNIQUE ("a"))', str(q))
111
+
112
+ with self.subTest("with system versioning"):
113
+ q = Query.create_table(self.new_table).columns(self.foo, self.bar).with_system_versioning()
114
+
115
+ self.assertEqual('CREATE TABLE "abc" ("a" INT,"b" VARCHAR(100)) WITH SYSTEM VERSIONING', str(q))
116
+
117
+ with self.subTest("with unlogged keyword"):
118
+ q = Query.create_table(self.new_table).unlogged().columns(self.foo, self.bar)
119
+
120
+ self.assertEqual('CREATE UNLOGGED TABLE "abc" ("a" INT,"b" VARCHAR(100))', str(q))
121
+
122
+ with self.subTest("with if not exists keyword"):
123
+ q = Query.create_table(self.new_table).if_not_exists().columns(self.foo, self.bar)
124
+
125
+ self.assertEqual('CREATE TABLE IF NOT EXISTS "abc" ("a" INT,"b" VARCHAR(100))', str(q))
126
+
127
+ def test_create_table_with_select(self):
128
+ select = Query.from_(self.existing_table).select(self.existing_table.foo, self.existing_table.bar)
129
+
130
+ with self.subTest("without temporary keyword"):
131
+ q = Query.create_table(self.new_table).as_select(select)
132
+
133
+ self.assertEqual('CREATE TABLE "abc" AS (SELECT "foo","bar" FROM "efg")', str(q))
134
+
135
+ with self.subTest("with temporary keyword"):
136
+ q = Query.create_table(self.new_table).temporary().as_select(select)
137
+
138
+ self.assertEqual(
139
+ 'CREATE TEMPORARY TABLE "abc" AS (SELECT "foo","bar" FROM "efg")',
140
+ str(q),
141
+ )
142
+
143
+ def test_create_table_without_columns_or_select_empty(self):
144
+ q = Query.create_table(self.new_table)
145
+
146
+ self.assertEqual("", str(q))
147
+
148
+ def test_create_table_with_select_and_columns_fails(self):
149
+ select = Query.from_(self.existing_table).select(self.existing_table.foo, self.existing_table.bar)
150
+
151
+ with self.subTest("for columns before as_select"):
152
+ with self.assertRaises(AttributeError):
153
+ Query.create_table(self.new_table).columns(self.foo, self.bar).as_select(select)
154
+
155
+ with self.subTest("for as_select before columns"):
156
+ with self.assertRaises(AttributeError):
157
+ Query.create_table(self.new_table).as_select(select).columns(self.foo, self.bar)
158
+
159
+ with self.subTest("repeated foreign key"):
160
+ with self.assertRaises(AttributeError):
161
+ Query.create_table(self.new_table).foreign_key([self.foo], self.existing_table, [self.bar]).foreign_key(
162
+ [self.foo], self.existing_table, [self.bar]
163
+ )
164
+
165
+ def test_create_table_as_select_not_query_raises_error(self):
166
+ with self.assertRaises(TypeError):
167
+ Query.create_table(self.new_table).as_select("abc")
168
+
169
+
170
+ class CreateIndexTests(unittest.TestCase):
171
+ new_index = Index("abc")
172
+ new_table = Table("my_table")
173
+ columns = (new_table.a, new_table.b)
174
+ where = new_table.a > 1
175
+
176
+ def test_create_index_simple(self) -> None:
177
+ q = Query.create_index(self.new_index).on(self.new_table).columns(*self.columns)
178
+ self.assertEqual(
179
+ f'CREATE INDEX "{self.new_index.name}" ON "{self.new_table.get_table_name()}"({", ".join([c.name for c in self.columns])})',
180
+ q.get_sql(),
181
+ )
182
+
183
+ def test_create_index_where(self) -> None:
184
+ q = Query.create_index(self.new_index).on(self.new_table).columns(*self.columns).where(self.where)
185
+ self.assertEqual(
186
+ f'CREATE INDEX "{self.new_index.name}" ON "{self.new_table.get_table_name()}"({", ".join([c.name for c in self.columns])}) WHERE {self.where.get_sql()}',
187
+ q.get_sql(),
188
+ )
189
+
190
+ def test_create_index_unique(self) -> None:
191
+ q = Query.create_index(self.new_index).on(self.new_table).columns(*self.columns).unique()
192
+ self.assertEqual(
193
+ f'CREATE UNIQUE INDEX "{self.new_index.name}" ON "{self.new_table.get_table_name()}"({", ".join([c.name for c in self.columns])})',
194
+ q.get_sql(),
195
+ )
196
+
197
+ def test_create_index_if_not_exists(self) -> None:
198
+ q = Query.create_index(self.new_index).on(self.new_table).columns(*self.columns).if_not_exists()
199
+ self.assertEqual(
200
+ f'CREATE INDEX IF NOT EXISTS "{self.new_index.name}" ON "{self.new_table.get_table_name()}"({", ".join([c.name for c in self.columns])})',
201
+ q.get_sql(),
202
+ )
203
+
204
+ def test_create_index_without_columns_raises_error(self) -> None:
205
+ with self.assertRaises(AttributeError):
206
+ Query.create_index(self.new_index).on(self.new_table).get_sql()
207
+
208
+ def test_create_index_without_table_raises_error(self) -> None:
209
+ with self.assertRaises(AttributeError):
210
+ Query.create_index(self.new_index).columns(*self.columns).get_sql()
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_criterions.py ADDED
@@ -0,0 +1,873 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+ from datetime import (
3
+ date,
4
+ datetime,
5
+ )
6
+
7
+ from pypika import (
8
+ Criterion,
9
+ EmptyCriterion,
10
+ Field,
11
+ Table,
12
+ functions as fn,
13
+ )
14
+ from pypika.queries import QueryBuilder
15
+ from pypika.terms import ExistsCriterion, Mod
16
+
17
+ __author__ = "Timothy Heys"
18
+ __email__ = "theys@kayak.com"
19
+
20
+
21
+ class CriterionTests(unittest.TestCase):
22
+ t = Table("test", alias="crit")
23
+
24
+ def test__criterion_with_alias(self):
25
+ c1 = (Field("foo") == Field("bar")).as_("criterion")
26
+
27
+ self.assertEqual('"foo"="bar"', str(c1))
28
+ self.assertEqual('"foo"="bar" "criterion"', c1.get_sql(with_alias=True, quote_char='"', alias_quote_char='"'))
29
+
30
+ def test__criterion_eq_number(self):
31
+ c1 = Field("foo") == 1
32
+ c2 = Field("foo", table=self.t).eq(0)
33
+ c3 = Field("foo", table=self.t) == -1
34
+
35
+ self.assertEqual('"foo"=1', str(c1))
36
+ self.assertEqual('"crit"."foo"=0', str(c2))
37
+ self.assertEqual('"crit"."foo"=-1', str(c3))
38
+
39
+ def test__criterion_eq_decimal(self):
40
+ c1 = Field("foo") == 1.0
41
+ c2 = Field("foo", table=self.t).eq(0.5)
42
+
43
+ self.assertEqual('"foo"=1.0', str(c1))
44
+ self.assertEqual('"crit"."foo"=0.5', str(c2))
45
+
46
+ def test__criterion_eq_bool(self):
47
+ c1 = Field("foo") == True
48
+ c2 = Field("foo", table=self.t).eq(True)
49
+ c3 = Field("foo") == False
50
+ c4 = Field("foo", table=self.t).eq(False)
51
+
52
+ self.assertEqual('"foo"=true', str(c1))
53
+ self.assertEqual('"crit"."foo"=true', str(c2))
54
+ self.assertEqual('"foo"=false', str(c3))
55
+ self.assertEqual('"crit"."foo"=false', str(c4))
56
+
57
+ def test__criterion_eq_str(self):
58
+ c1 = Field("foo") == "abc"
59
+ c2 = Field("foo", table=self.t).eq("abc")
60
+
61
+ self.assertEqual("\"foo\"='abc'", str(c1))
62
+ self.assertEqual('"crit"."foo"=\'abc\'', str(c2))
63
+
64
+ def test__criterion_eq_date(self):
65
+ c1 = Field("foo") == date(2000, 1, 1)
66
+ c2 = Field("foo", table=self.t).eq(date(2000, 1, 1))
67
+
68
+ self.assertEqual("\"foo\"='2000-01-01'", str(c1))
69
+ self.assertEqual('"crit"."foo"=\'2000-01-01\'', str(c2))
70
+
71
+ def test__criterion_eq_datetime(self):
72
+ c1 = Field("foo") == datetime(2000, 1, 1, 12, 30, 55)
73
+ c2 = Field("foo", table=self.t).eq(datetime(2000, 1, 1, 12, 30, 55))
74
+
75
+ self.assertEqual("\"foo\"='2000-01-01T12:30:55'", str(c1))
76
+ self.assertEqual('"crit"."foo"=\'2000-01-01T12:30:55\'', str(c2))
77
+
78
+ def test__criterion_eq_right(self):
79
+ c1 = 1 == Field("foo")
80
+ c2 = -1 == Field("foo", table=self.t)
81
+
82
+ self.assertEqual('"foo"=1', str(c1))
83
+ self.assertEqual('"crit"."foo"=-1', str(c2))
84
+
85
+ def test__criterion_is_null(self):
86
+ c1 = Field("foo").isnull()
87
+ c2 = Field("foo", table=self.t).isnull()
88
+
89
+ self.assertEqual('"foo" IS NULL', str(c1))
90
+ self.assertEqual('"crit"."foo" IS NULL', str(c2))
91
+
92
+ def test__criterion_is_null_with_alias(self):
93
+ c1 = Field("foo").isnull().as_('alias')
94
+ c2 = Field("foo", table=self.t).isnull().as_('alias')
95
+
96
+ self.assertEqual('"foo" IS NULL "alias"', str(c1))
97
+ self.assertEqual('"crit"."foo" IS NULL "alias"', str(c2))
98
+
99
+ def test__criterion_ne_number(self):
100
+ c1 = Field("foo") != 1
101
+ c2 = Field("foo", table=self.t).ne(0)
102
+ c3 = Field("foo") != -1
103
+
104
+ self.assertEqual('"foo"<>1', str(c1))
105
+ self.assertEqual('"crit"."foo"<>0', str(c2))
106
+ self.assertEqual('"foo"<>-1', str(c3))
107
+
108
+ def test__criterion_ne_str(self):
109
+ c1 = Field("foo") != "abc"
110
+ c2 = Field("foo", table=self.t).ne("abc")
111
+
112
+ self.assertEqual("\"foo\"<>'abc'", str(c1))
113
+ self.assertEqual('"crit"."foo"<>\'abc\'', str(c2))
114
+
115
+ def test__criterion_ne_date(self):
116
+ c1 = Field("foo") != date(2000, 1, 1)
117
+ c2 = Field("foo", table=self.t).ne(date(2000, 1, 1))
118
+
119
+ self.assertEqual("\"foo\"<>'2000-01-01'", str(c1))
120
+ self.assertEqual('"crit"."foo"<>\'2000-01-01\'', str(c2))
121
+
122
+ def test__criterion_ne_datetime(self):
123
+ c1 = Field("foo") != datetime(2000, 1, 1, 12, 30, 55)
124
+ c2 = Field("foo", table=self.t).ne(datetime(2000, 1, 1, 12, 30, 55))
125
+
126
+ self.assertEqual("\"foo\"<>'2000-01-01T12:30:55'", str(c1))
127
+ self.assertEqual('"crit"."foo"<>\'2000-01-01T12:30:55\'', str(c2))
128
+
129
+ def test__criterion_ne_right(self):
130
+ c1 = 1 != Field("foo")
131
+ c2 = -1 != Field("foo", table=self.t)
132
+
133
+ self.assertEqual('"foo"<>1', str(c1))
134
+ self.assertEqual('"crit"."foo"<>-1', str(c2))
135
+
136
+ def test__criterion_lt_number(self):
137
+ c1 = Field("foo") < 1
138
+ c2 = Field("foo", table=self.t).lt(0)
139
+ c3 = Field("foo") < -1
140
+
141
+ self.assertEqual('"foo"<1', str(c1))
142
+ self.assertEqual('"crit"."foo"<0', str(c2))
143
+ self.assertEqual('"foo"<-1', str(c3))
144
+
145
+ def test__criterion_lt_date(self):
146
+ c1 = Field("foo") < date(2000, 1, 1)
147
+ c2 = Field("foo", table=self.t).lt(date(2000, 1, 1))
148
+
149
+ self.assertEqual("\"foo\"<'2000-01-01'", str(c1))
150
+ self.assertEqual('"crit"."foo"<\'2000-01-01\'', str(c2))
151
+
152
+ def test__criterion_lt_datetime(self):
153
+ c1 = Field("foo") < datetime(2000, 1, 1, 12, 30, 55)
154
+ c2 = Field("foo", table=self.t).lt(datetime(2000, 1, 1, 12, 30, 55))
155
+
156
+ self.assertEqual("\"foo\"<'2000-01-01T12:30:55'", str(c1))
157
+ self.assertEqual('"crit"."foo"<\'2000-01-01T12:30:55\'', str(c2))
158
+
159
+ def test__criterion_lt_right(self):
160
+ c1 = 1 > Field("foo")
161
+ c2 = -1 > Field("foo", table=self.t)
162
+
163
+ self.assertEqual('"foo"<1', str(c1))
164
+ self.assertEqual('"crit"."foo"<-1', str(c2))
165
+
166
+ def test__criterion_gt_number(self):
167
+ c1 = Field("foo") > 1
168
+ c2 = Field("foo", table=self.t).gt(0)
169
+ c3 = Field("foo") > -1
170
+
171
+ self.assertEqual('"foo">1', str(c1))
172
+ self.assertEqual('"crit"."foo">0', str(c2))
173
+ self.assertEqual('"foo">-1', str(c3))
174
+
175
+ def test__criterion_gt_date(self):
176
+ c1 = Field("foo") > date(2000, 1, 1)
177
+ c2 = Field("foo", table=self.t).gt(date(2000, 1, 1))
178
+
179
+ self.assertEqual("\"foo\">'2000-01-01'", str(c1))
180
+ self.assertEqual('"crit"."foo">\'2000-01-01\'', str(c2))
181
+
182
+ def test__criterion_gt_datetime(self):
183
+ c1 = Field("foo") > datetime(2000, 1, 1, 12, 30, 55)
184
+ c2 = Field("foo", table=self.t).gt(datetime(2000, 1, 1, 12, 30, 55))
185
+
186
+ self.assertEqual("\"foo\">'2000-01-01T12:30:55'", str(c1))
187
+ self.assertEqual('"crit"."foo">\'2000-01-01T12:30:55\'', str(c2))
188
+
189
+ def test__criterion_gt_right(self):
190
+ c1 = 1 < Field("foo")
191
+ c2 = -1 < Field("foo", table=self.t)
192
+
193
+ self.assertEqual('"foo">1', str(c1))
194
+ self.assertEqual('"crit"."foo">-1', str(c2))
195
+
196
+ def test__criterion_lte_number(self):
197
+ c1 = Field("foo") <= 1
198
+ c2 = Field("foo", table=self.t).lte(0)
199
+ c3 = Field("foo") <= -1
200
+
201
+ self.assertEqual('"foo"<=1', str(c1))
202
+ self.assertEqual('"crit"."foo"<=0', str(c2))
203
+ self.assertEqual('"foo"<=-1', str(c3))
204
+
205
+ def test__criterion_lte_date(self):
206
+ c1 = Field("foo") <= date(2000, 1, 1)
207
+ c2 = Field("foo", table=self.t).lte(date(2000, 1, 1))
208
+
209
+ self.assertEqual("\"foo\"<='2000-01-01'", str(c1))
210
+ self.assertEqual('"crit"."foo"<=\'2000-01-01\'', str(c2))
211
+
212
+ def test__criterion_lte_datetime(self):
213
+ c1 = Field("foo") <= datetime(2000, 1, 1, 12, 30, 55)
214
+ c2 = Field("foo", table=self.t).lte(datetime(2000, 1, 1, 12, 30, 55))
215
+
216
+ self.assertEqual("\"foo\"<='2000-01-01T12:30:55'", str(c1))
217
+ self.assertEqual('"crit"."foo"<=\'2000-01-01T12:30:55\'', str(c2))
218
+
219
+ def test__criterion_lte_right(self):
220
+ c1 = 1 >= Field("foo")
221
+ c2 = -1 >= Field("foo", table=self.t)
222
+
223
+ self.assertEqual('"foo"<=1', str(c1))
224
+ self.assertEqual('"crit"."foo"<=-1', str(c2))
225
+
226
+ def test__criterion_gte_number(self):
227
+ c1 = Field("foo") >= 1
228
+ c2 = Field("foo", table=self.t).gte(0)
229
+ c3 = Field("foo") >= -1
230
+
231
+ self.assertEqual('"foo">=1', str(c1))
232
+ self.assertEqual('"crit"."foo">=0', str(c2))
233
+ self.assertEqual('"foo">=-1', str(c3))
234
+
235
+ def test__criterion_gte_date(self):
236
+ c1 = Field("foo") >= date(2000, 1, 1)
237
+ c2 = Field("foo", table=self.t).gte(date(2000, 1, 1))
238
+
239
+ self.assertEqual("\"foo\">='2000-01-01'", str(c1))
240
+ self.assertEqual('"crit"."foo">=\'2000-01-01\'', str(c2))
241
+
242
+ def test__criterion_gte_datetime(self):
243
+ c1 = Field("foo") >= datetime(2000, 1, 1, 12, 30, 55)
244
+ c2 = Field("foo", table=self.t).gte(datetime(2000, 1, 1, 12, 30, 55))
245
+
246
+ self.assertEqual("\"foo\">='2000-01-01T12:30:55'", str(c1))
247
+ self.assertEqual('"crit"."foo">=\'2000-01-01T12:30:55\'', str(c2))
248
+
249
+ def test__criterion_gte_right(self):
250
+ c1 = 1 <= Field("foo")
251
+ c2 = -1 <= Field("foo", table=self.t)
252
+
253
+ self.assertEqual('"foo">=1', str(c1))
254
+ self.assertEqual('"crit"."foo">=-1', str(c2))
255
+
256
+ def test__criterion_bitwise_and(self):
257
+ c1 = Field("foo").bitwiseand(2)
258
+ c2 = Field("foo", table=self.t).bitwiseand(10) == 2
259
+
260
+ self.assertEqual('("foo" & 2)', str(c1))
261
+ self.assertEqual('("crit"."foo" & 10)=2', str(c2))
262
+
263
+ def test__criterion_bitwise_and_with_alias(self):
264
+ c1 = Field("foo").bitwiseand(2).as_('alias')
265
+
266
+ self.assertEqual('("foo" & 2) "alias"', str(c1))
267
+
268
+ def test__bitwise_and_in_where_clause(self):
269
+ q = QueryBuilder().from_('items').select('abc').where(Field("foo").bitwiseand(1) == 1)
270
+
271
+ self.assertEqual('SELECT "abc" FROM "items" WHERE ("foo" & 1)=1', str(q))
272
+
273
+
274
+ class NotTests(unittest.TestCase):
275
+ table_abc, table_efg = Table("abc", alias="cx0"), Table("efg", alias="cx1")
276
+
277
+ def test_negate(self):
278
+ c1 = Field("foo") >= 1
279
+ c2 = c1.negate()
280
+
281
+ self.assertEqual('"foo">=1', str(c1))
282
+ self.assertEqual('NOT "foo">=1', str(c2))
283
+
284
+ def test_variable_access(self):
285
+ c1 = Field("foo").negate()
286
+
287
+ self.assertEqual(c1.is_aggregate, False)
288
+
289
+ def test_chained_function(self):
290
+ field1 = Field("foo").negate()
291
+ field2 = field1.eq("bar")
292
+
293
+ self.assertEqual('NOT "foo"', str(field1))
294
+ self.assertEqual("NOT \"foo\"='bar'", str(field2))
295
+ self.assertIsNot(field1, field2)
296
+
297
+ def test_not_null(self):
298
+ c1 = Field("foo").notnull()
299
+ c2 = Field("foo", table=self.table_abc).notnull()
300
+
301
+ self.assertEqual('NOT "foo" IS NULL', str(c1))
302
+ self.assertEqual('NOT "cx0"."foo" IS NULL', str(c2))
303
+
304
+ def test_is_not_null(self):
305
+ c1 = Field("foo").isnotnull()
306
+ c2 = Field("foo", table=self.table_abc).isnotnull()
307
+
308
+ self.assertEqual('"foo" IS NOT NULL', str(c1))
309
+ self.assertEqual('"cx0"."foo" IS NOT NULL', str(c2))
310
+
311
+ def test_not_null_with_alias(self):
312
+ c1 = Field("foo").notnull().as_("something")
313
+ c2 = Field("foo", table=self.table_abc).notnull().as_("something")
314
+
315
+ self.assertEqual('NOT "foo" IS NULL "something"', str(c1))
316
+ self.assertEqual('NOT "cx0"."foo" IS NULL "something"', str(c2))
317
+
318
+ def test_notnullcriterion_replace_table(self):
319
+ f = self.table_abc.foo.notnull().replace_table(self.table_abc, self.table_efg)
320
+
321
+ self.assertEqual('NOT "cx1"."foo" IS NULL', str(f))
322
+
323
+ def test_not_with_or_criterion(self):
324
+ self.assertEqual('NOT ("foo" OR "bar")', str(~(Field("foo") | Field("bar"))))
325
+
326
+ def test_not_with_and_criterion(self):
327
+ self.assertEqual('NOT ("foo" AND "bar")', str(~(Field("foo") & Field("bar"))))
328
+
329
+ def test_not_with_complex_criterion(self):
330
+ self.assertEqual(
331
+ 'NOT ("foo" AND "bar" AND "fizz" AND "buzz")',
332
+ str(~(Field("foo") & Field("bar") & Field("fizz") & Field("buzz"))),
333
+ )
334
+
335
+
336
+ class BetweenTests(unittest.TestCase):
337
+ t = Table("abc", alias="btw")
338
+
339
+ def test__between_number(self):
340
+ c1 = Field("foo").between(0, 1)
341
+ c2 = Field("foo", table=self.t).between(0, 1)
342
+ c3 = Field("foo")[0:1]
343
+
344
+ self.assertEqual('"foo" BETWEEN 0 AND 1', str(c1))
345
+ self.assertEqual('"btw"."foo" BETWEEN 0 AND 1', str(c2))
346
+ self.assertEqual('"foo" BETWEEN 0 AND 1', str(c3))
347
+
348
+ def test__between_with_alias(self):
349
+ c1 = Field("foo").between(0, 1).as_('alias')
350
+ c2 = Field("foo", table=self.t).between(0, 1).as_('alias')
351
+ c3 = Field("foo")[0:1].as_('alias')
352
+
353
+ self.assertEqual('"foo" BETWEEN 0 AND 1 "alias"', str(c1))
354
+ self.assertEqual('"btw"."foo" BETWEEN 0 AND 1 "alias"', str(c2))
355
+ self.assertEqual('"foo" BETWEEN 0 AND 1 "alias"', str(c3))
356
+
357
+ def test__between_date(self):
358
+ c1 = Field("foo").between(date(2000, 1, 1), date(2000, 12, 31))
359
+ c2 = Field("foo", table=self.t).between(date(2000, 1, 1), date(2000, 12, 31))
360
+ c3 = Field("foo")[date(2000, 1, 1) : date(2000, 12, 31)]
361
+
362
+ self.assertEqual("\"foo\" BETWEEN '2000-01-01' AND '2000-12-31'", str(c1))
363
+ self.assertEqual("\"btw\".\"foo\" BETWEEN '2000-01-01' AND '2000-12-31'", str(c2))
364
+ self.assertEqual("\"foo\" BETWEEN '2000-01-01' AND '2000-12-31'", str(c3))
365
+
366
+ def test__between_datetime(self):
367
+ c1 = Field("foo").between(datetime(2000, 1, 1, 0, 0, 0), datetime(2000, 12, 31, 23, 59, 59))
368
+ c2 = Field("foo", table=self.t).between(datetime(2000, 1, 1, 0, 0, 0), datetime(2000, 12, 31, 23, 59, 59))
369
+ c3 = Field("foo")[datetime(2000, 1, 1, 0, 0, 0) : datetime(2000, 12, 31, 23, 59, 59)]
370
+
371
+ self.assertEqual("\"foo\" BETWEEN '2000-01-01T00:00:00' AND '2000-12-31T23:59:59'", str(c1))
372
+ self.assertEqual(
373
+ "\"btw\".\"foo\" BETWEEN '2000-01-01T00:00:00' AND '2000-12-31T23:59:59'",
374
+ str(c2),
375
+ )
376
+ self.assertEqual("\"foo\" BETWEEN '2000-01-01T00:00:00' AND '2000-12-31T23:59:59'", str(c3))
377
+
378
+ def test__function_between(self):
379
+ c1 = fn.Coalesce(Field("foo"), 0)[0:1]
380
+ c2 = fn.Coalesce(Field("foo", table=self.t), 0)[0:1]
381
+
382
+ self.assertEqual('COALESCE("foo",0) BETWEEN 0 AND 1', str(c1))
383
+ self.assertEqual('COALESCE("btw"."foo",0) BETWEEN 0 AND 1', str(c2))
384
+
385
+ def test_get_item_only_works_with_slice(self):
386
+ with self.assertRaises(TypeError):
387
+ Field("foo")[0]
388
+
389
+ with self.assertRaises(TypeError):
390
+ Field("foo")[date(2000, 1, 1)]
391
+
392
+ with self.assertRaises(TypeError):
393
+ Field("foo")[datetime(2000, 1, 1, 0, 0, 0)]
394
+
395
+
396
+ class IsInTests(unittest.TestCase):
397
+ t = Table("abc", alias="isin")
398
+
399
+ def test__in_number(self):
400
+ c1 = Field("foo").isin([0, 1])
401
+ c2 = Field("foo", table=self.t).isin([0, 1])
402
+
403
+ self.assertEqual('"foo" IN (0,1)', str(c1))
404
+ self.assertEqual('"isin"."foo" IN (0,1)', str(c2))
405
+
406
+ def test__in_number_with_alias(self):
407
+ c1 = Field("foo").isin([0, 1]).as_('alias')
408
+ c2 = Field("foo", table=self.t).isin([0, 1]).as_('alias')
409
+
410
+ self.assertEqual('"foo" IN (0,1) "alias"', str(c1))
411
+ self.assertEqual('"isin"."foo" IN (0,1) "alias"', str(c2))
412
+
413
+ def test__in_character(self):
414
+ c1 = Field("foo").isin(["a", "b"])
415
+ c2 = Field("foo", table=self.t).isin(["a", "b"])
416
+
417
+ self.assertEqual("\"foo\" IN ('a','b')", str(c1))
418
+ self.assertEqual("\"isin\".\"foo\" IN ('a','b')", str(c2))
419
+
420
+ def test__in_date(self):
421
+ c1 = Field("foo").isin([date(2000, 1, 1), date(2000, 12, 31)])
422
+ c2 = Field("foo", table=self.t).isin([date(2000, 1, 1), date(2000, 12, 31)])
423
+
424
+ self.assertEqual("\"foo\" IN ('2000-01-01','2000-12-31')", str(c1))
425
+ self.assertEqual("\"isin\".\"foo\" IN ('2000-01-01','2000-12-31')", str(c2))
426
+
427
+ def test__in_datetime(self):
428
+ c1 = Field("foo").isin([datetime(2000, 1, 1, 0, 0, 0), datetime(2000, 12, 31, 23, 59, 59)])
429
+ c2 = Field("foo", table=self.t).isin([datetime(2000, 1, 1, 0, 0, 0), datetime(2000, 12, 31, 23, 59, 59)])
430
+
431
+ self.assertEqual("\"foo\" IN ('2000-01-01T00:00:00','2000-12-31T23:59:59')", str(c1))
432
+ self.assertEqual("\"isin\".\"foo\" IN ('2000-01-01T00:00:00','2000-12-31T23:59:59')", str(c2))
433
+
434
+ def test__function_isin(self):
435
+ c1 = fn.Coalesce(Field("foo"), 0).isin([0, 1])
436
+ c2 = fn.Coalesce(Field("foo", table=self.t), 0).isin([0, 1])
437
+
438
+ self.assertEqual('COALESCE("foo",0) IN (0,1)', str(c1))
439
+ self.assertEqual('COALESCE("isin"."foo",0) IN (0,1)', str(c2))
440
+
441
+ for t in (list, tuple, set, frozenset):
442
+ c_type = fn.Coalesce(Field("foo"), 0).isin(t([0, 1]))
443
+ self.assertEqual('COALESCE("foo",0) IN (0,1)', str(c_type))
444
+
445
+ self.assertRaises(AttributeError, lambda: str(fn.Coalesce(Field("foo"), 0).isin('SHOULD_FAIL')))
446
+
447
+ def test__in_unicode(self):
448
+ c1 = Field("foo").isin([u"a", u"b"])
449
+ c2 = Field("foo", table=self.t).isin([u"a", u"b"])
450
+
451
+ self.assertEqual("\"foo\" IN ('a','b')", str(c1))
452
+ self.assertEqual("\"isin\".\"foo\" IN ('a','b')", str(c2))
453
+
454
+
455
+ class ArithmeticExpressionTests(unittest.TestCase):
456
+ def test__lshift(self):
457
+ c1 = Field("foo").lshift("1")
458
+ c2 = Field("foo").lshift("2")
459
+
460
+ self.assertEqual("\"foo\"<<'1'", str(c1))
461
+ self.assertEqual("\"foo\"<<'2'", str(c2))
462
+
463
+ def test__rshift(self):
464
+ c1 = Field("foo").rshift("1")
465
+ c2 = Field("foo").rshift("2")
466
+
467
+ self.assertEqual("\"foo\">>'1'", str(c1))
468
+ self.assertEqual("\"foo\">>'2'", str(c2))
469
+
470
+
471
+ class NotInTests(unittest.TestCase):
472
+ t = Table("abc", alias="notin")
473
+
474
+ def test__notin_number(self):
475
+ c1 = Field("foo").notin([0, 1])
476
+ c2 = Field("foo", table=self.t).notin([0, 1])
477
+
478
+ self.assertEqual('"foo" NOT IN (0,1)', str(c1))
479
+ self.assertEqual('"notin"."foo" NOT IN (0,1)', str(c2))
480
+
481
+ def test__notin_number_with_alias(self):
482
+ c1 = Field("foo").notin([0, 1]).as_('alias')
483
+ c2 = Field("foo", table=self.t).notin([0, 1]).as_('alias')
484
+
485
+ self.assertEqual('"foo" NOT IN (0,1) "alias"', str(c1))
486
+ self.assertEqual('"notin"."foo" NOT IN (0,1) "alias"', str(c2))
487
+
488
+ def test__notin_character(self):
489
+ c1 = Field("foo").notin(["a", "b"])
490
+ c2 = Field("foo", table=self.t).notin(["a", "b"])
491
+
492
+ self.assertEqual("\"foo\" NOT IN ('a','b')", str(c1))
493
+ self.assertEqual("\"notin\".\"foo\" NOT IN ('a','b')", str(c2))
494
+
495
+ def test__notin_date(self):
496
+ c1 = Field("foo").notin([date(2000, 1, 1), date(2000, 12, 31)])
497
+ c2 = Field("foo", table=self.t).notin([date(2000, 1, 1), date(2000, 12, 31)])
498
+
499
+ self.assertEqual("\"foo\" NOT IN ('2000-01-01','2000-12-31')", str(c1))
500
+ self.assertEqual("\"notin\".\"foo\" NOT IN ('2000-01-01','2000-12-31')", str(c2))
501
+
502
+ def test__notin_datetime(self):
503
+ c1 = Field("foo").notin([datetime(2000, 1, 1, 0, 0, 0), datetime(2000, 12, 31, 23, 59, 59)])
504
+ c2 = Field("foo", table=self.t).notin([datetime(2000, 1, 1, 0, 0, 0), datetime(2000, 12, 31, 23, 59, 59)])
505
+
506
+ self.assertEqual("\"foo\" NOT IN ('2000-01-01T00:00:00','2000-12-31T23:59:59')", str(c1))
507
+ self.assertEqual(
508
+ "\"notin\".\"foo\" NOT IN ('2000-01-01T00:00:00','2000-12-31T23:59:59')",
509
+ str(c2),
510
+ )
511
+
512
+ def test__function_notin(self):
513
+ c1 = fn.Coalesce(Field("foo"), 0).notin([0, 1])
514
+ c2 = fn.Coalesce(Field("foo", table=self.t), 0).notin([0, 1])
515
+
516
+ self.assertEqual('COALESCE("foo",0) NOT IN (0,1)', str(c1))
517
+ self.assertEqual('COALESCE("notin"."foo",0) NOT IN (0,1)', str(c2))
518
+
519
+
520
+ class LikeTests(unittest.TestCase):
521
+ t = Table("abc", alias="like")
522
+
523
+ def test_like_starts_with(self):
524
+ c1 = Field("foo").like("ab%")
525
+ c2 = Field("foo", table=self.t).like("ab%")
526
+
527
+ self.assertEqual("\"foo\" LIKE 'ab%'", str(c1))
528
+ self.assertEqual('"like"."foo" LIKE \'ab%\'', str(c2))
529
+
530
+ def test_like_contains(self):
531
+ c1 = Field("foo").like("%ab%")
532
+ c2 = Field("foo", table=self.t).like("%ab%")
533
+
534
+ self.assertEqual("\"foo\" LIKE '%ab%'", str(c1))
535
+ self.assertEqual('"like"."foo" LIKE \'%ab%\'', str(c2))
536
+
537
+ def test_like_ends_with(self):
538
+ c1 = Field("foo").like("%ab")
539
+ c2 = Field("foo", table=self.t).like("%ab")
540
+
541
+ self.assertEqual("\"foo\" LIKE '%ab'", str(c1))
542
+ self.assertEqual('"like"."foo" LIKE \'%ab\'', str(c2))
543
+
544
+ def test_like_n_chars_long(self):
545
+ c1 = Field("foo").like("___")
546
+ c2 = Field("foo", table=self.t).like("___")
547
+
548
+ self.assertEqual("\"foo\" LIKE '___'", str(c1))
549
+ self.assertEqual('"like"."foo" LIKE \'___\'', str(c2))
550
+
551
+ def test_like_single_chars_and_various_chars(self):
552
+ c1 = Field("foo").like("a_b%c")
553
+ c2 = Field("foo", table=self.t).like("a_b%c")
554
+
555
+ self.assertEqual("\"foo\" LIKE 'a_b%c'", str(c1))
556
+ self.assertEqual('"like"."foo" LIKE \'a_b%c\'', str(c2))
557
+
558
+ def test_not_like_single_chars_and_various_chars(self):
559
+ c1 = Field("foo").not_like("a_b%c")
560
+ c2 = Field("foo", table=self.t).not_like("a_b%c")
561
+
562
+ self.assertEqual("\"foo\" NOT LIKE 'a_b%c'", str(c1))
563
+ self.assertEqual('"like"."foo" NOT LIKE \'a_b%c\'', str(c2))
564
+
565
+ def test_ilike_starts_with(self):
566
+ c1 = Field("foo").ilike("ab%")
567
+ c2 = Field("foo", table=self.t).ilike("ab%")
568
+
569
+ self.assertEqual("\"foo\" ILIKE 'ab%'", str(c1))
570
+ self.assertEqual('"like"."foo" ILIKE \'ab%\'', str(c2))
571
+
572
+ def test_ilike_contains(self):
573
+ c1 = Field("foo").ilike("%ab%")
574
+ c2 = Field("foo", table=self.t).ilike("%ab%")
575
+
576
+ self.assertEqual("\"foo\" ILIKE '%ab%'", str(c1))
577
+ self.assertEqual('"like"."foo" ILIKE \'%ab%\'', str(c2))
578
+
579
+ def test_ilike_ends_with(self):
580
+ c1 = Field("foo").ilike("%ab")
581
+ c2 = Field("foo", table=self.t).ilike("%ab")
582
+
583
+ self.assertEqual("\"foo\" ILIKE '%ab'", str(c1))
584
+ self.assertEqual('"like"."foo" ILIKE \'%ab\'', str(c2))
585
+
586
+ def test_ilike_n_chars_long(self):
587
+ c1 = Field("foo").ilike("___")
588
+ c2 = Field("foo", table=self.t).ilike("___")
589
+
590
+ self.assertEqual("\"foo\" ILIKE '___'", str(c1))
591
+ self.assertEqual('"like"."foo" ILIKE \'___\'', str(c2))
592
+
593
+ def test_ilike_single_chars_and_various_chars(self):
594
+ c1 = Field("foo").ilike("a_b%c")
595
+ c2 = Field("foo", table=self.t).ilike("a_b%c")
596
+
597
+ self.assertEqual("\"foo\" ILIKE 'a_b%c'", str(c1))
598
+ self.assertEqual('"like"."foo" ILIKE \'a_b%c\'', str(c2))
599
+
600
+ def test_not_ilike_single_chars_and_various_chars(self):
601
+ c1 = Field("foo").not_ilike("a_b%c")
602
+ c2 = Field("foo", table=self.t).not_ilike("a_b%c")
603
+
604
+ self.assertEqual("\"foo\" NOT ILIKE 'a_b%c'", str(c1))
605
+ self.assertEqual('"like"."foo" NOT ILIKE \'a_b%c\'', str(c2))
606
+
607
+ def test_rlike_escape_chars(self):
608
+ c1 = Field("foo").rlike("\\\\d+$")
609
+ c2 = Field("foo", table=self.t).rlike("\\\\d+$")
610
+
611
+ self.assertEqual("\"foo\" RLIKE '\\\\d+$'", str(c1))
612
+ self.assertEqual('"like"."foo" RLIKE \'\\\\d+$\'', str(c2))
613
+
614
+ def test_glob_single_chars_and_various_chars(self):
615
+ c1 = Field("foo").glob("a_b*")
616
+ c2 = Field("foo", table=self.t).glob("a_b*")
617
+
618
+ self.assertEqual("\"foo\" GLOB 'a_b*'", str(c1))
619
+ self.assertEqual('"like"."foo" GLOB \'a_b*\'', str(c2))
620
+
621
+
622
+ class ExistsCriterionTests(unittest.TestCase):
623
+ t2 = Table("abc", alias="t2")
624
+ q2 = QueryBuilder().from_(t2).select(t2.field2)
625
+
626
+ def test_exists(self):
627
+ t1 = Table("def", alias="t1")
628
+ q1 = QueryBuilder().from_(t1).where(ExistsCriterion(self.q2)).select(t1.field1)
629
+
630
+ self.assertEqual(
631
+ 'SELECT "t1"."field1" FROM "def" "t1" WHERE EXISTS (SELECT "t2"."field2" FROM "abc" "t2")', str(q1)
632
+ )
633
+
634
+ def test_not_exists(self):
635
+ t1 = Table("def", alias="t1")
636
+ q1 = QueryBuilder().from_(t1).where(ExistsCriterion(self.q2).negate()).select(t1.field1)
637
+
638
+ self.assertEqual(
639
+ 'SELECT "t1"."field1" FROM "def" "t1" WHERE NOT EXISTS (SELECT "t2"."field2" FROM "abc" "t2")', str(q1)
640
+ )
641
+
642
+
643
+ class ComplexCriterionTests(unittest.TestCase):
644
+ table_abc, table_efg = Table("abc", alias="cx0"), Table("efg", alias="cx1")
645
+
646
+ def test_and(self):
647
+ c1 = (Field("foo") == 1) & (Field("bar") == 2)
648
+ c2 = (Field("foo", table=self.table_abc) == 1) & (Field("bar", table=self.table_efg) == 2)
649
+
650
+ self.assertEqual('"foo"=1 AND "bar"=2', str(c1))
651
+ self.assertEqual('"cx0"."foo"=1 AND "cx1"."bar"=2', str(c2))
652
+
653
+ def test_or(self):
654
+ c1 = (Field("foo") == 1) | (Field("bar") == 2)
655
+ c2 = (Field("foo", table=self.table_abc) == 1) | (Field("bar", table=self.table_efg) == 2)
656
+
657
+ self.assertEqual('"foo"=1 OR "bar"=2', str(c1))
658
+ self.assertEqual('"cx0"."foo"=1 OR "cx1"."bar"=2', str(c2))
659
+
660
+ def test_xor(self):
661
+ c1 = (Field("foo") == 1) ^ (Field("bar") == 2)
662
+ c2 = (Field("foo", table=self.table_abc) == 1) ^ (Field("bar", table=self.table_efg) == 2)
663
+
664
+ self.assertEqual('"foo"=1 XOR "bar"=2', str(c1))
665
+ self.assertEqual('"cx0"."foo"=1 XOR "cx1"."bar"=2', str(c2))
666
+
667
+ def test_function_and(self):
668
+ c1 = fn.IsNull(Field("foo")) & (Field("bar") == 2)
669
+
670
+ self.assertEqual('ISNULL("foo") AND "bar"=2', str(c1))
671
+
672
+ def test_function_or(self):
673
+ c1 = fn.IsNull(Field("foo")) | (Field("bar") == 2)
674
+
675
+ self.assertEqual('ISNULL("foo") OR "bar"=2', str(c1))
676
+
677
+ def test_function_xor(self):
678
+ c1 = fn.IsNull(Field("foo")) ^ (Field("bar") == 2)
679
+
680
+ self.assertEqual('ISNULL("foo") XOR "bar"=2', str(c1))
681
+
682
+ def test__nested__and(self):
683
+ c = (Field("foo") == 1) & (Field("bar") == 2) & (Field("buz") == 3)
684
+
685
+ self.assertEqual('"foo"=1 AND "bar"=2 AND "buz"=3', str(c))
686
+
687
+ def test__nested__or(self):
688
+ c = (Field("foo") == 1) | (Field("bar") == 2) | (Field("buz") == 3)
689
+
690
+ self.assertEqual('"foo"=1 OR "bar"=2 OR "buz"=3', str(c))
691
+
692
+ def test__nested__mixed(self):
693
+ c = ((Field("foo") == 1) & (Field("bar") == 2)) | (Field("buz") == 3)
694
+
695
+ self.assertEqual('("foo"=1 AND "bar"=2) OR "buz"=3', str(c))
696
+
697
+ def test__between_and_isin(self):
698
+ c = Field("foo").isin([1, 2, 3]) & Field("bar").between(0, 1)
699
+
700
+ self.assertEqual('"foo" IN (1,2,3) AND "bar" BETWEEN 0 AND 1', str(c))
701
+
702
+ def test__between_and_field(self):
703
+ c1 = Field("foo").between(0, 1)
704
+ c2 = Field("bool_field")
705
+
706
+ self.assertEqual('"foo" BETWEEN 0 AND 1 AND "bool_field"', str(c1 & c2))
707
+ self.assertEqual('"bool_field" AND "foo" BETWEEN 0 AND 1', str(c2 & c1))
708
+
709
+
710
+ class FieldsAsCriterionTests(unittest.TestCase):
711
+ def test__field_and_field(self):
712
+ c1 = Field("a")
713
+ c2 = Field("b")
714
+
715
+ self.assertEqual('"a" AND "b"', str(c1 & c2))
716
+
717
+ def test__field_or_field(self):
718
+ c1 = Field("a")
719
+ c2 = Field("b")
720
+
721
+ self.assertEqual('"a" OR "b"', str(c1 | c2))
722
+
723
+ def test__field_xor_field(self):
724
+ c1 = Field("a")
725
+ c2 = Field("b")
726
+
727
+ self.assertEqual('"a" XOR "b"', str(c1 ^ c2))
728
+
729
+
730
+ class CriterionOperationsTests(unittest.TestCase):
731
+ table_abc, table_efg = Table("abc", alias="cx0"), Table("efg", alias="cx1")
732
+
733
+ def test_field_replace_table(self):
734
+ f = self.table_abc.foo.replace_table(self.table_abc, self.table_efg)
735
+
736
+ self.assertEqual('"cx1"."foo"', str(f))
737
+
738
+ def test_arithmeticfunction_replace_table(self):
739
+ f = (self.table_abc.foo + self.table_abc.bar).replace_table(self.table_abc, self.table_efg)
740
+
741
+ self.assertEqual('"cx1"."foo"+"cx1"."bar"', str(f))
742
+
743
+ def test_criterion_replace_table(self):
744
+ f = (self.table_abc.foo < self.table_abc.bar).replace_table(self.table_abc, self.table_efg)
745
+
746
+ self.assertEqual('"cx1"."foo"<"cx1"."bar"', str(f))
747
+
748
+ def test_complexcriterion_replace_table(self):
749
+ f = (self.table_abc.foo < self.table_abc.bar) & (self.table_abc.fiz > self.table_abc.buz)
750
+ f = f.replace_table(self.table_abc, self.table_efg)
751
+
752
+ self.assertEqual('"cx1"."foo"<"cx1"."bar" AND "cx1"."fiz">"cx1"."buz"', str(f))
753
+
754
+ def test_function_with_only_fields_replace_table(self):
755
+ f = fn.Sum(self.table_abc.foo).replace_table(self.table_abc, self.table_efg)
756
+
757
+ self.assertEqual('SUM("cx1"."foo")', str(f))
758
+
759
+ def test_function_with_values_and_fields_replace_table(self):
760
+ f = Mod(self.table_abc.foo, 2).replace_table(self.table_abc, self.table_efg)
761
+
762
+ self.assertEqual('MOD("cx1"."foo",2)', str(f))
763
+
764
+ def test_betweencriterion_replace_table(self):
765
+ f = self.table_abc.foo[0:1].replace_table(self.table_abc, self.table_efg)
766
+
767
+ self.assertEqual('"cx1"."foo" BETWEEN 0 AND 1', str(f))
768
+
769
+ def test_nullcriterion_replace_table(self):
770
+ f = self.table_abc.foo.isnull().replace_table(self.table_abc, self.table_efg)
771
+
772
+ self.assertEqual('"cx1"."foo" IS NULL', str(f))
773
+
774
+
775
+ class AnyTests(unittest.TestCase):
776
+ def test_zero_args_returns_empty_criterion(self):
777
+ crit = Criterion.any()
778
+ self.assertIsInstance(crit, EmptyCriterion)
779
+
780
+ def test_single_arg_returns_self(self):
781
+ f = Field("a")
782
+ crit = Criterion.any([f])
783
+ self.assertEqual(str(f), str(crit))
784
+
785
+ def test_multiple_args_returned_in_chain_of_ors(self):
786
+ crit = Criterion.any([Field("a"), Field("b"), Field("c"), Field("d")])
787
+ self.assertEqual(str(crit), '"a" OR "b" OR "c" OR "d"')
788
+
789
+ def test_with_generator(self):
790
+ crit = Criterion.any(Field(letter) for letter in "abcd")
791
+ self.assertEqual(str(crit), '"a" OR "b" OR "c" OR "d"')
792
+
793
+
794
+ class EmptyCriterionTests(unittest.TestCase):
795
+ def test_fields_(self):
796
+ empty_criterion = EmptyCriterion()
797
+
798
+ self.assertEqual(len(empty_criterion.fields_()), 0)
799
+
800
+ def test_empty_criterion_on_the_left(self):
801
+ t = Table("test1")
802
+ q = EmptyCriterion() & t.f1 == "v1"
803
+
804
+ self.assertEqual(
805
+ '"test1"."f1"=\'v1\'',
806
+ q.get_sql(with_namespace=True),
807
+ )
808
+
809
+ def test_empty_criterion_on_the_right(self):
810
+ t = Table("test1")
811
+ q = (t.f1 == "v1") & EmptyCriterion()
812
+
813
+ self.assertEqual(
814
+ '"test1"."f1"=\'v1\'',
815
+ q.get_sql(with_namespace=True),
816
+ )
817
+
818
+ def test_invertion_of_the_empty_criterion(self):
819
+ t = Table("test1")
820
+ q = (t.f1 == "v1") & ~EmptyCriterion()
821
+
822
+ self.assertEqual(
823
+ '"test1"."f1"=\'v1\'',
824
+ q.get_sql(with_namespace=True),
825
+ )
826
+
827
+ def test_different_operations_with_empty_criterion(self):
828
+ t = Table("test1")
829
+ q1 = (t.f1 == "v1") & EmptyCriterion()
830
+ q2 = (t.f1 == "v1") | EmptyCriterion()
831
+ q3 = (t.f1 == "v1") ^ EmptyCriterion()
832
+
833
+ expected_sql = '"test1"."f1"=\'v1\''
834
+ self.assertEqual(
835
+ expected_sql,
836
+ q1.get_sql(with_namespace=True),
837
+ )
838
+ self.assertEqual(
839
+ expected_sql,
840
+ q2.get_sql(with_namespace=True),
841
+ )
842
+ self.assertEqual(
843
+ expected_sql,
844
+ q3.get_sql(with_namespace=True),
845
+ )
846
+
847
+ def test_more_than_one_empty_criterions(self):
848
+ t = Table("test1")
849
+ q = EmptyCriterion() & ~(EmptyCriterion() | EmptyCriterion()) & (t.f1 == "v1") & ~EmptyCriterion()
850
+
851
+ self.assertEqual(
852
+ '"test1"."f1"=\'v1\'',
853
+ q.get_sql(with_namespace=True),
854
+ )
855
+
856
+
857
+ class AllTests(unittest.TestCase):
858
+ def test_zero_args_returns_empty_criterion(self):
859
+ crit = Criterion.all()
860
+ self.assertIsInstance(crit, EmptyCriterion)
861
+
862
+ def test_single_arg_returns_self(self):
863
+ f = Field("a")
864
+ crit = Criterion.all([f])
865
+ self.assertEqual(str(f), str(crit))
866
+
867
+ def test_multiple_args_returned_in_chain_of_ors(self):
868
+ crit = Criterion.all([Field("a"), Field("b"), Field("c"), Field("d")])
869
+ self.assertEqual(str(crit), '"a" AND "b" AND "c" AND "d"')
870
+
871
+ def test_with_generator(self):
872
+ crit = Criterion.all(Field(letter) for letter in "abcd")
873
+ self.assertEqual(str(crit), '"a" AND "b" AND "c" AND "d"')
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_custom_functions.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from pypika import CustomFunction, FunctionException, Query, Table
4
+
5
+ __author__ = "Airton Zanon"
6
+ __email__ = "me@airton.dev"
7
+
8
+
9
+ class TestUnitCustomFunction(unittest.TestCase):
10
+ def test_should_fail_with_wrong_arguments(self):
11
+ DateDiff = CustomFunction("DATE_DIFF", ["interval", "start_date", "end_date"])
12
+
13
+ with self.assertRaises(FunctionException):
14
+ DateDiff("foo")
15
+
16
+ def test_should_return_function_with_arguments(self):
17
+ DateDiff = CustomFunction("DATE_DIFF", ["interval", "start_date", "end_date"])
18
+
19
+ self.assertEqual(
20
+ "DATE_DIFF('day','start_date','end_date')",
21
+ str(DateDiff("day", "start_date", "end_date")),
22
+ )
23
+
24
+ def test_should_return_function_with_no_arguments(self):
25
+ CurrentDate = CustomFunction("CURRENT_DATE")
26
+
27
+ self.assertEqual("CURRENT_DATE()", str(CurrentDate()))
28
+
29
+
30
+ class TestFunctionalCustomFunction(unittest.TestCase):
31
+ def test_should_use_custom_function_on_select(self):
32
+ service = Table("service")
33
+
34
+ DateDiff = CustomFunction("DATE_DIFF", ["interval", "start_date", "end_date"])
35
+
36
+ q = Query.from_(service).select(DateDiff("day", service.created_date, service.updated_date))
37
+
38
+ self.assertEqual(
39
+ 'SELECT DATE_DIFF(\'day\',"created_date","updated_date") FROM "service"',
40
+ str(q),
41
+ )
42
+
43
+ def test_should_fail_use_custom_function_on_select_with_wrong_arguments(self):
44
+ service = Table("service")
45
+
46
+ DateDiff = CustomFunction("DATE_DIFF", ["interval", "start_date", "end_date"])
47
+
48
+ with self.assertRaises(FunctionException):
49
+ Query.from_(service).select(DateDiff("day", service.created_date))
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_data_types.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+ import uuid
3
+
4
+ from pypika.terms import ValueWrapper
5
+
6
+
7
+ class StringTests(unittest.TestCase):
8
+ def test_inline_string_concatentation(self):
9
+ self.assertEqual("'it''s'", ValueWrapper("it's").get_sql())
10
+
11
+
12
+ class UuidTests(unittest.TestCase):
13
+ def test_uuid_string_generation(self):
14
+ id = uuid.uuid4()
15
+ self.assertEqual("'{}'".format(id), ValueWrapper(id).get_sql())
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_date_math.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from pypika import (
4
+ Field as F,
5
+ Interval,
6
+ )
7
+ from pypika.enums import Dialects
8
+
9
+ dt = F("dt")
10
+
11
+
12
+ class AddIntervalTests(unittest.TestCase):
13
+ def test_add_microsecond(self):
14
+ c = dt + Interval(microseconds=1)
15
+
16
+ self.assertEqual("\"dt\"+INTERVAL '1 MICROSECOND'", str(c))
17
+
18
+ def test_add_second(self):
19
+ c = dt + Interval(seconds=1)
20
+
21
+ self.assertEqual("\"dt\"+INTERVAL '1 SECOND'", str(c))
22
+
23
+ def test_add_minute(self):
24
+ c = dt + Interval(minutes=1)
25
+
26
+ self.assertEqual("\"dt\"+INTERVAL '1 MINUTE'", str(c))
27
+
28
+ def test_add_day(self):
29
+ c = dt + Interval(days=1)
30
+
31
+ self.assertEqual("\"dt\"+INTERVAL '1 DAY'", str(c))
32
+
33
+ def test_add_week(self):
34
+ c = dt + Interval(weeks=1)
35
+
36
+ self.assertEqual("\"dt\"+INTERVAL '1 WEEK'", str(c))
37
+
38
+ def test_add_month(self):
39
+ c = dt + Interval(months=1)
40
+
41
+ self.assertEqual("\"dt\"+INTERVAL '1 MONTH'", str(c))
42
+
43
+ def test_add_quarter(self):
44
+ c = dt + Interval(quarters=1)
45
+
46
+ self.assertEqual("\"dt\"+INTERVAL '1 QUARTER'", str(c))
47
+
48
+ def test_add_year(self):
49
+ c = dt + Interval(years=1)
50
+
51
+ self.assertEqual("\"dt\"+INTERVAL '1 YEAR'", str(c))
52
+
53
+ def test_add_default(self):
54
+ c = dt + Interval(days=0)
55
+
56
+ self.assertEqual("\"dt\"+INTERVAL '0 DAY'", str(c))
57
+
58
+
59
+ class AddIntervalMultipleUnitsTests(unittest.TestCase):
60
+ def test_add_second_microsecond(self):
61
+ c = dt + Interval(seconds=1, microseconds=1)
62
+
63
+ self.assertEqual("\"dt\"+INTERVAL '1.1 SECOND_MICROSECOND'", str(c))
64
+
65
+ def test_add_minute_microsecond(self):
66
+ c = dt + Interval(minutes=1, microseconds=1)
67
+
68
+ self.assertEqual("\"dt\"+INTERVAL '1:0.1 MINUTE_MICROSECOND'", str(c))
69
+
70
+ def test_add_minute_second(self):
71
+ c = dt + Interval(minutes=1, seconds=1)
72
+
73
+ self.assertEqual("\"dt\"+INTERVAL '1:1 MINUTE_SECOND'", str(c))
74
+
75
+ def test_add_hour_microsecond(self):
76
+ c = dt + Interval(hours=1, microseconds=1)
77
+
78
+ self.assertEqual("\"dt\"+INTERVAL '1:0:0.1 HOUR_MICROSECOND'", str(c))
79
+
80
+ def test_add_hour_second(self):
81
+ c = dt + Interval(hours=1, seconds=1)
82
+
83
+ self.assertEqual("\"dt\"+INTERVAL '1:0:1 HOUR_SECOND'", str(c))
84
+
85
+ def test_add_hour_minute(self):
86
+ c = dt + Interval(hours=1, minutes=1)
87
+
88
+ self.assertEqual("\"dt\"+INTERVAL '1:1 HOUR_MINUTE'", str(c))
89
+
90
+ def test_add_day_microsecond(self):
91
+ c = dt + Interval(days=1, microseconds=1)
92
+
93
+ self.assertEqual("\"dt\"+INTERVAL '1 0:0:0.1 DAY_MICROSECOND'", str(c))
94
+
95
+ def test_add_day_second(self):
96
+ c = dt + Interval(days=1, seconds=1)
97
+
98
+ self.assertEqual("\"dt\"+INTERVAL '1 0:0:1 DAY_SECOND'", str(c))
99
+
100
+ def test_add_day_minute(self):
101
+ c = dt + Interval(days=1, minutes=1)
102
+
103
+ self.assertEqual("\"dt\"+INTERVAL '1 0:1 DAY_MINUTE'", str(c))
104
+
105
+ def test_add_day_hour(self):
106
+ c = dt + Interval(days=1, hours=1)
107
+
108
+ self.assertEqual("\"dt\"+INTERVAL '1 1 DAY_HOUR'", str(c))
109
+
110
+ def test_add_year_month(self):
111
+ c = dt + Interval(years=1, months=1)
112
+
113
+ self.assertEqual("\"dt\"+INTERVAL '1-1 YEAR_MONTH'", str(c))
114
+
115
+ def test_add_value_right(self):
116
+ c = Interval(microseconds=1) - dt
117
+
118
+ self.assertEqual("INTERVAL '1 MICROSECOND'-\"dt\"", str(c))
119
+
120
+ def test_add_value_complex_expressions(self):
121
+ c = dt + Interval(quarters=1) + Interval(weeks=1)
122
+
123
+ self.assertEqual("\"dt\"+INTERVAL '1 QUARTER'+INTERVAL '1 WEEK'", str(c))
124
+
125
+
126
+ class DialectIntervalTests(unittest.TestCase):
127
+ def test_mysql_dialect_uses_single_quotes_around_expression_in_an_interval(self):
128
+ c = Interval(days=1).get_sql(dialect=Dialects.MYSQL)
129
+ self.assertEqual("INTERVAL '1' DAY", c)
130
+
131
+ def test_oracle_dialect_uses_single_quotes_around_expression_in_an_interval(self):
132
+ c = Interval(days=1).get_sql(dialect=Dialects.ORACLE)
133
+ self.assertEqual("INTERVAL '1' DAY", c)
134
+
135
+ def test_vertica_dialect_uses_single_quotes_around_interval(self):
136
+ c = Interval(days=1).get_sql(dialect=Dialects.VERTICA)
137
+ self.assertEqual("INTERVAL '1 DAY'", c)
138
+
139
+ def test_redshift_dialect_uses_single_quotes_around_interval(self):
140
+ c = Interval(days=1).get_sql(dialect=Dialects.REDSHIFT)
141
+ self.assertEqual("INTERVAL '1 DAY'", c)
142
+
143
+ def test_postgresql_dialect_uses_single_quotes_around_interval(self):
144
+ c = Interval(days=1).get_sql(dialect=Dialects.POSTGRESQL)
145
+ self.assertEqual("INTERVAL '1 DAY'", c)
146
+
147
+
148
+ class TestNegativeIntervals(unittest.TestCase):
149
+ def test_day(self):
150
+ c = Interval(days=-1).get_sql()
151
+ self.assertEqual("INTERVAL '-1 DAY'", c)
152
+
153
+ def test_week(self):
154
+ c = Interval(weeks=-1).get_sql()
155
+ self.assertEqual("INTERVAL '-1 WEEK'", c)
156
+
157
+ def test_month(self):
158
+ c = Interval(months=-1).get_sql()
159
+ self.assertEqual("INTERVAL '-1 MONTH'", c)
160
+
161
+ def test_year(self):
162
+ c = Interval(years=-1).get_sql()
163
+ self.assertEqual("INTERVAL '-1 YEAR'", c)
164
+
165
+ def test_year_month(self):
166
+ c = Interval(years=-1, months=-4).get_sql()
167
+ self.assertEqual("INTERVAL '-1-4 YEAR_MONTH'", c)
168
+
169
+
170
+ class TruncateTrailingZerosTests(unittest.TestCase):
171
+ def test_do_not_truncate_integer_values(self):
172
+ i = Interval(seconds=10)
173
+
174
+ self.assertEqual("INTERVAL '10 SECOND'", str(i))
175
+
176
+ def test_do_not_truncate_months_between_years_and_datys(self):
177
+ i = Interval(years=10, days=10)
178
+
179
+ self.assertEqual("INTERVAL '10-0-10 YEAR_DAY'", str(i))
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_deletes.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from pypika import PostgreSQLQuery, Query, SYSTEM_TIME, Table
4
+
5
+ __author__ = "Timothy Heys"
6
+ __email__ = "theys@kayak.com"
7
+
8
+ from pypika.terms import Star
9
+
10
+
11
+ class DeleteTests(unittest.TestCase):
12
+ @classmethod
13
+ def setUpClass(cls) -> None:
14
+ super().setUpClass()
15
+ cls.table_abc = Table("abc")
16
+
17
+ def test_omit_where(self):
18
+ q = Query.from_("abc").delete()
19
+
20
+ self.assertEqual('DELETE FROM "abc"', str(q))
21
+
22
+ def test_omit_where__table_schema(self):
23
+ q = Query.from_(Table("abc", "schema1")).delete()
24
+
25
+ self.assertEqual('DELETE FROM "schema1"."abc"', str(q))
26
+
27
+ def test_where_field_equals(self):
28
+ q1 = Query.from_(self.table_abc).where(self.table_abc.foo == self.table_abc.bar).delete()
29
+ q2 = Query.from_(self.table_abc).where(self.table_abc.foo.eq(self.table_abc.bar)).delete()
30
+
31
+ self.assertEqual('DELETE FROM "abc" WHERE "foo"="bar"', str(q1))
32
+ self.assertEqual('DELETE FROM "abc" WHERE "foo"="bar"', str(q2))
33
+
34
+ def test_for_portion(self):
35
+ with self.subTest("with system time"):
36
+ q = Query.from_(self.table_abc.for_portion(SYSTEM_TIME.from_to('2020-01-01', '2020-02-01'))).delete()
37
+
38
+ self.assertEqual(
39
+ 'DELETE FROM "abc" FOR PORTION OF SYSTEM_TIME FROM \'2020-01-01\' TO \'2020-02-01\'', str(q)
40
+ )
41
+
42
+ with self.subTest("with column"):
43
+ q = Query.from_(
44
+ self.table_abc.for_portion(self.table_abc.valid_period.from_to('2020-01-01', '2020-02-01'))
45
+ ).delete()
46
+
47
+ self.assertEqual(
48
+ 'DELETE FROM "abc" FOR PORTION OF "valid_period" FROM \'2020-01-01\' TO \'2020-02-01\'', str(q)
49
+ )
50
+
51
+
52
+ class PostgresDeleteTests(unittest.TestCase):
53
+ @classmethod
54
+ def setUpClass(cls) -> None:
55
+ super().setUpClass()
56
+ cls.table_abc = Table("abc")
57
+
58
+ def test_delete_returning(self):
59
+ q1 = (
60
+ PostgreSQLQuery.from_(self.table_abc)
61
+ .where(self.table_abc.foo == self.table_abc.bar)
62
+ .delete()
63
+ .returning(self.table_abc.id)
64
+ )
65
+
66
+ self.assertEqual('DELETE FROM "abc" WHERE "foo"="bar" RETURNING "id"', str(q1))
67
+
68
+ def test_delete_returning_str(self):
69
+ q1 = (
70
+ PostgreSQLQuery.from_(self.table_abc)
71
+ .where(self.table_abc.foo == self.table_abc.bar)
72
+ .delete()
73
+ .returning("id")
74
+ )
75
+
76
+ self.assertEqual('DELETE FROM "abc" WHERE "foo"="bar" RETURNING "id"', str(q1))
77
+
78
+ def test_delete_returning_star(self):
79
+ q1 = (
80
+ PostgreSQLQuery.from_(self.table_abc)
81
+ .where(self.table_abc.foo == self.table_abc.bar)
82
+ .delete()
83
+ .returning(Star())
84
+ )
85
+
86
+ self.assertEqual('DELETE FROM "abc" WHERE "foo"="bar" RETURNING *', str(q1))
87
+
88
+ def test_delete_using(self):
89
+ table_trash = Table('trash')
90
+ q1 = (
91
+ PostgreSQLQuery.from_(self.table_abc)
92
+ .using(table_trash)
93
+ .where(self.table_abc.id == table_trash.abc_id)
94
+ .delete()
95
+ )
96
+
97
+ self.assertEqual('DELETE FROM "abc" USING "trash" WHERE "abc"."id"="trash"."abc_id"', str(q1))
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_drop.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from pypika import (
4
+ Columns,
5
+ Database,
6
+ Query,
7
+ Tables,
8
+ )
9
+
10
+
11
+ class DropTableTests(unittest.TestCase):
12
+ database_xyz = Database("mydb")
13
+ new_table, existing_table = Tables("abc", "efg")
14
+ foo, bar = Columns(("a", "INT"), ("b", "VARCHAR(100)"))
15
+
16
+ def test_drop_database(self):
17
+ q1 = Query.drop_database(self.database_xyz)
18
+ q2 = Query.drop_database(self.database_xyz).if_exists()
19
+
20
+ self.assertEqual('DROP DATABASE "mydb"', str(q1))
21
+ self.assertEqual('DROP DATABASE IF EXISTS "mydb"', str(q2))
22
+
23
+ def test_drop_table(self):
24
+ q1 = Query.drop_table(self.new_table)
25
+ q2 = Query.drop_table(self.new_table).if_exists()
26
+
27
+ self.assertEqual('DROP TABLE "abc"', str(q1))
28
+ self.assertEqual('DROP TABLE IF EXISTS "abc"', str(q2))
29
+
30
+ def test_drop_user(self):
31
+ q1 = Query.drop_user("myuser")
32
+ q2 = Query.drop_user("myuser").if_exists()
33
+
34
+ self.assertEqual('DROP USER "myuser"', str(q1))
35
+ self.assertEqual('DROP USER IF EXISTS "myuser"', str(q2))
36
+
37
+ def test_drop_view(self):
38
+ q1 = Query.drop_view("myview")
39
+ q2 = Query.drop_view("myview").if_exists()
40
+
41
+ self.assertEqual('DROP VIEW "myview"', str(q1))
42
+ self.assertEqual('DROP VIEW IF EXISTS "myview"', str(q2))
43
+
44
+ def test_drop_index(self):
45
+ q1 = Query.drop_index("myindex")
46
+ q2 = Query.drop_index("myindex").if_exists()
47
+
48
+ self.assertEqual('DROP INDEX "myindex"', str(q1))
49
+ self.assertEqual('DROP INDEX IF EXISTS "myindex"', str(q2))
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_formats.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from pypika import Query, Tables, functions as fn
4
+
5
+
6
+ class QuoteTests(unittest.TestCase):
7
+ maxDiff = None
8
+
9
+ table_abc, table_efg = Tables("abc", "efg")
10
+
11
+ def setUp(self):
12
+ subquery1 = (
13
+ Query.from_(self.table_abc)
14
+ .select(
15
+ self.table_abc.foo,
16
+ fn.Sum(self.table_abc.fizz + self.table_abc.buzz).as_("fizzbuzz"),
17
+ )
18
+ .groupby(self.table_abc.foo)
19
+ )
20
+
21
+ subquery2 = Query.from_(self.table_efg).select(
22
+ self.table_efg.foo.as_("foo_two"),
23
+ self.table_efg.bar,
24
+ )
25
+
26
+ self.query = (
27
+ Query.from_(subquery1)
28
+ .select(subquery1.foo, subquery1.fizzbuzz)
29
+ .join(subquery2)
30
+ .on(subquery1.foo == subquery2.foo_two)
31
+ .select(subquery2.foo_two, subquery2.bar)
32
+ )
33
+
34
+ def test_replace_quote_char_in_complex_query(self):
35
+ self.assertEqual(
36
+ "SELECT "
37
+ "`sq0`.`foo`,`sq0`.`fizzbuzz`,"
38
+ "`sq1`.`foo_two`,`sq1`.`bar` "
39
+ "FROM ("
40
+ "SELECT "
41
+ "`foo`,SUM(`fizz`+`buzz`) `fizzbuzz` "
42
+ "FROM `abc` "
43
+ "GROUP BY `foo`"
44
+ ") `sq0` JOIN ("
45
+ "SELECT "
46
+ "`foo` `foo_two`,`bar` "
47
+ "FROM `efg`"
48
+ ") `sq1` ON `sq0`.`foo`=`sq1`.`foo_two`",
49
+ self.query.get_sql(quote_char="`"),
50
+ )
51
+
52
+ def test_no_quote_char_in_complex_query(self):
53
+ self.assertEqual(
54
+ "SELECT "
55
+ "sq0.foo,sq0.fizzbuzz,"
56
+ "sq1.foo_two,sq1.bar "
57
+ "FROM ("
58
+ "SELECT "
59
+ "foo,SUM(fizz+buzz) fizzbuzz "
60
+ "FROM abc "
61
+ "GROUP BY foo"
62
+ ") sq0 JOIN ("
63
+ "SELECT "
64
+ "foo foo_two,bar "
65
+ "FROM efg"
66
+ ") sq1 ON sq0.foo=sq1.foo_two",
67
+ self.query.get_sql(quote_char=None),
68
+ )
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_functions.py ADDED
@@ -0,0 +1,847 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from pypika import (
4
+ Case,
5
+ CaseException,
6
+ DatePart,
7
+ Field as F,
8
+ Query,
9
+ Query as Q,
10
+ Schema,
11
+ Table as T,
12
+ VerticaQuery,
13
+ functions as fn,
14
+ )
15
+ from pypika.enums import Dialects, SqlTypes
16
+
17
+ __author__ = "Timothy Heys"
18
+ __email__ = "theys@kayak.com"
19
+
20
+
21
+ class FunctionTests(unittest.TestCase):
22
+ def test_dialect_propagation(self):
23
+ func = fn.Function("func", ["a"], ["b"])
24
+ self.assertEqual("func(ARRAY['a'],ARRAY['b'])", func.get_sql(dialect=Dialects.POSTGRESQL))
25
+
26
+ def test_is_aggregate_None_for_non_aggregate_function_or_function_with_no_aggregate_functions(self):
27
+ self.assertIsNone(fn.Coalesce('a', 0).is_aggregate)
28
+ self.assertIsNone(fn.Coalesce(fn.NullIf('a', 0), 0).is_aggregate)
29
+
30
+ def test_is_aggregate_True_for_aggregate_function_or_function_with_aggregate_functions(self):
31
+ self.assertTrue(fn.Sum('a').is_aggregate)
32
+ self.assertTrue(fn.Coalesce(fn.Avg('a'), 0).is_aggregate)
33
+ self.assertTrue(fn.Coalesce(fn.NullIf(fn.Sum('a'), 0), 0).is_aggregate)
34
+
35
+
36
+ class SchemaTests(unittest.TestCase):
37
+ def test_schema_no_schema_in_sql_when_none_set(self):
38
+ func = fn.Function("my_proc", 1, 2, 3)
39
+
40
+ self.assertEqual("my_proc(1,2,3)", func.get_sql(quote_char='"'))
41
+
42
+ def test_schema_included_in_function_sql(self):
43
+ a = Schema("a")
44
+ func = fn.Function("my_proc", 1, 2, 3, schema=a)
45
+
46
+ self.assertEqual('"a".my_proc(1,2,3)', func.get_sql(quote_char='"'))
47
+
48
+
49
+ class ArithmeticTests(unittest.TestCase):
50
+ t = T("abc")
51
+
52
+ def test__addition__fields(self):
53
+ q1 = Q.from_("abc").select(F("a") + F("b"))
54
+ q2 = Q.from_(self.t).select(self.t.a + self.t.b)
55
+
56
+ self.assertEqual('SELECT "a"+"b" FROM "abc"', str(q1))
57
+ self.assertEqual('SELECT "a"+"b" FROM "abc"', str(q2))
58
+
59
+ def test__addition__number(self):
60
+ q1 = Q.from_("abc").select(F("a") + 1)
61
+ q2 = Q.from_(self.t).select(self.t.a + 1)
62
+
63
+ self.assertEqual('SELECT "a"+1 FROM "abc"', str(q1))
64
+ self.assertEqual('SELECT "a"+1 FROM "abc"', str(q2))
65
+
66
+ def test__addition__decimal(self):
67
+ q1 = Q.from_("abc").select(F("a") + 1.0)
68
+ q2 = Q.from_(self.t).select(self.t.a + 1.0)
69
+
70
+ self.assertEqual('SELECT "a"+1.0 FROM "abc"', str(q1))
71
+ self.assertEqual('SELECT "a"+1.0 FROM "abc"', str(q2))
72
+
73
+ def test__addition__right(self):
74
+ q1 = Q.from_("abc").select(1 + F("a"))
75
+ q2 = Q.from_(self.t).select(1 + self.t.a)
76
+
77
+ self.assertEqual('SELECT 1+"a" FROM "abc"', str(q1))
78
+ self.assertEqual('SELECT 1+"a" FROM "abc"', str(q2))
79
+
80
+ def test__subtraction__fields(self):
81
+ q1 = Q.from_("abc").select(F("a") - F("b"))
82
+ q2 = Q.from_(self.t).select(self.t.a - self.t.b)
83
+
84
+ self.assertEqual('SELECT "a"-"b" FROM "abc"', str(q1))
85
+ self.assertEqual('SELECT "a"-"b" FROM "abc"', str(q2))
86
+
87
+ def test__subtraction__number(self):
88
+ q1 = Q.from_("abc").select(F("a") - 1)
89
+ q2 = Q.from_(self.t).select(self.t.a - 1)
90
+
91
+ self.assertEqual('SELECT "a"-1 FROM "abc"', str(q1))
92
+ self.assertEqual('SELECT "a"-1 FROM "abc"', str(q2))
93
+
94
+ def test__subtraction__decimal(self):
95
+ q1 = Q.from_("abc").select(F("a") - 1.0)
96
+ q2 = Q.from_(self.t).select(self.t.a - 1.0)
97
+
98
+ self.assertEqual('SELECT "a"-1.0 FROM "abc"', str(q1))
99
+ self.assertEqual('SELECT "a"-1.0 FROM "abc"', str(q2))
100
+
101
+ def test__subtraction__right(self):
102
+ q1 = Q.from_("abc").select(1 - F("a"))
103
+ q2 = Q.from_(self.t).select(1 - self.t.a)
104
+
105
+ self.assertEqual('SELECT 1-"a" FROM "abc"', str(q1))
106
+ self.assertEqual('SELECT 1-"a" FROM "abc"', str(q2))
107
+
108
+ def test__multiplication__fields(self):
109
+ q1 = Q.from_("abc").select(F("a") * F("b"))
110
+ q2 = Q.from_(self.t).select(self.t.a * self.t.b)
111
+
112
+ self.assertEqual('SELECT "a"*"b" FROM "abc"', str(q1))
113
+ self.assertEqual('SELECT "a"*"b" FROM "abc"', str(q2))
114
+
115
+ def test__multiplication__number(self):
116
+ q1 = Q.from_("abc").select(F("a") * 1)
117
+ q2 = Q.from_(self.t).select(self.t.a * 1)
118
+
119
+ self.assertEqual('SELECT "a"*1 FROM "abc"', str(q1))
120
+ self.assertEqual('SELECT "a"*1 FROM "abc"', str(q2))
121
+
122
+ def test__multiplication__decimal(self):
123
+ q1 = Q.from_("abc").select(F("a") * 1.0)
124
+ q2 = Q.from_(self.t).select(self.t.a * 1.0)
125
+
126
+ self.assertEqual('SELECT "a"*1.0 FROM "abc"', str(q1))
127
+ self.assertEqual('SELECT "a"*1.0 FROM "abc"', str(q2))
128
+
129
+ def test__multiplication__right(self):
130
+ q1 = Q.from_("abc").select(1 * F("a"))
131
+ q2 = Q.from_(self.t).select(1 * self.t.a)
132
+
133
+ self.assertEqual('SELECT 1*"a" FROM "abc"', str(q1))
134
+ self.assertEqual('SELECT 1*"a" FROM "abc"', str(q2))
135
+
136
+ def test__division__fields(self):
137
+ q1 = Q.from_("abc").select(F("a") / F("b"))
138
+ q2 = Q.from_(self.t).select(self.t.a / self.t.b)
139
+
140
+ self.assertEqual('SELECT "a"/"b" FROM "abc"', str(q1))
141
+ self.assertEqual('SELECT "a"/"b" FROM "abc"', str(q2))
142
+
143
+ def test__division__number(self):
144
+ q1 = Q.from_("abc").select(F("a") / 1)
145
+ q2 = Q.from_(self.t).select(self.t.a / 1)
146
+
147
+ self.assertEqual('SELECT "a"/1 FROM "abc"', str(q1))
148
+ self.assertEqual('SELECT "a"/1 FROM "abc"', str(q2))
149
+
150
+ def test__division__decimal(self):
151
+ q1 = Q.from_("abc").select(F("a") / 1.0)
152
+ q2 = Q.from_(self.t).select(self.t.a / 1.0)
153
+
154
+ self.assertEqual('SELECT "a"/1.0 FROM "abc"', str(q1))
155
+ self.assertEqual('SELECT "a"/1.0 FROM "abc"', str(q2))
156
+
157
+ def test__division__right(self):
158
+ q1 = Q.from_("abc").select(1 / F("a"))
159
+ q2 = Q.from_(self.t).select(1 / self.t.a)
160
+
161
+ self.assertEqual('SELECT 1/"a" FROM "abc"', str(q1))
162
+ self.assertEqual('SELECT 1/"a" FROM "abc"', str(q2))
163
+
164
+ def test__leftshift__fields(self):
165
+ q1 = Q.from_("abc").select(F("a") << F("b"))
166
+ q2 = Q.from_(self.t).select(self.t.a << self.t.b)
167
+
168
+ self.assertEqual('SELECT "a"<<"b" FROM "abc"', str(q1))
169
+ self.assertEqual('SELECT "a"<<"b" FROM "abc"', str(q2))
170
+
171
+ def test__leftshift__number(self):
172
+ q1 = Q.from_("abc").select(F('a') << 2)
173
+ q2 = Q.from_(self.t).select(self.t.a << 2)
174
+
175
+ self.assertEqual('SELECT "a"<<2 FROM "abc"', str(q1))
176
+ self.assertEqual('SELECT "a"<<2 FROM "abc"', str(q2))
177
+
178
+ def test__leftshift__right(self):
179
+ q1 = Q.from_("abc").select(1 << F("a"))
180
+ q2 = Q.from_(self.t).select(1 << self.t.a)
181
+
182
+ self.assertEqual('SELECT 1<<"a" FROM "abc"', str(q1))
183
+ self.assertEqual('SELECT 1<<"a" FROM "abc"', str(q2))
184
+
185
+ def test__rightshift__fields(self):
186
+ q1 = Q.from_("abc").select(F("a") >> F("b"))
187
+ q2 = Q.from_(self.t).select(self.t.a >> self.t.b)
188
+
189
+ self.assertEqual('SELECT "a">>"b" FROM "abc"', str(q1))
190
+ self.assertEqual('SELECT "a">>"b" FROM "abc"', str(q2))
191
+
192
+ def test__rightshift__number(self):
193
+ q1 = Q.from_("abc").select(F('a') >> 2)
194
+ q2 = Q.from_(self.t).select(self.t.a >> 2)
195
+
196
+ self.assertEqual('SELECT "a">>2 FROM "abc"', str(q1))
197
+ self.assertEqual('SELECT "a">>2 FROM "abc"', str(q2))
198
+
199
+ def test__rightshift__right(self):
200
+ q1 = Q.from_("abc").select(1 >> F("a"))
201
+ q2 = Q.from_(self.t).select(1 >> self.t.a)
202
+
203
+ self.assertEqual('SELECT 1>>"a" FROM "abc"', str(q1))
204
+ self.assertEqual('SELECT 1>>"a" FROM "abc"', str(q2))
205
+
206
+ def test__complex_op(self):
207
+ q1 = Q.from_("abc").select(2 + 1 / F("a") - 5)
208
+ q2 = Q.from_(self.t).select(2 + 1 / self.t.a - 5)
209
+
210
+ self.assertEqual('SELECT 2+1/"a"-5 FROM "abc"', str(q1))
211
+ self.assertEqual('SELECT 2+1/"a"-5 FROM "abc"', str(q2))
212
+
213
+ def test__complex_op_add_parentheses(self):
214
+ q1 = Q.from_("abc").select((F("a") + 1) + (F("b") - 5))
215
+ q2 = Q.from_(self.t).select((self.t.a + 1) + (self.t.b - 5))
216
+ q3 = Q.from_("abc").select((F("a") / 1) + (F("b") / 5))
217
+ q4 = Q.from_(self.t).select((self.t.a / 1) + (self.t.b / 5))
218
+
219
+ self.assertEqual('SELECT "a"+1+"b"-5 FROM "abc"', str(q1))
220
+ self.assertEqual('SELECT "a"+1+"b"-5 FROM "abc"', str(q2))
221
+ self.assertEqual('SELECT "a"/1+"b"/5 FROM "abc"', str(q3))
222
+ self.assertEqual('SELECT "a"/1+"b"/5 FROM "abc"', str(q4))
223
+
224
+ def test__complex_op_sub_parentheses(self):
225
+ q1 = Q.from_("abc").select((F("a") + 1) - (F("b") - 5))
226
+ q2 = Q.from_(self.t).select((self.t.a + 1) - (self.t.b - 5))
227
+ q3 = Q.from_("abc").select((F("a") / 1) - (F("b") / 5))
228
+ q4 = Q.from_(self.t).select((self.t.a / 1) - (self.t.b / 5))
229
+
230
+ self.assertEqual('SELECT "a"+1-("b"-5) FROM "abc"', str(q1))
231
+ self.assertEqual('SELECT "a"+1-("b"-5) FROM "abc"', str(q2))
232
+ self.assertEqual('SELECT "a"/1-"b"/5 FROM "abc"', str(q3))
233
+ self.assertEqual('SELECT "a"/1-"b"/5 FROM "abc"', str(q4))
234
+
235
+ def test__complex_op_mul_parentheses(self):
236
+ q1 = Q.from_("abc").select((F("a") + 1) * (F("b") - 5))
237
+ q2 = Q.from_(self.t).select((self.t.a + 1) * (self.t.b - 5))
238
+ q3 = Q.from_("abc").select((F("a") / 1) * (F("b") - 5))
239
+ q4 = Q.from_(self.t).select((self.t.a / 1) * (self.t.b - 5))
240
+ q5 = Q.from_("abc").select((F("a") + 1) * (F("b") / 5))
241
+ q6 = Q.from_(self.t).select((self.t.a + 1) * (self.t.b / 5))
242
+
243
+ self.assertEqual('SELECT ("a"+1)*("b"-5) FROM "abc"', str(q1))
244
+ self.assertEqual('SELECT ("a"+1)*("b"-5) FROM "abc"', str(q2))
245
+ self.assertEqual('SELECT "a"/1*("b"-5) FROM "abc"', str(q3))
246
+ self.assertEqual('SELECT "a"/1*("b"-5) FROM "abc"', str(q4))
247
+ self.assertEqual('SELECT ("a"+1)*"b"/5 FROM "abc"', str(q5))
248
+ self.assertEqual('SELECT ("a"+1)*"b"/5 FROM "abc"', str(q6))
249
+
250
+ def test__complex_op_mul_no_parentheses(self):
251
+ q = Q.from_("abc").select(F("a") + 1 * F("b") - 5)
252
+
253
+ self.assertEqual('SELECT "a"+1*"b"-5 FROM "abc"', str(q))
254
+
255
+ def test__complex_op_div_parentheses(self):
256
+ q1 = Q.from_("abc").select((F("a") + 1) / (F("b") - 5))
257
+ q2 = Q.from_(self.t).select((self.t.a + 1) / (self.t.b - 5))
258
+ q3 = Q.from_("abc").select((F("a") / 1) / (F("b") - 5))
259
+ q4 = Q.from_(self.t).select((self.t.a / 1) / (self.t.b - 5))
260
+ q5 = Q.from_("abc").select((F("a") + 1) / (F("b") * 5))
261
+ q6 = Q.from_(self.t).select((self.t.a + 1) / (self.t.b * 5))
262
+
263
+ self.assertEqual('SELECT ("a"+1)/("b"-5) FROM "abc"', str(q1))
264
+ self.assertEqual('SELECT ("a"+1)/("b"-5) FROM "abc"', str(q2))
265
+ self.assertEqual('SELECT "a"/1/("b"-5) FROM "abc"', str(q3))
266
+ self.assertEqual('SELECT "a"/1/("b"-5) FROM "abc"', str(q4))
267
+ self.assertEqual('SELECT ("a"+1)/("b"*5) FROM "abc"', str(q5))
268
+ self.assertEqual('SELECT ("a"+1)/("b"*5) FROM "abc"', str(q6))
269
+
270
+ def test__complex_op_div_no_parentheses(self):
271
+ q = Q.from_("abc").select(F("a") + 1 / F("b") - 5)
272
+
273
+ self.assertEqual('SELECT "a"+1/"b"-5 FROM "abc"', str(q))
274
+
275
+ def test__complex_op_exponent_parentheses(self):
276
+ q1 = Q.from_("abc").select(F("a") / (F("b") ** 2))
277
+ q2 = Q.from_(self.t).select(self.t.a / (self.t.b**2))
278
+ q3 = Q.from_("abc").select(F("a") ** (F("b") / 2))
279
+ q4 = Q.from_(self.t).select(self.t.a ** (self.t.b / 2))
280
+ q5 = Q.from_("abc").select((F("a") ** F("b")) ** 2)
281
+ q6 = Q.from_(self.t).select((self.t.a**self.t.b) ** 2)
282
+
283
+ self.assertEqual('SELECT "a"/POW("b",2) FROM "abc"', str(q1))
284
+ self.assertEqual('SELECT "a"/POW("b",2) FROM "abc"', str(q2))
285
+ self.assertEqual('SELECT POW("a","b"/2) FROM "abc"', str(q3))
286
+ self.assertEqual('SELECT POW("a","b"/2) FROM "abc"', str(q4))
287
+ self.assertEqual('SELECT POW(POW("a","b"),2) FROM "abc"', str(q5))
288
+ self.assertEqual('SELECT POW(POW("a","b"),2) FROM "abc"', str(q6))
289
+
290
+ def test__complex_op_exponent_no_parentheses(self):
291
+ q1 = Q.from_("abc").select(F("a") ** F("b") ** 2)
292
+ q2 = Q.from_(self.t).select(self.t.a**self.t.b**2)
293
+ q3 = Q.from_("abc").select(F("a") / F("b") ** 2)
294
+ q4 = Q.from_(self.t).select(self.t.a / self.t.b**2)
295
+
296
+ self.assertEqual('SELECT POW("a",POW("b",2)) FROM "abc"', str(q1))
297
+ self.assertEqual('SELECT POW("a",POW("b",2)) FROM "abc"', str(q2))
298
+ self.assertEqual('SELECT "a"/POW("b",2) FROM "abc"', str(q3))
299
+ self.assertEqual('SELECT "a"/POW("b",2) FROM "abc"', str(q4))
300
+
301
+ def test__complex_op_function_parentheses(self):
302
+ q1 = Q.from_("abc").select(F("a") / (fn.Sum(F("b")) / 2))
303
+ q2 = Q.from_("abc").select(self.t.a / (fn.Sum(self.t.b) / 2))
304
+ q3 = Q.from_("abc").select(fn.Sum(F("a") / (F("b") / 2)))
305
+ q4 = Q.from_("abc").select(fn.Sum(self.t.a / (self.t.b / 2)))
306
+
307
+ self.assertEqual('SELECT "a"/(SUM("b")/2) FROM "abc"', str(q1))
308
+ self.assertEqual('SELECT "a"/(SUM("b")/2) FROM "abc"', str(q2))
309
+ self.assertEqual('SELECT SUM("a"/("b"/2)) FROM "abc"', str(q3))
310
+ self.assertEqual('SELECT SUM("a"/("b"/2)) FROM "abc"', str(q4))
311
+
312
+ def test__complex_op_modulus_parentheses(self):
313
+ q1 = Q.from_("abc").select(F("a") / (F("b") % 2))
314
+ q2 = Q.from_(self.t).select(self.t.a / (self.t.b % 2))
315
+ q3 = Q.from_("abc").select(F("a") % (F("b") / 2))
316
+ q4 = Q.from_(self.t).select(self.t.a % (self.t.b / 2))
317
+ q5 = Q.from_("abc").select(F("a") % (F("b") % 2))
318
+ q6 = Q.from_("abc").select(self.t.a % (self.t.b % 2))
319
+
320
+ self.assertEqual('SELECT "a"/MOD("b",2) FROM "abc"', str(q1))
321
+ self.assertEqual('SELECT "a"/MOD("b",2) FROM "abc"', str(q2))
322
+ self.assertEqual('SELECT MOD("a","b"/2) FROM "abc"', str(q3))
323
+ self.assertEqual('SELECT MOD("a","b"/2) FROM "abc"', str(q4))
324
+ self.assertEqual('SELECT MOD("a",MOD("b",2)) FROM "abc"', str(q5))
325
+ self.assertEqual('SELECT MOD("a",MOD("b",2)) FROM "abc"', str(q6))
326
+
327
+ def test__complex_op_modulus_no_parentheses(self):
328
+ q1 = Q.from_("abc").select(F("a") % F("b") % 2)
329
+ q2 = Q.from_(self.t).select(self.t.a % self.t.b % 2)
330
+ q3 = Q.from_("abc").select(F("a") / F("b") % 2)
331
+ q4 = Q.from_(self.t).select(self.t.a / self.t.b % 2)
332
+
333
+ self.assertEqual('SELECT MOD(MOD("a","b"),2) FROM "abc"', str(q1))
334
+ self.assertEqual('SELECT MOD(MOD("a","b"),2) FROM "abc"', str(q2))
335
+ self.assertEqual('SELECT MOD("a"/"b",2) FROM "abc"', str(q3))
336
+ self.assertEqual('SELECT MOD("a"/"b",2) FROM "abc"', str(q4))
337
+
338
+ def test__complex_op_floor_parentheses(self):
339
+ q1 = Q.from_("abc").select(F("a") / (fn.Floor(F("b")) / 2))
340
+ q2 = Q.from_("abc").select(self.t.a / (fn.Floor(self.t.b) / 2))
341
+ q3 = Q.from_("abc").select(fn.Floor(F("a") / (F("b") / 2)))
342
+ q4 = Q.from_("abc").select(fn.Floor(self.t.a / (self.t.b / 2)))
343
+
344
+ self.assertEqual('SELECT "a"/(FLOOR("b")/2) FROM "abc"', str(q1))
345
+ self.assertEqual('SELECT "a"/(FLOOR("b")/2) FROM "abc"', str(q2))
346
+ self.assertEqual('SELECT FLOOR("a"/("b"/2)) FROM "abc"', str(q3))
347
+ self.assertEqual('SELECT FLOOR("a"/("b"/2)) FROM "abc"', str(q4))
348
+
349
+ def test__complex_op_nested_parentheses(self):
350
+ q1 = Q.from_("abc").select(F("a") / (F("b") / ((F("c") / 2))))
351
+ q2 = Q.from_("abc").select(self.t.a / (self.t.b / ((self.t.c / 2))))
352
+
353
+ self.assertEqual('SELECT "a"/("b"/("c"/2)) FROM "abc"', str(q1))
354
+ self.assertEqual('SELECT "a"/("b"/("c"/2)) FROM "abc"', str(q2))
355
+
356
+ def test__arithmetic_equality(self):
357
+ q1 = Q.from_("abc").select(F("a") / 2 == 2)
358
+ q2 = Q.from_(self.t).select(self.t.a / 2 == 2)
359
+
360
+ self.assertEqual('SELECT "a"/2=2 FROM "abc"', str(q1))
361
+ self.assertEqual('SELECT "a"/2=2 FROM "abc"', str(q2))
362
+
363
+ def test__arithmetic_with_function(self):
364
+ q1 = Q.from_("abc").select(fn.Sum(F("foo")) + 1)
365
+ q2 = Q.from_(self.t).select(fn.Sum(self.t.foo) + 1)
366
+
367
+ self.assertEqual('SELECT SUM("foo")+1 FROM "abc"', str(q1))
368
+ self.assertEqual('SELECT SUM("foo")+1 FROM "abc"', str(q2))
369
+
370
+ def test__exponent__number(self):
371
+ q1 = Q.from_("abc").select(F("a") ** 2)
372
+ q2 = Q.from_(self.t).select(self.t.a**2)
373
+
374
+ self.assertEqual('SELECT POW("a",2) FROM "abc"', str(q1))
375
+ self.assertEqual('SELECT POW("a",2) FROM "abc"', str(q2))
376
+
377
+ def test__exponent__decimal(self):
378
+ q1 = Q.from_("abc").select(F("a") ** 0.5)
379
+ q2 = Q.from_(self.t).select(self.t.a**0.5)
380
+
381
+ self.assertEqual('SELECT POW("a",0.5) FROM "abc"', str(q1))
382
+ self.assertEqual('SELECT POW("a",0.5) FROM "abc"', str(q2))
383
+
384
+ def test__modulus__number(self):
385
+ q1 = Q.from_("abc").select(F("a") % 2)
386
+ q2 = Q.from_(self.t).select(self.t.a % 2)
387
+
388
+ self.assertEqual('SELECT MOD("a",2) FROM "abc"', str(q1))
389
+ self.assertEqual('SELECT MOD("a",2) FROM "abc"', str(q2))
390
+
391
+ def test__floor(self):
392
+ q1 = Q.from_("abc").select(fn.Floor(F("foo")))
393
+ q2 = Q.from_(self.t).select(fn.Floor(self.t.foo))
394
+
395
+ self.assertEqual('SELECT FLOOR("foo") FROM "abc"', str(q1))
396
+ self.assertEqual('SELECT FLOOR("foo") FROM "abc"', str(q2))
397
+
398
+
399
+ class AggregationTests(unittest.TestCase):
400
+ def test__count(self):
401
+ q = Q.from_("abc").select(fn.Count(F("foo")))
402
+
403
+ self.assertEqual('SELECT COUNT("foo") FROM "abc"', str(q))
404
+
405
+ def test__count__star(self):
406
+ q = Q.from_("abc").select(fn.Count("*"))
407
+
408
+ self.assertEqual('SELECT COUNT(*) FROM "abc"', str(q))
409
+
410
+ def test__sum(self):
411
+ q = Q.from_("abc").select(fn.Sum(F("foo")))
412
+
413
+ self.assertEqual('SELECT SUM("foo") FROM "abc"', str(q))
414
+
415
+ def test__avg(self):
416
+ q = Q.from_("abc").select(fn.Avg(F("foo")))
417
+
418
+ self.assertEqual('SELECT AVG("foo") FROM "abc"', str(q))
419
+
420
+ def test__first(self):
421
+ q = Q.from_("abc").select(fn.First(F("foo")))
422
+
423
+ self.assertEqual('SELECT FIRST("foo") FROM "abc"', str(q))
424
+
425
+ def test__last(self):
426
+ q = Q.from_("abc").select(fn.Last(F("foo")))
427
+
428
+ self.assertEqual('SELECT LAST("foo") FROM "abc"', str(q))
429
+
430
+ def test__min(self):
431
+ q = Q.from_("abc").select(fn.Min(F("foo")))
432
+
433
+ self.assertEqual('SELECT MIN("foo") FROM "abc"', str(q))
434
+
435
+ def test__max(self):
436
+ q = Q.from_("abc").select(fn.Max(F("foo")))
437
+
438
+ self.assertEqual('SELECT MAX("foo") FROM "abc"', str(q))
439
+
440
+ def test__std(self):
441
+ q = Q.from_("abc").select(fn.Std(F("foo")))
442
+
443
+ self.assertEqual('SELECT STD("foo") FROM "abc"', str(q))
444
+
445
+ def test__stddev(self):
446
+ q = Q.from_("abc").select(fn.StdDev(F("foo")))
447
+
448
+ self.assertEqual('SELECT STDDEV("foo") FROM "abc"', str(q))
449
+
450
+ def test__approx_percentile(self):
451
+ q = Q.from_("abc").select(fn.ApproximatePercentile(F("foo"), 0.5))
452
+
453
+ self.assertEqual(
454
+ 'SELECT APPROXIMATE_PERCENTILE("foo" USING PARAMETERS percentile=0.5) FROM "abc"',
455
+ str(q),
456
+ )
457
+
458
+ def test__subquery_in_params_functions(self):
459
+ subquery = Query.from_('table').select('id')
460
+ func = fn.Function('func', 'id', subquery)
461
+ self.assertEqual("func('id',(SELECT id FROM table))", func.get_sql())
462
+
463
+
464
+ class ConditionTests(unittest.TestCase):
465
+ def test__case__raw(self):
466
+ q = Q.from_("abc").select(Case().when(F("foo") == 1, "a"))
467
+
468
+ self.assertEqual('SELECT CASE WHEN "foo"=1 THEN \'a\' END FROM "abc"', str(q))
469
+
470
+ def test__case__else(self):
471
+ q = Q.from_("abc").select(Case().when(F("foo") == 1, "a").else_("b"))
472
+
473
+ self.assertEqual("SELECT CASE WHEN \"foo\"=1 THEN 'a' ELSE 'b' END FROM \"abc\"", str(q))
474
+
475
+ def test__case__field(self):
476
+ q = Q.from_("abc").select(Case().when(F("foo") == 1, F("bar")).else_(F("buz")))
477
+
478
+ self.assertEqual('SELECT CASE WHEN "foo"=1 THEN "bar" ELSE "buz" END FROM "abc"', str(q))
479
+
480
+ def test__case__multi(self):
481
+ q = Q.from_("abc").select(Case().when(F("foo") > 0, F("fiz")).when(F("bar") <= 0, F("buz")).else_(1))
482
+
483
+ self.assertEqual(
484
+ 'SELECT CASE WHEN "foo">0 THEN "fiz" WHEN "bar"<=0 THEN "buz" ELSE 1 END FROM "abc"',
485
+ str(q),
486
+ )
487
+
488
+ def test__case__no_cases(self):
489
+ with self.assertRaises(CaseException):
490
+ q = Q.from_("abc").select(Case())
491
+
492
+ str(q)
493
+
494
+
495
+ class StringTests(unittest.TestCase):
496
+ t = T("abc")
497
+
498
+ def test__ascii__str(self):
499
+ q = Q.select(fn.Ascii("2"))
500
+
501
+ self.assertEqual("SELECT ASCII('2')", str(q))
502
+
503
+ def test__ascii__int(self):
504
+ q = Q.select(fn.Ascii(2))
505
+
506
+ self.assertEqual("SELECT ASCII(2)", str(q))
507
+
508
+ def test__ascii__field(self):
509
+ q = Q.from_(self.t).select(fn.Ascii(self.t.foo))
510
+
511
+ self.assertEqual('SELECT ASCII("foo") FROM "abc"', str(q))
512
+
513
+ def test__bin__str(self):
514
+ q = Q.select(fn.Bin("2"))
515
+
516
+ self.assertEqual("SELECT BIN('2')", str(q))
517
+
518
+ def test__bin__int(self):
519
+ q = Q.select(fn.Bin(2))
520
+
521
+ self.assertEqual("SELECT BIN(2)", str(q))
522
+
523
+ def test__bin__field(self):
524
+ q = Q.from_(self.t).select(fn.Bin(self.t.foo))
525
+
526
+ self.assertEqual('SELECT BIN("foo") FROM "abc"', str(q))
527
+
528
+ def test__concat__str(self):
529
+ q = Q.select(fn.Concat("p", "y", "q", "b"))
530
+
531
+ self.assertEqual("SELECT CONCAT('p','y','q','b')", str(q))
532
+
533
+ def test__concat__field(self):
534
+ q = Q.from_(self.t).select(fn.Concat(self.t.foo, self.t.bar))
535
+
536
+ self.assertEqual('SELECT CONCAT("foo","bar") FROM "abc"', str(q))
537
+
538
+ def test__insert__str(self):
539
+ q = Q.select(fn.Insert("Quadratic", 3, 4, "What"))
540
+
541
+ self.assertEqual("SELECT INSERT('Quadratic',3,4,'What')", str(q))
542
+
543
+ def test__insert__field(self):
544
+ q = Q.from_(self.t).select(fn.Insert(self.t.foo, 3, 4, self.t.bar))
545
+
546
+ self.assertEqual('SELECT INSERT("foo",3,4,"bar") FROM "abc"', str(q))
547
+
548
+ def test__lower__str(self):
549
+ q = Q.select(fn.Lower("ABC"))
550
+
551
+ self.assertEqual("SELECT LOWER('ABC')", str(q))
552
+
553
+ def test__lower__field(self):
554
+ q = Q.from_(self.t).select(fn.Lower(self.t.foo))
555
+
556
+ self.assertEqual('SELECT LOWER("foo") FROM "abc"', str(q))
557
+
558
+ def test__length__str(self):
559
+ q = Q.select(fn.Length("ABC"))
560
+
561
+ self.assertEqual("SELECT LENGTH('ABC')", str(q))
562
+
563
+ def test__length__field(self):
564
+ q = Q.from_(self.t).select(fn.Length(self.t.foo))
565
+
566
+ self.assertEqual('SELECT LENGTH("foo") FROM "abc"', str(q))
567
+
568
+ def test__substring(self):
569
+ q = Q.from_(self.t).select(fn.Substring(self.t.foo, 2, 6))
570
+
571
+ self.assertEqual('SELECT SUBSTRING("foo",2,6) FROM "abc"', str(q))
572
+
573
+ def test__replace__str(self):
574
+ q = Q.select(fn.Replace("ABC_DEF", "_", " "))
575
+ self.assertEqual("SELECT REPLACE('ABC_DEF','_',' ')", str(q))
576
+
577
+ def test__replace__field(self):
578
+ q = Q.from_(self.t).select(fn.Replace(self.t.foobar, self.t.foo, self.t.bar))
579
+ self.assertEqual('SELECT REPLACE("foobar","foo","bar") FROM "abc"', str(q))
580
+
581
+
582
+ class SplitPartFunctionTests(unittest.TestCase):
583
+ t = T("abc")
584
+
585
+ def test__split_part(self):
586
+ q = VerticaQuery.from_(self.t).select(fn.SplitPart(self.t.foo, "|", 3))
587
+
588
+ self.assertEqual('SELECT SPLIT_PART("foo",\'|\',3) FROM "abc"', str(q))
589
+
590
+
591
+ class RegexpLikeFunctionTests(unittest.TestCase):
592
+ t = T("abc")
593
+
594
+ def test__regexp_like(self):
595
+ q = VerticaQuery.from_(self.t).select(fn.RegexpLike(self.t.foo, "^a", "x"))
596
+
597
+ self.assertEqual("SELECT REGEXP_LIKE(\"foo\",'^a','x') FROM \"abc\"", str(q))
598
+
599
+
600
+ class CastTests(unittest.TestCase):
601
+ t = T("abc")
602
+
603
+ def test__cast__as(self):
604
+ q = Q.from_(self.t).select(fn.Cast(self.t.foo, SqlTypes.UNSIGNED))
605
+
606
+ self.assertEqual('SELECT CAST("foo" AS UNSIGNED) FROM "abc"', str(q))
607
+
608
+ def test__cast__signed(self):
609
+ q1 = Q.from_(self.t).select(fn.Signed(self.t.foo))
610
+ q2 = Q.from_(self.t).select(fn.Cast(self.t.foo, SqlTypes.SIGNED))
611
+
612
+ self.assertEqual('SELECT CAST("foo" AS SIGNED) FROM "abc"', str(q1))
613
+ self.assertEqual('SELECT CAST("foo" AS SIGNED) FROM "abc"', str(q2))
614
+
615
+ def test__cast__unsigned(self):
616
+ q1 = Q.from_(self.t).select(fn.Unsigned(self.t.foo))
617
+ q2 = Q.from_(self.t).select(fn.Cast(self.t.foo, SqlTypes.UNSIGNED))
618
+
619
+ self.assertEqual('SELECT CAST("foo" AS UNSIGNED) FROM "abc"', str(q1))
620
+ self.assertEqual('SELECT CAST("foo" AS UNSIGNED) FROM "abc"', str(q2))
621
+
622
+ def test__cast__date(self):
623
+ q1 = Q.from_(self.t).select(fn.Date(self.t.foo))
624
+ q2 = Q.from_(self.t).select(fn.Cast(self.t.foo, SqlTypes.DATE))
625
+
626
+ self.assertEqual('SELECT DATE("foo") FROM "abc"', str(q1))
627
+ self.assertEqual('SELECT CAST("foo" AS DATE) FROM "abc"', str(q2))
628
+
629
+ def test__cast__timestamp(self):
630
+ q1 = Q.from_(self.t).select(fn.Timestamp(self.t.foo))
631
+ q2 = Q.from_(self.t).select(fn.Cast(self.t.foo, SqlTypes.TIMESTAMP))
632
+
633
+ self.assertEqual('SELECT TIMESTAMP("foo") FROM "abc"', str(q1))
634
+ self.assertEqual('SELECT CAST("foo" AS TIMESTAMP) FROM "abc"', str(q2))
635
+
636
+ def test__cast__char(self):
637
+ q = Q.from_(self.t).select(fn.Cast(self.t.foo, SqlTypes.CHAR))
638
+
639
+ self.assertEqual('SELECT CAST("foo" AS CHAR) FROM "abc"', str(q))
640
+
641
+ def test__cast__char_with_arg(self):
642
+ q = Q.from_(self.t).select(fn.Cast(self.t.foo, SqlTypes.VARCHAR(24)))
643
+
644
+ self.assertEqual('SELECT CAST("foo" AS VARCHAR(24)) FROM "abc"', str(q))
645
+
646
+ def test__cast__varchar(self):
647
+ q = Q.from_(self.t).select(fn.Cast(self.t.foo, SqlTypes.VARCHAR))
648
+
649
+ self.assertEqual('SELECT CAST("foo" AS VARCHAR) FROM "abc"', str(q))
650
+
651
+ def test__cast__varchar_with_arg(self):
652
+ q = Q.from_(self.t).select(fn.Cast(self.t.foo, SqlTypes.VARCHAR(24)))
653
+
654
+ self.assertEqual('SELECT CAST("foo" AS VARCHAR(24)) FROM "abc"', str(q))
655
+
656
+ def test__cast__long_varchar(self):
657
+ q = Q.from_(self.t).select(fn.Cast(self.t.foo, SqlTypes.LONG_VARCHAR))
658
+
659
+ self.assertEqual('SELECT CAST("foo" AS LONG VARCHAR) FROM "abc"', str(q))
660
+
661
+ def test__cast__long_varchar_with_arg(self):
662
+ q = Q.from_(self.t).select(fn.Cast(self.t.foo, SqlTypes.LONG_VARCHAR(24)))
663
+
664
+ self.assertEqual('SELECT CAST("foo" AS LONG VARCHAR(24)) FROM "abc"', str(q))
665
+
666
+ def test__cast__binary(self):
667
+ q = Q.from_(self.t).select(fn.Cast(self.t.foo, SqlTypes.BINARY))
668
+
669
+ self.assertEqual('SELECT CAST("foo" AS BINARY) FROM "abc"', str(q))
670
+
671
+ def test__cast__binary_with_arg(self):
672
+ q = Q.from_(self.t).select(fn.Cast(self.t.foo, SqlTypes.BINARY(24)))
673
+
674
+ self.assertEqual('SELECT CAST("foo" AS BINARY(24)) FROM "abc"', str(q))
675
+
676
+ def test__cast__varbinary(self):
677
+ q = Q.from_(self.t).select(fn.Cast(self.t.foo, SqlTypes.VARBINARY))
678
+
679
+ self.assertEqual('SELECT CAST("foo" AS VARBINARY) FROM "abc"', str(q))
680
+
681
+ def test__cast__varbinary_with_arg(self):
682
+ q = Q.from_(self.t).select(fn.Cast(self.t.foo, SqlTypes.VARBINARY(24)))
683
+
684
+ self.assertEqual('SELECT CAST("foo" AS VARBINARY(24)) FROM "abc"', str(q))
685
+
686
+ def test__cast__long_varbinary(self):
687
+ q = Q.from_(self.t).select(fn.Cast(self.t.foo, SqlTypes.LONG_VARBINARY))
688
+
689
+ self.assertEqual('SELECT CAST("foo" AS LONG VARBINARY) FROM "abc"', str(q))
690
+
691
+ def test__cast__long_varbinary_with_arg(self):
692
+ q = Q.from_(self.t).select(fn.Cast(self.t.foo, SqlTypes.LONG_VARBINARY(24)))
693
+
694
+ self.assertEqual('SELECT CAST("foo" AS LONG VARBINARY(24)) FROM "abc"', str(q))
695
+
696
+ def test__cast__boolean(self):
697
+ q = Q.from_(self.t).select(fn.Cast(self.t.foo, SqlTypes.BOOLEAN))
698
+
699
+ self.assertEqual('SELECT CAST("foo" AS BOOLEAN) FROM "abc"', str(q))
700
+
701
+ def test__cast__integer(self):
702
+ q = Q.from_(self.t).select(fn.Cast(self.t.foo, SqlTypes.INTEGER))
703
+
704
+ self.assertEqual('SELECT CAST("foo" AS INTEGER) FROM "abc"', str(q))
705
+
706
+ def test__cast__float(self):
707
+ q = Q.from_(self.t).select(fn.Cast(self.t.foo, SqlTypes.FLOAT))
708
+
709
+ self.assertEqual('SELECT CAST("foo" AS FLOAT) FROM "abc"', str(q))
710
+
711
+ def test__cast__numeric(self):
712
+ q = Q.from_(self.t).select(fn.Cast(self.t.foo, SqlTypes.NUMERIC))
713
+
714
+ self.assertEqual('SELECT CAST("foo" AS NUMERIC) FROM "abc"', str(q))
715
+
716
+ def test__tochar__(self):
717
+ q = Q.from_(self.t).select(fn.ToChar(self.t.foo, "SomeFormat"))
718
+
719
+ self.assertEqual('SELECT TO_CHAR("foo",\'SomeFormat\') FROM "abc"', str(q))
720
+
721
+
722
+ class DateFunctionsTests(unittest.TestCase):
723
+ dt = F("dt")
724
+ t = T("abc")
725
+ t2 = T("efg")
726
+
727
+ def _test_extract_datepart(self, date_part):
728
+ q = Q.from_(self.t).select(fn.Extract(date_part, self.t.foo))
729
+
730
+ value = getattr(date_part, 'value', date_part)
731
+ self.assertEqual('SELECT EXTRACT(%s FROM "foo") FROM "abc"' % value, str(q))
732
+
733
+ def test_extract_microsecond(self):
734
+ self._test_extract_datepart(DatePart.microsecond)
735
+ self._test_extract_datepart(DatePart.microsecond.value)
736
+
737
+ def test_extract_second(self):
738
+ self._test_extract_datepart(DatePart.second)
739
+ self._test_extract_datepart(DatePart.second.value)
740
+
741
+ def test_extract_minute(self):
742
+ self._test_extract_datepart(DatePart.minute)
743
+ self._test_extract_datepart(DatePart.minute.value)
744
+
745
+ def test_extract_hour(self):
746
+ self._test_extract_datepart(DatePart.hour)
747
+ self._test_extract_datepart(DatePart.hour.value)
748
+
749
+ def test_extract_day(self):
750
+ self._test_extract_datepart(DatePart.day)
751
+ self._test_extract_datepart(DatePart.day.value)
752
+
753
+ def test_extract_week(self):
754
+ self._test_extract_datepart(DatePart.week)
755
+ self._test_extract_datepart(DatePart.week.value)
756
+
757
+ def test_extract_month(self):
758
+ self._test_extract_datepart(DatePart.month)
759
+ self._test_extract_datepart(DatePart.month.value)
760
+
761
+ def test_extract_quarter(self):
762
+ self._test_extract_datepart(DatePart.quarter)
763
+ self._test_extract_datepart(DatePart.quarter.value)
764
+
765
+ def test_extract_year(self):
766
+ self._test_extract_datepart(DatePart.year)
767
+ self._test_extract_datepart(DatePart.year.value)
768
+
769
+ def test_extract_join(self):
770
+ q = Q.from_(self.t).join(self.t2).on(self.t.id == self.t2.t_id).select(fn.Extract(DatePart.year, self.t.foo))
771
+
772
+ self.assertEqual(
773
+ 'SELECT EXTRACT(YEAR FROM "abc"."foo") FROM "abc" ' 'JOIN "efg" ON "abc"."id"="efg"."t_id"', str(q)
774
+ )
775
+
776
+ def test_timestampadd(self):
777
+ a = fn.TimestampAdd("year", 1, "2017-10-01")
778
+ self.assertEqual(str(a), "TIMESTAMPADD(year,1,'2017-10-01')")
779
+
780
+ def test_time_diff(self):
781
+ a = fn.TimeDiff("18:00:00", "10:00:00")
782
+ self.assertEqual(str(a), "TIMEDIFF('18:00:00','10:00:00')")
783
+
784
+ def test_date_add(self):
785
+ a = fn.DateAdd("year", 1, "2017-10-01")
786
+ self.assertEqual(str(a), "DATE_ADD(year,1,'2017-10-01')")
787
+
788
+ def test_now(self):
789
+ query = Query.select(fn.Now())
790
+
791
+ self.assertEqual("SELECT NOW()", str(query))
792
+
793
+ def test_utc_timestamp(self):
794
+ query = Query.select(fn.UtcTimestamp())
795
+
796
+ self.assertEqual("SELECT UTC_TIMESTAMP()", str(query))
797
+
798
+ def test_current_date(self):
799
+ query = Query.select(fn.CurDate())
800
+
801
+ self.assertEqual("SELECT CURRENT_DATE()", str(query))
802
+
803
+ def test_current_time(self):
804
+ query = Query.select(fn.CurTime())
805
+
806
+ self.assertEqual("SELECT CURRENT_TIME()", str(query))
807
+
808
+ def test_current_timestamp(self):
809
+ query = Query.select(fn.CurTimestamp())
810
+
811
+ self.assertEqual("SELECT CURRENT_TIMESTAMP", str(query))
812
+
813
+ def test_current_timestamp_with_alias(self):
814
+ query = Query.select(fn.CurTimestamp("ts"))
815
+
816
+ self.assertEqual('SELECT CURRENT_TIMESTAMP "ts"', str(query))
817
+
818
+ def test_to_date(self):
819
+ q1 = fn.ToDate("2019-06-21", "yyyy-mm-dd")
820
+ q2 = Query.from_(self.t).select(fn.ToDate("2019-06-21", "yyyy-mm-dd"))
821
+ q3 = Query.from_(self.t).select(fn.ToDate(F("foo"), "yyyy-mm-dd"))
822
+
823
+ self.assertEqual(str(q1), "TO_DATE('2019-06-21','yyyy-mm-dd')")
824
+ self.assertEqual(str(q2), "SELECT TO_DATE('2019-06-21','yyyy-mm-dd') FROM \"abc\"")
825
+ self.assertEqual(str(q3), 'SELECT TO_DATE("foo",\'yyyy-mm-dd\') FROM "abc"')
826
+
827
+
828
+ class NullFunctionsTests(unittest.TestCase):
829
+ def test_isnull(self):
830
+ q = Q.from_("abc").select(fn.IsNull(F("foo")))
831
+
832
+ self.assertEqual('SELECT ISNULL("foo") FROM "abc"', str(q))
833
+
834
+ def test_coalesce(self):
835
+ q = Q.from_("abc").select(fn.Coalesce(F("foo"), 0))
836
+
837
+ self.assertEqual('SELECT COALESCE("foo",0) FROM "abc"', str(q))
838
+
839
+ def test_nullif(self):
840
+ q = Q.from_("abc").select(fn.NullIf(F("foo"), 0))
841
+
842
+ self.assertEqual('SELECT NULLIF("foo",0) FROM "abc"', str(q))
843
+
844
+ def test_nvl(self):
845
+ q = Q.from_("abc").select(fn.NVL(F("foo"), 0))
846
+
847
+ self.assertEqual('SELECT NVL("foo",0) FROM "abc"', str(q))
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_groupby_modifiers.py ADDED
@@ -0,0 +1,168 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from pypika import Query, Rollup, RollupException, Table, functions as fn
4
+
5
+ __author__ = "Timothy Heys"
6
+ __email__ = "theys@kayak.com"
7
+
8
+
9
+ class RollupTests(unittest.TestCase):
10
+ table = Table("abc")
11
+
12
+ def test_mysql_one_groupby(self):
13
+ q = (
14
+ Query.from_(self.table)
15
+ .select(self.table.foo, fn.Sum(self.table.bar))
16
+ .rollup(self.table.foo, vendor="mysql")
17
+ )
18
+
19
+ self.assertEqual('SELECT "foo",SUM("bar") FROM "abc" GROUP BY "foo" WITH ROLLUP', str(q))
20
+
21
+ def test_mysql_rollup_two_groupbys(self):
22
+ q = (
23
+ Query.from_(self.table)
24
+ .select(self.table.foo, self.table.fiz, fn.Sum(self.table.bar))
25
+ .rollup(self.table.foo, self.table.fiz, vendor="mysql")
26
+ )
27
+
28
+ self.assertEqual(
29
+ 'SELECT "foo","fiz",SUM("bar") FROM "abc" GROUP BY "foo","fiz" WITH ROLLUP',
30
+ str(q),
31
+ )
32
+
33
+ def test_no_rollup_before_groupby(self):
34
+ with self.assertRaises(RollupException):
35
+ Query.from_(self.table).select(self.table.foo, fn.Sum(self.table.bar)).rollup(vendor="mysql")
36
+
37
+ def test_no_rollup_after_rollup_mysql(self):
38
+ with self.assertRaises(AttributeError):
39
+ Query.from_(self.table).select(self.table.foo, self.table.fiz, fn.Sum(self.table.bar)).rollup(
40
+ self.table.foo, vendor="mysql"
41
+ ).rollup(self.table.fiz, vendor="mysql")
42
+
43
+ def test_verticaoracle_func_one_groupby(self):
44
+ q = Query.from_(self.table).select(self.table.foo, fn.Sum(self.table.bar)).groupby(Rollup(self.table.foo))
45
+
46
+ self.assertEqual('SELECT "foo",SUM("bar") FROM "abc" GROUP BY ROLLUP("foo")', str(q))
47
+
48
+ def test_verticaoracle_func_two_groupbys(self):
49
+ q = (
50
+ Query.from_(self.table)
51
+ .select(self.table.foo, self.table.fiz, fn.Sum(self.table.bar))
52
+ .groupby(
53
+ Rollup(
54
+ self.table.foo,
55
+ self.table.fiz,
56
+ )
57
+ )
58
+ )
59
+
60
+ self.assertEqual(
61
+ 'SELECT "foo","fiz",SUM("bar") FROM "abc" GROUP BY ROLLUP("foo","fiz")',
62
+ str(q),
63
+ )
64
+
65
+ def test_verticaoracle_func_partial(self):
66
+ q = (
67
+ Query.from_(self.table)
68
+ .select(self.table.foo, self.table.fiz, self.table.buz, fn.Sum(self.table.bar))
69
+ .groupby(
70
+ Rollup(
71
+ self.table.foo,
72
+ self.table.fiz,
73
+ ),
74
+ self.table.buz,
75
+ )
76
+ )
77
+
78
+ self.assertEqual(
79
+ 'SELECT "foo","fiz","buz",SUM("bar") FROM "abc" GROUP BY ROLLUP("foo","fiz"),"buz"',
80
+ str(q),
81
+ )
82
+
83
+ def test_verticaoracle_from_groupbys(self):
84
+ q = Query.from_(self.table).select(self.table.foo, fn.Sum(self.table.bar)).rollup(self.table.foo)
85
+
86
+ self.assertEqual('SELECT "foo",SUM("bar") FROM "abc" GROUP BY ROLLUP("foo")', str(q))
87
+
88
+ def test_verticaoracle_from_two_groupbys(self):
89
+ q = (
90
+ Query.from_(self.table)
91
+ .select(self.table.foo, self.table.fiz, fn.Sum(self.table.bar))
92
+ .rollup(
93
+ self.table.foo,
94
+ self.table.fiz,
95
+ )
96
+ )
97
+
98
+ self.assertEqual(
99
+ 'SELECT "foo","fiz",SUM("bar") FROM "abc" GROUP BY ROLLUP("foo","fiz")',
100
+ str(q),
101
+ )
102
+
103
+ def test_verticaoracle_from_parameters(self):
104
+ q = (
105
+ Query.from_(self.table)
106
+ .select(self.table.foo, self.table.fiz, fn.Sum(self.table.bar))
107
+ .groupby(
108
+ self.table.foo,
109
+ )
110
+ .rollup(
111
+ self.table.fiz,
112
+ )
113
+ )
114
+
115
+ self.assertEqual(
116
+ 'SELECT "foo","fiz",SUM("bar") FROM "abc" GROUP BY "foo",ROLLUP("fiz")',
117
+ str(q),
118
+ )
119
+
120
+ def test_verticaoracle_multiple_rollups(self):
121
+ q = (
122
+ Query.from_(self.table)
123
+ .select(self.table.foo, self.table.fiz, fn.Sum(self.table.bar))
124
+ .rollup(
125
+ self.table.foo,
126
+ )
127
+ .rollup(
128
+ self.table.fiz,
129
+ )
130
+ )
131
+
132
+ self.assertEqual(
133
+ 'SELECT "foo","fiz",SUM("bar") FROM "abc" GROUP BY ROLLUP("foo","fiz")',
134
+ str(q),
135
+ )
136
+
137
+ def test_verticaoracle_rollups_with_parity(self):
138
+ q = (
139
+ Query.from_(self.table)
140
+ .select(
141
+ self.table.buz,
142
+ )
143
+ .rollup(
144
+ [self.table.foo, self.table.bar],
145
+ self.table.fiz,
146
+ )
147
+ )
148
+
149
+ self.assertEqual('SELECT "buz" FROM "abc" GROUP BY ROLLUP(("foo","bar"),"fiz")', str(q))
150
+
151
+ def test_verticaoracle_rollups_with_multiple_rollups_and_parity(self):
152
+ q = (
153
+ Query.from_(self.table)
154
+ .select(
155
+ self.table.buz,
156
+ )
157
+ .rollup(
158
+ [self.table.foo, self.table.bar],
159
+ )
160
+ .rollup(
161
+ [self.table.fiz, self.table.buz],
162
+ )
163
+ )
164
+
165
+ self.assertEqual(
166
+ 'SELECT "buz" FROM "abc" GROUP BY ROLLUP(("foo","bar"),("fiz","buz"))',
167
+ str(q),
168
+ )
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_immutability.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from pypika import (
4
+ AliasedQuery,
5
+ Query,
6
+ Tables,
7
+ )
8
+
9
+
10
+ class ImmutabilityTests(unittest.TestCase):
11
+ table_a, table_b, table_c = Tables("a", "b", "c")
12
+
13
+ def test_select_returns_new_query_instance(self):
14
+ query_a = Query.from_(self.table_a).select(self.table_a.foo)
15
+ query_b = query_a.select(self.table_a.bar)
16
+
17
+ self.assertIsNot(query_a, query_b)
18
+ self.assertNotEqual(str(query_a), str(query_b))
19
+
20
+ def test_groupby_returns_new_query_instance(self):
21
+ query_a = Query.from_(self.table_a).select(self.table_a.foo).groupby(self.table_a.foo)
22
+ query_b = query_a.groupby(self.table_a.bar)
23
+
24
+ self.assertIsNot(query_a, query_b)
25
+ self.assertNotEqual(str(query_a), str(query_b))
26
+
27
+ def test_orderby_return_new_query_instance(self):
28
+ query_a = Query.from_(self.table_a).select(self.table_a.foo).orderby(self.table_a.foo)
29
+ query_b = query_a.orderby(self.table_a.bar)
30
+
31
+ self.assertIsNot(query_a, query_b)
32
+ self.assertNotEqual(str(query_a), str(query_b))
33
+
34
+ def test_join_return_new_query_instance(self):
35
+ base = Query.from_(self.table_a).select(self.table_a.foo)
36
+ query_a = base.join(self.table_b).on(self.table_a.foo == self.table_b.bar).select(self.table_b.bar)
37
+ query_b = query_a.join(self.table_c).on(self.table_a.foo == self.table_c.baz).select(self.table_c.baz)
38
+
39
+ self.assertIsNot(query_a, query_b)
40
+ self.assertNotEqual(str(query_a), str(query_b))
41
+
42
+ def test_use_index_return_new_query_instance(self):
43
+ query_a = Query.from_(self.table_a).select(self.table_a.foo).use_index('idx1')
44
+ query_b = query_a.use_index('idx2')
45
+
46
+ self.assertIsNot(query_a, query_b)
47
+ self.assertNotEqual(str(query_a), str(query_b))
48
+
49
+ def test_force_index_return_new_query_instance(self):
50
+ query_a = Query.from_(self.table_a).select(self.table_a.foo).force_index('idx1')
51
+ query_b = query_a.force_index('idx2')
52
+
53
+ self.assertIsNot(query_a, query_b)
54
+ self.assertNotEqual(str(query_a), str(query_b))
55
+
56
+ def test_with_return_new_query_instance(self):
57
+ alias_1 = Query.from_(self.table_a).select(self.table_a.foo)
58
+ alias_2 = Query.from_(self.table_b).select(self.table_b.bar)
59
+ query_a = Query.from_(AliasedQuery('a1')).select('foo').with_(alias_1, 'a1')
60
+ query_b = query_a.with_(alias_2, 'a2')
61
+
62
+ self.assertIsNot(query_a, query_b)
63
+ self.assertNotEqual(str(query_a), str(query_b))
64
+
65
+ def test_insert_into_return_new_query_instance(self):
66
+ query_a = Query.into(self.table_a).insert('foo1')
67
+ query_b = query_a.insert('foo2')
68
+
69
+ self.assertIsNot(query_a, query_b)
70
+ self.assertNotEqual(str(query_a), str(query_b))
71
+
72
+ def test_replace_into_return_new_query_instance(self):
73
+ query_a = Query.into(self.table_a).replace('foo')
74
+ query_b = query_a.replace('bar')
75
+
76
+ self.assertIsNot(query_a, query_b)
77
+ self.assertNotEqual(str(query_a), str(query_b))
78
+
79
+ def test_update_set_return_new_query_instance(self):
80
+ query_a = Query.update(self.table_a).set(self.table_a.foo, 'foo')
81
+ query_b = query_a.set(self.table_a.bar, 'bar')
82
+
83
+ self.assertIsNot(query_a, query_b)
84
+ self.assertNotEqual(str(query_a), str(query_b))
85
+
86
+ def test_queries_after_join(self):
87
+ query1 = Query.from_(self.table_a).select(self.table_a.foo)
88
+ query2 = query1.join(self.table_b).on(self.table_a.foo == self.table_b.bar).select(self.table_b.buz)
89
+
90
+ self.assertEqual('SELECT "foo" FROM "a"', str(query1))
91
+ self.assertEqual(
92
+ 'SELECT "a"."foo","b"."buz" FROM "a" JOIN "b" ON "a"."foo"="b"."bar"',
93
+ str(query2),
94
+ )
95
+
96
+ def test_immutable_kwarg_on_query_builder_disables_immutability(self):
97
+ query0 = Query.from_(self.table_a, immutable=False)
98
+ query1 = query0.select(self.table_a.foo)
99
+ self.assertIs(query0, query1)
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_inserts.py ADDED
@@ -0,0 +1,885 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from pypika import (
4
+ AliasedQuery,
5
+ Case,
6
+ Field as F,
7
+ MySQLQuery,
8
+ PostgreSQLQuery,
9
+ Query,
10
+ Table,
11
+ Tables,
12
+ functions as fn,
13
+ )
14
+ from pypika.functions import (
15
+ Avg,
16
+ Cast,
17
+ )
18
+ from pypika.terms import Values
19
+ from pypika.utils import QueryException
20
+
21
+ __author__ = "Timothy Heys"
22
+ __email__ = "theys@kayak.com"
23
+
24
+
25
+ class InsertIntoTests(unittest.TestCase):
26
+ table_abc = Table("abc")
27
+
28
+ def test_insert_one_column(self):
29
+ query = Query.into(self.table_abc).insert(1)
30
+
31
+ self.assertEqual('INSERT INTO "abc" VALUES (1)', str(query))
32
+
33
+ def test_insert_one_column_single_element_array(self):
34
+ query = Query.into(self.table_abc).insert((1,))
35
+
36
+ self.assertEqual('INSERT INTO "abc" VALUES (1)', str(query))
37
+
38
+ def test_insert_one_column_multi_element_array(self):
39
+ query = Query.into(self.table_abc).insert((1,), (2,))
40
+
41
+ self.assertEqual('INSERT INTO "abc" VALUES (1),(2)', str(query))
42
+
43
+ def test_insert_single_row_with_array_value(self):
44
+ query = Query.into(self.table_abc).insert(1, ["a", "b", "c"])
45
+
46
+ self.assertEqual("INSERT INTO \"abc\" VALUES (1,['a','b','c'])", str(query))
47
+
48
+ def test_insert_multiple_rows_with_array_value(self):
49
+ query = Query.into(self.table_abc).insert(
50
+ (1, ["a", "b", "c"]),
51
+ (2, ["c", "d", "e"]),
52
+ )
53
+
54
+ self.assertEqual(
55
+ 'INSERT INTO "abc" ' "VALUES (1,['a','b','c']),(2,['c','d','e'])",
56
+ str(query),
57
+ )
58
+
59
+ def test_insert_all_columns(self):
60
+ query = Query.into(self.table_abc).insert(1, "a", True)
61
+
62
+ self.assertEqual("INSERT INTO \"abc\" VALUES (1,'a',true)", str(query))
63
+
64
+ def test_insert_all_columns_single_element(self):
65
+ query = Query.into(self.table_abc).insert((1, "a", True))
66
+
67
+ self.assertEqual("INSERT INTO \"abc\" VALUES (1,'a',true)", str(query))
68
+
69
+ def test_insert_all_columns_multi_rows(self):
70
+ query = Query.into(self.table_abc).insert((1, "a", True), (2, "b", False))
71
+
72
+ self.assertEqual("INSERT INTO \"abc\" VALUES (1,'a',true),(2,'b',false)", str(query))
73
+
74
+ def test_insert_all_columns_multi_rows_chained(self):
75
+ query = Query.into(self.table_abc).insert(1, "a", True).insert(2, "b", False)
76
+
77
+ self.assertEqual("INSERT INTO \"abc\" VALUES (1,'a',true),(2,'b',false)", str(query))
78
+
79
+ def test_insert_all_columns_multi_rows_chained_mixed(self):
80
+ query = Query.into(self.table_abc).insert((1, "a", True), (2, "b", False)).insert(3, "c", True)
81
+
82
+ self.assertEqual(
83
+ 'INSERT INTO "abc" VALUES ' "(1,'a',true),(2,'b',false)," "(3,'c',true)",
84
+ str(query),
85
+ )
86
+
87
+ def test_insert_all_columns_multi_rows_chained_multiple_rows(self):
88
+ query = (
89
+ Query.into(self.table_abc).insert((1, "a", True), (2, "b", False)).insert((3, "c", True), (4, "d", False))
90
+ )
91
+
92
+ self.assertEqual(
93
+ 'INSERT INTO "abc" VALUES ' "(1,'a',true),(2,'b',false)," "(3,'c',true),(4,'d',false)",
94
+ str(query),
95
+ )
96
+
97
+ def test_insert_selected_columns(self):
98
+ query = (
99
+ Query.into(self.table_abc)
100
+ .columns(self.table_abc.foo, self.table_abc.bar, self.table_abc.buz)
101
+ .insert(1, "a", True)
102
+ )
103
+
104
+ self.assertEqual('INSERT INTO "abc" ("foo","bar","buz") VALUES (1,\'a\',true)', str(query))
105
+
106
+ def test_insert_empty_columns(self):
107
+ query = Query.into(self.table_abc).columns().insert(1, "a", True)
108
+
109
+ self.assertEqual('INSERT INTO "abc" VALUES (1,\'a\',true)', str(query))
110
+
111
+ def test_insert_selected_columns_type_list(self):
112
+ query = (
113
+ Query.into(self.table_abc)
114
+ .columns([self.table_abc.foo, self.table_abc.bar, self.table_abc.buz])
115
+ .insert(1, "a", True)
116
+ )
117
+
118
+ self.assertEqual('INSERT INTO "abc" ("foo","bar","buz") VALUES (1,\'a\',true)', str(query))
119
+
120
+ def test_insert_empty_columns_type_list(self):
121
+ query = Query.into(self.table_abc).columns([]).insert(1, "a", True)
122
+
123
+ self.assertEqual('INSERT INTO "abc" VALUES (1,\'a\',true)', str(query))
124
+
125
+ def test_insert_selected_columns_type_tuple(self):
126
+ query = (
127
+ Query.into(self.table_abc)
128
+ .columns((self.table_abc.foo, self.table_abc.bar, self.table_abc.buz))
129
+ .insert(1, "a", True)
130
+ )
131
+
132
+ self.assertEqual('INSERT INTO "abc" ("foo","bar","buz") VALUES (1,\'a\',true)', str(query))
133
+
134
+ def test_insert_empty_columns_type_tuple(self):
135
+ query = Query.into(self.table_abc).columns(()).insert(1, "a", True)
136
+
137
+ self.assertEqual('INSERT INTO "abc" VALUES (1,\'a\',true)', str(query))
138
+
139
+ def test_insert_none_skipped(self):
140
+ query = Query.into(self.table_abc).insert()
141
+
142
+ self.assertEqual("", str(query))
143
+
144
+ def test_insert_ignore(self):
145
+ query = Query.into(self.table_abc).insert(1).ignore()
146
+
147
+ self.assertEqual('INSERT IGNORE INTO "abc" VALUES (1)', str(query))
148
+
149
+ def test_insert_null(self):
150
+ query = Query.into(self.table_abc).insert(None)
151
+
152
+ self.assertEqual('INSERT INTO "abc" VALUES (NULL)', str(query))
153
+
154
+ def test_insert_column_using_table_alias(self):
155
+ q = self.table_abc.insert(1)
156
+ self.assertEqual('INSERT INTO "abc" VALUES (1)', str(q))
157
+
158
+ def test_insert_column_using_alias_with_chain(self):
159
+ q = self.table_abc.insert(1, "a", True).insert(2, "b", False)
160
+ self.assertEqual("INSERT INTO \"abc\" VALUES (1,'a',true),(2,'b',false)", str(q))
161
+
162
+ def test_insert_with_statement(self):
163
+ sub_query = Query().select(self.table_abc.id).from_(self.table_abc)
164
+ aliased = AliasedQuery('sub_qs')
165
+
166
+ q = Query().with_(sub_query, 'sub_qs').into(self.table_abc).select(aliased.id).from_(aliased)
167
+ self.assertEqual(
168
+ 'WITH sub_qs AS (SELECT "id" FROM "abc") INSERT INTO "abc" SELECT "sub_qs"."id" FROM sub_qs', str(q)
169
+ )
170
+
171
+
172
+ class PostgresInsertIntoOnConflictTests(unittest.TestCase):
173
+ table_abc = Table("abc")
174
+
175
+ def test_insert_on_conflict_do_nothing_field(self):
176
+ query = PostgreSQLQuery.into(self.table_abc).insert(1).on_conflict(self.table_abc.id).do_nothing()
177
+
178
+ self.assertEqual('INSERT INTO "abc" VALUES (1) ON CONFLICT ("id") DO NOTHING', str(query))
179
+
180
+ def test_insert_on_conflict_do_nothing_multiple_fields(self):
181
+ query = (
182
+ PostgreSQLQuery.into(self.table_abc)
183
+ .insert(1)
184
+ .on_conflict(self.table_abc.id, self.table_abc.sub_id)
185
+ .do_nothing()
186
+ )
187
+
188
+ self.assertEqual('INSERT INTO "abc" VALUES (1) ON CONFLICT ("id", "sub_id") DO NOTHING', str(query))
189
+
190
+ def test_insert_on_conflict_do_nothing_field_str(self):
191
+ query = PostgreSQLQuery.into(self.table_abc).insert(1).on_conflict("id").do_nothing()
192
+
193
+ self.assertEqual('INSERT INTO "abc" VALUES (1) ON CONFLICT ("id") DO NOTHING', str(query))
194
+
195
+ def test_insert_on_conflict_do_nothing_multiple_fields_str(self):
196
+ query = PostgreSQLQuery.into(self.table_abc).insert(1).on_conflict("id", "sub_id").do_nothing()
197
+
198
+ self.assertEqual('INSERT INTO "abc" VALUES (1) ON CONFLICT ("id", "sub_id") DO NOTHING', str(query))
199
+
200
+ def test_insert_on_conflict_do_nothing_mixed_fields(self):
201
+ query = PostgreSQLQuery.into(self.table_abc).insert(1).on_conflict("id", self.table_abc.sub_id).do_nothing()
202
+
203
+ self.assertEqual('INSERT INTO "abc" VALUES (1) ON CONFLICT ("id", "sub_id") DO NOTHING', str(query))
204
+
205
+ def test_insert_on_conflict_do_update_field(self):
206
+ query = (
207
+ PostgreSQLQuery.into(self.table_abc)
208
+ .insert(1, "m")
209
+ .on_conflict(self.table_abc.id)
210
+ .do_update(self.table_abc.name, "m")
211
+ )
212
+
213
+ self.assertEqual(
214
+ "INSERT INTO \"abc\" VALUES (1,'m') ON CONFLICT (\"id\") DO UPDATE SET \"name\"='m'",
215
+ str(query),
216
+ )
217
+
218
+ def test_insert_on_conflict_do_update_multiple_fields(self):
219
+ query = (
220
+ PostgreSQLQuery.into(self.table_abc)
221
+ .insert(1, "m")
222
+ .on_conflict(self.table_abc.id, self.table_abc.sub_id)
223
+ .do_update(self.table_abc.name, "m")
224
+ )
225
+
226
+ self.assertEqual(
227
+ "INSERT INTO \"abc\" VALUES (1,'m') ON CONFLICT (\"id\", \"sub_id\") DO UPDATE SET \"name\"='m'",
228
+ str(query),
229
+ )
230
+
231
+ def test_insert_on_conflict_do_update_field_str(self):
232
+ query = PostgreSQLQuery.into(self.table_abc).insert(1, "m").on_conflict("id").do_update("name", "m")
233
+
234
+ self.assertEqual(
235
+ "INSERT INTO \"abc\" VALUES (1,'m') ON CONFLICT (\"id\") DO UPDATE SET \"name\"='m'",
236
+ str(query),
237
+ )
238
+
239
+ def test_insert_on_conflict_do_update_multiple_fields_str(self):
240
+ query = PostgreSQLQuery.into(self.table_abc).insert(1, "m").on_conflict("id", "sub_id").do_update("name", "m")
241
+
242
+ self.assertEqual(
243
+ "INSERT INTO \"abc\" VALUES (1,'m') ON CONFLICT (\"id\", \"sub_id\") DO UPDATE SET \"name\"='m'",
244
+ str(query),
245
+ )
246
+
247
+ def test_insert_on_conflict_do_update_multiple_mixed_fields(self):
248
+ query = (
249
+ PostgreSQLQuery.into(self.table_abc)
250
+ .insert(1, "m")
251
+ .on_conflict("id", self.table_abc.sub_id)
252
+ .do_update("name", "m")
253
+ )
254
+
255
+ self.assertEqual(
256
+ "INSERT INTO \"abc\" VALUES (1,'m') ON CONFLICT (\"id\", \"sub_id\") DO UPDATE SET \"name\"='m'",
257
+ str(query),
258
+ )
259
+
260
+ def test_insert_on_conflict_no_handler(self):
261
+ with self.assertRaises(QueryException):
262
+ query = str(PostgreSQLQuery.into(self.table_abc).insert(1).on_conflict(self.table_abc.id))
263
+
264
+ def test_insert_on_conflict_two_handlers_do_nothing(self):
265
+ with self.assertRaises(QueryException):
266
+ query = (
267
+ PostgreSQLQuery.into(self.table_abc)
268
+ .insert(1)
269
+ .on_conflict("id")
270
+ .do_nothing()
271
+ .do_update(self.table_abc.name, "m")
272
+ )
273
+
274
+ def test_insert_on_conflict_two_handlers_do_update(self):
275
+ with self.assertRaises(QueryException):
276
+ query = (
277
+ PostgreSQLQuery.into(self.table_abc)
278
+ .insert(1)
279
+ .on_conflict(self.table_abc.id)
280
+ .do_update(self.table_abc.name, "m")
281
+ .do_nothing()
282
+ )
283
+
284
+ def test_non_insert_on_conflict_do_nothing(self):
285
+ with self.assertRaises(QueryException):
286
+ query = PostgreSQLQuery.update(self.table_abc).set("foo", "bar").on_conflict("id").do_nothing()
287
+
288
+ def test_non_insert_on_conflict_do_update(self):
289
+ with self.assertRaises(QueryException):
290
+ query = (
291
+ PostgreSQLQuery.update(self.table_abc).set("foo", "bar").on_conflict("id").do_update(["name"], ["m"])
292
+ )
293
+
294
+ def test_insert_on_fieldless_conflict_do_nothing(self):
295
+ query = PostgreSQLQuery.into(self.table_abc).insert(1).on_conflict(None).do_nothing()
296
+
297
+ self.assertEqual('INSERT INTO "abc" VALUES (1) ON CONFLICT DO NOTHING', str(query))
298
+
299
+ def test_insert_on_fieldless_conflict_do_update_field(self):
300
+ with self.assertRaises(QueryException):
301
+ query = str(
302
+ PostgreSQLQuery.into(self.table_abc)
303
+ .insert(1, "m")
304
+ .on_conflict(None)
305
+ .do_update(self.table_abc.name, "m")
306
+ )
307
+
308
+ def test_on_conflict_from_subquery(self):
309
+ table_bcd = Table('bcd')
310
+ query = (
311
+ PostgreSQLQuery.into(self.table_abc)
312
+ .insert(self.table_abc.fname, self.table_abc.lname)
313
+ .select(table_bcd.fname, table_bcd.lname)
314
+ .from_(table_bcd)
315
+ .on_conflict(self.table_abc.id, self.table_abc.sub_id)
316
+ .do_update(self.table_abc.fname, 1)
317
+ .do_update(self.table_abc.lname, table_bcd.lname)
318
+ .do_update(
319
+ self.table_abc.cname,
320
+ Case().when(self.table_abc.cname.eq('cname'), 'new_name').else_(self.table_abc.cname),
321
+ )
322
+ )
323
+
324
+ self.assertEqual(
325
+ 'INSERT INTO "abc" VALUES ("fname","lname") '
326
+ 'ON CONFLICT ("id", "sub_id") '
327
+ 'DO UPDATE SET "fname"=1,"lname"="bcd"."lname",'
328
+ '"cname"=CASE WHEN "abc"."cname"=\'cname\' THEN \'new_name\' ELSE "abc"."cname" END',
329
+ str(query),
330
+ )
331
+
332
+ def test_on_conflict_where_empty_conflict_fields_do_nothing(self):
333
+ with self.assertRaises(QueryException):
334
+ (
335
+ PostgreSQLQuery.into(self.table_abc)
336
+ .insert(1, "m")
337
+ .on_conflict()
338
+ .where(self.table_abc.abc.eq(0))
339
+ .where(self.table_abc.cde.eq(0))
340
+ .do_nothing()
341
+ )
342
+
343
+ def test_on_conflict_where_conflict_fields_do_nothing(self):
344
+ qs = (
345
+ PostgreSQLQuery.into(self.table_abc)
346
+ .insert(1, "m")
347
+ .on_conflict('id')
348
+ .where(self.table_abc.abc.eq(0))
349
+ .where(self.table_abc.cde.eq(0))
350
+ .do_nothing()
351
+ )
352
+ self.assertEqual(
353
+ '''INSERT INTO "abc" VALUES (1,'m') ON CONFLICT ("id") WHERE "abc"=0 AND "cde"=0 DO NOTHING''', str(qs)
354
+ )
355
+
356
+ def test_on_conflict_where_empty_conflict_fields_do_update(self):
357
+ with self.assertRaises(QueryException):
358
+ (
359
+ PostgreSQLQuery.into(self.table_abc)
360
+ .insert(1, "m")
361
+ .on_conflict()
362
+ .where(self.table_abc.abc.eq(0))
363
+ .where(self.table_abc.cde.eq(0))
364
+ .do_update('field', 'val')
365
+ )
366
+
367
+ def test_on_conflict_where_conflict_fields_do_update(self):
368
+ qs = (
369
+ PostgreSQLQuery.into(self.table_abc)
370
+ .insert(1, "m")
371
+ .on_conflict('id')
372
+ .where(self.table_abc.abc.eq(0))
373
+ .where(self.table_abc.cde.eq(0))
374
+ .do_update('field', 'val')
375
+ )
376
+ self.assertEqual(
377
+ '''INSERT INTO "abc" VALUES (1,'m') ON CONFLICT ("id") WHERE "abc"=0 AND "cde"=0 '''
378
+ '''DO UPDATE SET "field"='val\'''',
379
+ str(qs),
380
+ )
381
+
382
+ def test_where_and_on_conflict_where(self):
383
+ table_bcd = Table('bcd')
384
+
385
+ qs = (
386
+ PostgreSQLQuery.into(self.table_abc)
387
+ .select(table_bcd.abc)
388
+ .from_(table_bcd)
389
+ .where(table_bcd.abc.eq('1'))
390
+ .on_conflict('id')
391
+ .where(self.table_abc.abc.eq(0))
392
+ .where(self.table_abc.cde.eq(0))
393
+ .do_update('field', 'val')
394
+ )
395
+
396
+ self.assertEqual(
397
+ 'INSERT INTO "abc" SELECT "abc" FROM "bcd" WHERE "abc"=\'1\' '
398
+ 'ON CONFLICT ("id") WHERE "abc"=0 AND "cde"=0 DO UPDATE SET "field"=\'val\'',
399
+ str(qs),
400
+ )
401
+
402
+ def test_on_conflict_do_nothing_where(self):
403
+ with self.assertRaises(QueryException):
404
+ (
405
+ PostgreSQLQuery.into(self.table_abc)
406
+ .insert(1, "m")
407
+ .on_conflict()
408
+ .do_nothing()
409
+ .where(self.table_abc.abc.eq(1))
410
+ )
411
+
412
+ def test_empty_on_conflict_do_update_where(self):
413
+ with self.assertRaises(QueryException):
414
+ (
415
+ PostgreSQLQuery.into(self.table_abc)
416
+ .insert(1, "m")
417
+ .on_conflict()
418
+ .do_update('abc', 1)
419
+ .where(self.table_abc.abc.eq(1))
420
+ )
421
+
422
+ def test_on_conflict_do_update_where(self):
423
+ qs = (
424
+ PostgreSQLQuery.into(self.table_abc)
425
+ .insert(1, "m")
426
+ .on_conflict("id")
427
+ .do_update('abc', 1)
428
+ .where(self.table_abc.abc.eq(1))
429
+ )
430
+ self.assertEqual(
431
+ 'INSERT INTO "abc" VALUES (1,\'m\') ON CONFLICT ("id") DO UPDATE SET "abc"=1 WHERE "abc"."abc"=1', str(qs)
432
+ )
433
+
434
+ def test_on_conflict_do_update_with_excluded_where(self):
435
+ qs = (
436
+ PostgreSQLQuery.into(self.table_abc)
437
+ .insert(1, "m")
438
+ .on_conflict("id")
439
+ .do_update('abc')
440
+ .where(self.table_abc.abc.eq(1))
441
+ )
442
+ self.assertEqual(
443
+ 'INSERT INTO "abc" VALUES (1,\'m\') ON CONFLICT ("id") DO UPDATE SET "abc"=EXCLUDED."abc" WHERE '
444
+ '"abc"."abc"=1',
445
+ str(qs),
446
+ )
447
+
448
+ def test_on_conflict_where_complex(self):
449
+ table_bcd = Table('bcd')
450
+
451
+ qs = (
452
+ PostgreSQLQuery.into(self.table_abc)
453
+ .select(table_bcd.abc)
454
+ .from_(table_bcd)
455
+ .where(table_bcd.abc.eq('1'))
456
+ .on_conflict('id')
457
+ .where(self.table_abc.abc.eq(0))
458
+ .where(self.table_abc.cde.eq(0))
459
+ .do_update('field', 'val')
460
+ .where(self.table_abc.id.eq(2))
461
+ .where(self.table_abc.sub_id.eq(3))
462
+ )
463
+ self.assertEqual(
464
+ 'INSERT INTO "abc" SELECT "abc" FROM "bcd" WHERE "abc"=\'1\' '
465
+ 'ON CONFLICT ("id") WHERE "abc"=0 AND "cde"=0 '
466
+ 'DO UPDATE SET "field"=\'val\' WHERE "abc"."id"=2 AND "abc"."sub_id"=3',
467
+ str(qs),
468
+ )
469
+
470
+
471
+ class PostgresInsertIntoReturningTests(unittest.TestCase):
472
+ table_abc = Table("abc")
473
+
474
+ def test_insert_returning_one_field(self):
475
+ query = PostgreSQLQuery.into(self.table_abc).insert(1).returning(self.table_abc.id)
476
+
477
+ self.assertEqual('INSERT INTO "abc" VALUES (1) RETURNING "id"', str(query))
478
+
479
+ def test_insert_returning_one_field_str(self):
480
+ query = PostgreSQLQuery.into(self.table_abc).insert(1).returning("id")
481
+
482
+ self.assertEqual('INSERT INTO "abc" VALUES (1) RETURNING "id"', str(query))
483
+
484
+ def test_insert_returning_all_fields(self):
485
+ query = PostgreSQLQuery.into(self.table_abc).insert(1).returning(self.table_abc.star)
486
+
487
+ self.assertEqual('INSERT INTO "abc" VALUES (1) RETURNING *', str(query))
488
+
489
+ def test_insert_returning_all_fields_and_arithmetics(self):
490
+ query = (
491
+ PostgreSQLQuery.into(self.table_abc)
492
+ .insert(1)
493
+ .returning(self.table_abc.star, self.table_abc.f1 + self.table_abc.f2)
494
+ )
495
+
496
+ self.assertEqual('INSERT INTO "abc" VALUES (1) RETURNING *,"f1"+"f2"', str(query))
497
+
498
+ def test_insert_all_columns_multi_rows_chained_returning_star(self):
499
+ query = (
500
+ PostgreSQLQuery.into(self.table_abc)
501
+ .insert(1, "a", True)
502
+ .insert(2, "b", False)
503
+ .returning(self.table_abc.star)
504
+ )
505
+
506
+ self.assertEqual(
507
+ "INSERT INTO \"abc\" VALUES (1,'a',true),(2,'b',false) RETURNING *",
508
+ str(query),
509
+ )
510
+
511
+ def test_insert_all_columns_multi_rows_chained_returning_star_and_id(self):
512
+ query = (
513
+ PostgreSQLQuery.into(self.table_abc)
514
+ .insert(1, "a", True)
515
+ .insert(2, "b", False)
516
+ .returning(
517
+ self.table_abc.name,
518
+ self.table_abc.star,
519
+ self.table_abc.id,
520
+ )
521
+ )
522
+
523
+ self.assertEqual(
524
+ "INSERT INTO \"abc\" VALUES (1,'a',true),(2,'b',false) RETURNING *",
525
+ str(query),
526
+ )
527
+
528
+ def test_insert_all_columns_multi_rows_chained_returning_star_str(self):
529
+ query = PostgreSQLQuery.into(self.table_abc).insert(1, "a", True).insert(2, "b", False).returning("*")
530
+
531
+ self.assertEqual(
532
+ "INSERT INTO \"abc\" VALUES (1,'a',true),(2,'b',false) RETURNING *",
533
+ str(query),
534
+ )
535
+
536
+ def test_insert_all_columns_single_element_arrays(self):
537
+ query = PostgreSQLQuery.into(self.table_abc).insert((1, "a", True)).returning(self.table_abc.star)
538
+
539
+ self.assertEqual("INSERT INTO \"abc\" VALUES (1,'a',true) RETURNING *", str(query))
540
+
541
+ def test_insert_returning_null(self):
542
+ query = PostgreSQLQuery.into(self.table_abc).insert(1).returning(None)
543
+
544
+ self.assertEqual('INSERT INTO "abc" VALUES (1) RETURNING NULL', str(query))
545
+
546
+ def test_insert_returning_tuple(self):
547
+ query = PostgreSQLQuery.into(self.table_abc).insert(1).returning((1, 2, 3))
548
+
549
+ self.assertEqual('INSERT INTO "abc" VALUES (1) RETURNING (1,2,3)', str(query))
550
+
551
+ def test_insert_returning_arithmetics(self):
552
+ query = PostgreSQLQuery.into(self.table_abc).insert(1).returning(self.table_abc.f1 + self.table_abc.f2)
553
+
554
+ self.assertEqual('INSERT INTO "abc" VALUES (1) RETURNING "f1"+"f2"', str(query))
555
+
556
+ def test_insert_returning_functions(self):
557
+ query = PostgreSQLQuery.into(self.table_abc).insert(1).returning(Cast(self.table_abc.f1, "int"))
558
+
559
+ self.assertEqual('INSERT INTO "abc" VALUES (1) RETURNING CAST("f1" AS INT)', str(query))
560
+
561
+ def test_insert_returning_aggregate(self):
562
+ with self.assertRaises(QueryException):
563
+ PostgreSQLQuery.into(self.table_abc).insert(1).returning(Avg(self.table_abc.views))
564
+
565
+ def test_insert_returning_from_other_table(self):
566
+ table_cba = Table("cba")
567
+ with self.assertRaises(QueryException):
568
+ PostgreSQLQuery.into(self.table_abc).insert(1).returning(table_cba.id)
569
+
570
+
571
+ class InsertIntoOnDuplicateTests(unittest.TestCase):
572
+ table_abc = Table("abc")
573
+
574
+ def test_insert_one_column(self):
575
+ query = (
576
+ MySQLQuery.into(self.table_abc).insert(1).on_duplicate_key_update(self.table_abc.foo, self.table_abc.foo)
577
+ )
578
+ self.assertEqual(
579
+ "INSERT INTO `abc` VALUES (1) ON DUPLICATE KEY UPDATE `foo`=`foo`",
580
+ str(query),
581
+ )
582
+
583
+ def test_insert_one_column_using_values(self):
584
+ query = (
585
+ MySQLQuery.into(self.table_abc)
586
+ .insert(1)
587
+ .on_duplicate_key_update(self.table_abc.foo, Values(self.table_abc.foo))
588
+ )
589
+ self.assertEqual(
590
+ "INSERT INTO `abc` VALUES (1) ON DUPLICATE KEY UPDATE `foo`=VALUES(`foo`)",
591
+ str(query),
592
+ )
593
+
594
+ def test_insert_one_column_single_element_array(self):
595
+ query = (
596
+ MySQLQuery.into(self.table_abc).insert((1,)).on_duplicate_key_update(self.table_abc.foo, self.table_abc.foo)
597
+ )
598
+
599
+ self.assertEqual(
600
+ "INSERT INTO `abc` VALUES (1) ON DUPLICATE KEY UPDATE `foo`=`foo`",
601
+ str(query),
602
+ )
603
+
604
+ def test_insert_one_column_multi_element_array(self):
605
+ query = (
606
+ MySQLQuery.into(self.table_abc)
607
+ .insert((1,), (2,))
608
+ .on_duplicate_key_update(self.table_abc.foo, self.table_abc.foo)
609
+ )
610
+
611
+ self.assertEqual(
612
+ "INSERT INTO `abc` VALUES (1),(2) ON DUPLICATE KEY UPDATE `foo`=`foo`",
613
+ str(query),
614
+ )
615
+
616
+ def test_insert_multiple_columns_on_duplicate_update_one_with_same_value(self):
617
+ query = (
618
+ MySQLQuery.into(self.table_abc)
619
+ .insert(1, "a")
620
+ .on_duplicate_key_update(self.table_abc.bar, Values(self.table_abc.bar))
621
+ )
622
+
623
+ self.assertEqual(
624
+ "INSERT INTO `abc` VALUES (1,'a') ON DUPLICATE KEY UPDATE `bar`=VALUES(`bar`)",
625
+ str(query),
626
+ )
627
+
628
+ def test_insert_multiple_columns_on_duplicate_update_one_with_different_value(self):
629
+ query = MySQLQuery.into(self.table_abc).insert(1, "a").on_duplicate_key_update(self.table_abc.bar, "b")
630
+
631
+ self.assertEqual(
632
+ "INSERT INTO `abc` VALUES (1,'a') ON DUPLICATE KEY UPDATE `bar`='b'",
633
+ str(query),
634
+ )
635
+
636
+ def test_insert_multiple_columns_on_duplicate_update_one_with_expression(self):
637
+ query = (
638
+ MySQLQuery.into(self.table_abc).insert(1, 2).on_duplicate_key_update(self.table_abc.bar, 4 + F("bar"))
639
+ ) # todo sql expression? not python
640
+
641
+ self.assertEqual(
642
+ "INSERT INTO `abc` VALUES (1,2) ON DUPLICATE KEY UPDATE `bar`=4+`bar`",
643
+ str(query),
644
+ )
645
+
646
+ def test_insert_multiple_columns_on_duplicate_update_one_with_expression_using_original_field_value(
647
+ self,
648
+ ):
649
+ query = (
650
+ MySQLQuery.into(self.table_abc)
651
+ .insert(1, "a")
652
+ .on_duplicate_key_update(self.table_abc.bar, fn.Concat(self.table_abc.bar, "update"))
653
+ )
654
+
655
+ self.assertEqual(
656
+ "INSERT INTO `abc` VALUES (1,'a') ON DUPLICATE KEY UPDATE `bar`=CONCAT(`bar`,'update')",
657
+ str(query),
658
+ )
659
+
660
+ def test_insert_multiple_columns_on_duplicate_update_one_with_expression_using_values(
661
+ self,
662
+ ):
663
+ query = (
664
+ MySQLQuery.into(self.table_abc)
665
+ .insert(1, "a")
666
+ .on_duplicate_key_update(self.table_abc.bar, fn.Concat(Values(self.table_abc.bar), "update"))
667
+ )
668
+
669
+ self.assertEqual(
670
+ "INSERT INTO `abc` VALUES (1,'a') ON DUPLICATE KEY UPDATE `bar`=CONCAT(VALUES(`bar`),'update')",
671
+ str(query),
672
+ )
673
+
674
+ def test_insert_multiple_columns_on_duplicate_update_multiple(self):
675
+ query = (
676
+ MySQLQuery.into(self.table_abc)
677
+ .insert(1, "a", "b")
678
+ .on_duplicate_key_update(self.table_abc.bar, "b")
679
+ .on_duplicate_key_update(self.table_abc.baz, "c")
680
+ )
681
+
682
+ self.assertEqual(
683
+ "INSERT INTO `abc` VALUES (1,'a','b') ON DUPLICATE KEY UPDATE `bar`='b',`baz`='c'",
684
+ str(query),
685
+ )
686
+
687
+ def test_insert_multi_rows_chained_mixed_on_duplicate_update_multiple(self):
688
+ query = (
689
+ MySQLQuery.into(self.table_abc)
690
+ .insert((1, "a", True), (2, "b", False))
691
+ .insert(3, "c", True)
692
+ .on_duplicate_key_update(self.table_abc.foo, self.table_abc.foo)
693
+ .on_duplicate_key_update(self.table_abc.bar, Values(self.table_abc.bar))
694
+ )
695
+
696
+ self.assertEqual(
697
+ "INSERT INTO `abc` VALUES (1,'a',true),(2,'b',false),(3,'c',true) "
698
+ "ON DUPLICATE KEY UPDATE `foo`=`foo`,`bar`=VALUES(`bar`)",
699
+ str(query),
700
+ )
701
+
702
+ def test_insert_selected_columns_on_duplicate_update_one(self):
703
+ query = (
704
+ MySQLQuery.into(self.table_abc)
705
+ .columns(self.table_abc.foo, self.table_abc.bar, self.table_abc.baz)
706
+ .insert(1, "a", True)
707
+ .on_duplicate_key_update(self.table_abc.baz, False)
708
+ )
709
+
710
+ self.assertEqual(
711
+ "INSERT INTO `abc` (`foo`,`bar`,`baz`) VALUES (1,'a',true) ON DUPLICATE KEY UPDATE `baz`=false",
712
+ str(query),
713
+ )
714
+
715
+ def test_insert_selected_columns_on_duplicate_update_multiple(self):
716
+ query = (
717
+ MySQLQuery.into(self.table_abc)
718
+ .columns(self.table_abc.foo, self.table_abc.bar, self.table_abc.baz)
719
+ .insert(1, "a", True)
720
+ .on_duplicate_key_update(self.table_abc.baz, False)
721
+ .on_duplicate_key_update(self.table_abc.bar, Values(self.table_abc.bar))
722
+ )
723
+
724
+ self.assertEqual(
725
+ "INSERT INTO `abc` (`foo`,`bar`,`baz`) VALUES (1,'a',true) "
726
+ "ON DUPLICATE KEY UPDATE `baz`=false,`bar`=VALUES(`bar`)",
727
+ str(query),
728
+ )
729
+
730
+ def test_insert_none_skipped(self):
731
+ query = MySQLQuery.into(self.table_abc).insert().on_duplicate_key_update(self.table_abc.baz, False)
732
+
733
+ self.assertEqual("", str(query))
734
+
735
+ def test_insert_ignore(self):
736
+ query = MySQLQuery.into(self.table_abc).insert(1).ignore().on_duplicate_key_update(self.table_abc.baz, False)
737
+
738
+ self.assertEqual(
739
+ "INSERT IGNORE INTO `abc` VALUES (1) ON DUPLICATE KEY UPDATE `baz`=false",
740
+ str(query),
741
+ )
742
+
743
+
744
+ class InsertSelectFromTests(unittest.TestCase):
745
+ table_abc, table_efg, table_hij = Tables("abc", "efg", "hij")
746
+
747
+ def test_insert_star(self):
748
+ query = Query.into(self.table_abc).from_(self.table_efg).select("*")
749
+
750
+ self.assertEqual('INSERT INTO "abc" SELECT * FROM "efg"', str(query))
751
+
752
+ def test_insert_ignore_star(self):
753
+ query = Query.into(self.table_abc).from_(self.table_efg).select("*").ignore()
754
+
755
+ self.assertEqual('INSERT IGNORE INTO "abc" SELECT * FROM "efg"', str(query))
756
+
757
+ def test_insert_from_columns(self):
758
+ query = (
759
+ Query.into(self.table_abc)
760
+ .from_(self.table_efg)
761
+ .select(self.table_efg.fiz, self.table_efg.buz, self.table_efg.baz)
762
+ )
763
+
764
+ self.assertEqual('INSERT INTO "abc" ' 'SELECT "fiz","buz","baz" FROM "efg"', str(query))
765
+
766
+ def test_insert_columns_from_star(self):
767
+ query = (
768
+ Query.into(self.table_abc)
769
+ .columns(
770
+ self.table_abc.foo,
771
+ self.table_abc.bar,
772
+ self.table_abc.buz,
773
+ )
774
+ .from_(self.table_efg)
775
+ .select("*")
776
+ )
777
+
778
+ self.assertEqual('INSERT INTO "abc" ("foo","bar","buz") ' 'SELECT * FROM "efg"', str(query))
779
+
780
+ def test_insert_columns_from_columns(self):
781
+ query = (
782
+ Query.into(self.table_abc)
783
+ .columns(self.table_abc.foo, self.table_abc.bar, self.table_abc.buz)
784
+ .from_(self.table_efg)
785
+ .select(self.table_efg.fiz, self.table_efg.buz, self.table_efg.baz)
786
+ )
787
+
788
+ self.assertEqual(
789
+ 'INSERT INTO "abc" ("foo","bar","buz") ' 'SELECT "fiz","buz","baz" FROM "efg"',
790
+ str(query),
791
+ )
792
+
793
+ def test_insert_columns_from_columns_with_join(self):
794
+ query = (
795
+ Query.into(self.table_abc)
796
+ .columns(
797
+ self.table_abc.c1,
798
+ self.table_abc.c2,
799
+ self.table_abc.c3,
800
+ self.table_abc.c4,
801
+ )
802
+ .from_(self.table_efg)
803
+ .select(self.table_efg.foo, self.table_efg.bar)
804
+ .join(self.table_hij)
805
+ .on(self.table_efg.id == self.table_hij.abc_id)
806
+ .select(self.table_hij.fiz, self.table_hij.buz)
807
+ )
808
+
809
+ self.assertEqual(
810
+ 'INSERT INTO "abc" ("c1","c2","c3","c4") '
811
+ 'SELECT "efg"."foo","efg"."bar","hij"."fiz","hij"."buz" FROM "efg" '
812
+ 'JOIN "hij" ON "efg"."id"="hij"."abc_id"',
813
+ str(query),
814
+ )
815
+
816
+
817
+ class InsertSubqueryTests(unittest.TestCase):
818
+ def test_insert_subquery_wrapped_in_brackets(self):
819
+ purchase_order_item, part = Tables("purchase_order_item", "part")
820
+
821
+ q = (
822
+ Query.into(purchase_order_item)
823
+ .columns(purchase_order_item.id_part, purchase_order_item.id_customer)
824
+ .insert(
825
+ Query.from_(part).select(part.part_id).where(part.part_number == "FOOBAR"),
826
+ 12345,
827
+ )
828
+ )
829
+
830
+ self.assertEqual(
831
+ 'INSERT INTO "purchase_order_item" '
832
+ '("id_part","id_customer") '
833
+ "VALUES "
834
+ '((SELECT "part_id" FROM "part" WHERE "part_number"=\'FOOBAR\'),12345)',
835
+ str(q),
836
+ )
837
+
838
+
839
+ class SelectIntoTests(unittest.TestCase):
840
+ table_abc, table_efg, table_hij = Tables("abc", "efg", "hij")
841
+
842
+ def test_select_star_into(self):
843
+ query = Query.from_(self.table_abc).select("*").into(self.table_efg)
844
+
845
+ self.assertEqual('SELECT * INTO "efg" FROM "abc"', str(query))
846
+
847
+ def test_select_columns_into(self):
848
+ query = (
849
+ Query.from_(self.table_abc)
850
+ .select(self.table_abc.foo, self.table_abc.bar, self.table_abc.buz)
851
+ .into(self.table_efg)
852
+ )
853
+
854
+ self.assertEqual('SELECT "foo","bar","buz" INTO "efg" FROM "abc"', str(query))
855
+
856
+ def test_select_columns_into_with_join(self):
857
+ query = (
858
+ Query.from_(self.table_abc)
859
+ .select(self.table_abc.foo, self.table_abc.bar)
860
+ .join(self.table_hij)
861
+ .on(self.table_abc.id == self.table_hij.abc_id)
862
+ .select(self.table_hij.fiz, self.table_hij.buz)
863
+ .into(self.table_efg)
864
+ )
865
+
866
+ self.assertEqual(
867
+ 'SELECT "abc"."foo","abc"."bar","hij"."fiz","hij"."buz" '
868
+ 'INTO "efg" FROM "abc" '
869
+ 'JOIN "hij" ON "abc"."id"="hij"."abc_id"',
870
+ str(query),
871
+ )
872
+
873
+
874
+ class ReplaceTests(unittest.TestCase):
875
+ table_abc, table_def = Tables("abc", "efg")
876
+
877
+ def test_replace_simple(self):
878
+ query = Query.into(self.table_abc).replace("v1", "v2", "v3")
879
+ expected_output = "REPLACE INTO \"abc\" VALUES ('v1','v2','v3')"
880
+ self.assertEqual(str(query), expected_output)
881
+
882
+ def test_replace_subquery(self):
883
+ query = Query.into(self.table_abc).replace(Query.from_(self.table_def).select("f1", "f2"))
884
+ expected_output = 'REPLACE INTO "abc" VALUES ((SELECT "f1","f2" FROM "efg"))'
885
+ self.assertEqual(str(query), expected_output)
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_internals.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from pypika import (
4
+ Case,
5
+ Field,
6
+ Table,
7
+ )
8
+ from pypika.terms import Star
9
+
10
+
11
+ class TablesTests(unittest.TestCase):
12
+ def test__criterion_replace_table_with_value(self):
13
+ table = Table("a")
14
+
15
+ c = (Field("foo") > 1).replace_table(None, table)
16
+ self.assertEqual(c.left, table)
17
+ self.assertEqual(c.tables_, {table})
18
+
19
+ def test__case_tables(self):
20
+ table = Table("a")
21
+
22
+ c = Case().when(table.a == 1, 2 * table.a)
23
+ self.assertIsInstance(c.tables_, set)
24
+ self.assertSetEqual(c.tables_, {table})
25
+
26
+ def test__star_tables(self):
27
+ star = Star()
28
+
29
+ self.assertEqual(star.tables_, set())
30
+
31
+ def test__table_star_tables(self):
32
+ table = Table("a")
33
+ star = Star(table=table)
34
+
35
+ self.assertEqual(star.tables_, {table})
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_joins.py ADDED
@@ -0,0 +1,1168 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from pypika import (
4
+ Field,
5
+ Interval,
6
+ JoinException,
7
+ JoinType,
8
+ MySQLQuery,
9
+ Query,
10
+ SetOperationException,
11
+ Table,
12
+ Tables,
13
+ functions as fn,
14
+ SYSTEM_TIME,
15
+ )
16
+
17
+ __author__ = "Timothy Heys"
18
+ __email__ = "theys@kayak.com"
19
+
20
+
21
+ class SelectQueryJoinTests(unittest.TestCase):
22
+ table0, table1, hij = Tables("abc", "efg", "hij")
23
+
24
+ def test_default_join_type(self):
25
+ query = Query.from_(self.table0).join(self.table1).on(self.table0.foo == self.table1.bar).select("*")
26
+
27
+ self.assertEqual('SELECT * FROM "abc" JOIN "efg" ON "abc"."foo"="efg"."bar"', str(query))
28
+
29
+ def test_inner_join(self):
30
+ expected = 'SELECT * FROM "abc" JOIN "efg" ON "abc"."foo"="efg"."bar"'
31
+
32
+ with self.subTest("join with enum"):
33
+ query = (
34
+ Query.from_(self.table0)
35
+ .join(self.table1, how=JoinType.inner)
36
+ .on(self.table0.foo == self.table1.bar)
37
+ .select("*")
38
+ )
39
+ self.assertEqual(expected, str(query))
40
+
41
+ with self.subTest("join function"):
42
+ query = Query.from_(self.table0).inner_join(self.table1).on(self.table0.foo == self.table1.bar).select("*")
43
+ self.assertEqual(expected, str(query))
44
+
45
+ def test_left_join(self):
46
+ expected = 'SELECT * FROM "abc" LEFT JOIN "efg" ON "abc"."foo"="efg"."bar"'
47
+
48
+ with self.subTest("join with enum"):
49
+ query = (
50
+ Query.from_(self.table0)
51
+ .join(self.table1, how=JoinType.left)
52
+ .on(self.table0.foo == self.table1.bar)
53
+ .select("*")
54
+ )
55
+
56
+ self.assertEqual(expected, str(query))
57
+
58
+ with self.subTest("join function"):
59
+ query = Query.from_(self.table0).left_join(self.table1).on(self.table0.foo == self.table1.bar).select("*")
60
+ self.assertEqual(expected, str(query))
61
+
62
+ def test_right_join(self):
63
+ expected = 'SELECT * FROM "abc" RIGHT JOIN "efg" ON "abc"."foo"="efg"."bar"'
64
+
65
+ with self.subTest("join with enum"):
66
+ query = (
67
+ Query.from_(self.table0)
68
+ .join(self.table1, how=JoinType.right)
69
+ .on(self.table0.foo == self.table1.bar)
70
+ .select("*")
71
+ )
72
+
73
+ self.assertEqual(expected, str(query))
74
+
75
+ with self.subTest("join function"):
76
+ query = Query.from_(self.table0).right_join(self.table1).on(self.table0.foo == self.table1.bar).select("*")
77
+ self.assertEqual(expected, str(query))
78
+
79
+ def test_hash_join(self):
80
+ expected = 'SELECT * FROM "abc" HASH JOIN "efg" ON "abc"."foo"="efg"."bar"'
81
+
82
+ with self.subTest("join with enum"):
83
+ query = (
84
+ Query.from_(self.table0)
85
+ .join(self.table1, how=JoinType.hash)
86
+ .on(self.table0.foo == self.table1.bar)
87
+ .select("*")
88
+ )
89
+
90
+ self.assertEqual(expected, str(query))
91
+
92
+ with self.subTest("join function"):
93
+ query = Query.from_(self.table0).hash_join(self.table1).on(self.table0.foo == self.table1.bar).select("*")
94
+ self.assertEqual(expected, str(query))
95
+
96
+ def test_outer_join(self):
97
+ expected = 'SELECT * FROM "abc" FULL OUTER JOIN "efg" ON "abc"."foo"="efg"."bar"'
98
+
99
+ with self.subTest("join with enum"):
100
+ query = (
101
+ Query.from_(self.table0)
102
+ .join(self.table1, how=JoinType.outer)
103
+ .on(self.table0.foo == self.table1.bar)
104
+ .select("*")
105
+ )
106
+
107
+ self.assertEqual(expected, str(query))
108
+
109
+ with self.subTest("join function"):
110
+ query = Query.from_(self.table0).outer_join(self.table1).on(self.table0.foo == self.table1.bar).select("*")
111
+ self.assertEqual(expected, str(query))
112
+
113
+ def test_cross_join(self):
114
+ expected = 'SELECT * FROM "abc" CROSS JOIN "efg" ON "abc"."foo"="efg"."bar"'
115
+
116
+ with self.subTest("join with enum"):
117
+ query = (
118
+ Query.from_(self.table0)
119
+ .join(self.table1, how=JoinType.cross)
120
+ .on(self.table0.foo == self.table1.bar)
121
+ .select("*")
122
+ )
123
+
124
+ self.assertEqual(expected, str(query))
125
+
126
+ with self.subTest("join function"):
127
+ query = Query.from_(self.table0).cross_join(self.table1).on(self.table0.foo == self.table1.bar).select("*")
128
+ self.assertEqual(expected, str(query))
129
+
130
+ def test_left_outer_join(self):
131
+ expected = 'SELECT * FROM "abc" LEFT OUTER JOIN "efg" ON "abc"."foo"="efg"."bar"'
132
+ with self.subTest("join with enum"):
133
+ query = (
134
+ Query.from_(self.table0)
135
+ .join(self.table1, how=JoinType.left_outer)
136
+ .on(self.table0.foo == self.table1.bar)
137
+ .select("*")
138
+ )
139
+
140
+ self.assertEqual(expected, str(query))
141
+
142
+ with self.subTest("join function"):
143
+ query = (
144
+ Query.from_(self.table0).left_outer_join(self.table1).on(self.table0.foo == self.table1.bar).select("*")
145
+ )
146
+
147
+ self.assertEqual(expected, str(query))
148
+
149
+ def test_right_outer_join(self):
150
+ expected = 'SELECT * FROM "abc" RIGHT OUTER JOIN "efg" ON "abc"."foo"="efg"."bar"'
151
+ with self.subTest("join with enum"):
152
+ query = (
153
+ Query.from_(self.table0)
154
+ .join(self.table1, how=JoinType.right_outer)
155
+ .on(self.table0.foo == self.table1.bar)
156
+ .select("*")
157
+ )
158
+
159
+ self.assertEqual(expected, str(query))
160
+
161
+ with self.subTest("join function"):
162
+ query = (
163
+ Query.from_(self.table0)
164
+ .right_outer_join(self.table1)
165
+ .on(self.table0.foo == self.table1.bar)
166
+ .select("*")
167
+ )
168
+
169
+ self.assertEqual(expected, str(query))
170
+
171
+ def test_full_outer_join(self):
172
+ expected = 'SELECT * FROM "abc" FULL OUTER JOIN "efg" ON "abc"."foo"="efg"."bar"'
173
+
174
+ with self.subTest("join with enum"):
175
+ query = (
176
+ Query.from_(self.table0)
177
+ .join(self.table1, how=JoinType.full_outer)
178
+ .on(self.table0.foo == self.table1.bar)
179
+ .select("*")
180
+ )
181
+
182
+ self.assertEqual(expected, str(query))
183
+
184
+ with self.subTest("join function"):
185
+ query = (
186
+ Query.from_(self.table0).full_outer_join(self.table1).on(self.table0.foo == self.table1.bar).select("*")
187
+ )
188
+
189
+ self.assertEqual(expected, str(query))
190
+
191
+ def test_join_on_field_single(self):
192
+ query = Query.from_(self.table0).join(self.table1).on_field("foo").select("*")
193
+ self.assertEqual('SELECT * FROM "abc" JOIN "efg" ON "abc"."foo"="efg"."foo"', str(query))
194
+
195
+ def test_join_on_field_multi(self):
196
+ query = Query.from_(self.table0).join(self.table1).on_field("foo", "bar").select("*")
197
+ self.assertEqual(
198
+ 'SELECT * FROM "abc" JOIN "efg" ON "abc"."foo"="efg"."foo" ' 'AND "abc"."bar"="efg"."bar"',
199
+ str(query),
200
+ )
201
+
202
+ def test_join_on_field_multi_with_extra_join(self):
203
+ query = (
204
+ Query.from_(self.table0)
205
+ .join(self.hij)
206
+ .on_field("buzz")
207
+ .join(self.table1)
208
+ .on_field("foo", "bar")
209
+ .select("*")
210
+ )
211
+
212
+ self.assertEqual(
213
+ 'SELECT * FROM "abc" JOIN "hij" ON "abc"."buzz"="hij"."buzz" '
214
+ 'JOIN "efg" ON "abc"."foo"="efg"."foo" AND "abc"."bar"="efg"."bar"',
215
+ str(query),
216
+ )
217
+
218
+ def test_join_using_string_field_name(self):
219
+ query = Query.from_(self.table0).join(self.table1).using("id").select("*")
220
+
221
+ self.assertEqual('SELECT * FROM "abc" JOIN "efg" USING ("id")', str(query))
222
+
223
+ def test_join_using_multiple_fields(self):
224
+ query = Query.from_(self.table0).join(self.table1).using("foo", "bar").select("*")
225
+
226
+ self.assertEqual('SELECT * FROM "abc" JOIN "efg" USING ("foo","bar")', str(query))
227
+
228
+ def test_join_using_with_quote_char(self):
229
+ query = Query.from_(self.table0).join(self.table1).using("foo", "bar").select("*")
230
+
231
+ self.assertEqual("SELECT * FROM abc JOIN efg USING (foo,bar)", query.get_sql(quote_char=""))
232
+
233
+ def test_join_using_without_fields_raises_exception(self):
234
+ with self.assertRaises(JoinException):
235
+ Query.from_(self.table0).join(self.table1).using()
236
+
237
+ def test_join_on_field_without_fields_raises_exception(self):
238
+ with self.assertRaises(JoinException):
239
+ Query.from_(self.table0).join(self.table1).on_field()
240
+
241
+ def test_join_arithmetic_field(self):
242
+ q = (
243
+ Query.from_(self.table0)
244
+ .join(self.table1)
245
+ .on(self.table0.dt == (self.table1.dt - Interval(weeks=1)))
246
+ .select("*")
247
+ )
248
+
249
+ self.assertEqual(
250
+ 'SELECT * FROM "abc" ' 'JOIN "efg" ON "abc"."dt"="efg"."dt"-INTERVAL \'1 WEEK\'',
251
+ str(q),
252
+ )
253
+
254
+ def test_join_with_arithmetic_function_in_select(self):
255
+ q = (
256
+ Query.from_(
257
+ self.table0,
258
+ )
259
+ .join(self.table1)
260
+ .on(self.table0.dt == (self.table1.dt - Interval(weeks=1)))
261
+ .select(self.table0.fiz - self.table0.buz, self.table1.star)
262
+ )
263
+
264
+ self.assertEqual(
265
+ 'SELECT "abc"."fiz"-"abc"."buz","efg".* FROM "abc" '
266
+ 'JOIN "efg" ON "abc"."dt"="efg"."dt"-INTERVAL \'1 WEEK\'',
267
+ str(q),
268
+ )
269
+
270
+ def test_join_on_complex_criteria(self):
271
+ q = (
272
+ Query.from_(self.table0)
273
+ .join(self.table1, how=JoinType.right)
274
+ .on((self.table0.foo == self.table1.fiz) & (self.table0.bar == self.table1.buz))
275
+ .select("*")
276
+ )
277
+
278
+ self.assertEqual(
279
+ 'SELECT * FROM "abc" ' 'RIGHT JOIN "efg" ON "abc"."foo"="efg"."fiz" AND "abc"."bar"="efg"."buz"',
280
+ str(q),
281
+ )
282
+
283
+ def test_join_on_subquery_criteria(self):
284
+ table_a, table_b, table_c = Tables("a", "b", "c")
285
+ subquery = Query.from_(table_c).select("id").limit(1)
286
+ query = (
287
+ Query.from_(table_a).select("*").join(table_b).on((table_a.b_id == table_b.id) & (table_b.c_id == subquery))
288
+ )
289
+
290
+ self.assertEqual(
291
+ "SELECT * " 'FROM "a" ' 'JOIN "b" ON "a"."b_id"="b"."id" AND "b"."c_id"=' '(SELECT "id" FROM "c" LIMIT 1)',
292
+ str(query),
293
+ )
294
+
295
+ def test_use_different_table_objects_for_same_table(self):
296
+ table = Table("t")
297
+ q = Query.from_(table).select("*").where(Field("id", table=table) == 1)
298
+
299
+ self.assertEqual('SELECT * FROM "t" WHERE "id"=1', str(q))
300
+
301
+ def test_join_second_table_in_from_clause(self):
302
+ table_a, table_b, table_c = Tables("a", "b", "c")
303
+ q = Query.from_(table_a).from_(table_b).select("*").join(table_c).on(table_b.c_id == table_c.id)
304
+
305
+ self.assertEqual("SELECT * " 'FROM "a","b" ' 'JOIN "c" ON "b"."c_id"="c"."id"', str(q))
306
+
307
+ def test_cross_join_on_table(self):
308
+ table_a, table_b = Tables("a", "b")
309
+ q = Query.from_(table_a).join(table_b).cross().select("*")
310
+
311
+ self.assertEqual('SELECT * FROM "a" CROSS JOIN "b"', str(q))
312
+
313
+ def test_cross_join_on_subquery(self):
314
+ table_a, table_b = Tables("a", "b")
315
+ q_a = Query.from_(table_a).select("*")
316
+ q_b = Query.from_(table_b).select("*").join(q_a).cross().select("*")
317
+
318
+ self.assertEqual('SELECT * FROM "b" CROSS JOIN (SELECT * FROM "a") "sq0"', str(q_b))
319
+
320
+ def test_join_on_collate(self):
321
+ table_a, table_b = Tables("a", "b")
322
+
323
+ q1 = (
324
+ Query.from_(table_a)
325
+ .select(table_b.ouch)
326
+ .join(table_b)
327
+ .on(table_a.foo == table_b.boo, collate="utf8_general_ci")
328
+ )
329
+ q2 = Query.from_(table_a).select(table_b.ouch).join(table_b).on(table_a.foo == table_b.boo)
330
+
331
+ self.assertEqual(
332
+ 'SELECT "b"."ouch" FROM "a" JOIN "b" ON "a"."foo"="b"."boo" COLLATE utf8_general_ci',
333
+ str(q1),
334
+ )
335
+ self.assertEqual('SELECT "b"."ouch" FROM "a" JOIN "b" ON "a"."foo"="b"."boo"', str(q2))
336
+
337
+ def test_temporal_join(self):
338
+ t0 = self.table0.for_(SYSTEM_TIME.as_of('2020-01-01'))
339
+ t1 = self.table1.for_(SYSTEM_TIME.as_of('2020-01-01'))
340
+ query = Query.from_(t0).join(t1).on(t0.foo == t1.bar).select("*")
341
+
342
+ self.assertEqual(
343
+ 'SELECT * FROM "abc" FOR SYSTEM_TIME AS OF \'2020-01-01\' '
344
+ 'JOIN "efg" FOR SYSTEM_TIME AS OF \'2020-01-01\' '
345
+ 'ON "abc"."foo"="efg"."bar"',
346
+ str(query),
347
+ )
348
+
349
+
350
+ class JoinBehaviorTests(unittest.TestCase):
351
+ table_abc, table_efg, table_hij, table_klm = Tables("abc", "efg", "hij", "klm")
352
+
353
+ def test_select__ordered_select_clauses(self):
354
+ q = (
355
+ Query.from_(self.table_abc)
356
+ .join(self.table_efg)
357
+ .on(self.table_abc.foo == self.table_efg.bar)
358
+ .select(
359
+ self.table_abc.baz,
360
+ self.table_efg.buz,
361
+ self.table_abc.fiz,
362
+ self.table_efg.bam,
363
+ )
364
+ )
365
+
366
+ self.assertEqual(
367
+ 'SELECT "abc"."baz","efg"."buz","abc"."fiz","efg"."bam" FROM "abc" '
368
+ 'JOIN "efg" ON "abc"."foo"="efg"."bar"',
369
+ str(q),
370
+ )
371
+
372
+ def test_select__star_for_table(self):
373
+ q = (
374
+ Query.from_(self.table_abc)
375
+ .join(self.table_efg)
376
+ .on(self.table_abc.foo == self.table_efg.bar)
377
+ .join(self.table_hij)
378
+ .on(self.table_abc.buz == self.table_hij.bam)
379
+ .select(self.table_abc.star)
380
+ .select(self.table_efg.star)
381
+ .select(self.table_hij.star)
382
+ )
383
+
384
+ self.assertEqual(
385
+ 'SELECT "abc".*,"efg".*,"hij".* FROM "abc" '
386
+ 'JOIN "efg" ON "abc"."foo"="efg"."bar" '
387
+ 'JOIN "hij" ON "abc"."buz"="hij"."bam"',
388
+ str(q),
389
+ )
390
+
391
+ def test_select__star_for_table__replacement(self):
392
+ q = (
393
+ Query.from_(self.table_abc)
394
+ .join(self.table_efg)
395
+ .on(self.table_abc.foo == self.table_efg.bar)
396
+ .join(self.table_hij)
397
+ .on(self.table_abc.buz == self.table_hij.bam)
398
+ .select(self.table_abc.foo, self.table_efg.bar, self.table_hij.bam)
399
+ .select(self.table_abc.star, self.table_efg.star, self.table_hij.star)
400
+ )
401
+
402
+ self.assertEqual(
403
+ 'SELECT "abc".*,"efg".*,"hij".* FROM "abc" '
404
+ 'JOIN "efg" ON "abc"."foo"="efg"."bar" '
405
+ 'JOIN "hij" ON "abc"."buz"="hij"."bam"',
406
+ str(q),
407
+ )
408
+
409
+ def test_select_fields_with_where(self):
410
+ q = (
411
+ Query.from_(self.table_abc)
412
+ .join(self.table_efg)
413
+ .on(self.table_abc.foo == self.table_efg.bar)
414
+ .join(self.table_hij)
415
+ .on(self.table_abc.buz == self.table_hij.bam)
416
+ .select(self.table_abc.foo, self.table_efg.bar, self.table_hij.bam)
417
+ .where(self.table_abc.foo > 1)
418
+ .where(self.table_efg.bar != 2)
419
+ )
420
+
421
+ self.assertEqual(
422
+ 'SELECT "abc"."foo","efg"."bar","hij"."bam" FROM "abc" '
423
+ 'JOIN "efg" ON "abc"."foo"="efg"."bar" '
424
+ 'JOIN "hij" ON "abc"."buz"="hij"."bam" '
425
+ 'WHERE "abc"."foo">1 AND "efg"."bar"<>2',
426
+ str(q),
427
+ )
428
+
429
+ def test_require_condition(self):
430
+ with self.assertRaises(JoinException):
431
+ Query.from_(self.table_abc).join(self.table_efg).on(None)
432
+
433
+ def test_require_condition_with_both_tables(self):
434
+ with self.assertRaises(JoinException):
435
+ Query.from_(self.table_abc).join(self.table_efg).on(self.table_abc.foo == self.table_hij.bar)
436
+
437
+ with self.assertRaises(JoinException):
438
+ Query.from_(self.table_abc).join(self.table_efg).on(self.table_hij.foo == self.table_efg.bar)
439
+
440
+ with self.assertRaises(JoinException):
441
+ Query.from_(self.table_abc).join(self.table_efg).on(self.table_hij.foo == self.table_klm.bar)
442
+
443
+ def test_join_same_table(self):
444
+ table1 = Table("abc")
445
+ table2 = Table("abc")
446
+ q = Query.from_(table1).join(table2).on(table1.foo == table2.bar).select(table1.foo, table2.buz)
447
+
448
+ self.assertEqual(
449
+ 'SELECT "abc"."foo","abc2"."buz" FROM "abc" ' 'JOIN "abc" "abc2" ON "abc"."foo"="abc2"."bar"',
450
+ str(q),
451
+ )
452
+
453
+ def test_join_same_table_with_prefixes(self):
454
+ table1 = Table("abc").as_("x")
455
+ table2 = Table("abc").as_("y")
456
+ q = Query.from_(table1).join(table2).on(table1.foo == table2.bar).select(table1.foo, table2.buz)
457
+
458
+ self.assertEqual(
459
+ 'SELECT "x"."foo","y"."buz" FROM "abc" "x" ' 'JOIN "abc" "y" ON "x"."foo"="y"."bar"',
460
+ str(q),
461
+ )
462
+
463
+ def test_join_table_twice(self):
464
+ table1, table2 = Table("efg").as_("efg1"), Table("efg").as_("efg2")
465
+ q = (
466
+ Query.from_(self.table_abc)
467
+ .join(table1)
468
+ .on(self.table_abc.foo == table1.bar)
469
+ .join(table2)
470
+ .on(self.table_abc.foo == table2.bam)
471
+ .select(self.table_abc.foo, table1.fiz, table2.buz)
472
+ )
473
+
474
+ self.assertEqual(
475
+ 'SELECT "abc"."foo","efg1"."fiz","efg2"."buz" FROM "abc" '
476
+ 'JOIN "efg" "efg1" ON "abc"."foo"="efg1"."bar" '
477
+ 'JOIN "efg" "efg2" ON "abc"."foo"="efg2"."bam"',
478
+ str(q),
479
+ )
480
+
481
+ def test_select__fields_after_table_star(self):
482
+ q = (
483
+ Query.from_(self.table_abc)
484
+ .join(self.table_efg)
485
+ .on(self.table_abc.foo == self.table_efg.bar)
486
+ .select(self.table_abc.star, self.table_efg.bar)
487
+ .select(self.table_abc.foo)
488
+ )
489
+
490
+ self.assertEqual(
491
+ 'SELECT "abc".*,"efg"."bar" FROM "abc" JOIN "efg" ON "abc"."foo"="efg"."bar"',
492
+ str(q),
493
+ )
494
+
495
+ def test_fail_when_joining_unknown_type(self):
496
+ with self.assertRaises(ValueError):
497
+ Query.from_(self.table_abc).join("this is a string")
498
+
499
+ def test_immutable__tables(self):
500
+ query1 = Query.from_(self.table_abc).select(self.table_abc.foo)
501
+ query2 = (
502
+ Query.from_(self.table_abc)
503
+ .join(self.table_efg)
504
+ .on(self.table_abc.foo == self.table_efg.bar)
505
+ .select(self.table_abc.foo, self.table_efg.buz)
506
+ )
507
+
508
+ self.assertEqual(
509
+ 'SELECT "abc"."foo","efg"."buz" FROM "abc" ' 'JOIN "efg" ON "abc"."foo"="efg"."bar"',
510
+ str(query2),
511
+ )
512
+ self.assertEqual('SELECT "foo" FROM "abc"', str(query1))
513
+
514
+ def test_select_field_from_missing_table(self):
515
+ """
516
+ Previously we validated the select field was a member of a table
517
+ in the query (e.g. FROM clause or a JOIN). However this made
518
+ correlated subqueries awkward to write (subqueries which refer
519
+ to their parent table. So the validation was removed.
520
+
521
+ None of the following raise an error. Previously raised JoinException
522
+ """
523
+ Query.from_(self.table_abc).select(self.table_efg.foo)
524
+
525
+ Query.from_(self.table_abc).where(self.table_efg.foo == 0)
526
+
527
+ Query.from_(self.table_abc).where(fn.Sum(self.table_efg.foo) == 0)
528
+
529
+ Query.from_(self.table_abc).select(fn.Sum(self.table_abc.bar * 2) + fn.Sum(self.table_efg.foo * 2))
530
+
531
+ Query.from_(self.table_abc).groupby(self.table_efg.foo)
532
+
533
+ Query.from_(self.table_abc).groupby(self.table_abc.foo).having(self.table_efg.bar)
534
+
535
+ subquery = (
536
+ Query.from_(self.table_efg).select(self.table_efg.id).where(self.table_efg.abc_id == self.table_abc.id)
537
+ )
538
+ query = Query.from_(self.table_abc).select(subquery.as_("efg_id").limit(1))
539
+ self.assertEqual(
540
+ 'SELECT (SELECT "efg"."id" FROM "efg" WHERE "efg"."abc_id"="abc"."id" LIMIT 1) "efg_id" FROM "abc"',
541
+ str(query),
542
+ )
543
+
544
+ def test_ignore_table_references(self):
545
+ query = Query.from_(Table("abc")).select(Table("abc").foo)
546
+
547
+ self.assertEqual('SELECT "foo" FROM "abc"', str(query))
548
+
549
+ def test_prefixes_added_to_groupby(self):
550
+ test_query = (
551
+ Query.from_(self.table_abc)
552
+ .join(self.table_efg)
553
+ .on(self.table_abc.foo == self.table_efg.bar)
554
+ .select(self.table_abc.foo, fn.Sum(self.table_efg.buz))
555
+ .groupby(self.table_abc.foo)
556
+ )
557
+
558
+ self.assertEqual(
559
+ 'SELECT "abc"."foo",SUM("efg"."buz") FROM "abc" '
560
+ 'JOIN "efg" ON "abc"."foo"="efg"."bar" '
561
+ 'GROUP BY "abc"."foo"',
562
+ str(test_query),
563
+ )
564
+
565
+ def test_prefixes_added_to_orderby(self):
566
+ test_query = (
567
+ Query.from_(self.table_abc)
568
+ .join(self.table_efg)
569
+ .on(self.table_abc.foo == self.table_efg.bar)
570
+ .select(self.table_abc.foo, self.table_efg.buz)
571
+ .orderby(self.table_abc.foo)
572
+ )
573
+
574
+ self.assertEqual(
575
+ 'SELECT "abc"."foo","efg"."buz" FROM "abc" '
576
+ 'JOIN "efg" ON "abc"."foo"="efg"."bar" '
577
+ 'ORDER BY "abc"."foo"',
578
+ str(test_query),
579
+ )
580
+
581
+ def test_prefixes_added_to_function_in_orderby(self):
582
+ test_query = (
583
+ Query.from_(self.table_abc)
584
+ .join(self.table_efg)
585
+ .on(self.table_abc.foo == self.table_efg.bar)
586
+ .select(self.table_abc.foo, self.table_efg.buz)
587
+ .orderby(fn.Date(self.table_abc.foo))
588
+ )
589
+
590
+ self.assertEqual(
591
+ 'SELECT "abc"."foo","efg"."buz" FROM "abc" '
592
+ 'JOIN "efg" ON "abc"."foo"="efg"."bar" '
593
+ 'ORDER BY DATE("abc"."foo")',
594
+ str(test_query),
595
+ )
596
+
597
+ def test_join_from_join(self):
598
+ test_query = (
599
+ Query.from_(self.table_abc)
600
+ .join(self.table_efg)
601
+ .on(self.table_abc.efg_id == self.table_efg.id)
602
+ .join(self.table_hij)
603
+ .on(self.table_efg.hij_id == self.table_hij.id)
604
+ .select(self.table_abc.foo, self.table_efg.bar, self.table_hij.fizz)
605
+ )
606
+
607
+ self.assertEqual(
608
+ 'SELECT "abc"."foo","efg"."bar","hij"."fizz" FROM "abc" '
609
+ 'JOIN "efg" ON "abc"."efg_id"="efg"."id" '
610
+ 'JOIN "hij" ON "efg"."hij_id"="hij"."id"',
611
+ str(test_query),
612
+ )
613
+
614
+ def test_join_query_without_alias(self):
615
+ subquery = Query.from_(self.table_efg).select(
616
+ self.table_efg.base_id.as_("x"), self.table_efg.fizz, self.table_efg.buzz
617
+ )
618
+
619
+ test_query = (
620
+ Query.from_(self.table_abc)
621
+ .join(subquery)
622
+ .on(subquery.x == self.table_abc.id)
623
+ .select(self.table_abc.foo, subquery.fizz, subquery.buzz)
624
+ )
625
+
626
+ self.assertEqual(
627
+ 'SELECT "abc"."foo","sq0"."fizz","sq0"."buzz" '
628
+ 'FROM "abc" JOIN ('
629
+ 'SELECT "base_id" "x","fizz","buzz" FROM "efg"'
630
+ ') "sq0" '
631
+ 'ON "sq0"."x"="abc"."id"',
632
+ str(test_query),
633
+ )
634
+
635
+ def test_join_query_with_column_alias(self):
636
+ subquery = (
637
+ Query.from_(self.table_efg)
638
+ .select(
639
+ self.table_efg.base_id.as_("x"),
640
+ self.table_efg.fizz,
641
+ self.table_efg.buzz,
642
+ )
643
+ .as_("subq")
644
+ )
645
+
646
+ test_query = (
647
+ Query.from_(self.table_abc)
648
+ .join(subquery)
649
+ .on(subquery.x == self.table_abc.id)
650
+ .select(self.table_abc.foo, subquery.fizz, subquery.buzz)
651
+ )
652
+
653
+ self.assertEqual(
654
+ 'SELECT "abc"."foo","subq"."fizz","subq"."buzz" '
655
+ 'FROM "abc" JOIN ('
656
+ 'SELECT "base_id" "x","fizz","buzz" FROM "efg"'
657
+ ') "subq" '
658
+ 'ON "subq"."x"="abc"."id"',
659
+ str(test_query),
660
+ )
661
+
662
+ def test_join_query_with_table_alias(self):
663
+ xxx = self.table_efg.as_("xxx")
664
+ subquery = Query.from_(xxx).select(xxx.base_id, xxx.fizz, xxx.buzz).as_("subq")
665
+
666
+ test_query = (
667
+ Query.from_(self.table_abc)
668
+ .join(subquery)
669
+ .on(subquery.x == self.table_abc.id)
670
+ .select(self.table_abc.foo, subquery.fizz, subquery.buzz)
671
+ )
672
+
673
+ self.assertEqual(
674
+ 'SELECT "abc"."foo","subq"."fizz","subq"."buzz" '
675
+ 'FROM "abc" JOIN ('
676
+ 'SELECT "xxx"."base_id","xxx"."fizz","xxx"."buzz" FROM "efg" "xxx"'
677
+ ') "subq" '
678
+ 'ON "subq"."x"="abc"."id"',
679
+ str(test_query),
680
+ )
681
+
682
+ def test_join_query_with_setoperation(self):
683
+ subquery = Query.from_(self.table_abc).select("*").union(Query.from_(self.table_abc).select("*")).as_("subq")
684
+
685
+ test_query = (
686
+ Query.from_(self.table_abc).join(subquery).on(subquery.x == self.table_abc.id).select(self.table_abc.foo)
687
+ )
688
+
689
+ self.assertEqual(
690
+ 'SELECT "abc"."foo" FROM "abc" '
691
+ 'JOIN '
692
+ '((SELECT * FROM "abc") '
693
+ 'UNION '
694
+ '(SELECT * FROM "abc")) "subq" '
695
+ 'ON "subq"."x"="abc"."id"',
696
+ str(test_query),
697
+ )
698
+
699
+
700
+ class UnionTests(unittest.TestCase):
701
+ table1, table2 = Tables("abc", "efg")
702
+
703
+ def test_union(self):
704
+ query1 = Query.from_(self.table1).select(self.table1.foo)
705
+ query2 = Query.from_(self.table2).select(self.table2.bar)
706
+
707
+ self.assertEqual(
708
+ '(SELECT "foo" FROM "abc") UNION (SELECT "bar" FROM "efg")',
709
+ str(query1 + query2),
710
+ )
711
+ self.assertEqual(
712
+ '(SELECT "foo" FROM "abc") UNION (SELECT "bar" FROM "efg")',
713
+ str(query1.union(query2)),
714
+ )
715
+
716
+ def test_union_multiple(self):
717
+ table3, table4 = Tables("hij", "lmn")
718
+ query1 = Query.from_(self.table1).select(self.table1.foo)
719
+ query2 = Query.from_(self.table2).select(self.table2.bar)
720
+ query3 = Query.from_(table3).select(table3.baz)
721
+ query4 = Query.from_(table4).select(table4.faz)
722
+
723
+ self.assertEqual(
724
+ '(SELECT "foo" FROM "abc") UNION '
725
+ '(SELECT "bar" FROM "efg") UNION '
726
+ '(SELECT "baz" FROM "hij") UNION '
727
+ '(SELECT "faz" FROM "lmn")',
728
+ str(query1 + query2 + query3 + query4),
729
+ )
730
+ self.assertEqual(
731
+ '(SELECT "foo" FROM "abc") UNION '
732
+ '(SELECT "bar" FROM "efg") UNION '
733
+ '(SELECT "baz" FROM "hij") UNION '
734
+ '(SELECT "faz" FROM "lmn")',
735
+ str(query1.union(query2).union(query3).union(query4)),
736
+ )
737
+
738
+ def test_union_all(self):
739
+ query1 = Query.from_(self.table1).select(self.table1.foo)
740
+ query2 = Query.from_(self.table2).select(self.table2.bar)
741
+
742
+ self.assertEqual(
743
+ '(SELECT "foo" FROM "abc") UNION ALL (SELECT "bar" FROM "efg")',
744
+ str(query1 * query2),
745
+ )
746
+ self.assertEqual(
747
+ '(SELECT "foo" FROM "abc") UNION ALL (SELECT "bar" FROM "efg")',
748
+ str(query1.union_all(query2)),
749
+ )
750
+
751
+ def test_union_all_multiple(self):
752
+ table3, table4 = Tables("hij", "lmn")
753
+ query1 = Query.from_(self.table1).select(self.table1.foo)
754
+ query2 = Query.from_(self.table2).select(self.table2.bar)
755
+ query3 = Query.from_(table3).select(table3.baz)
756
+ query4 = Query.from_(table4).select(table4.faz)
757
+
758
+ self.assertEqual(
759
+ '(SELECT "foo" FROM "abc") UNION ALL '
760
+ '(SELECT "bar" FROM "efg") UNION ALL '
761
+ '(SELECT "baz" FROM "hij") UNION ALL '
762
+ '(SELECT "faz" FROM "lmn")',
763
+ str(query1 * query2 * query3 * query4),
764
+ )
765
+ self.assertEqual(
766
+ '(SELECT "foo" FROM "abc") UNION ALL '
767
+ '(SELECT "bar" FROM "efg") UNION ALL '
768
+ '(SELECT "baz" FROM "hij") UNION ALL '
769
+ '(SELECT "faz" FROM "lmn")',
770
+ str(query1.union_all(query2).union_all(query3).union_all(query4)),
771
+ )
772
+
773
+ def test_union_with_order_by(self):
774
+ query1 = Query.from_(self.table1).select(self.table1.foo.as_("a"))
775
+ query2 = Query.from_(self.table2).select(self.table2.bar.as_("a"))
776
+
777
+ union_query = str((query1 + query2).orderby(query1.field("a")))
778
+ union_all_query = str((query1 * query2).orderby(query1.field("a")))
779
+
780
+ self.assertEqual(
781
+ '(SELECT "foo" "a" FROM "abc") ' "UNION " '(SELECT "bar" "a" FROM "efg") ' 'ORDER BY "a"',
782
+ union_query,
783
+ )
784
+
785
+ self.assertEqual(
786
+ '(SELECT "foo" "a" FROM "abc") ' "UNION ALL " '(SELECT "bar" "a" FROM "efg") ' 'ORDER BY "a"',
787
+ union_all_query,
788
+ )
789
+
790
+ def test_union_with_order_by_use_union_query_field(self):
791
+ query1 = Query.from_(self.table1).select(self.table1.foo.as_("a"))
792
+ query2 = Query.from_(self.table2).select(self.table2.bar.as_("a"))
793
+
794
+ union_query = query1 + query2
795
+ union_query = str(union_query.orderby(union_query.field("a")))
796
+ union_all_query = query1 * query2
797
+ union_all_query = str(union_all_query.orderby(union_all_query.field("a")))
798
+
799
+ self.assertEqual(
800
+ '(SELECT "foo" "a" FROM "abc") ' "UNION " '(SELECT "bar" "a" FROM "efg") ' 'ORDER BY "a"',
801
+ union_query,
802
+ )
803
+ self.assertEqual(
804
+ '(SELECT "foo" "a" FROM "abc") ' "UNION ALL " '(SELECT "bar" "a" FROM "efg") ' 'ORDER BY "a"',
805
+ union_all_query,
806
+ )
807
+
808
+ def test_union_with_order_by_with_aliases(self):
809
+ query1 = Query.from_(self.table1.as_("a")).select(self.table1.foo.as_("a"))
810
+ query2 = Query.from_(self.table2.as_("b")).select(self.table2.bar.as_("a"))
811
+
812
+ union_all_query = str((query1 * query2).orderby(query1.field("a")))
813
+ union_query = str((query1 + query2).orderby(query1.field("a")))
814
+ self.assertEqual(
815
+ '(SELECT "foo" "a" FROM "abc" "a")' " UNION ALL " '(SELECT "bar" "a" FROM "efg" "b")' ' ORDER BY "a"',
816
+ union_all_query,
817
+ )
818
+ self.assertEqual(
819
+ '(SELECT "foo" "a" FROM "abc" "a")' " UNION " '(SELECT "bar" "a" FROM "efg" "b")' ' ORDER BY "a"',
820
+ union_query,
821
+ )
822
+
823
+ def test_union_with_order_by_use_union_query_field_with_aliases(self):
824
+ query1 = Query.from_(self.table1).select(self.table1.foo.as_("a"))
825
+ query2 = Query.from_(self.table2).select(self.table2.bar.as_("a"))
826
+
827
+ union_query = (query1 + query2).as_("x")
828
+ union_query = union_query.orderby(union_query.field("a"))
829
+ union_all_query = (query1 * query2).as_("x")
830
+ union_all_query = union_all_query.orderby(union_all_query.field("a"))
831
+
832
+ self.assertEqual(
833
+ '(SELECT "foo" "a" FROM "abc") ' "UNION " '(SELECT "bar" "a" FROM "efg") ' 'ORDER BY "x"."a"',
834
+ str(union_query),
835
+ )
836
+ self.assertEqual(
837
+ '(SELECT "foo" "a" FROM "abc") ' "UNION ALL " '(SELECT "bar" "a" FROM "efg") ' 'ORDER BY "x"."a"',
838
+ str(union_all_query),
839
+ )
840
+
841
+ def test_require_equal_number_of_fields(self):
842
+ query1 = Query.from_(self.table1).select(self.table1.foo)
843
+ query2 = Query.from_(self.table2).select(self.table2.fiz, self.table2.buz)
844
+
845
+ with self.assertRaises(SetOperationException):
846
+ str(query1 + query2)
847
+
848
+ def test_mysql_query_wraps_unioned_queries(self):
849
+ query1 = MySQLQuery.from_(self.table1).select(self.table1.foo)
850
+ query2 = Query.from_(self.table2).select(self.table2.bar)
851
+
852
+ self.assertEqual(
853
+ "(SELECT `foo` FROM `abc`) UNION (SELECT `bar` FROM `efg`)",
854
+ str(query1 + query2),
855
+ )
856
+
857
+ def test_union_as_subquery(self):
858
+ abc, efg = Tables("abc", "efg")
859
+ hij = Query.from_(abc).select(abc.t).union(Query.from_(efg).select(efg.t))
860
+ q = Query.from_(hij).select(fn.Avg(hij.t))
861
+
862
+ self.assertEqual(
863
+ 'SELECT AVG("sq0"."t") FROM ((SELECT "t" FROM "abc") UNION (SELECT "t" FROM "efg")) "sq0"',
864
+ str(q),
865
+ )
866
+
867
+ def test_union_with_no_quote_char(self):
868
+ abc, efg = Tables("abc", "efg")
869
+ hij = Query.from_(abc).select(abc.t).union(Query.from_(efg).select(efg.t))
870
+ q = Query.from_(hij).select(fn.Avg(hij.t))
871
+
872
+ self.assertEqual(
873
+ "SELECT AVG(sq0.t) FROM ((SELECT t FROM abc) UNION (SELECT t FROM efg)) sq0",
874
+ q.get_sql(quote_char=None),
875
+ )
876
+
877
+
878
+ class InsertQueryJoinTests(unittest.TestCase):
879
+ def test_join_table_on_insert_query(self):
880
+ a, b, c = Tables("a", "b", "c")
881
+ q = Query.into(c).from_(a).join(b).on(a.fkey_id == b.id).where(b.foo == 1).select("a.*", "b.*")
882
+
883
+ self.assertEqual(
884
+ 'INSERT INTO "c" '
885
+ 'SELECT "a"."a.*","a"."b.*" '
886
+ 'FROM "a" JOIN "b" '
887
+ 'ON "a"."fkey_id"="b"."id" '
888
+ 'WHERE "b"."foo"=1',
889
+ str(q),
890
+ )
891
+
892
+
893
+ class UpdateQueryJoinTests(unittest.TestCase):
894
+ def test_join_table_on_update_query(self):
895
+ a, b = Tables("a", "b")
896
+ q = Query.update(a).join(b).on(a.fkey_id == b.id).where(b.foo == 1).set("adwords_batch_job_id", 1)
897
+
898
+ self.assertEqual(
899
+ 'UPDATE "a" ' 'JOIN "b" ' 'ON "a"."fkey_id"="b"."id" ' 'SET "adwords_batch_job_id"=1 ' 'WHERE "b"."foo"=1',
900
+ str(q),
901
+ )
902
+
903
+
904
+ class IntersectTests(unittest.TestCase):
905
+ table1, table2 = Tables("abc", "efg")
906
+
907
+ def test_intersect(self):
908
+ query1 = Query.from_(self.table1).select(self.table1.foo)
909
+ query2 = Query.from_(self.table2).select(self.table2.bar)
910
+
911
+ self.assertEqual(
912
+ '(SELECT "foo" FROM "abc") INTERSECT (SELECT "bar" FROM "efg")',
913
+ str(query1.intersect(query2)),
914
+ )
915
+
916
+ def test_intersect_multiple(self):
917
+ table3, table4 = Tables("hij", "lmn")
918
+ query1 = Query.from_(self.table1).select(self.table1.foo)
919
+ query2 = Query.from_(self.table2).select(self.table2.bar)
920
+ query3 = Query.from_(table3).select(table3.baz)
921
+ query4 = Query.from_(table4).select(table4.faz)
922
+
923
+ self.assertEqual(
924
+ '(SELECT "foo" FROM "abc") INTERSECT '
925
+ '(SELECT "bar" FROM "efg") INTERSECT '
926
+ '(SELECT "baz" FROM "hij") INTERSECT '
927
+ '(SELECT "faz" FROM "lmn")',
928
+ str(query1.intersect(query2).intersect(query3).intersect(query4)),
929
+ )
930
+
931
+ def test_intersect_with_order_by(self):
932
+ query1 = Query.from_(self.table1).select(self.table1.foo.as_("a"))
933
+ query2 = Query.from_(self.table2).select(self.table2.bar.as_("a"))
934
+
935
+ intersect_query = str((query1.intersect(query2)).orderby(query1.field("a")))
936
+
937
+ self.assertEqual(
938
+ '(SELECT "foo" "a" FROM "abc") ' "INTERSECT " '(SELECT "bar" "a" FROM "efg") ' 'ORDER BY "a"',
939
+ intersect_query,
940
+ )
941
+
942
+ def test_intersect_with_limit(self):
943
+ query1 = Query.from_(self.table1).select(self.table1.foo.as_("a"))
944
+ query2 = Query.from_(self.table2).select(self.table2.bar.as_("a"))
945
+
946
+ intersect_query = str((query1.intersect(query2)).limit(10))
947
+
948
+ self.assertEqual(
949
+ '(SELECT "foo" "a" FROM "abc") ' "INTERSECT " '(SELECT "bar" "a" FROM "efg") ' 'LIMIT 10',
950
+ intersect_query,
951
+ )
952
+
953
+ def test_intersect_with_offset(self):
954
+ query1 = Query.from_(self.table1).select(self.table1.foo.as_("a"))
955
+ query2 = Query.from_(self.table2).select(self.table2.bar.as_("a"))
956
+
957
+ intersect_query = str((query1.intersect(query2)).offset(10))
958
+
959
+ self.assertEqual(
960
+ '(SELECT "foo" "a" FROM "abc") ' "INTERSECT " '(SELECT "bar" "a" FROM "efg") ' 'OFFSET 10',
961
+ intersect_query,
962
+ )
963
+
964
+ def test_require_equal_number_of_fields_intersect(self):
965
+ query1 = Query.from_(self.table1).select(self.table1.foo)
966
+ query2 = Query.from_(self.table2).select(self.table2.fiz, self.table2.buz)
967
+
968
+ with self.assertRaises(SetOperationException):
969
+ str(query1.intersect(query2))
970
+
971
+ def test_mysql_query_wraps_intersected_queries(self):
972
+ query1 = MySQLQuery.from_(self.table1).select(self.table1.foo)
973
+ query2 = Query.from_(self.table2).select(self.table2.bar)
974
+
975
+ self.assertEqual(
976
+ "(SELECT `foo` FROM `abc`) INTERSECT (SELECT `bar` FROM `efg`)",
977
+ str(query1.intersect(query2)),
978
+ )
979
+
980
+ def test_intersect_as_subquery(self):
981
+ abc, efg = Tables("abc", "efg")
982
+ hij = Query.from_(abc).select(abc.t).intersect(Query.from_(efg).select(efg.t))
983
+ q = Query.from_(hij).select(fn.Avg(hij.t))
984
+
985
+ self.assertEqual(
986
+ 'SELECT AVG("sq0"."t") FROM ((SELECT "t" FROM "abc") INTERSECT (SELECT "t" FROM "efg")) "sq0"',
987
+ str(q),
988
+ )
989
+
990
+ def test_intersect_with_no_quote_char(self):
991
+ abc, efg = Tables("abc", "efg")
992
+ hij = Query.from_(abc).select(abc.t).intersect(Query.from_(efg).select(efg.t))
993
+ q = Query.from_(hij).select(fn.Avg(hij.t))
994
+
995
+ self.assertEqual(
996
+ "SELECT AVG(sq0.t) FROM ((SELECT t FROM abc) INTERSECT (SELECT t FROM efg)) sq0",
997
+ q.get_sql(quote_char=None),
998
+ )
999
+
1000
+
1001
+ class MinusTests(unittest.TestCase):
1002
+ table1, table2 = Tables("abc", "efg")
1003
+
1004
+ def test_minus(self):
1005
+ query1 = Query.from_(self.table1).select(self.table1.foo)
1006
+ query2 = Query.from_(self.table2).select(self.table2.bar)
1007
+
1008
+ self.assertEqual('(SELECT "foo" FROM "abc") MINUS (SELECT "bar" FROM "efg")', str(query1.minus(query2)))
1009
+
1010
+ self.assertEqual(
1011
+ '(SELECT "foo" FROM "abc") MINUS (SELECT "bar" FROM "efg")',
1012
+ str(query1 - query2),
1013
+ )
1014
+
1015
+ def test_minus_multiple(self):
1016
+ table3, table4 = Tables("hij", "lmn")
1017
+ query1 = Query.from_(self.table1).select(self.table1.foo)
1018
+ query2 = Query.from_(self.table2).select(self.table2.bar)
1019
+ query3 = Query.from_(table3).select(table3.baz)
1020
+ query4 = Query.from_(table4).select(table4.faz)
1021
+
1022
+ self.assertEqual(
1023
+ '(SELECT "foo" FROM "abc") MINUS '
1024
+ '(SELECT "bar" FROM "efg") MINUS '
1025
+ '(SELECT "baz" FROM "hij") MINUS '
1026
+ '(SELECT "faz" FROM "lmn")',
1027
+ str(query1 - query2 - query3 - query4),
1028
+ )
1029
+
1030
+ self.assertEqual(
1031
+ '(SELECT "foo" FROM "abc") MINUS '
1032
+ '(SELECT "bar" FROM "efg") MINUS '
1033
+ '(SELECT "baz" FROM "hij") MINUS '
1034
+ '(SELECT "faz" FROM "lmn")',
1035
+ str(query1.minus(query2).minus(query3).minus(query4)),
1036
+ )
1037
+
1038
+ def test_minus_with_order_by(self):
1039
+ query1 = Query.from_(self.table1).select(self.table1.foo.as_("a"))
1040
+ query2 = Query.from_(self.table2).select(self.table2.bar.as_("a"))
1041
+
1042
+ minus_query = str(query1.minus(query2).orderby(query1.field("a")))
1043
+
1044
+ self.assertEqual(
1045
+ '(SELECT "foo" "a" FROM "abc") ' "MINUS " '(SELECT "bar" "a" FROM "efg") ' 'ORDER BY "a"', minus_query
1046
+ )
1047
+
1048
+ def test_minus_query_with_order_by_use_minus_query_field(self):
1049
+ query1 = Query.from_(self.table1).select(self.table1.foo.as_("a"))
1050
+ query2 = Query.from_(self.table2).select(self.table2.bar.as_("a"))
1051
+
1052
+ minus_query = query1.minus(query2)
1053
+ minus_query = str(minus_query.orderby(minus_query.field("b")))
1054
+
1055
+ self.assertEqual(
1056
+ '(SELECT "foo" "a" FROM "abc") ' "MINUS " '(SELECT "bar" "a" FROM "efg") ' 'ORDER BY "b"',
1057
+ minus_query,
1058
+ )
1059
+
1060
+ def test_require_equal_number_of_fields(self):
1061
+ query1 = Query.from_(self.table1).select(self.table1.foo)
1062
+ query2 = Query.from_(self.table2).select(self.table2.fiz, self.table2.buz)
1063
+
1064
+ with self.assertRaises(SetOperationException):
1065
+ str(query1.minus(query2))
1066
+
1067
+ def test_mysql_query_wraps_minus_queries(self):
1068
+ query1 = MySQLQuery.from_(self.table1).select(self.table1.foo)
1069
+ query2 = Query.from_(self.table2).select(self.table2.bar)
1070
+
1071
+ self.assertEqual(
1072
+ "(SELECT `foo` FROM `abc`) MINUS (SELECT `bar` FROM `efg`)",
1073
+ str(query1 - query2),
1074
+ )
1075
+
1076
+ def test_minus_as_subquery(self):
1077
+ abc, efg = Tables("abc", "efg")
1078
+ hij = Query.from_(abc).select(abc.t).minus(Query.from_(efg).select(efg.t))
1079
+ q = Query.from_(hij).select(fn.Avg(hij.t))
1080
+
1081
+ self.assertEqual(
1082
+ 'SELECT AVG("sq0"."t") FROM ((SELECT "t" FROM "abc") MINUS (SELECT "t" FROM "efg")) "sq0"',
1083
+ str(q),
1084
+ )
1085
+
1086
+ def test_minus_with_no_quote_char(self):
1087
+ abc, efg = Tables("abc", "efg")
1088
+ hij = Query.from_(abc).select(abc.t).minus(Query.from_(efg).select(efg.t))
1089
+ q = Query.from_(hij).select(fn.Avg(hij.t))
1090
+
1091
+ self.assertEqual(
1092
+ "SELECT AVG(sq0.t) FROM ((SELECT t FROM abc) MINUS (SELECT t FROM efg)) sq0",
1093
+ q.get_sql(quote_char=None),
1094
+ )
1095
+
1096
+
1097
+ class ExceptOfTests(unittest.TestCase):
1098
+ table1, table2 = Tables("abc", "efg")
1099
+
1100
+ def test_except(self):
1101
+ query1 = Query.from_(self.table1).select(self.table1.foo)
1102
+ query2 = Query.from_(self.table2).select(self.table2.bar)
1103
+
1104
+ self.assertEqual('(SELECT "foo" FROM "abc") EXCEPT (SELECT "bar" FROM "efg")', str(query1.except_of(query2)))
1105
+
1106
+ def test_except_multiple(self):
1107
+ table3, table4 = Tables("hij", "lmn")
1108
+ query1 = Query.from_(self.table1).select(self.table1.foo)
1109
+ query2 = Query.from_(self.table2).select(self.table2.bar)
1110
+ query3 = Query.from_(table3).select(table3.baz)
1111
+ query4 = Query.from_(table4).select(table4.faz)
1112
+
1113
+ self.assertEqual(
1114
+ '(SELECT "foo" FROM "abc") EXCEPT '
1115
+ '(SELECT "bar" FROM "efg") EXCEPT '
1116
+ '(SELECT "baz" FROM "hij") EXCEPT '
1117
+ '(SELECT "faz" FROM "lmn")',
1118
+ str(query1.except_of(query2).except_of(query3).except_of(query4)),
1119
+ )
1120
+
1121
+ def test_except_with_order_by(self):
1122
+ query1 = Query.from_(self.table1).select(self.table1.foo.as_("a"))
1123
+ query2 = Query.from_(self.table2).select(self.table2.bar.as_("a"))
1124
+
1125
+ except_query = str(query1.except_of(query2).orderby(query1.field("a")))
1126
+
1127
+ self.assertEqual(
1128
+ '(SELECT "foo" "a" FROM "abc") ' "EXCEPT " '(SELECT "bar" "a" FROM "efg") ' 'ORDER BY "a"', except_query
1129
+ )
1130
+
1131
+ def test_except_query_with_order_by_use_minus_query_field(self):
1132
+ query1 = Query.from_(self.table1).select(self.table1.foo.as_("a"))
1133
+ query2 = Query.from_(self.table2).select(self.table2.bar.as_("a"))
1134
+
1135
+ except_query = query1.except_of(query2)
1136
+ except_query = str(except_query.orderby(except_query.field("b")))
1137
+
1138
+ self.assertEqual(
1139
+ '(SELECT "foo" "a" FROM "abc") ' "EXCEPT " '(SELECT "bar" "a" FROM "efg") ' 'ORDER BY "b"',
1140
+ except_query,
1141
+ )
1142
+
1143
+ def test_require_equal_number_of_fields_except_of(self):
1144
+ query1 = Query.from_(self.table1).select(self.table1.foo)
1145
+ query2 = Query.from_(self.table2).select(self.table2.fiz, self.table2.buz)
1146
+
1147
+ with self.assertRaises(SetOperationException):
1148
+ str(query1.except_of(query2))
1149
+
1150
+ def test_except_as_subquery(self):
1151
+ abc, efg = Tables("abc", "efg")
1152
+ hij = Query.from_(abc).select(abc.t).except_of(Query.from_(efg).select(efg.t))
1153
+ q = Query.from_(hij).select(fn.Avg(hij.t))
1154
+
1155
+ self.assertEqual(
1156
+ 'SELECT AVG("sq0"."t") FROM ((SELECT "t" FROM "abc") EXCEPT (SELECT "t" FROM "efg")) "sq0"',
1157
+ str(q),
1158
+ )
1159
+
1160
+ def test_except_with_no_quote_char(self):
1161
+ abc, efg = Tables("abc", "efg")
1162
+ hij = Query.from_(abc).select(abc.t).except_of(Query.from_(efg).select(efg.t))
1163
+ q = Query.from_(hij).select(fn.Avg(hij.t))
1164
+
1165
+ self.assertEqual(
1166
+ "SELECT AVG(sq0.t) FROM ((SELECT t FROM abc) EXCEPT (SELECT t FROM efg)) sq0",
1167
+ q.get_sql(quote_char=None),
1168
+ )
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_negation.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from pypika import (
4
+ Tables,
5
+ functions as fn,
6
+ )
7
+ from pypika.terms import ValueWrapper
8
+
9
+
10
+ class NegationTests(unittest.TestCase):
11
+ table_abc, table_efg = Tables("abc", "efg")
12
+
13
+ def test_negate_wrapped_float(self):
14
+ q = -ValueWrapper(1.0)
15
+
16
+ self.assertEqual("-1.0", q.get_sql())
17
+
18
+ def test_negate_wrapped_int(self):
19
+ q = -ValueWrapper(1)
20
+
21
+ self.assertEqual("-1", q.get_sql())
22
+
23
+ def test_negate_field(self):
24
+ q = -self.table_abc.foo
25
+
26
+ self.assertEqual('-"abc"."foo"', q.get_sql(with_namespace=True, quote_char='"'))
27
+
28
+ def test_negate_function(self):
29
+ q = -fn.Sum(self.table_abc.foo)
30
+
31
+ self.assertEqual('-SUM("abc"."foo")', q.get_sql(with_namespace=True, quote_char='"'))
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_parameter.py ADDED
@@ -0,0 +1,206 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+ from datetime import date
3
+
4
+ from pypika import (
5
+ FormatParameter,
6
+ NamedParameter,
7
+ NumericParameter,
8
+ Parameter,
9
+ PyformatParameter,
10
+ QmarkParameter,
11
+ Query,
12
+ Tables,
13
+ )
14
+ from pypika.terms import ListParameter, ParameterValueWrapper
15
+
16
+
17
+ class ParametrizedTests(unittest.TestCase):
18
+ table_abc, table_efg = Tables("abc", "efg")
19
+
20
+ def test_param_insert(self):
21
+ q = Query.into(self.table_abc).columns("a", "b", "c").insert(Parameter("?"), Parameter("?"), Parameter("?"))
22
+
23
+ self.assertEqual('INSERT INTO "abc" ("a","b","c") VALUES (?,?,?)', q.get_sql())
24
+
25
+ def test_param_select_join(self):
26
+ q = (
27
+ Query.from_(self.table_abc)
28
+ .select("*")
29
+ .where(self.table_abc.category == Parameter("%s"))
30
+ .join(self.table_efg)
31
+ .on(self.table_abc.id == self.table_efg.abc_id)
32
+ .where(self.table_efg.date >= Parameter("%s"))
33
+ .limit(10)
34
+ )
35
+
36
+ self.assertEqual(
37
+ 'SELECT * FROM "abc" JOIN "efg" ON "abc"."id"="efg"."abc_id" WHERE "abc"."category"=%s AND "efg"."date">=%s LIMIT 10',
38
+ q.get_sql(),
39
+ )
40
+
41
+ def test_param_select_subquery(self):
42
+ q = (
43
+ Query.from_(self.table_abc)
44
+ .select("*")
45
+ .where(self.table_abc.category == Parameter("&1"))
46
+ .where(
47
+ self.table_abc.id.isin(
48
+ Query.from_(self.table_efg)
49
+ .select(self.table_efg.abc_id)
50
+ .where(self.table_efg.date >= Parameter("&2"))
51
+ )
52
+ )
53
+ .limit(10)
54
+ )
55
+
56
+ self.assertEqual(
57
+ 'SELECT * FROM "abc" WHERE "category"=&1 AND "id" IN (SELECT "abc_id" FROM "efg" WHERE "date">=&2) LIMIT 10',
58
+ q.get_sql(),
59
+ )
60
+
61
+ def test_join(self):
62
+ subquery = (
63
+ Query.from_(self.table_efg)
64
+ .select(self.table_efg.fiz, self.table_efg.buz)
65
+ .where(self.table_efg.buz == Parameter(":buz"))
66
+ )
67
+
68
+ q = (
69
+ Query.from_(self.table_abc)
70
+ .join(subquery)
71
+ .on(self.table_abc.bar == subquery.buz)
72
+ .select(self.table_abc.foo, subquery.fiz)
73
+ .where(self.table_abc.bar == Parameter(":bar"))
74
+ )
75
+
76
+ self.assertEqual(
77
+ 'SELECT "abc"."foo","sq0"."fiz" FROM "abc" JOIN (SELECT "fiz","buz" FROM "efg" WHERE "buz"=:buz)'
78
+ ' "sq0" ON "abc"."bar"="sq0"."buz" WHERE "abc"."bar"=:bar',
79
+ q.get_sql(),
80
+ )
81
+
82
+ def test_qmark_parameter(self):
83
+ self.assertEqual('?', QmarkParameter().get_sql())
84
+
85
+ def test_numeric_parameter(self):
86
+ self.assertEqual(':14', NumericParameter('14').get_sql())
87
+ self.assertEqual(':15', NumericParameter(15).get_sql())
88
+
89
+ def test_named_parameter(self):
90
+ self.assertEqual(':buz', NamedParameter('buz').get_sql())
91
+
92
+ def test_format_parameter(self):
93
+ self.assertEqual('%s', FormatParameter().get_sql())
94
+
95
+ def test_pyformat_parameter(self):
96
+ self.assertEqual('%(buz)s', PyformatParameter('buz').get_sql())
97
+
98
+
99
+ class ParametrizedTestsWithValues(unittest.TestCase):
100
+ table_abc, table_efg = Tables("abc", "efg")
101
+
102
+ def test_param_insert(self):
103
+ q = Query.into(self.table_abc).columns("a", "b", "c").insert(1, 2.2, 'foo')
104
+
105
+ parameter = QmarkParameter()
106
+ sql = q.get_sql(parameter=parameter)
107
+ self.assertEqual('INSERT INTO "abc" ("a","b","c") VALUES (?,?,?)', sql)
108
+ self.assertEqual([1, 2.2, 'foo'], parameter.get_parameters())
109
+
110
+ def test_param_select_join(self):
111
+ q = (
112
+ Query.from_(self.table_abc)
113
+ .select("*")
114
+ .where(self.table_abc.category == 'foobar')
115
+ .join(self.table_efg)
116
+ .on(self.table_abc.id == self.table_efg.abc_id)
117
+ .where(self.table_efg.date >= date(2024, 2, 22))
118
+ .limit(10)
119
+ )
120
+
121
+ parameter = FormatParameter()
122
+ sql = q.get_sql(parameter=parameter)
123
+ self.assertEqual(
124
+ 'SELECT * FROM "abc" JOIN "efg" ON "abc"."id"="efg"."abc_id" WHERE "abc"."category"=%s AND "efg"."date">=%s LIMIT 10',
125
+ sql,
126
+ )
127
+ self.assertEqual(['foobar', '2024-02-22'], parameter.get_parameters())
128
+
129
+ def test_param_select_subquery(self):
130
+ q = (
131
+ Query.from_(self.table_abc)
132
+ .select("*")
133
+ .where(self.table_abc.category == 'foobar')
134
+ .where(
135
+ self.table_abc.id.isin(
136
+ Query.from_(self.table_efg)
137
+ .select(self.table_efg.abc_id)
138
+ .where(self.table_efg.date >= date(2024, 2, 22))
139
+ )
140
+ )
141
+ .limit(10)
142
+ )
143
+
144
+ parameter = ListParameter(placeholder=lambda idx: f'&{idx+1}')
145
+ sql = q.get_sql(parameter=parameter)
146
+ self.assertEqual(
147
+ 'SELECT * FROM "abc" WHERE "category"=&1 AND "id" IN (SELECT "abc_id" FROM "efg" WHERE "date">=&2) LIMIT 10',
148
+ sql,
149
+ )
150
+ self.assertEqual(['foobar', '2024-02-22'], parameter.get_parameters())
151
+
152
+ def test_join(self):
153
+ subquery = (
154
+ Query.from_(self.table_efg)
155
+ .select(self.table_efg.fiz, self.table_efg.buz)
156
+ .where(self.table_efg.buz == 'buz')
157
+ )
158
+
159
+ q = (
160
+ Query.from_(self.table_abc)
161
+ .join(subquery)
162
+ .on(self.table_abc.bar == subquery.buz)
163
+ .select(self.table_abc.foo, subquery.fiz)
164
+ .where(self.table_abc.bar == 'bar')
165
+ )
166
+
167
+ parameter = NamedParameter()
168
+ sql = q.get_sql(parameter=parameter)
169
+ self.assertEqual(
170
+ 'SELECT "abc"."foo","sq0"."fiz" FROM "abc" JOIN (SELECT "fiz","buz" FROM "efg" WHERE "buz"=:param1)'
171
+ ' "sq0" ON "abc"."bar"="sq0"."buz" WHERE "abc"."bar"=:param2',
172
+ sql,
173
+ )
174
+ self.assertEqual({'param1': 'buz', 'param2': 'bar'}, parameter.get_parameters())
175
+
176
+ def test_join_with_parameter_value_wrapper(self):
177
+ subquery = (
178
+ Query.from_(self.table_efg)
179
+ .select(self.table_efg.fiz, self.table_efg.buz)
180
+ .where(self.table_efg.buz == ParameterValueWrapper(Parameter(':buz'), 'buz'))
181
+ )
182
+
183
+ q = (
184
+ Query.from_(self.table_abc)
185
+ .join(subquery)
186
+ .on(self.table_abc.bar == subquery.buz)
187
+ .select(self.table_abc.foo, subquery.fiz)
188
+ .where(self.table_abc.bar == ParameterValueWrapper(NamedParameter('bar'), 'bar'))
189
+ )
190
+
191
+ parameter = NamedParameter()
192
+ sql = q.get_sql(parameter=parameter)
193
+ self.assertEqual(
194
+ 'SELECT "abc"."foo","sq0"."fiz" FROM "abc" JOIN (SELECT "fiz","buz" FROM "efg" WHERE "buz"=:buz)'
195
+ ' "sq0" ON "abc"."bar"="sq0"."buz" WHERE "abc"."bar"=:bar',
196
+ sql,
197
+ )
198
+ self.assertEqual({':buz': 'buz', 'bar': 'bar'}, parameter.get_parameters())
199
+
200
+ def test_pyformat_parameter(self):
201
+ q = Query.into(self.table_abc).columns("a", "b", "c").insert(1, 2.2, 'foo')
202
+
203
+ parameter = PyformatParameter()
204
+ sql = q.get_sql(parameter=parameter)
205
+ self.assertEqual('INSERT INTO "abc" ("a","b","c") VALUES (%(param1)s,%(param2)s,%(param3)s)', sql)
206
+ self.assertEqual({"param1": 1, "param2": 2.2, "param3": "foo"}, parameter.get_parameters())
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_pseudocolumns.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from pypika import Query, Table
4
+ from pypika.pseudocolumns import (
5
+ ColumnValue,
6
+ ObjectID,
7
+ ObjectValue,
8
+ RowID,
9
+ RowNum,
10
+ SysDate,
11
+ )
12
+ from pypika.terms import PseudoColumn
13
+
14
+
15
+ class PseudoColumnsTest(unittest.TestCase):
16
+ @classmethod
17
+ def setUpClass(cls):
18
+ super(PseudoColumnsTest, cls).setUpClass()
19
+ cls.table1 = Table("table1")
20
+
21
+ def test_column_value(self):
22
+ self.assertEqual("COLUMN_VALUE", ColumnValue)
23
+
24
+ def test_object_id(self):
25
+ self.assertEqual("OBJECT_ID", ObjectID)
26
+
27
+ def test_object_value(self):
28
+ self.assertEqual("OBJECT_VALUE", ObjectValue)
29
+
30
+ def test_row_id(self):
31
+ self.assertEqual("ROWID", RowID)
32
+
33
+ def test_row_num(self):
34
+ self.assertEqual("ROWNUM", RowNum)
35
+
36
+ def test_sys_date(self):
37
+ self.assertEqual("SYSDATE", SysDate)
38
+
39
+ def test_can_be_used_in_a_select_statement(self):
40
+ query = Query.from_(self.table1).where(self.table1.is_active == 1).select(PseudoColumn("abcde"))
41
+
42
+ self.assertEqual(str(query), 'SELECT abcde FROM "table1" WHERE "is_active"=1')
43
+
44
+ def test_can_be_used_in_a_where_clause(self):
45
+ query = Query.from_(self.table1).where(PseudoColumn("abcde") > 1).select(self.table1.is_active)
46
+
47
+ self.assertEqual(str(query), 'SELECT "is_active" FROM "table1" WHERE abcde>1')
48
+
49
+ def test_can_be_used_in_orderby(self):
50
+ query = Query.from_(self.table1).select(self.table1.abcde.as_("some_name")).orderby(PseudoColumn("some_name"))
51
+ self.assertEqual(str(query), 'SELECT "abcde" "some_name" FROM "table1" ORDER BY some_name')
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_query.py ADDED
@@ -0,0 +1,242 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from pypika import Case, Query, Tables, Tuple, functions, Field
4
+ from pypika.dialects import (
5
+ ClickHouseQuery,
6
+ ClickHouseQueryBuilder,
7
+ MSSQLQuery,
8
+ MSSQLQueryBuilder,
9
+ MySQLLoadQueryBuilder,
10
+ MySQLQuery,
11
+ MySQLQueryBuilder,
12
+ OracleQuery,
13
+ OracleQueryBuilder,
14
+ PostgreSQLQuery,
15
+ PostgreSQLQueryBuilder,
16
+ RedShiftQueryBuilder,
17
+ RedshiftQuery,
18
+ SQLLiteQuery,
19
+ SQLLiteQueryBuilder,
20
+ SnowflakeQuery,
21
+ SnowflakeQueryBuilder,
22
+ VerticaCopyQueryBuilder,
23
+ VerticaCreateQueryBuilder,
24
+ VerticaQuery,
25
+ VerticaQueryBuilder,
26
+ )
27
+ from pypika.queries import CreateQueryBuilder, DropQueryBuilder, QueryBuilder
28
+
29
+
30
+ class QueryTablesTests(unittest.TestCase):
31
+ table_a, table_b, table_c, table_d = Tables("a", "b", "c", "d")
32
+
33
+ def test_replace_table(self):
34
+ query = Query.from_(self.table_a).select(self.table_a.time)
35
+ query = query.replace_table(self.table_a, self.table_b)
36
+
37
+ self.assertEqual('SELECT "time" FROM "b"', str(query))
38
+
39
+ def test_replace_only_specified_table(self):
40
+ query = Query.from_(self.table_a).select(self.table_a.time)
41
+ query = query.replace_table(self.table_b, self.table_c)
42
+
43
+ self.assertEqual('SELECT "time" FROM "a"', str(query))
44
+
45
+ def test_replace_insert_table(self):
46
+ query = Query.into(self.table_a).insert(1)
47
+ query = query.replace_table(self.table_a, self.table_b)
48
+
49
+ self.assertEqual('INSERT INTO "b" VALUES (1)', str(query))
50
+
51
+ def test_replace_insert_table_current_table_not_match(self):
52
+ query = Query.into(self.table_a).insert(1)
53
+ query = query.replace_table(self.table_c, self.table_b)
54
+
55
+ self.assertEqual('INSERT INTO "a" VALUES (1)', str(query))
56
+
57
+ def test_replace_update_table(self):
58
+ query = Query.update(self.table_a).set("foo", "bar")
59
+ query = query.replace_table(self.table_a, self.table_b)
60
+
61
+ self.assertEqual('UPDATE "b" SET "foo"=\'bar\'', str(query))
62
+
63
+ def test_replace_update_table_current_table_not_match(self):
64
+ query = Query.update(self.table_a).set("foo", "bar")
65
+ query = query.replace_table(self.table_c, self.table_b)
66
+
67
+ self.assertEqual('UPDATE "a" SET "foo"=\'bar\'', str(query))
68
+
69
+ def test_replace_delete_table(self):
70
+ query = Query.from_(self.table_a).delete()
71
+ query = query.replace_table(self.table_a, self.table_b)
72
+
73
+ self.assertEqual('DELETE FROM "b"', str(query))
74
+
75
+ def test_replace_join_tables(self):
76
+ query = (
77
+ Query.from_(self.table_a)
78
+ .join(self.table_b)
79
+ .on(self.table_a.customer_id == self.table_b.id)
80
+ .join(self.table_c)
81
+ .on(self.table_b.seller_id == self.table_c.id)
82
+ .select(self.table_a.star)
83
+ )
84
+ query = query.replace_table(self.table_a, self.table_d)
85
+
86
+ self.assertEqual(
87
+ 'SELECT "d".* '
88
+ 'FROM "d" '
89
+ 'JOIN "b" ON "d"."customer_id"="b"."id" '
90
+ 'JOIN "c" ON "b"."seller_id"="c"."id"',
91
+ str(query),
92
+ )
93
+
94
+ def test_replace_filter_tables(self):
95
+ query = Query.from_(self.table_a).select(self.table_a.name).where(self.table_a.name == "Mustermann")
96
+ query = query.replace_table(self.table_a, self.table_b)
97
+
98
+ self.assertEqual('SELECT "name" FROM "b" WHERE "name"=\'Mustermann\'', str(query))
99
+
100
+ def test_replace_having_table(self):
101
+ query = (
102
+ Query.from_(self.table_a)
103
+ .select(functions.Sum(self.table_a.revenue))
104
+ .groupby(self.table_a.customer)
105
+ .having(functions.Sum(self.table_a.revenue) >= 1000)
106
+ )
107
+ query = query.replace_table(self.table_a, self.table_b)
108
+
109
+ self.assertEqual(
110
+ 'SELECT SUM("revenue") ' 'FROM "b" ' 'GROUP BY "customer" ' 'HAVING SUM("revenue")>=1000',
111
+ str(query),
112
+ )
113
+
114
+ def test_replace_case_table(self):
115
+ query = Query.from_(self.table_a).select(
116
+ Case()
117
+ .when(self.table_a.fname == "Tom", "It was Tom")
118
+ .when(self.table_a.fname == "John", "It was John")
119
+ .else_("It was someone else.")
120
+ .as_("who_was_it")
121
+ )
122
+ query = query.replace_table(self.table_a, self.table_b)
123
+
124
+ self.assertEqual(
125
+ "SELECT CASE "
126
+ "WHEN \"fname\"='Tom' THEN 'It was Tom' "
127
+ "WHEN \"fname\"='John' THEN 'It was John' "
128
+ "ELSE 'It was someone else.' END \"who_was_it\" "
129
+ 'FROM "b"',
130
+ str(query),
131
+ )
132
+
133
+ def test_replace_orderby_table(self):
134
+ query = Query.from_(self.table_a).select(self.table_a.customer).orderby(self.table_a.customer)
135
+ query = query.replace_table(self.table_a, self.table_b)
136
+
137
+ self.assertEqual('SELECT "customer" FROM "b" ORDER BY "customer"', str(query))
138
+
139
+ def test_replace_tuple_table(self):
140
+ query = (
141
+ Query.from_(self.table_a)
142
+ .select(self.table_a.cost, self.table_a.revenue)
143
+ .where((self.table_a.cost, self.table_a.revenue) == Tuple(1, 2))
144
+ )
145
+
146
+ query = query.replace_table(self.table_a, self.table_b)
147
+
148
+ # Order is reversed due to lack of right equals method
149
+ self.assertEqual(
150
+ 'SELECT "cost","revenue" FROM "b" WHERE (1,2)=("cost","revenue")',
151
+ str(query),
152
+ )
153
+
154
+ def test_is_joined(self):
155
+ q = Query.from_(self.table_a).join(self.table_b).on(self.table_a.foo == self.table_b.boo)
156
+
157
+ self.assertTrue(q.is_joined(self.table_b))
158
+ self.assertFalse(q.is_joined(self.table_c))
159
+
160
+
161
+ class QueryBuilderTests(unittest.TestCase):
162
+ def test_query_builders_have_reference_to_correct_query_class(self):
163
+ with self.subTest('QueryBuilder'):
164
+ self.assertEqual(Query, QueryBuilder.QUERY_CLS)
165
+
166
+ with self.subTest('DropQueryBuilder'):
167
+ self.assertEqual(Query, DropQueryBuilder.QUERY_CLS)
168
+
169
+ with self.subTest('CreateQueryBuilder'):
170
+ self.assertEqual(Query, CreateQueryBuilder.QUERY_CLS)
171
+
172
+ with self.subTest('MySQLQueryBuilder'):
173
+ self.assertEqual(MySQLQuery, MySQLQueryBuilder.QUERY_CLS)
174
+
175
+ with self.subTest('MySQLLoadQueryBuilder'):
176
+ self.assertEqual(MySQLQuery, MySQLLoadQueryBuilder.QUERY_CLS)
177
+
178
+ with self.subTest('VerticaQueryBuilder'):
179
+ self.assertEqual(VerticaQuery, VerticaQueryBuilder.QUERY_CLS)
180
+
181
+ with self.subTest('VerticaCreateQueryBuilder'):
182
+ self.assertEqual(VerticaQuery, VerticaCreateQueryBuilder.QUERY_CLS)
183
+
184
+ with self.subTest('VerticaCopyQueryBuilder'):
185
+ self.assertEqual(VerticaQuery, VerticaCopyQueryBuilder.QUERY_CLS)
186
+
187
+ with self.subTest('PostgreSQLQueryBuilder'):
188
+ self.assertEqual(PostgreSQLQuery, PostgreSQLQueryBuilder.QUERY_CLS)
189
+
190
+ with self.subTest('MSSQLQueryBuilder'):
191
+ self.assertEqual(MSSQLQuery, MSSQLQueryBuilder.QUERY_CLS)
192
+
193
+ with self.subTest('SnowflakeQueryBuilder'):
194
+ self.assertEqual(SnowflakeQuery, SnowflakeQueryBuilder.QUERY_CLS)
195
+
196
+ with self.subTest('ClickHouseQueryBuilder'):
197
+ self.assertEqual(ClickHouseQuery, ClickHouseQueryBuilder.QUERY_CLS)
198
+
199
+ with self.subTest('RedShiftQueryBuilder'):
200
+ self.assertEqual(RedshiftQuery, RedShiftQueryBuilder.QUERY_CLS)
201
+
202
+ with self.subTest('SQLLiteQueryBuilder'):
203
+ self.assertEqual(SQLLiteQuery, SQLLiteQueryBuilder.QUERY_CLS)
204
+
205
+ with self.subTest('OracleQueryBuilder'):
206
+ self.assertEqual(OracleQuery, OracleQueryBuilder.QUERY_CLS)
207
+
208
+ def test_pipe(self) -> None:
209
+ base_query = Query.from_("test")
210
+
211
+ def select(query: QueryBuilder) -> QueryBuilder:
212
+ return query.select("test1", "test2")
213
+
214
+ def count_group(query: QueryBuilder, *groups) -> QueryBuilder:
215
+ return query.groupby(*groups).select(*groups, functions.Count("*"))
216
+
217
+ for func, args, kwargs, expected_str in [
218
+ (select, [], {}, 'SELECT "test1","test2" FROM "test"'),
219
+ (
220
+ count_group,
221
+ ["test1", "test2"],
222
+ {},
223
+ 'SELECT "test1","test2",COUNT(*) FROM "test" GROUP BY "test1","test2"',
224
+ ),
225
+ (count_group, ["test1"], {}, 'SELECT "test1",COUNT(*) FROM "test" GROUP BY "test1"'),
226
+ ]:
227
+ result_str = str(base_query.pipe(func, *args, **kwargs))
228
+ self.assertEqual(result_str, str(func(base_query, *args, **kwargs)))
229
+ self.assertEqual(result_str, expected_str)
230
+
231
+ def where_clause(query: QueryBuilder, num_days: int) -> QueryBuilder:
232
+ return query.where(Field("date") > functions.Now() - num_days)
233
+
234
+ result_str = str(base_query.pipe(select).pipe(where_clause, num_days=1))
235
+ self.assertEqual(
236
+ result_str,
237
+ str(select(where_clause(base_query, num_days=1))),
238
+ )
239
+ self.assertEqual(
240
+ result_str,
241
+ 'SELECT "test1","test2" FROM "test" WHERE "date">NOW()-1',
242
+ )
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_selects.py ADDED
@@ -0,0 +1,1331 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+ from datetime import date
3
+ from enum import Enum
4
+
5
+ from pypika import (
6
+ AliasedQuery,
7
+ Case,
8
+ ClickHouseQuery,
9
+ EmptyCriterion,
10
+ Field as F,
11
+ Index,
12
+ MSSQLQuery,
13
+ MySQLQuery,
14
+ NullValue,
15
+ OracleQuery,
16
+ Order,
17
+ PostgreSQLQuery,
18
+ Query,
19
+ QueryException,
20
+ RedshiftQuery,
21
+ SQLLiteQuery,
22
+ Table,
23
+ Tables,
24
+ VerticaQuery,
25
+ functions as fn,
26
+ SYSTEM_TIME,
27
+ )
28
+ from pypika.terms import ValueWrapper
29
+
30
+ __author__ = "Timothy Heys"
31
+ __email__ = "theys@kayak.com"
32
+
33
+
34
+ class SelectTests(unittest.TestCase):
35
+ table_abc, table_efg = Tables("abc", "efg")
36
+
37
+ def test_empty_query(self):
38
+ q = Query.from_("abc")
39
+
40
+ self.assertEqual("", str(q))
41
+
42
+ def test_select_no_from(self):
43
+ q = Query.select(1)
44
+
45
+ self.assertEqual("SELECT 1", str(q))
46
+
47
+ def test_select_no_with_alias_from(self):
48
+ q = Query.select(ValueWrapper(1, "test"))
49
+
50
+ self.assertEqual('SELECT 1 "test"', str(q))
51
+
52
+ def test_select_no_from_with_field_raises_exception(self):
53
+ with self.assertRaises(QueryException):
54
+ Query.select("asdf")
55
+
56
+ def test_select__star(self):
57
+ q = Query.from_("abc").select("*")
58
+
59
+ self.assertEqual('SELECT * FROM "abc"', str(q))
60
+
61
+ def test_select__table_schema(self):
62
+ q = Query.from_(Table("abc", "schema1")).select("*")
63
+
64
+ self.assertEqual('SELECT * FROM "schema1"."abc"', str(q))
65
+
66
+ def test_select__table_schema_with_multiple_levels_as_tuple(self):
67
+ q = Query.from_(Table("abc", ("schema1", "schema2"))).select("*")
68
+
69
+ self.assertEqual('SELECT * FROM "schema1"."schema2"."abc"', str(q))
70
+
71
+ def test_select__table_schema_with_multiple_levels_as_list(self):
72
+ q = Query.from_(Table("abc", ["schema1", "schema2"])).select("*")
73
+
74
+ self.assertEqual('SELECT * FROM "schema1"."schema2"."abc"', str(q))
75
+
76
+ def test_select__star__replacement(self):
77
+ q = Query.from_("abc").select("foo").select("*")
78
+
79
+ self.assertEqual('SELECT * FROM "abc"', str(q))
80
+
81
+ def test_select__distinct__single(self):
82
+ q = Query.from_("abc").select("foo").distinct()
83
+
84
+ self.assertEqual('SELECT DISTINCT "foo" FROM "abc"', str(q))
85
+
86
+ def test_select__distinct__multi(self):
87
+ q = Query.from_("abc").select("foo", "bar").distinct()
88
+
89
+ self.assertEqual('SELECT DISTINCT "foo","bar" FROM "abc"', str(q))
90
+
91
+ def test_select__column__single__str(self):
92
+ q = Query.from_("abc").select("foo")
93
+
94
+ self.assertEqual('SELECT "foo" FROM "abc"', str(q))
95
+
96
+ def test_select__column__single__alias__str(self):
97
+ q = Query.from_(self.table_abc).select(self.table_abc.foo.as_("bar"))
98
+
99
+ self.assertEqual('SELECT "foo" "bar" FROM "abc"', str(q))
100
+
101
+ def test_select__column__single__table_alias__str(self):
102
+ q = Query.from_(self.table_abc.as_("fizzbuzz")).select(self.table_abc.foo.as_("bar"))
103
+
104
+ self.assertEqual('SELECT "foo" "bar" FROM "abc" "fizzbuzz"', str(q))
105
+
106
+ def test_select__column__single__field(self):
107
+ t = Table("abc")
108
+ q = Query.from_(t).select(t.foo)
109
+
110
+ self.assertEqual('SELECT "foo" FROM "abc"', str(q))
111
+
112
+ def test_select__columns__multi__str(self):
113
+ q1 = Query.from_("abc").select("foo", "bar")
114
+ q2 = Query.from_("abc").select("foo").select("bar")
115
+
116
+ self.assertEqual('SELECT "foo","bar" FROM "abc"', str(q1))
117
+ self.assertEqual('SELECT "foo","bar" FROM "abc"', str(q2))
118
+
119
+ def test_select__columns__multi__field(self):
120
+ q1 = Query.from_(self.table_abc).select(self.table_abc.foo, self.table_abc.bar)
121
+ q2 = Query.from_(self.table_abc).select(self.table_abc.foo).select(self.table_abc.bar)
122
+
123
+ self.assertEqual('SELECT "foo","bar" FROM "abc"', str(q1))
124
+ self.assertEqual('SELECT "foo","bar" FROM "abc"', str(q2))
125
+
126
+ def test_select__multiple_tables(self):
127
+ q = Query.from_(self.table_abc).select(self.table_abc.foo).from_(self.table_efg).select(self.table_efg.bar)
128
+
129
+ self.assertEqual('SELECT "abc"."foo","efg"."bar" FROM "abc","efg"', str(q))
130
+
131
+ def test_select__subquery(self):
132
+ subquery = Query.from_(self.table_abc).select("*")
133
+ q = Query.from_(subquery).select(subquery.foo, subquery.bar)
134
+
135
+ self.assertEqual('SELECT "sq0"."foo","sq0"."bar" ' 'FROM (SELECT * FROM "abc") "sq0"', str(q))
136
+
137
+ def test_select__multiple_subqueries(self):
138
+ subquery0 = Query.from_(self.table_abc).select("foo")
139
+ subquery1 = Query.from_(self.table_efg).select("bar")
140
+ q = Query.from_(subquery0).from_(subquery1).select(subquery0.foo, subquery1.bar)
141
+
142
+ self.assertEqual(
143
+ 'SELECT "sq0"."foo","sq1"."bar" ' 'FROM (SELECT "foo" FROM "abc") "sq0",' '(SELECT "bar" FROM "efg") "sq1"',
144
+ str(q),
145
+ )
146
+
147
+ def test_select__nested_subquery(self):
148
+ subquery0 = Query.from_(self.table_abc).select("*")
149
+ subquery1 = Query.from_(subquery0).select(subquery0.foo, subquery0.bar)
150
+ subquery2 = Query.from_(subquery1).select(subquery1.foo)
151
+
152
+ q = Query.from_(subquery2).select(subquery2.foo)
153
+
154
+ self.assertEqual(
155
+ 'SELECT "sq2"."foo" '
156
+ 'FROM (SELECT "sq1"."foo" '
157
+ 'FROM (SELECT "sq0"."foo","sq0"."bar" '
158
+ 'FROM (SELECT * FROM "abc") "sq0") "sq1") "sq2"',
159
+ str(q),
160
+ )
161
+
162
+ def test_select__no_table(self):
163
+ q = Query.select(1, 2, 3)
164
+
165
+ self.assertEqual("SELECT 1,2,3", str(q))
166
+
167
+ def test_select_then_add_table(self):
168
+ q = Query.select(1).select(2, 3).from_("abc").select("foo")
169
+
170
+ self.assertEqual('SELECT 1,2,3,"foo" FROM "abc"', str(q))
171
+
172
+ def test_select_with_limit(self):
173
+ q1 = Query.from_("abc").select("foo")[:10]
174
+
175
+ self.assertEqual('SELECT "foo" FROM "abc" LIMIT 10', str(q1))
176
+
177
+ def test_select_with_limit_zero(self):
178
+ with self.subTest('using python slice'):
179
+ q1 = Query.from_("abc").select("foo")[:0]
180
+ self.assertEqual('SELECT "foo" FROM "abc" LIMIT 0', str(q1))
181
+
182
+ with self.subTest('using limit method'):
183
+ q2 = Query.from_("abc").select("foo").limit(0)
184
+ self.assertEqual('SELECT "foo" FROM "abc" LIMIT 0', str(q2))
185
+
186
+ def test_select_with_limit__func(self):
187
+ q1 = Query.from_("abc").select("foo").limit(10)
188
+
189
+ self.assertEqual('SELECT "foo" FROM "abc" LIMIT 10', str(q1))
190
+
191
+ def test_select_with_offset(self):
192
+ q1 = Query.from_("abc").select("foo")[10:]
193
+
194
+ self.assertEqual('SELECT "foo" FROM "abc" OFFSET 10', str(q1))
195
+
196
+ def test_select_with_offset__func(self):
197
+ q1 = Query.from_("abc").select("foo").offset(10)
198
+
199
+ self.assertEqual('SELECT "foo" FROM "abc" OFFSET 10', str(q1))
200
+
201
+ def test_select_with_limit_and_offset(self):
202
+ q1 = Query.from_("abc").select("foo")[10:10]
203
+
204
+ self.assertEqual('SELECT "foo" FROM "abc" LIMIT 10 OFFSET 10', str(q1))
205
+
206
+ def test_select_with_force_index(self):
207
+ q = Query.from_("abc").select("foo").force_index("egg")
208
+
209
+ self.assertEqual('SELECT "foo" FROM "abc" FORCE INDEX ("egg")', str(q))
210
+
211
+ def test_select_with_force_index_with_index_object(self):
212
+ index = Index("egg")
213
+ q = Query.from_("abc").select("foo").force_index(index)
214
+
215
+ self.assertEqual('SELECT "foo" FROM "abc" FORCE INDEX ("egg")', str(q))
216
+
217
+ def test_select_with_force_index_multiple_indexes(self):
218
+ q = Query.from_("abc").select("foo").force_index("egg", "bacon")
219
+
220
+ self.assertEqual('SELECT "foo" FROM "abc" FORCE INDEX ("egg","bacon")', str(q))
221
+
222
+ def test_select_with_force_index_multiple_calls(self):
223
+ q = (
224
+ Query.from_("abc")
225
+ .select("foo")
226
+ .force_index(
227
+ "egg",
228
+ )
229
+ .force_index("spam")
230
+ )
231
+
232
+ self.assertEqual('SELECT "foo" FROM "abc" FORCE INDEX ("egg","spam")', str(q))
233
+
234
+ def test_select_with_use_index(self):
235
+ q = Query.from_("abc").select("foo").use_index("egg")
236
+
237
+ self.assertEqual('SELECT "foo" FROM "abc" USE INDEX ("egg")', str(q))
238
+
239
+ def test_select_with_use_index_with_index_object(self):
240
+ index = Index("egg")
241
+ q = Query.from_("abc").select("foo").use_index(index)
242
+
243
+ self.assertEqual('SELECT "foo" FROM "abc" USE INDEX ("egg")', str(q))
244
+
245
+ def test_select_with_use_index_multiple_indexes(self):
246
+ q = Query.from_("abc").select("foo").use_index("egg", "bacon")
247
+
248
+ self.assertEqual('SELECT "foo" FROM "abc" USE INDEX ("egg","bacon")', str(q))
249
+
250
+ def test_select_with_use_index_multiple_calls(self):
251
+ q = (
252
+ Query.from_("abc")
253
+ .select("foo")
254
+ .use_index(
255
+ "egg",
256
+ )
257
+ .use_index("spam")
258
+ )
259
+
260
+ self.assertEqual('SELECT "foo" FROM "abc" USE INDEX ("egg","spam")', str(q))
261
+
262
+ def test_mysql_query_uses_backtick_quote_chars(self):
263
+ q = MySQLQuery.from_("abc").select("foo", "bar")
264
+
265
+ self.assertEqual("SELECT `foo`,`bar` FROM `abc`", str(q))
266
+
267
+ def test_vertica_query_uses_double_quote_chars(self):
268
+ q = VerticaQuery.from_("abc").select("foo", "bar")
269
+
270
+ self.assertEqual('SELECT "foo","bar" FROM "abc"', str(q))
271
+
272
+ def test_mssql_query_uses_double_quote_chars(self):
273
+ q = MSSQLQuery.from_("abc").select("foo", "bar")
274
+
275
+ self.assertEqual('SELECT "foo","bar" FROM "abc"', str(q))
276
+
277
+ def test_oracle_query_uses_no_quote_chars(self):
278
+ q = OracleQuery.from_("abc").select("foo", "bar")
279
+
280
+ self.assertEqual('SELECT foo,bar FROM abc', str(q))
281
+
282
+ def test_postgresql_query_uses_double_quote_chars(self):
283
+ q = PostgreSQLQuery.from_("abc").select("foo", "bar")
284
+
285
+ self.assertEqual('SELECT "foo","bar" FROM "abc"', str(q))
286
+
287
+ def test_redshift_query_uses_double_quote_chars(self):
288
+ q = RedshiftQuery.from_("abc").select("foo", "bar")
289
+
290
+ self.assertEqual('SELECT "foo","bar" FROM "abc"', str(q))
291
+
292
+ def test_table_select_alias(self):
293
+ q = self.table_abc.select(1)
294
+
295
+ self.assertEqual('SELECT 1 FROM "abc"', str(q))
296
+ self.assertEqual(q, Query.from_("abc").select(1))
297
+
298
+ def test_table_select_alias_with_offset_and_limit(self):
299
+ self.assertEqual(self.table_abc.select("foo")[10:10], Query.from_("abc").select("foo")[10:10])
300
+ self.assertEqual(
301
+ self.table_abc.select(self.table_abc.foo)[10:10],
302
+ Query.from_("abc").select("foo")[10:10],
303
+ )
304
+
305
+ def test_temporal_select(self):
306
+ t = Table("abc")
307
+
308
+ with self.subTest("with system time as of"):
309
+ q = Query.from_(t.for_(SYSTEM_TIME.as_of('2020-01-01'))).select("*")
310
+
311
+ self.assertEqual('SELECT * FROM "abc" FOR SYSTEM_TIME AS OF \'2020-01-01\'', str(q))
312
+
313
+ with self.subTest("with system time between"):
314
+ q = Query.from_(t.for_(SYSTEM_TIME.between('2020-01-01', '2020-02-01'))).select("*")
315
+
316
+ self.assertEqual('SELECT * FROM "abc" FOR SYSTEM_TIME BETWEEN \'2020-01-01\' AND \'2020-02-01\'', str(q))
317
+
318
+ with self.subTest("with system time from to"):
319
+ q = Query.from_(t.for_(SYSTEM_TIME.from_to('2020-01-01', '2020-02-01'))).select("*")
320
+
321
+ self.assertEqual('SELECT * FROM "abc" FOR SYSTEM_TIME FROM \'2020-01-01\' TO \'2020-02-01\'', str(q))
322
+
323
+ with self.subTest("with ALL"):
324
+ q = Query.from_(t.for_(SYSTEM_TIME.all_())).select("*")
325
+
326
+ self.assertEqual('SELECT * FROM "abc" FOR SYSTEM_TIME ALL', str(q))
327
+
328
+ with self.subTest("with period between"):
329
+ q = Query.from_(t.for_(t.valid_period.between('2020-01-01', '2020-02-01'))).select("*")
330
+
331
+ self.assertEqual('SELECT * FROM "abc" FOR "valid_period" BETWEEN \'2020-01-01\' AND \'2020-02-01\'', str(q))
332
+
333
+ with self.subTest("with period from to"):
334
+ q = Query.from_(t.for_(t.valid_period.from_to('2020-01-01', '2020-02-01'))).select("*")
335
+
336
+ self.assertEqual('SELECT * FROM "abc" FOR "valid_period" FROM \'2020-01-01\' TO \'2020-02-01\'', str(q))
337
+
338
+ with self.subTest("with ALL"):
339
+ q = Query.from_(t.for_(t.valid_period.all_())).select("*")
340
+
341
+ self.assertEqual('SELECT * FROM "abc" FOR "valid_period" ALL', str(q))
342
+
343
+
344
+ class MyEnum(Enum):
345
+ STR = "foo"
346
+ INT = 0
347
+ BOOL = True
348
+ DATE = date(2020, 2, 2)
349
+ NONE = None
350
+
351
+
352
+ class WhereTests(unittest.TestCase):
353
+ t = Table("abc")
354
+ t2 = Table("cba")
355
+
356
+ def test_where_enum(self):
357
+ q1 = Query.from_(self.t).select("*").where(self.t.foo == MyEnum.STR)
358
+ q2 = Query.from_(self.t).select("*").where(self.t.foo == MyEnum.INT)
359
+ q3 = Query.from_(self.t).select("*").where(self.t.foo == MyEnum.BOOL)
360
+ q4 = Query.from_(self.t).select("*").where(self.t.foo == MyEnum.DATE)
361
+ q5 = Query.from_(self.t).select("*").where(self.t.foo == MyEnum.NONE)
362
+
363
+ self.assertEqual('SELECT * FROM "abc" WHERE "foo"=\'foo\'', str(q1))
364
+ self.assertEqual('SELECT * FROM "abc" WHERE "foo"=0', str(q2))
365
+ self.assertEqual('SELECT * FROM "abc" WHERE "foo"=true', str(q3))
366
+ self.assertEqual('SELECT * FROM "abc" WHERE "foo"=\'2020-02-02\'', str(q4))
367
+ self.assertEqual('SELECT * FROM "abc" WHERE "foo"=null', str(q5))
368
+
369
+ def test_where_field_equals(self):
370
+ q1 = Query.from_(self.t).select("*").where(self.t.foo == self.t.bar)
371
+ q2 = Query.from_(self.t).select("*").where(self.t.foo.eq(self.t.bar))
372
+
373
+ self.assertEqual('SELECT * FROM "abc" WHERE "foo"="bar"', str(q1))
374
+ self.assertEqual('SELECT * FROM "abc" WHERE "foo"="bar"', str(q2))
375
+ q = self.t.select("*").where(self.t.foo == self.t.bar)
376
+ self.assertEqual(q, q1)
377
+
378
+ def test_where_field_equals_for_update(self):
379
+ q = Query.from_(self.t).select("*").where(self.t.foo == self.t.bar).for_update()
380
+ self.assertEqual('SELECT * FROM "abc" WHERE "foo"="bar" FOR UPDATE', str(q))
381
+
382
+ def test_where_field_equals_for_update_nowait(self):
383
+ for query_cls in [
384
+ MySQLQuery,
385
+ PostgreSQLQuery,
386
+ ]:
387
+ quote_char = query_cls._builder().QUOTE_CHAR if isinstance(query_cls._builder().QUOTE_CHAR, str) else '"'
388
+ q = query_cls.from_(self.t).select("*").where(self.t.foo == self.t.bar).for_update(nowait=True)
389
+ self.assertEqual(
390
+ 'SELECT * '
391
+ 'FROM {quote_char}abc{quote_char} '
392
+ 'WHERE {quote_char}foo{quote_char}={quote_char}bar{quote_char} '
393
+ 'FOR UPDATE NOWAIT'.format(
394
+ quote_char=quote_char,
395
+ ),
396
+ str(q),
397
+ )
398
+
399
+ def test_where_field_equals_for_update_skip_locked(self):
400
+ for query_cls in [
401
+ MySQLQuery,
402
+ PostgreSQLQuery,
403
+ ]:
404
+ quote_char = query_cls._builder().QUOTE_CHAR if isinstance(query_cls._builder().QUOTE_CHAR, str) else '"'
405
+ q = query_cls.from_(self.t).select("*").where(self.t.foo == self.t.bar).for_update(skip_locked=True)
406
+ self.assertEqual(
407
+ 'SELECT * '
408
+ 'FROM {quote_char}abc{quote_char} '
409
+ 'WHERE {quote_char}foo{quote_char}={quote_char}bar{quote_char} '
410
+ 'FOR UPDATE SKIP LOCKED'.format(
411
+ quote_char=quote_char,
412
+ ),
413
+ str(q),
414
+ )
415
+
416
+ def test_where_field_equals_for_update_of(self):
417
+ for query_cls in [
418
+ MySQLQuery,
419
+ PostgreSQLQuery,
420
+ ]:
421
+ quote_char = query_cls._builder().QUOTE_CHAR if isinstance(query_cls._builder().QUOTE_CHAR, str) else '"'
422
+ q = query_cls.from_(self.t).select("*").where(self.t.foo == self.t.bar).for_update(of=("abc",))
423
+ self.assertEqual(
424
+ 'SELECT * '
425
+ 'FROM {quote_char}abc{quote_char} '
426
+ 'WHERE {quote_char}foo{quote_char}={quote_char}bar{quote_char} '
427
+ 'FOR UPDATE OF {quote_char}abc{quote_char}'.format(
428
+ quote_char=quote_char,
429
+ ),
430
+ str(q),
431
+ )
432
+
433
+ def test_where_field_equals_for_update_of_multiple_tables(self):
434
+ for query_cls in [
435
+ MySQLQuery,
436
+ PostgreSQLQuery,
437
+ ]:
438
+ q = (
439
+ query_cls.from_(self.t)
440
+ .join(self.t2)
441
+ .on(self.t.id == self.t2.abc_id)
442
+ .select("*")
443
+ .where(self.t.foo == self.t.bar)
444
+ .for_update(of=("abc", "cba"))
445
+ )
446
+ quote_char = query_cls._builder().QUOTE_CHAR if isinstance(query_cls._builder().QUOTE_CHAR, str) else '"'
447
+ self.assertIn(
448
+ str(q),
449
+ [
450
+ 'SELECT * '
451
+ 'FROM {quote_char}abc{quote_char} '
452
+ 'JOIN {quote_char}cba{quote_char} '
453
+ 'ON {quote_char}abc{quote_char}.{quote_char}id{quote_char}='
454
+ '{quote_char}cba{quote_char}.{quote_char}abc_id{quote_char} '
455
+ 'WHERE {quote_char}abc{quote_char}.{quote_char}foo{quote_char}='
456
+ '{quote_char}abc{quote_char}.{quote_char}bar{quote_char} '
457
+ 'FOR UPDATE OF {quote_char}cba{quote_char}, {quote_char}abc{quote_char}'.format(
458
+ quote_char=quote_char,
459
+ ),
460
+ 'SELECT * '
461
+ 'FROM {quote_char}abc{quote_char} '
462
+ 'JOIN {quote_char}cba{quote_char} '
463
+ 'ON {quote_char}abc{quote_char}.{quote_char}id{quote_char}='
464
+ '{quote_char}cba{quote_char}.{quote_char}abc_id{quote_char} '
465
+ 'WHERE {quote_char}abc{quote_char}.{quote_char}foo{quote_char}='
466
+ '{quote_char}abc{quote_char}.{quote_char}bar{quote_char} '
467
+ 'FOR UPDATE OF {quote_char}abc{quote_char}, {quote_char}cba{quote_char}'.format(
468
+ quote_char=quote_char,
469
+ ),
470
+ ],
471
+ )
472
+
473
+ def test_where_field_equals_for_update_of_nowait(self):
474
+ for query_cls in [
475
+ MySQLQuery,
476
+ PostgreSQLQuery,
477
+ ]:
478
+ q = query_cls.from_(self.t).select("*").where(self.t.foo == self.t.bar).for_update(of=("abc",), nowait=True)
479
+ quote_char = query_cls._builder().QUOTE_CHAR if isinstance(query_cls._builder().QUOTE_CHAR, str) else '"'
480
+ self.assertEqual(
481
+ 'SELECT * '
482
+ 'FROM {quote_char}abc{quote_char} '
483
+ 'WHERE {quote_char}foo{quote_char}={quote_char}bar{quote_char} '
484
+ 'FOR UPDATE OF {quote_char}abc{quote_char} NOWAIT'.format(
485
+ quote_char=quote_char,
486
+ ),
487
+ str(q),
488
+ )
489
+
490
+ def test_where_field_equals_for_update_of_skip_locked(self):
491
+ for query_cls in [
492
+ MySQLQuery,
493
+ PostgreSQLQuery,
494
+ ]:
495
+ q = (
496
+ query_cls.from_(self.t)
497
+ .select("*")
498
+ .where(self.t.foo == self.t.bar)
499
+ .for_update(of=("abc",), skip_locked=True)
500
+ )
501
+ quote_char = query_cls._builder().QUOTE_CHAR if isinstance(query_cls._builder().QUOTE_CHAR, str) else '"'
502
+ self.assertEqual(
503
+ 'SELECT * '
504
+ 'FROM {quote_char}abc{quote_char} '
505
+ 'WHERE {quote_char}foo{quote_char}={quote_char}bar{quote_char} '
506
+ 'FOR UPDATE OF {quote_char}abc{quote_char} SKIP LOCKED'.format(
507
+ quote_char=quote_char,
508
+ ),
509
+ str(q),
510
+ )
511
+
512
+ def test_where_field_equals_for_update_skip_locked_and_of(self):
513
+ for query_cls in [
514
+ MySQLQuery,
515
+ PostgreSQLQuery,
516
+ ]:
517
+ q = (
518
+ query_cls.from_(self.t)
519
+ .select("*")
520
+ .where(self.t.foo == self.t.bar)
521
+ .for_update(nowait=False, skip_locked=True, of=("abc",))
522
+ )
523
+ quote_char = query_cls._builder().QUOTE_CHAR if isinstance(query_cls._builder().QUOTE_CHAR, str) else '"'
524
+ self.assertEqual(
525
+ 'SELECT * '
526
+ 'FROM {quote_char}abc{quote_char} '
527
+ 'WHERE {quote_char}foo{quote_char}={quote_char}bar{quote_char} '
528
+ 'FOR UPDATE OF {quote_char}abc{quote_char} SKIP LOCKED'.format(
529
+ quote_char=quote_char,
530
+ ),
531
+ str(q),
532
+ )
533
+
534
+ def test_where_field_equals_where(self):
535
+ q = Query.from_(self.t).select("*").where(self.t.foo == 1).where(self.t.bar == self.t.baz)
536
+
537
+ self.assertEqual('SELECT * FROM "abc" WHERE "foo"=1 AND "bar"="baz"', str(q))
538
+
539
+ def test_where_field_equals_where_not(self):
540
+ q = Query.from_(self.t).select("*").where((self.t.foo == 1).negate()).where(self.t.bar == self.t.baz)
541
+
542
+ self.assertEqual('SELECT * FROM "abc" WHERE NOT "foo"=1 AND "bar"="baz"', str(q))
543
+
544
+ def test_where_field_equals_where_two_not(self):
545
+ q = Query.from_(self.t).select("*").where((self.t.foo == 1).negate()).where((self.t.bar == self.t.baz).negate())
546
+
547
+ self.assertEqual('SELECT * FROM "abc" WHERE NOT "foo"=1 AND NOT "bar"="baz"', str(q))
548
+
549
+ def test_where_single_quote(self):
550
+ q1 = Query.from_(self.t).select("*").where(self.t.foo == "bar'foo")
551
+
552
+ self.assertEqual("SELECT * FROM \"abc\" WHERE \"foo\"='bar''foo'", str(q1))
553
+
554
+ def test_where_field_equals_and(self):
555
+ q = Query.from_(self.t).select("*").where((self.t.foo == 1) & (self.t.bar == self.t.baz))
556
+
557
+ self.assertEqual('SELECT * FROM "abc" WHERE "foo"=1 AND "bar"="baz"', str(q))
558
+
559
+ def test_where_field_equals_or(self):
560
+ q = Query.from_(self.t).select("*").where((self.t.foo == 1) | (self.t.bar == self.t.baz))
561
+
562
+ self.assertEqual('SELECT * FROM "abc" WHERE "foo"=1 OR "bar"="baz"', str(q))
563
+
564
+ def test_where_nested_conditions(self):
565
+ q = Query.from_(self.t).select("*").where((self.t.foo == 1) | (self.t.bar == self.t.baz)).where(self.t.baz == 0)
566
+
567
+ self.assertEqual('SELECT * FROM "abc" WHERE ("foo"=1 OR "bar"="baz") AND "baz"=0', str(q))
568
+
569
+ def test_where_field_starts_with(self):
570
+ q = Query.from_(self.t).select(self.t.star).where(self.t.foo.like("ab%"))
571
+
572
+ self.assertEqual('SELECT * FROM "abc" WHERE "foo" LIKE \'ab%\'', str(q))
573
+
574
+ def test_where_field_contains(self):
575
+ q = Query.from_(self.t).select(self.t.star).where(self.t.foo.like("%fg%"))
576
+
577
+ self.assertEqual('SELECT * FROM "abc" WHERE "foo" LIKE \'%fg%\'', str(q))
578
+
579
+ def test_where_field_ends_with(self):
580
+ q = Query.from_(self.t).select(self.t.star).where(self.t.foo.like("%yz"))
581
+
582
+ self.assertEqual('SELECT * FROM "abc" WHERE "foo" LIKE \'%yz\'', str(q))
583
+
584
+ def test_where_field_is_n_chars_long(self):
585
+ q = Query.from_(self.t).select(self.t.star).where(self.t.foo.like("___"))
586
+
587
+ self.assertEqual('SELECT * FROM "abc" WHERE "foo" LIKE \'___\'', str(q))
588
+
589
+ def test_where_field_does_not_start_with(self):
590
+ q = Query.from_(self.t).select(self.t.star).where(self.t.foo.not_like("ab%"))
591
+
592
+ self.assertEqual('SELECT * FROM "abc" WHERE "foo" NOT LIKE \'ab%\'', str(q))
593
+
594
+ def test_where_field_does_not_contain(self):
595
+ q = Query.from_(self.t).select(self.t.star).where(self.t.foo.not_like("%fg%"))
596
+
597
+ self.assertEqual('SELECT * FROM "abc" WHERE "foo" NOT LIKE \'%fg%\'', str(q))
598
+
599
+ def test_where_field_does_not_end_with(self):
600
+ q = Query.from_(self.t).select(self.t.star).where(self.t.foo.not_like("%yz"))
601
+
602
+ self.assertEqual('SELECT * FROM "abc" WHERE "foo" NOT LIKE \'%yz\'', str(q))
603
+
604
+ def test_where_field_is_not_n_chars_long(self):
605
+ q = Query.from_(self.t).select(self.t.star).where(self.t.foo.not_like("___"))
606
+
607
+ self.assertEqual('SELECT * FROM "abc" WHERE "foo" NOT LIKE \'___\'', str(q))
608
+
609
+ def test_where_field_matches_regex(self):
610
+ q = Query.from_(self.t).select(self.t.star).where(self.t.foo.regex(r"^b"))
611
+
612
+ self.assertEqual('SELECT * FROM "abc" WHERE "foo" REGEX \'^b\'', str(q))
613
+
614
+ def test_where_field_matches_regexp(self):
615
+ q = Query.from_(self.t).select(self.t.star).where(self.t.foo.regexp(r"^b"))
616
+
617
+ self.assertEqual('SELECT * FROM "abc" WHERE "foo" REGEXP \'^b\'', str(q))
618
+
619
+ def test_where_field_matches_rlike(self):
620
+ q = Query.from_(self.t).select(self.t.star).where(self.t.foo.rlike(r"^b"))
621
+
622
+ self.assertEqual('SELECT * FROM "abc" WHERE "foo" RLIKE \'^b\'', str(q))
623
+
624
+ def test_ignore_empty_criterion_where(self):
625
+ q1 = Query.from_(self.t).select("*").where(EmptyCriterion())
626
+
627
+ self.assertEqual('SELECT * FROM "abc"', str(q1))
628
+
629
+ def test_ignore_empty_criterion_having(self):
630
+ q1 = Query.from_(self.t).select("*").having(EmptyCriterion())
631
+
632
+ self.assertEqual('SELECT * FROM "abc"', str(q1))
633
+
634
+ def test_select_with_force_index_and_where(self):
635
+ q = Query.from_("abc").select("foo").where(self.t.foo == self.t.bar).force_index("egg")
636
+
637
+ self.assertEqual('SELECT "foo" FROM "abc" FORCE INDEX ("egg") WHERE "foo"="bar"', str(q))
638
+
639
+ def test_where_with_multiple_wheres_using_and_case(self):
640
+ case_stmt = Case().when(self.t.foo == 'bar', 1).else_(0)
641
+ query = Query.from_(self.t).select(case_stmt).where(case_stmt & self.t.blah.isin(['test']))
642
+
643
+ self.assertEqual(
644
+ 'SELECT CASE WHEN "foo"=\'bar\' THEN 1 ELSE 0 END FROM "abc" WHERE CASE WHEN "foo"=\'bar\' THEN 1 ELSE 0 '
645
+ 'END AND "blah" IN (\'test\')',
646
+ str(query),
647
+ )
648
+
649
+ def test_where_with_multiple_wheres_using_or_case(self):
650
+ case_stmt = Case().when(self.t.foo == 'bar', 1).else_(0)
651
+ query = Query.from_(self.t).select(case_stmt).where(case_stmt | self.t.blah.isin(['test']))
652
+
653
+ self.assertEqual(
654
+ 'SELECT CASE WHEN "foo"=\'bar\' THEN 1 ELSE 0 END FROM "abc" WHERE CASE WHEN "foo"=\'bar\' THEN 1 ELSE 0 '
655
+ 'END OR "blah" IN (\'test\')',
656
+ str(query),
657
+ )
658
+
659
+
660
+ class PreWhereTests(WhereTests):
661
+ t = Table("abc")
662
+
663
+ def test_prewhere_field_equals(self):
664
+ q1 = Query.from_(self.t).select("*").prewhere(self.t.foo == self.t.bar)
665
+ q2 = Query.from_(self.t).select("*").prewhere(self.t.foo.eq(self.t.bar))
666
+
667
+ self.assertEqual('SELECT * FROM "abc" PREWHERE "foo"="bar"', str(q1))
668
+ self.assertEqual('SELECT * FROM "abc" PREWHERE "foo"="bar"', str(q2))
669
+
670
+ def test_where_and_prewhere(self):
671
+ q = Query.from_(self.t).select("*").prewhere(self.t.foo == self.t.bar).where(self.t.foo == self.t.bar)
672
+
673
+ self.assertEqual('SELECT * FROM "abc" PREWHERE "foo"="bar" WHERE "foo"="bar"', str(q))
674
+
675
+
676
+ class GroupByTests(unittest.TestCase):
677
+ t = Table("abc")
678
+ maxDiff = None
679
+
680
+ def test_groupby__single(self):
681
+ q = Query.from_(self.t).groupby(self.t.foo).select(self.t.foo)
682
+
683
+ self.assertEqual('SELECT "foo" FROM "abc" GROUP BY "foo"', str(q))
684
+
685
+ def test_groupby__multi(self):
686
+ q = Query.from_(self.t).groupby(self.t.foo, self.t.bar).select(self.t.foo, self.t.bar)
687
+
688
+ self.assertEqual('SELECT "foo","bar" FROM "abc" GROUP BY "foo","bar"', str(q))
689
+
690
+ def test_groupby__count_star(self):
691
+ q = Query.from_(self.t).groupby(self.t.foo).select(self.t.foo, fn.Count("*"))
692
+
693
+ self.assertEqual('SELECT "foo",COUNT(*) FROM "abc" GROUP BY "foo"', str(q))
694
+
695
+ def test_groupby__count_field(self):
696
+ q = Query.from_(self.t).groupby(self.t.foo).select(self.t.foo, fn.Count(self.t.bar))
697
+
698
+ self.assertEqual('SELECT "foo",COUNT("bar") FROM "abc" GROUP BY "foo"', str(q))
699
+
700
+ def test_groupby__count_distinct(self):
701
+ q = Query.from_(self.t).groupby(self.t.foo).select(self.t.foo, fn.Count("*").distinct())
702
+
703
+ self.assertEqual('SELECT "foo",COUNT(DISTINCT *) FROM "abc" GROUP BY "foo"', str(q))
704
+
705
+ def test_groupby__sum_distinct(self):
706
+ q = Query.from_(self.t).groupby(self.t.foo).select(self.t.foo, fn.Sum(self.t.bar).distinct())
707
+
708
+ self.assertEqual('SELECT "foo",SUM(DISTINCT "bar") FROM "abc" GROUP BY "foo"', str(q))
709
+
710
+ def test_groupby__sum_filter(self):
711
+ q = (
712
+ Query.from_(self.t)
713
+ .groupby(self.t.foo)
714
+ .select(self.t.foo, fn.Sum(self.t.bar).filter(self.t.id.eq(1) & self.t.cid.gt(2)))
715
+ )
716
+
717
+ self.assertEqual('SELECT "foo",SUM("bar") FILTER(WHERE "id"=1 AND "cid">2) FROM "abc" GROUP BY "foo"', str(q))
718
+
719
+ def test_groupby__str(self):
720
+ q = Query.from_("abc").groupby("foo").select("foo", fn.Count("*").distinct())
721
+
722
+ self.assertEqual('SELECT "foo",COUNT(DISTINCT *) FROM "abc" GROUP BY "foo"', str(q))
723
+
724
+ def test_groupby__int(self):
725
+ q = Query.from_("abc").groupby(1).select("foo", fn.Count("*").distinct())
726
+
727
+ self.assertEqual('SELECT "foo",COUNT(DISTINCT *) FROM "abc" GROUP BY 1', str(q))
728
+
729
+ def test_groupby__alias(self):
730
+ bar = self.t.bar.as_("bar01")
731
+ q = Query.from_(self.t).select(fn.Sum(self.t.foo), bar).groupby(bar)
732
+
733
+ self.assertEqual('SELECT SUM("foo"),"bar" "bar01" FROM "abc" GROUP BY "bar01"', str(q))
734
+
735
+ def test_groupby__no_alias(self):
736
+ bar = self.t.bar.as_("bar01")
737
+ q = Query.from_(self.t).select(fn.Sum(self.t.foo), bar).groupby(bar)
738
+
739
+ self.assertEqual(
740
+ 'SELECT SUM("foo"),"bar" "bar01" FROM "abc" GROUP BY "bar"',
741
+ q.get_sql(groupby_alias=False),
742
+ )
743
+
744
+ def test_groupby__no_alias_mssql(self):
745
+ bar = self.t.bar.as_("bar01")
746
+ q = MSSQLQuery.from_(self.t).select(fn.Sum(self.t.foo), bar).groupby(bar)
747
+
748
+ self.assertEqual('SELECT SUM("foo"),"bar" "bar01" FROM "abc" GROUP BY "bar"', str(q))
749
+
750
+ def test_groupby__no_alias_oracle(self):
751
+ bar = self.t.bar.as_("bar01")
752
+ q = OracleQuery.from_(self.t).select(fn.Sum(self.t.foo), bar).groupby(bar)
753
+
754
+ self.assertEqual('SELECT SUM(foo),bar bar01 FROM abc GROUP BY bar', str(q))
755
+
756
+ def test_groupby__alias_platforms(self):
757
+ bar = self.t.bar.as_("bar01")
758
+
759
+ for query_cls in [
760
+ MySQLQuery,
761
+ VerticaQuery,
762
+ PostgreSQLQuery,
763
+ RedshiftQuery,
764
+ ClickHouseQuery,
765
+ SQLLiteQuery,
766
+ ]:
767
+ q = query_cls.from_(self.t).select(fn.Sum(self.t.foo), bar).groupby(bar)
768
+
769
+ quote_char = query_cls._builder().QUOTE_CHAR if isinstance(query_cls._builder().QUOTE_CHAR, str) else '"'
770
+
771
+ self.assertEqual(
772
+ "SELECT "
773
+ "SUM({quote_char}foo{quote_char}),"
774
+ "{quote_char}bar{quote_char}{as_keyword}{quote_char}bar01{quote_char} "
775
+ "FROM {quote_char}abc{quote_char} "
776
+ "GROUP BY {quote_char}bar01{quote_char}".format(
777
+ as_keyword=' AS ' if query_cls is ClickHouseQuery else ' ', quote_char=quote_char
778
+ ),
779
+ str(q),
780
+ )
781
+
782
+ def test_groupby__alias_with_join(self):
783
+ table1 = Table("table1", alias="t1")
784
+ bar = table1.bar.as_("bar01")
785
+ q = Query.from_(self.t).join(table1).on(self.t.id == table1.t_ref).select(fn.Sum(self.t.foo), bar).groupby(bar)
786
+
787
+ self.assertEqual(
788
+ 'SELECT SUM("abc"."foo"),"t1"."bar" "bar01" FROM "abc" '
789
+ 'JOIN "table1" "t1" ON "abc"."id"="t1"."t_ref" '
790
+ 'GROUP BY "bar01"',
791
+ str(q),
792
+ )
793
+
794
+ def test_groupby_with_case_uses_the_alias(self):
795
+ q = (
796
+ Query.from_(self.t)
797
+ .select(
798
+ fn.Sum(self.t.foo).as_("bar"),
799
+ Case().when(self.t.fname == "Tom", "It was Tom").else_("It was someone else.").as_("who_was_it"),
800
+ )
801
+ .groupby(Case().when(self.t.fname == "Tom", "It was Tom").else_("It was someone else.").as_("who_was_it"))
802
+ )
803
+
804
+ self.assertEqual(
805
+ 'SELECT SUM("foo") "bar",'
806
+ "CASE WHEN \"fname\"='Tom' THEN 'It was Tom' "
807
+ "ELSE 'It was someone else.' END \"who_was_it\" "
808
+ 'FROM "abc" '
809
+ 'GROUP BY "who_was_it"',
810
+ str(q),
811
+ )
812
+
813
+ def test_mysql_query_uses_backtick_quote_chars(self):
814
+ q = MySQLQuery.from_(self.t).groupby(self.t.foo).select(self.t.foo)
815
+
816
+ self.assertEqual("SELECT `foo` FROM `abc` GROUP BY `foo`", str(q))
817
+
818
+ def test_vertica_query_uses_double_quote_chars(self):
819
+ q = VerticaQuery.from_(self.t).groupby(self.t.foo).select(self.t.foo)
820
+
821
+ self.assertEqual('SELECT "foo" FROM "abc" GROUP BY "foo"', str(q))
822
+
823
+ def test_mssql_query_uses_double_quote_chars(self):
824
+ q = MSSQLQuery.from_(self.t).groupby(self.t.foo).select(self.t.foo)
825
+
826
+ self.assertEqual('SELECT "foo" FROM "abc" GROUP BY "foo"', str(q))
827
+
828
+ def test_oracle_query_uses_no_quote_chars(self):
829
+ q = OracleQuery.from_(self.t).groupby(self.t.foo).select(self.t.foo)
830
+
831
+ self.assertEqual('SELECT foo FROM abc GROUP BY foo', str(q))
832
+
833
+ def test_postgres_query_uses_double_quote_chars(self):
834
+ q = PostgreSQLQuery.from_(self.t).groupby(self.t.foo).select(self.t.foo)
835
+
836
+ self.assertEqual('SELECT "foo" FROM "abc" GROUP BY "foo"', str(q))
837
+
838
+ def test_redshift_query_uses_double_quote_chars(self):
839
+ q = RedshiftQuery.from_(self.t).groupby(self.t.foo).select(self.t.foo)
840
+
841
+ self.assertEqual('SELECT "foo" FROM "abc" GROUP BY "foo"', str(q))
842
+
843
+ def test_group_by__single_with_totals(self):
844
+ q = Query.from_(self.t).groupby(self.t.foo).select(self.t.foo).with_totals()
845
+
846
+ self.assertEqual('SELECT "foo" FROM "abc" GROUP BY "foo" WITH TOTALS', str(q))
847
+
848
+ def test_groupby__multi_with_totals(self):
849
+ q = Query.from_(self.t).groupby(self.t.foo, self.t.bar).select(self.t.foo, self.t.bar).with_totals()
850
+
851
+ self.assertEqual('SELECT "foo","bar" FROM "abc" GROUP BY "foo","bar" WITH TOTALS', str(q))
852
+
853
+
854
+ class HavingTests(unittest.TestCase):
855
+ table_abc, table_efg = Tables("abc", "efg")
856
+
857
+ def test_having_greater_than(self):
858
+ q = (
859
+ Query.from_(self.table_abc)
860
+ .select(self.table_abc.foo, fn.Sum(self.table_abc.bar))
861
+ .groupby(self.table_abc.foo)
862
+ .having(fn.Sum(self.table_abc.bar) > 1)
863
+ )
864
+
865
+ self.assertEqual(
866
+ 'SELECT "foo",SUM("bar") FROM "abc" GROUP BY "foo" HAVING SUM("bar")>1',
867
+ str(q),
868
+ )
869
+
870
+ def test_having_and(self):
871
+ q = (
872
+ Query.from_(self.table_abc)
873
+ .select(self.table_abc.foo, fn.Sum(self.table_abc.bar))
874
+ .groupby(self.table_abc.foo)
875
+ .having((fn.Sum(self.table_abc.bar) > 1) & (fn.Sum(self.table_abc.bar) < 100))
876
+ )
877
+
878
+ self.assertEqual(
879
+ 'SELECT "foo",SUM("bar") FROM "abc" GROUP BY "foo" HAVING SUM("bar")>1 AND SUM("bar")<100',
880
+ str(q),
881
+ )
882
+
883
+ def test_having_join_and_equality(self):
884
+ q = (
885
+ Query.from_(self.table_abc)
886
+ .join(self.table_efg)
887
+ .on(self.table_abc.foo == self.table_efg.foo)
888
+ .select(self.table_abc.foo, fn.Sum(self.table_efg.bar), self.table_abc.buz)
889
+ .groupby(self.table_abc.foo)
890
+ .having(self.table_abc.buz == "fiz")
891
+ .having(fn.Sum(self.table_efg.bar) > 100)
892
+ )
893
+
894
+ self.assertEqual(
895
+ 'SELECT "abc"."foo",SUM("efg"."bar"),"abc"."buz" FROM "abc" '
896
+ 'JOIN "efg" ON "abc"."foo"="efg"."foo" '
897
+ 'GROUP BY "abc"."foo" '
898
+ 'HAVING "abc"."buz"=\'fiz\' AND SUM("efg"."bar")>100',
899
+ str(q),
900
+ )
901
+
902
+ def test_mysql_query_uses_backtick_quote_chars(self):
903
+ q = (
904
+ MySQLQuery.from_(self.table_abc)
905
+ .select(self.table_abc.foo)
906
+ .groupby(self.table_abc.foo)
907
+ .having(self.table_abc.buz == "fiz")
908
+ )
909
+ self.assertEqual("SELECT `foo` FROM `abc` GROUP BY `foo` HAVING `buz`='fiz'", str(q))
910
+
911
+ def test_vertica_query_uses_double_quote_chars(self):
912
+ q = (
913
+ VerticaQuery.from_(self.table_abc)
914
+ .select(self.table_abc.foo)
915
+ .groupby(self.table_abc.foo)
916
+ .having(self.table_abc.buz == "fiz")
917
+ )
918
+ self.assertEqual('SELECT "foo" FROM "abc" GROUP BY "foo" HAVING "buz"=\'fiz\'', str(q))
919
+
920
+ def test_mssql_query_uses_double_quote_chars(self):
921
+ q = (
922
+ MSSQLQuery.from_(self.table_abc)
923
+ .select(self.table_abc.foo)
924
+ .groupby(self.table_abc.foo)
925
+ .having(self.table_abc.buz == "fiz")
926
+ )
927
+ self.assertEqual('SELECT "foo" FROM "abc" GROUP BY "foo" HAVING "buz"=\'fiz\'', str(q))
928
+
929
+ def test_oracle_query_uses_no_quote_chars(self):
930
+ q = (
931
+ OracleQuery.from_(self.table_abc)
932
+ .select(self.table_abc.foo)
933
+ .groupby(self.table_abc.foo)
934
+ .having(self.table_abc.buz == "fiz")
935
+ )
936
+ self.assertEqual('SELECT foo FROM abc GROUP BY foo HAVING buz=\'fiz\'', str(q))
937
+
938
+ def test_postgres_query_uses_double_quote_chars(self):
939
+ q = (
940
+ PostgreSQLQuery.from_(self.table_abc)
941
+ .select(self.table_abc.foo)
942
+ .groupby(self.table_abc.foo)
943
+ .having(self.table_abc.buz == "fiz")
944
+ )
945
+ self.assertEqual('SELECT "foo" FROM "abc" GROUP BY "foo" HAVING "buz"=\'fiz\'', str(q))
946
+
947
+ def test_redshift_query_uses_double_quote_chars(self):
948
+ q = (
949
+ RedshiftQuery.from_(self.table_abc)
950
+ .select(self.table_abc.foo)
951
+ .groupby(self.table_abc.foo)
952
+ .having(self.table_abc.buz == "fiz")
953
+ )
954
+ self.assertEqual('SELECT "foo" FROM "abc" GROUP BY "foo" HAVING "buz"=\'fiz\'', str(q))
955
+
956
+
957
+ class OrderByTests(unittest.TestCase):
958
+ t = Table("abc")
959
+
960
+ def test_orderby_single_field(self):
961
+ q = Query.from_(self.t).orderby(self.t.foo).select(self.t.foo)
962
+
963
+ self.assertEqual('SELECT "foo" FROM "abc" ORDER BY "foo"', str(q))
964
+
965
+ def test_orderby_multi_fields(self):
966
+ q = Query.from_(self.t).orderby(self.t.foo, self.t.bar).select(self.t.foo, self.t.bar)
967
+
968
+ self.assertEqual('SELECT "foo","bar" FROM "abc" ORDER BY "foo","bar"', str(q))
969
+
970
+ def test_orderby_single_str(self):
971
+ q = Query.from_("abc").orderby("foo").select("foo")
972
+
973
+ self.assertEqual('SELECT "foo" FROM "abc" ORDER BY "foo"', str(q))
974
+
975
+ def test_orderby_asc(self):
976
+ q = Query.from_(self.t).orderby(self.t.foo, order=Order.asc).select(self.t.foo)
977
+
978
+ self.assertEqual('SELECT "foo" FROM "abc" ORDER BY "foo" ASC', str(q))
979
+
980
+ def test_orderby_desc(self):
981
+ q = Query.from_(self.t).orderby(self.t.foo, order=Order.desc).select(self.t.foo)
982
+
983
+ self.assertEqual('SELECT "foo" FROM "abc" ORDER BY "foo" DESC', str(q))
984
+
985
+ def test_orderby_no_alias(self):
986
+ bar = self.t.bar.as_("bar01")
987
+ q = Query.from_(self.t).select(fn.Sum(self.t.foo), bar).orderby(bar)
988
+
989
+ self.assertEqual(
990
+ 'SELECT SUM("foo"),"bar" "bar01" FROM "abc" ORDER BY "bar"',
991
+ q.get_sql(orderby_alias=False),
992
+ )
993
+
994
+ def test_orderby_alias(self):
995
+ bar = self.t.bar.as_("bar01")
996
+ q = Query.from_(self.t).select(fn.Sum(self.t.foo), bar).orderby(bar)
997
+
998
+ self.assertEqual('SELECT SUM("foo"),"bar" "bar01" FROM "abc" ORDER BY "bar01"', q.get_sql())
999
+
1000
+
1001
+ class AliasTests(unittest.TestCase):
1002
+ t = Table("abc")
1003
+
1004
+ def test_table_field(self):
1005
+ q = Query.from_(self.t).select(self.t.foo.as_("bar"))
1006
+
1007
+ self.assertEqual('SELECT "foo" "bar" FROM "abc"', str(q))
1008
+
1009
+ def test_table_field__multi(self):
1010
+ q = Query.from_(self.t).select(self.t.foo.as_("bar"), self.t.fiz.as_("buz"))
1011
+
1012
+ self.assertEqual('SELECT "foo" "bar","fiz" "buz" FROM "abc"', str(q))
1013
+
1014
+ def test_arithmetic_function(self):
1015
+ q = Query.from_(self.t).select((self.t.foo + self.t.bar).as_("biz"))
1016
+
1017
+ self.assertEqual('SELECT "foo"+"bar" "biz" FROM "abc"', str(q))
1018
+
1019
+ def test_functions_using_as(self):
1020
+ q = Query.from_(self.t).select(fn.Count("*").as_("foo"))
1021
+
1022
+ self.assertEqual('SELECT COUNT(*) "foo" FROM "abc"', str(q))
1023
+
1024
+ def test_functions_using_constructor_param(self):
1025
+ q = Query.from_(self.t).select(fn.Count("*", alias="foo"))
1026
+
1027
+ self.assertEqual('SELECT COUNT(*) "foo" FROM "abc"', str(q))
1028
+
1029
+ def test_function_using_as_nested(self):
1030
+ """
1031
+ We don't show aliases of fields that are arguments of a function.
1032
+ """
1033
+ q = Query.from_(self.t).select(fn.Sqrt(fn.Count("*").as_("foo")).as_("bar"))
1034
+
1035
+ self.assertEqual('SELECT SQRT(COUNT(*)) "bar" FROM "abc"', str(q))
1036
+
1037
+ def test_functions_using_constructor_param_nested(self):
1038
+ """
1039
+ We don't show aliases of fields that are arguments of a function.
1040
+ """
1041
+ q = Query.from_(self.t).select(fn.Sqrt(fn.Count("*", alias="foo"), alias="bar"))
1042
+
1043
+ self.assertEqual('SELECT SQRT(COUNT(*)) "bar" FROM "abc"', str(q))
1044
+
1045
+ def test_ignored_in_where(self):
1046
+ q = Query.from_(self.t).select(self.t.foo).where(self.t.foo.as_("bar") == 1)
1047
+
1048
+ self.assertEqual('SELECT "foo" FROM "abc" WHERE "foo"=1', str(q))
1049
+
1050
+ def test_ignored_in_groupby(self):
1051
+ q = Query.from_(self.t).select(self.t.foo).groupby(self.t.foo.as_("bar"))
1052
+
1053
+ self.assertEqual('SELECT "foo" FROM "abc" GROUP BY "foo"', str(q))
1054
+
1055
+ def test_ignored_in_orderby(self):
1056
+ q = Query.from_(self.t).select(self.t.foo).orderby(self.t.foo.as_("bar"))
1057
+
1058
+ self.assertEqual('SELECT "foo" FROM "abc" ORDER BY "foo"', str(q))
1059
+
1060
+ def test_ignored_in_criterion(self):
1061
+ c = self.t.foo.as_("bar") == 1
1062
+
1063
+ self.assertEqual('"foo"=1', str(c))
1064
+
1065
+ def test_ignored_in_criterion_comparison(self):
1066
+ c = self.t.foo.as_("bar") == self.t.fiz.as_("buz")
1067
+
1068
+ self.assertEqual('"foo"="fiz"', str(c))
1069
+
1070
+ def test_ignored_in_field_inside_case(self):
1071
+ q = Query.from_(self.t).select(Case().when(self.t.foo == 1, "a").else_(self.t.bar.as_('"buz"')))
1072
+
1073
+ self.assertEqual('SELECT CASE WHEN "foo"=1 THEN \'a\' ELSE "bar" END FROM "abc"', str(q))
1074
+
1075
+ def test_case_using_as(self):
1076
+ q = Query.from_(self.t).select(Case().when(self.t.foo == 1, "a").else_("b").as_("bar"))
1077
+
1078
+ self.assertEqual(
1079
+ 'SELECT CASE WHEN "foo"=1 THEN \'a\' ELSE \'b\' END "bar" FROM "abc"',
1080
+ str(q),
1081
+ )
1082
+
1083
+ def test_case_using_constructor_param(self):
1084
+ q = Query.from_(self.t).select(Case(alias="bar").when(self.t.foo == 1, "a").else_("b"))
1085
+
1086
+ self.assertEqual(
1087
+ 'SELECT CASE WHEN "foo"=1 THEN \'a\' ELSE \'b\' END "bar" FROM "abc"',
1088
+ str(q),
1089
+ )
1090
+
1091
+ def test_select__multiple_tables(self):
1092
+ table_abc, table_efg = Table("abc", alias="q0"), Table("efg", alias="q1")
1093
+
1094
+ q = Query.from_(table_abc).select(table_abc.foo).from_(table_efg).select(table_efg.bar)
1095
+
1096
+ self.assertEqual('SELECT "q0"."foo","q1"."bar" FROM "abc" "q0","efg" "q1"', str(q))
1097
+
1098
+ def test_use_aliases_in_groupby_and_orderby(self):
1099
+ table_abc = Table("abc", alias="q0")
1100
+
1101
+ my_foo = table_abc.foo.as_("my_foo")
1102
+ q = Query.from_(table_abc).select(my_foo, table_abc.bar).groupby(my_foo).orderby(my_foo)
1103
+
1104
+ self.assertEqual(
1105
+ 'SELECT "q0"."foo" "my_foo","q0"."bar" ' 'FROM "abc" "q0" ' 'GROUP BY "my_foo" ' 'ORDER BY "my_foo"',
1106
+ str(q),
1107
+ )
1108
+
1109
+ def test_table_with_schema_and_alias(self):
1110
+ table = Table("abc", schema="schema", alias="alias")
1111
+ self.assertEqual('"schema"."abc" "alias"', str(table))
1112
+
1113
+ def test_null_value_with_alias(self):
1114
+ q = Query.select(NullValue().as_("abcdef"))
1115
+
1116
+ self.assertEqual('SELECT NULL "abcdef"', str(q))
1117
+
1118
+
1119
+ class SubqueryTests(unittest.TestCase):
1120
+ maxDiff = None
1121
+
1122
+ table_abc, table_efg, table_hij = Tables("abc", "efg", "hij")
1123
+
1124
+ def test_where__in(self):
1125
+ q = (
1126
+ Query.from_(self.table_abc)
1127
+ .select("*")
1128
+ .where(
1129
+ self.table_abc.foo.isin(
1130
+ Query.from_(self.table_efg).select(self.table_efg.foo).where(self.table_efg.bar == 0)
1131
+ )
1132
+ )
1133
+ )
1134
+
1135
+ self.assertEqual(
1136
+ 'SELECT * FROM "abc" WHERE "foo" IN (SELECT "foo" FROM "efg" WHERE "bar"=0)',
1137
+ str(q),
1138
+ )
1139
+
1140
+ def test_where__in_nested(self):
1141
+ q = Query.from_(self.table_abc).select("*").where(self.table_abc.foo).isin(self.table_efg.select("*"))
1142
+
1143
+ self.assertEqual('SELECT * FROM "abc" WHERE "foo" IN (SELECT * FROM "efg")', str(q))
1144
+
1145
+ def test_join(self):
1146
+ subquery = Query.from_("efg").select("fiz", "buz").where(F("buz") == 0)
1147
+
1148
+ q = (
1149
+ Query.from_(self.table_abc)
1150
+ .join(subquery)
1151
+ .on(self.table_abc.bar == subquery.buz)
1152
+ .select(self.table_abc.foo, subquery.fiz)
1153
+ )
1154
+
1155
+ self.assertEqual(
1156
+ 'SELECT "abc"."foo","sq0"."fiz" FROM "abc" '
1157
+ 'JOIN (SELECT "fiz","buz" FROM "efg" WHERE "buz"=0) "sq0" '
1158
+ 'ON "abc"."bar"="sq0"."buz"',
1159
+ str(q),
1160
+ )
1161
+
1162
+ def test_select_subquery(self):
1163
+ subq = Query.from_(self.table_efg).select("fizzbuzz").where(self.table_efg.id == 1)
1164
+
1165
+ q = Query.from_(self.table_abc).select("foo", "bar").select(subq)
1166
+
1167
+ self.assertEqual(
1168
+ 'SELECT "foo","bar",(SELECT "fizzbuzz" FROM "efg" WHERE "id"=1) ' 'FROM "abc"',
1169
+ str(q),
1170
+ )
1171
+
1172
+ def test_select_subquery_with_alias(self):
1173
+ subq = Query.from_(self.table_efg).select("fizzbuzz").where(self.table_efg.id == 1)
1174
+
1175
+ q = Query.from_(self.table_abc).select("foo", "bar").select(subq.as_("sq"))
1176
+
1177
+ self.assertEqual(
1178
+ 'SELECT "foo","bar",(SELECT "fizzbuzz" FROM "efg" WHERE "id"=1) "sq" ' 'FROM "abc"',
1179
+ str(q),
1180
+ )
1181
+
1182
+ def test_where__equality(self):
1183
+ subquery = Query.from_("efg").select("fiz").where(F("buz") == 0)
1184
+ query = (
1185
+ Query.from_(self.table_abc)
1186
+ .select(self.table_abc.foo, self.table_abc.bar)
1187
+ .where(self.table_abc.bar == subquery)
1188
+ )
1189
+
1190
+ self.assertEqual(
1191
+ 'SELECT "foo","bar" FROM "abc" ' 'WHERE "bar"=(SELECT "fiz" FROM "efg" WHERE "buz"=0)',
1192
+ str(query),
1193
+ )
1194
+
1195
+ def test_select_from_nested_query(self):
1196
+ subquery = Query.from_(self.table_abc).select(
1197
+ self.table_abc.foo,
1198
+ self.table_abc.bar,
1199
+ (self.table_abc.fizz + self.table_abc.buzz).as_("fizzbuzz"),
1200
+ )
1201
+
1202
+ query = Query.from_(subquery).select(subquery.foo, subquery.bar, subquery.fizzbuzz)
1203
+
1204
+ self.assertEqual(
1205
+ 'SELECT "sq0"."foo","sq0"."bar","sq0"."fizzbuzz" '
1206
+ "FROM ("
1207
+ 'SELECT "foo","bar","fizz"+"buzz" "fizzbuzz" '
1208
+ 'FROM "abc"'
1209
+ ') "sq0"',
1210
+ str(query),
1211
+ )
1212
+
1213
+ def test_select_from_nested_query_with_join(self):
1214
+ subquery1 = (
1215
+ Query.from_(self.table_abc)
1216
+ .select(
1217
+ self.table_abc.foo,
1218
+ fn.Sum(self.table_abc.fizz + self.table_abc.buzz).as_("fizzbuzz"),
1219
+ )
1220
+ .groupby(self.table_abc.foo)
1221
+ )
1222
+
1223
+ subquery2 = Query.from_(self.table_efg).select(
1224
+ self.table_efg.foo.as_("foo_two"),
1225
+ self.table_efg.bar,
1226
+ )
1227
+
1228
+ query = (
1229
+ Query.from_(subquery1)
1230
+ .select(subquery1.foo, subquery1.fizzbuzz)
1231
+ .join(subquery2)
1232
+ .on(subquery1.foo == subquery2.foo_two)
1233
+ .select(subquery2.foo_two, subquery2.bar)
1234
+ )
1235
+
1236
+ self.assertEqual(
1237
+ "SELECT "
1238
+ '"sq0"."foo","sq0"."fizzbuzz",'
1239
+ '"sq1"."foo_two","sq1"."bar" '
1240
+ "FROM ("
1241
+ "SELECT "
1242
+ '"foo",SUM("fizz"+"buzz") "fizzbuzz" '
1243
+ 'FROM "abc" '
1244
+ 'GROUP BY "foo"'
1245
+ ') "sq0" JOIN ('
1246
+ "SELECT "
1247
+ '"foo" "foo_two","bar" '
1248
+ 'FROM "efg"'
1249
+ ') "sq1" ON "sq0"."foo"="sq1"."foo_two"',
1250
+ str(query),
1251
+ )
1252
+
1253
+ def test_from_subquery_without_alias(self):
1254
+ subquery = Query.from_(self.table_efg).select(
1255
+ self.table_efg.base_id.as_("x"), self.table_efg.fizz, self.table_efg.buzz
1256
+ )
1257
+
1258
+ test_query = Query.from_(subquery).select(subquery.x, subquery.fizz, subquery.buzz)
1259
+
1260
+ self.assertEqual(
1261
+ 'SELECT "sq0"."x","sq0"."fizz","sq0"."buzz" '
1262
+ "FROM ("
1263
+ 'SELECT "base_id" "x","fizz","buzz" FROM "efg"'
1264
+ ') "sq0"',
1265
+ str(test_query),
1266
+ )
1267
+
1268
+ def test_join_query_with_alias(self):
1269
+ subquery = (
1270
+ Query.from_(self.table_efg)
1271
+ .select(
1272
+ self.table_efg.base_id.as_("x"),
1273
+ self.table_efg.fizz,
1274
+ self.table_efg.buzz,
1275
+ )
1276
+ .as_("subq")
1277
+ )
1278
+
1279
+ test_query = Query.from_(subquery).select(subquery.x, subquery.fizz, subquery.buzz)
1280
+
1281
+ self.assertEqual(
1282
+ 'SELECT "subq"."x","subq"."fizz","subq"."buzz" '
1283
+ "FROM ("
1284
+ 'SELECT "base_id" "x","fizz","buzz" FROM "efg"'
1285
+ ') "subq"',
1286
+ str(test_query),
1287
+ )
1288
+
1289
+ def test_with(self):
1290
+ sub_query = Query.from_(self.table_efg).select("fizz")
1291
+ test_query = Query.with_(sub_query, "an_alias").from_(AliasedQuery("an_alias")).select("*")
1292
+
1293
+ self.assertEqual(
1294
+ 'WITH an_alias AS (SELECT "fizz" FROM "efg") SELECT * FROM an_alias',
1295
+ str(test_query),
1296
+ )
1297
+
1298
+ def test_join_with_with(self):
1299
+ sub_query = Query.from_(self.table_efg).select("fizz")
1300
+ test_query = (
1301
+ Query.with_(sub_query, "an_alias")
1302
+ .from_(self.table_abc)
1303
+ .join(AliasedQuery("an_alias"))
1304
+ .on(AliasedQuery("an_alias").fizz == self.table_abc.buzz)
1305
+ .select("*")
1306
+ )
1307
+ self.assertEqual(
1308
+ 'WITH an_alias AS (SELECT "fizz" FROM "efg") '
1309
+ 'SELECT * FROM "abc" JOIN an_alias ON "an_alias"."fizz"="abc"."buzz"',
1310
+ str(test_query),
1311
+ )
1312
+
1313
+ def test_select_from_with_returning(self):
1314
+ sub_query = PostgreSQLQuery.into(self.table_abc).insert(1).returning('*')
1315
+ test_query = Query.with_(sub_query, "an_alias").from_(AliasedQuery("an_alias")).select("*")
1316
+ self.assertEqual(
1317
+ 'WITH an_alias AS (INSERT INTO "abc" VALUES (1) RETURNING *) SELECT * FROM an_alias', str(test_query)
1318
+ )
1319
+
1320
+
1321
+ class QuoteTests(unittest.TestCase):
1322
+ def test_extraneous_quotes(self):
1323
+ t1 = Table("table1", alias="t1")
1324
+ t2 = Table("table2", alias="t2")
1325
+
1326
+ query = Query.from_(t1).join(t2).on(t1.Value.between(t2.start, t2.end)).select(t1.value)
1327
+
1328
+ self.assertEqual(
1329
+ "SELECT t1.value FROM table1 t1 " "JOIN table2 t2 ON t1.Value " "BETWEEN t2.start AND t2.end",
1330
+ query.get_sql(quote_char=None),
1331
+ )
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_tables.py ADDED
@@ -0,0 +1,186 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # from pypika.terms import ValueWrapper, SystemTimeValue
2
+ import unittest
3
+
4
+ from pypika import Database, Dialects, Schema, SQLLiteQuery, Table, Tables, Query, SYSTEM_TIME
5
+
6
+ __author__ = "Timothy Heys"
7
+ __email__ = "theys@kayak.com"
8
+
9
+
10
+ class TableStructureTests(unittest.TestCase):
11
+ def test_table_sql(self):
12
+ table = Table("test_table")
13
+
14
+ self.assertEqual('"test_table"', str(table))
15
+
16
+ def test_table_with_alias(self):
17
+ table = Table("test_table").as_("my_table")
18
+
19
+ self.assertEqual('"test_table" "my_table"', table.get_sql(with_alias=True, quote_char='"'))
20
+
21
+ def test_schema_table_attr(self):
22
+ table = Schema("x_schema").test_table
23
+
24
+ self.assertEqual('"x_schema"."test_table"', str(table))
25
+
26
+ def test_table_with_schema_arg(self):
27
+ table = Table("test_table", schema=Schema("x_schema"))
28
+
29
+ self.assertEqual('"x_schema"."test_table"', str(table))
30
+
31
+ def test_database_schema_table_attr(self):
32
+ table = Database("x_db").x_schema.test_table
33
+
34
+ self.assertEqual('"x_db"."x_schema"."test_table"', str(table))
35
+
36
+ def test_table_with_schema_and_schema_parent_arg(self):
37
+ table = Table("test_table", schema=Schema("x_schema", parent=Database("x_db")))
38
+
39
+ self.assertEqual('"x_db"."x_schema"."test_table"', str(table))
40
+
41
+ def test_table_for_system_time_sql(self):
42
+ with self.subTest("with between criterion"):
43
+ table = Table("test_table").for_(SYSTEM_TIME.between('2020-01-01', '2020-02-01'))
44
+
45
+ self.assertEqual('"test_table" FOR SYSTEM_TIME BETWEEN \'2020-01-01\' AND \'2020-02-01\'', str(table))
46
+
47
+ with self.subTest("with as of criterion"):
48
+ table = Table("test_table").for_(SYSTEM_TIME.as_of('2020-01-01'))
49
+
50
+ self.assertEqual('"test_table" FOR SYSTEM_TIME AS OF \'2020-01-01\'', str(table))
51
+
52
+ with self.subTest("with from to criterion"):
53
+ table = Table("test_table").for_(SYSTEM_TIME.from_to('2020-01-01', '2020-02-01'))
54
+
55
+ self.assertEqual('"test_table" FOR SYSTEM_TIME FROM \'2020-01-01\' TO \'2020-02-01\'', str(table))
56
+
57
+ def test_table_for_period_sql(self):
58
+ with self.subTest("with between criterion"):
59
+ table = Table("test_table")
60
+ table = table.for_(table.valid_period.between('2020-01-01', '2020-02-01'))
61
+
62
+ self.assertEqual('"test_table" FOR "valid_period" BETWEEN \'2020-01-01\' AND \'2020-02-01\'', str(table))
63
+
64
+ with self.subTest("with as of criterion"):
65
+ table = Table("test_table")
66
+ table = table.for_(table.valid_period.as_of('2020-01-01'))
67
+
68
+ self.assertEqual('"test_table" FOR "valid_period" AS OF \'2020-01-01\'', str(table))
69
+
70
+ with self.subTest("with from to criterion"):
71
+ table = Table("test_table")
72
+ table = table.for_(table.valid_period.from_to('2020-01-01', '2020-02-01'))
73
+
74
+ self.assertEqual('"test_table" FOR "valid_period" FROM \'2020-01-01\' TO \'2020-02-01\'', str(table))
75
+
76
+
77
+ class TableEqualityTests(unittest.TestCase):
78
+ def test_tables_equal_by_name(self):
79
+ t1 = Table("t")
80
+ t2 = Table("t")
81
+
82
+ self.assertEqual(t1, t2)
83
+
84
+ def test_tables_equal_by_schema_and_name(self):
85
+ t1 = Table("t", schema="a")
86
+ t2 = Table("t", schema="a")
87
+
88
+ self.assertEqual(t1, t2)
89
+
90
+ def test_tables_equal_by_schema_and_name_using_schema(self):
91
+ a = Schema("a")
92
+ t1 = Table("t", schema=a)
93
+ t2 = Table("t", schema=a)
94
+
95
+ self.assertEqual(t1, t2)
96
+
97
+ def test_tables_equal_by_schema_and_name_using_schema_with_parent(self):
98
+ parent = Schema("parent")
99
+ a = Schema("a", parent=parent)
100
+ t1 = Table("t", schema=a)
101
+ t2 = Table("t", schema=a)
102
+
103
+ self.assertEqual(t1, t2)
104
+
105
+ def test_tables_not_equal_by_schema_and_name_using_schema_with_different_parents(
106
+ self,
107
+ ):
108
+ parent = Schema("parent")
109
+ a = Schema("a", parent=parent)
110
+ t1 = Table("t", schema=a)
111
+ t2 = Table("t", schema=Schema("a"))
112
+
113
+ self.assertNotEqual(t1, t2)
114
+
115
+ def test_tables_not_equal_with_different_schemas(self):
116
+ t1 = Table("t", schema="a")
117
+ t2 = Table("t", schema="b")
118
+
119
+ self.assertNotEqual(t1, t2)
120
+
121
+ def test_tables_not_equal_with_different_names(self):
122
+ t1 = Table("t", schema="a")
123
+ t2 = Table("q", schema="a")
124
+
125
+ self.assertNotEqual(t1, t2)
126
+
127
+ def test_many_tables_with_alias(self):
128
+ tables_data = [("table1", "t1"), ("table2", "t2"), ("table3", "t3")]
129
+ tables = Tables(*tables_data)
130
+ for el in tables:
131
+ self.assertIsNotNone(el.alias)
132
+
133
+ def test_many_tables_without_alias(self):
134
+ tables_data = ["table1", "table2", "table3"]
135
+ tables = Tables(*tables_data)
136
+ for el in tables:
137
+ self.assertIsNone(el.alias)
138
+
139
+ def test_many_tables_with_or_not_alias(self):
140
+ tables_data = [("table1", "t1"), ("table2"), "table3"]
141
+ tables = Tables(*tables_data)
142
+ for i in range(len(tables)):
143
+ if isinstance(tables_data[i], tuple):
144
+ self.assertIsNotNone(tables[i].alias)
145
+ else:
146
+ self.assertIsNone(tables[i].alias)
147
+
148
+
149
+ class TableDialectTests(unittest.TestCase):
150
+ def test_table_with_default_query_cls(self):
151
+ table = Table("abc")
152
+ q = table.select("1")
153
+ self.assertIs(q.dialect, None)
154
+
155
+ def test_table_with_dialect_query_cls(self):
156
+ table = Table("abc", query_cls=SQLLiteQuery)
157
+ q = table.select("1")
158
+ self.assertIs(q.dialect, Dialects.SQLLITE)
159
+
160
+ def test_table_factory_with_default_query_cls(self):
161
+ table = Query.Table("abc")
162
+ q = table.select("1")
163
+ self.assertIs(q.dialect, None)
164
+
165
+ def test_table_factory_with_dialect_query_cls(self):
166
+ table = SQLLiteQuery.Table("abc")
167
+ q = table.select("1")
168
+ self.assertIs(q.dialect, Dialects.SQLLITE)
169
+
170
+ def test_make_tables_factory_with_default_query_cls(self):
171
+ t1, t2 = Query.Tables("abc", "def")
172
+ q1 = t1.select("1")
173
+ q2 = t2.select("2")
174
+ self.assertIs(q1.dialect, None)
175
+ self.assertIs(q2.dialect, None)
176
+
177
+ def test_make_tables_factory_with_dialect_query_cls(self):
178
+ t1, t2 = SQLLiteQuery.Tables("abc", "def")
179
+ q1 = t1.select("1")
180
+ q2 = t2.select("2")
181
+ self.assertIs(q1.dialect, Dialects.SQLLITE)
182
+ self.assertIs(q2.dialect, Dialects.SQLLITE)
183
+
184
+ def test_table_with_bad_query_cls(self):
185
+ with self.assertRaises(TypeError):
186
+ Table("abc", query_cls=object)
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_terms.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from unittest import TestCase
2
+
3
+ from pypika import Query, Table, Field
4
+ from pypika.terms import AtTimezone
5
+
6
+
7
+ class FieldAliasTests(TestCase):
8
+ t = Table("test", alias="crit")
9
+
10
+ def test_when_alias_specified(self):
11
+ c1 = Field("foo", alias="bar")
12
+ self.assertEqual('bar', str(c1.alias))
13
+
14
+ c1 = Field("foo").as_("bar")
15
+ self.assertEqual('bar', str(c1.alias))
16
+
17
+
18
+ class FieldInitTests(TestCase):
19
+ def test_init_with_str_table(self):
20
+ test_table_name = "test_table"
21
+ field = Field(name="name", table=test_table_name)
22
+ self.assertEqual(field.table, Table(name=test_table_name))
23
+
24
+
25
+ class FieldHashingTests(TestCase):
26
+ def test_tabled_eq_fields_equally_hashed(self):
27
+ client_name1 = Field(name="name", table=Table("clients"))
28
+ client_name2 = Field(name="name", table=Table("clients"))
29
+ self.assertTrue(hash(client_name1) == hash(client_name2))
30
+
31
+ def test_tabled_ne_fields_differently_hashed(self):
32
+ customer_name = Field(name="name", table=Table("customers"))
33
+ client_name = Field(name="name", table=Table("clients"))
34
+ self.assertTrue(hash(customer_name) != hash(client_name))
35
+
36
+ def test_non_tabled_aliased_eq_fields_equally_hashed(self):
37
+ self.assertTrue(hash(Field(name="A", alias="my_a")) == hash(Field(name="A", alias="my_a")))
38
+
39
+ def test_non_tabled_aliased_ne_fields_differently_hashed(self):
40
+ self.assertTrue(hash(Field(name="A", alias="my_a1")) != hash(Field(name="A", alias="my_a2")))
41
+
42
+ def test_non_tabled_eq_fields_equally_hashed(self):
43
+ self.assertTrue(hash(Field(name="A")) == hash(Field(name="A")))
44
+
45
+ def test_non_tabled_ne_fields_differently_hashed(self):
46
+ self.assertTrue(hash(Field(name="A")) != hash(Field(name="B")))
47
+
48
+
49
+ class AtTimezoneTests(TestCase):
50
+ def test_when_interval_not_specified(self):
51
+ query = Query.from_("customers").select(AtTimezone("date", "US/Eastern"))
52
+ self.assertEqual('SELECT "date" AT TIME ZONE \'US/Eastern\' FROM "customers"', str(query))
53
+
54
+ def test_when_interval_specified(self):
55
+ query = Query.from_("customers").select(AtTimezone("date", "-06:00", interval=True))
56
+ self.assertEqual(
57
+ 'SELECT "date" AT TIME ZONE INTERVAL \'-06:00\' FROM "customers"',
58
+ str(query),
59
+ )
60
+
61
+ def test_when_alias_specified(self):
62
+ query = Query.from_("customers").select(AtTimezone("date", "US/Eastern", alias="alias1"))
63
+ self.assertEqual(
64
+ 'SELECT "date" AT TIME ZONE \'US/Eastern\' "alias1" FROM "customers"',
65
+ str(query),
66
+ )
67
+
68
+ def test_passes_kwargs_to_field_get_sql(self):
69
+ customers = Table("customers")
70
+ accounts = Table("accounts")
71
+ query = (
72
+ Query.from_(customers)
73
+ .join(accounts)
74
+ .on(customers.account_id == accounts.account_id)
75
+ .select(AtTimezone(customers.date, "US/Eastern", alias="alias1"))
76
+ )
77
+
78
+ self.assertEqual(
79
+ 'SELECT "customers"."date" AT TIME ZONE \'US/Eastern\' "alias1" '
80
+ 'FROM "customers" JOIN "accounts" ON "customers"."account_id"="accounts"."account_id"',
81
+ query.get_sql(with_namespace=True),
82
+ )
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_tuples.py ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from pypika import (
4
+ Array,
5
+ Bracket,
6
+ Query,
7
+ PostgreSQLQuery,
8
+ Table,
9
+ Tables,
10
+ Tuple,
11
+ )
12
+ from pypika.functions import Coalesce, NullIf, Sum
13
+ from pypika.terms import Field
14
+
15
+
16
+ class TupleTests(unittest.TestCase):
17
+ table_abc, table_efg = Tables("abc", "efg")
18
+
19
+ def test_tuple_equality_tuple_on_both(self):
20
+ q = (
21
+ Query.from_(self.table_abc)
22
+ .select(self.table_abc.foo, self.table_abc.bar)
23
+ .where(Tuple(self.table_abc.foo, self.table_abc.bar) == Tuple(1, 2))
24
+ )
25
+
26
+ self.assertEqual('SELECT "foo","bar" FROM "abc" ' 'WHERE ("foo","bar")=(1,2)', str(q))
27
+
28
+ def test_tuple_equality_tuple_on_left(self):
29
+ q = (
30
+ Query.from_(self.table_abc)
31
+ .select(self.table_abc.foo, self.table_abc.bar)
32
+ .where(Tuple(self.table_abc.foo, self.table_abc.bar) == (1, 2))
33
+ )
34
+
35
+ self.assertEqual('SELECT "foo","bar" FROM "abc" ' 'WHERE ("foo","bar")=(1,2)', str(q))
36
+
37
+ def test_tuple_equality_tuple_on_right(self):
38
+ q = (
39
+ Query.from_(self.table_abc)
40
+ .select(self.table_abc.foo, self.table_abc.bar)
41
+ .where((self.table_abc.foo, self.table_abc.bar) == Tuple(1, 2))
42
+ )
43
+
44
+ # Order is reversed due to lack of right equals method
45
+ self.assertEqual('SELECT "foo","bar" FROM "abc" ' 'WHERE (1,2)=("foo","bar")', str(q))
46
+
47
+ def test_tuple_in_using_python_tuples(self):
48
+ q = (
49
+ Query.from_(self.table_abc)
50
+ .select(self.table_abc.foo, self.table_abc.bar)
51
+ .where(Tuple(self.table_abc.foo, self.table_abc.bar).isin([(1, 1), (2, 2), (3, 3)]))
52
+ )
53
+
54
+ self.assertEqual(
55
+ 'SELECT "foo","bar" FROM "abc" ' 'WHERE ("foo","bar") IN ((1,1),(2,2),(3,3))',
56
+ str(q),
57
+ )
58
+
59
+ def test_tuple_in_using_pypika_tuples(self):
60
+ q = (
61
+ Query.from_(self.table_abc)
62
+ .select(self.table_abc.foo, self.table_abc.bar)
63
+ .where(Tuple(self.table_abc.foo, self.table_abc.bar).isin([Tuple(1, 1), Tuple(2, 2), Tuple(3, 3)]))
64
+ )
65
+
66
+ self.assertEqual(
67
+ 'SELECT "foo","bar" FROM "abc" ' 'WHERE ("foo","bar") IN ((1,1),(2,2),(3,3))',
68
+ str(q),
69
+ )
70
+
71
+ def test_tuple_in_using_mixed_tuples(self):
72
+ q = (
73
+ Query.from_(self.table_abc)
74
+ .select(self.table_abc.foo, self.table_abc.bar)
75
+ .where(Tuple(self.table_abc.foo, self.table_abc.bar).isin([(1, 1), Tuple(2, 2), (3, 3)]))
76
+ )
77
+
78
+ self.assertEqual(
79
+ 'SELECT "foo","bar" FROM "abc" ' 'WHERE ("foo","bar") IN ((1,1),(2,2),(3,3))',
80
+ str(q),
81
+ )
82
+
83
+ def test_tuples_in_join(self):
84
+ query = (
85
+ Query.from_(self.table_abc)
86
+ .join(self.table_efg)
87
+ .on(self.table_abc.foo == self.table_efg.bar)
88
+ .select("*")
89
+ .where(Tuple(self.table_abc.foo, self.table_efg.bar).isin([(1, 1), Tuple(2, 2), (3, 3)]))
90
+ )
91
+
92
+ self.assertEqual(
93
+ 'SELECT * FROM "abc" JOIN "efg" ON "abc"."foo"="efg"."bar" '
94
+ 'WHERE ("abc"."foo","efg"."bar") IN ((1,1),(2,2),(3,3))',
95
+ str(query),
96
+ )
97
+
98
+ def test_render_alias_in_array_sql(self):
99
+ tb = Table("tb")
100
+
101
+ q = Query.from_(tb).select(Tuple(tb.col).as_("different_name"))
102
+ self.assertEqual(str(q), 'SELECT ("col") "different_name" FROM "tb"')
103
+
104
+ def test_tuple_is_aggregate(self):
105
+ with self.subTest('None if single argument returns None for is_aggregate'):
106
+ self.assertEqual(None, Tuple(0).is_aggregate)
107
+ self.assertEqual(None, Tuple(Coalesce('col')).is_aggregate)
108
+
109
+ with self.subTest('None if multiple arguments all return None for is_aggregate'):
110
+ self.assertEqual(None, Tuple(0, 'a').is_aggregate)
111
+ self.assertEqual(None, Tuple(Coalesce('col'), NullIf('col2', 0)).is_aggregate)
112
+
113
+ with self.subTest('True if single argument returns True for is_aggregate'):
114
+ self.assertEqual(True, Tuple(Sum('col')).is_aggregate)
115
+
116
+ with self.subTest('True if multiple arguments return True for is_aggregate'):
117
+ self.assertEqual(True, Tuple(Sum('col'), Sum('col2')).is_aggregate)
118
+
119
+ with self.subTest('True when mix of arguments returning None and True for is_aggregate'):
120
+ self.assertEqual(True, Tuple(Coalesce('col'), Coalesce('col2', 0), Sum('col3')).is_aggregate)
121
+
122
+ with self.subTest('False when one of the arguments returns False for is_aggregate'):
123
+ self.assertEqual(False, Tuple(Field('col1'), Sum('col2')).is_aggregate)
124
+
125
+
126
+ class ArrayTests(unittest.TestCase):
127
+ table_abc, table_efg = Tables("abc", "efg")
128
+
129
+ def test_array_general(self):
130
+ query = Query.from_(self.table_abc).select(Array(1, "a", ["b", 2, 3]))
131
+
132
+ self.assertEqual("SELECT [1,'a',['b',2,3]] FROM \"abc\"", str(query))
133
+
134
+ def test_empty_psql_array(self):
135
+ query = PostgreSQLQuery.from_(self.table_abc).select(Array())
136
+
137
+ self.assertEqual("SELECT '{}' FROM \"abc\"", str(query))
138
+
139
+ def test_psql_array_general(self):
140
+ query = PostgreSQLQuery.from_(self.table_abc).select(Array(1, Array(2, 2, 2), 3))
141
+
142
+ self.assertEqual("SELECT ARRAY[1,ARRAY[2,2,2],3] FROM \"abc\"", str(query))
143
+
144
+ def test_render_alias_in_array_sql(self):
145
+ tb = Table("tb")
146
+
147
+ q = Query.from_(tb).select(Array(tb.col).as_("different_name"))
148
+ self.assertEqual(str(q), 'SELECT ["col"] "different_name" FROM "tb"')
149
+
150
+
151
+ class BracketTests(unittest.TestCase):
152
+ table_abc, table_efg = Tables("abc", "efg")
153
+
154
+ def test_arithmetic_with_brackets(self):
155
+ q = Query.from_(self.table_abc).select(Bracket(self.table_abc.foo / 2) / 2)
156
+
157
+ self.assertEqual('SELECT ("foo"/2)/2 FROM "abc"', str(q))
158
+
159
+ def test_arithmetic_with_brackets_and_alias(self):
160
+ q = Query.from_(self.table_abc).select(Bracket(self.table_abc.foo / 2).as_("alias"))
161
+
162
+ self.assertEqual('SELECT ("foo"/2) "alias" FROM "abc"', str(q))
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_updates.py ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from pypika import AliasedQuery, PostgreSQLQuery, Query, SQLLiteQuery, SYSTEM_TIME, Table
4
+
5
+ __author__ = "Timothy Heys"
6
+ __email__ = "theys@kayak.com"
7
+
8
+ from pypika.terms import Star
9
+
10
+
11
+ class UpdateTests(unittest.TestCase):
12
+ table_abc = Table("abc")
13
+ table_def = Table("def")
14
+
15
+ def test_empty_query(self):
16
+ q = Query.update("abc")
17
+
18
+ self.assertEqual("", str(q))
19
+
20
+ def test_omit_where(self):
21
+ q = Query.update(self.table_abc).set("foo", "bar")
22
+
23
+ self.assertEqual('UPDATE "abc" SET "foo"=\'bar\'', str(q))
24
+
25
+ def test_single_quote_escape_in_set(self):
26
+ q = Query.update(self.table_abc).set("foo", "bar'foo")
27
+
28
+ self.assertEqual("UPDATE \"abc\" SET \"foo\"='bar''foo'", str(q))
29
+
30
+ def test_update__table_schema(self):
31
+ table = Table("abc", "schema1")
32
+ q = Query.update(table).set(table.foo, 1).where(table.foo == 0)
33
+
34
+ self.assertEqual('UPDATE "schema1"."abc" SET "foo"=1 WHERE "foo"=0', str(q))
35
+
36
+ def test_update_with_none(self):
37
+ q = Query.update("abc").set("foo", None)
38
+ self.assertEqual('UPDATE "abc" SET "foo"=null', str(q))
39
+
40
+ def test_update_with_join(self):
41
+ q = (
42
+ Query.update(self.table_abc)
43
+ .join(self.table_def)
44
+ .on(self.table_def.abc_id == self.table_abc.id)
45
+ .set(self.table_abc.lname, self.table_def.lname)
46
+ )
47
+ self.assertEqual(
48
+ 'UPDATE "abc" JOIN "def" ON "def"."abc_id"="abc"."id" SET "lname"="def"."lname"',
49
+ str(q),
50
+ )
51
+
52
+ def test_update_with_limit(self):
53
+ q = Query.update(self.table_abc).set(self.table_abc.lname, "test").limit(1)
54
+ self.assertEqual('UPDATE "abc" SET "lname"=\'test\' LIMIT 1', str(q))
55
+
56
+ def test_update_from(self):
57
+ from_table = Table('from_table')
58
+
59
+ q = Query.update(self.table_abc).set(self.table_abc.lname, from_table.long_name).from_(from_table)
60
+ self.assertEqual('UPDATE "abc" SET "lname"="from_table"."long_name" FROM "from_table"', str(q))
61
+
62
+ def test_update_from_with_where(self):
63
+ from_table = Table('from_table')
64
+
65
+ q = (
66
+ Query.update(self.table_abc)
67
+ .set(self.table_abc.lname, from_table.long_name)
68
+ .from_(from_table)
69
+ .where(self.table_abc.fname.eq(from_table.full_name))
70
+ )
71
+ self.assertEqual(
72
+ 'UPDATE "abc" SET "lname"="from_table"."long_name" FROM "from_table" '
73
+ 'WHERE "abc"."fname"="from_table"."full_name"',
74
+ str(q),
75
+ )
76
+
77
+ def test_update_with_statement(self):
78
+ table_efg = Table('efg')
79
+
80
+ sub_query = Query.from_(table_efg).select("fizz")
81
+ an_alias = AliasedQuery("an_alias")
82
+
83
+ q = (
84
+ Query.with_(sub_query, "an_alias")
85
+ .update(self.table_abc)
86
+ .from_(an_alias)
87
+ .set(self.table_abc.lname, an_alias.long_name)
88
+ .where(self.table_abc.comp.eq(an_alias.alias_comp))
89
+ )
90
+ self.assertEqual(
91
+ 'WITH an_alias AS (SELECT "fizz" FROM "efg") '
92
+ 'UPDATE "abc" SET "lname"="an_alias"."long_name" FROM an_alias '
93
+ 'WHERE "abc"."comp"="an_alias"."alias_comp"',
94
+ str(q),
95
+ )
96
+
97
+ def test_for_portion(self):
98
+ with self.subTest("with system time"):
99
+ q = Query.update(self.table_abc.for_portion(SYSTEM_TIME.from_to('2020-01-01', '2020-02-01'))).set(
100
+ "foo", "bar"
101
+ )
102
+
103
+ self.assertEqual(
104
+ 'UPDATE "abc" FOR PORTION OF SYSTEM_TIME FROM \'2020-01-01\' TO \'2020-02-01\' SET "foo"=\'bar\'',
105
+ str(q),
106
+ )
107
+
108
+ with self.subTest("with column"):
109
+ q = Query.update(
110
+ self.table_abc.for_portion(self.table_abc.valid_period.from_to('2020-01-01', '2020-02-01'))
111
+ ).set("foo", "bar")
112
+
113
+ self.assertEqual(
114
+ 'UPDATE "abc" FOR PORTION OF "valid_period" FROM \'2020-01-01\' TO \'2020-02-01\' SET "foo"=\'bar\'',
115
+ str(q),
116
+ )
117
+
118
+
119
+ class PostgresUpdateTests(unittest.TestCase):
120
+ table_abc = Table("abc")
121
+
122
+ def test_update_returning_str(self):
123
+ q = PostgreSQLQuery.update(self.table_abc).where(self.table_abc.foo == 0).set("foo", "bar").returning("id")
124
+
125
+ self.assertEqual('UPDATE "abc" SET "foo"=\'bar\' WHERE "foo"=0 RETURNING "abc"."id"', str(q))
126
+
127
+ def test_update_returning(self):
128
+ q = (
129
+ PostgreSQLQuery.update(self.table_abc)
130
+ .where(self.table_abc.foo == 0)
131
+ .set("foo", "bar")
132
+ .returning(self.table_abc.id)
133
+ )
134
+
135
+ self.assertEqual('UPDATE "abc" SET "foo"=\'bar\' WHERE "foo"=0 RETURNING "abc"."id"', str(q))
136
+
137
+ def test_update_returning_from_different_tables(self):
138
+ table_bcd = Table('bcd')
139
+
140
+ q = (
141
+ PostgreSQLQuery.update(self.table_abc)
142
+ .from_(table_bcd)
143
+ .set(self.table_abc.lname, table_bcd.long_name)
144
+ .returning(self.table_abc.id, table_bcd.fname)
145
+ )
146
+ self.assertEqual(
147
+ 'UPDATE "abc" SET "lname"="bcd"."long_name" FROM "bcd" RETURNING "abc"."id","bcd"."fname"', str(q)
148
+ )
149
+
150
+ def test_update_returning_star(self):
151
+ q = PostgreSQLQuery.update(self.table_abc).where(self.table_abc.foo == 0).set("foo", "bar").returning(Star())
152
+
153
+ self.assertEqual('UPDATE "abc" SET "foo"=\'bar\' WHERE "foo"=0 RETURNING *', str(q))
154
+
155
+
156
+ class SQLLiteUpdateTests(unittest.TestCase):
157
+ table_abc = Table("abc")
158
+
159
+ def test_update_with_bool(self):
160
+ q = SQLLiteQuery.update(self.table_abc).set(self.table_abc.foo, True)
161
+
162
+ self.assertEqual('UPDATE "abc" SET "foo"=1', str(q))
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pypika/tests/test_utils.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from pypika import utils
4
+
5
+ __author__ = "Timothy Heys"
6
+ __email__ = "theys@kayak.com"
7
+
8
+
9
+ class ImmutabilityTests(unittest.TestCase):
10
+ def test_raise_attribute_error_for_deepcopy(self):
11
+ with self.assertRaises(AttributeError):
12
+ utils.raise_attribute_for_copy("Test", "__deepcopy__")
13
+
14
+ def test_raise_attribute_error_for_getstate(self):
15
+ with self.assertRaises(AttributeError):
16
+ utils.raise_attribute_for_copy("Test", "__getstate__")
17
+
18
+ def test_raise_attribute_error_for_setstate(self):
19
+ with self.assertRaises(AttributeError):
20
+ utils.raise_attribute_for_copy("Test", "__setstate__")
21
+
22
+ def test_raise_attribute_error_for_getnewargs(self):
23
+ with self.assertRaises(AttributeError):
24
+ utils.raise_attribute_for_copy("Test", "__getnewargs__")
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/pyproject.toml ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [tool.black]
2
+ line-length = 120
3
+ skip-string-normalization = true
4
+ target-version = ['py38', 'py39', 'py310', 'py311']
5
+ force-exclude = '''
6
+ (
7
+ /(
8
+ \.eggs # exclude a few common directories in the
9
+ | \.git # root of the project
10
+ | \.hg
11
+ | \.mypy_cache
12
+ | \.tox
13
+ | \.venv
14
+ | _build
15
+ | buck-out
16
+ | build
17
+ | dist
18
+ | docs
19
+ )/
20
+ | setup.py
21
+ )
22
+ '''
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/setup.cfg ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [bumpversion]
2
+ current_version = 0.48.9
3
+ commit = True
4
+ tag = True
5
+
6
+ [bumpversion:file:pypika/__init__.py]
7
+ search = __version__ = "{current_version}"
8
+ replace = __version__ = "{new_version}"
9
+
10
+ [bdist_wheel]
11
+ universal = 1
12
+
13
+ [flake8]
14
+ exclude = docs, .git, __pycache__
15
+ max-line-length = 120
16
+ per-file-ignores = __init__.py:F401
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/setup.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import ast
2
+
3
+ from setuptools import setup
4
+
5
+
6
+ def readme():
7
+ with open('README.rst', 'rb') as f:
8
+ return f.read().decode('UTF-8')
9
+
10
+
11
+ def version():
12
+ path = 'pypika/__init__.py'
13
+ with open(path, 'r') as file:
14
+ t = compile(file.read(), path, 'exec', ast.PyCF_ONLY_AST)
15
+ for node in (n for n in t.body if isinstance(n, ast.Assign)):
16
+ if len(node.targets) == 1:
17
+ name = node.targets[0]
18
+ if isinstance(name, ast.Name) and name.id in ('__version__', '__version_info__', 'VERSION'):
19
+ v = node.value
20
+ if isinstance(v, ast.Str):
21
+ return v.s
22
+
23
+ if isinstance(v, ast.Tuple):
24
+ r = []
25
+ for e in v.elts:
26
+ if isinstance(e, ast.Str):
27
+ r.append(e.s)
28
+ elif isinstance(e, ast.Num):
29
+ r.append(str(e.n))
30
+ return '.'.join(r)
31
+
32
+
33
+ setup(
34
+ # Application name:
35
+ name="PyPika",
36
+
37
+ # Version number:
38
+ version=version(),
39
+
40
+ # Application author details:
41
+ author="Timothy Heys",
42
+ author_email="theys@kayak.com",
43
+
44
+ # License
45
+ license='Apache License Version 2.0',
46
+
47
+ # Packages
48
+ packages=["pypika", "pypika.clickhouse"],
49
+
50
+ # Include additional files into the package
51
+ package_data={"pypika": ["py.typed"]},
52
+ include_package_data=True,
53
+
54
+ install_requires=[],
55
+
56
+ # Details
57
+ url="https://github.com/kayak/pypika",
58
+
59
+ description="A SQL query builder API for Python",
60
+ long_description=readme(),
61
+
62
+ classifiers=[
63
+ 'License :: OSI Approved :: Apache Software License',
64
+ 'Development Status :: 5 - Production/Stable',
65
+ 'Intended Audience :: Developers',
66
+ 'Programming Language :: Python :: 3',
67
+ 'Programming Language :: PL/SQL',
68
+ 'Topic :: Software Development :: Libraries :: Python Modules',
69
+ 'Topic :: Scientific/Engineering :: Information Analysis',
70
+ 'Topic :: Scientific/Engineering :: Mathematics',
71
+ 'Operating System :: POSIX',
72
+ 'Operating System :: MacOS :: MacOS X',
73
+ 'Operating System :: Microsoft :: Windows',
74
+ ],
75
+ keywords=('pypika python query builder querybuilder sql mysql postgres psql oracle vertica aggregated '
76
+ 'relational database rdbms business analytics bi data science analysis pandas '
77
+ 'orm object mapper'),
78
+
79
+ # Dependent packages (distributions)
80
+ test_suite="pypika.tests",
81
+ )
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/tests/trusted/tox.ini ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [tox]
2
+ envlist = py38,py39,py310,py311,pypy3
3
+
4
+ [testenv]
5
+ description = testing against {basepython}
6
+ deps = -r requirements-dev.txt
7
+ commands =
8
+ coverage run
9
+ coverage xml
10
+
11
+ [gh-actions]
12
+ python =
13
+ 3.8: py38
14
+ 3.9: py39
15
+ 3.10: py310
16
+ 3.11: py311
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/validation/canaries/first-hunk-only.sh ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ set -Eeuo pipefail
3
+ cd /testbed/repo
4
+ cat > /tmp/openenv-reference.diff <<'OPENENV_PARTIAL'
5
+ diff --git a/pypika/clickhouse/array.py b/pypika/clickhouse/array.py
6
+ index 21d2148b4bb0389126619823d5f1f2a780b24c80..67929f1664e8a3d63d5b1a83cdf66ae590c53ce4 100644
7
+ --- a/pypika/clickhouse/array.py
8
+ +++ b/pypika/clickhouse/array.py
9
+ @@ -85,10 +85,10 @@
10
+ class Empty(_AbstractArrayFunction):
11
+ @classmethod
12
+ def clickhouse_function(cls) -> str:
13
+ - return "Empty"
14
+ + return "empty"
15
+
16
+
17
+ class Length(_AbstractArrayFunction):
18
+ @classmethod
19
+ def clickhouse_function(cls) -> str:
20
+ - return "count"
21
+ + return "length"
22
+ OPENENV_PARTIAL
23
+ /usr/bin/git -c core.hooksPath=/dev/null -c core.fsmonitor=false apply --whitespace=nowarn /tmp/openenv-reference.diff
tasks/kayak__pypika.1c9646f0.combine_module__44udpu9o/validation/canaries/original-bug.sh ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ #!/bin/bash
2
+ set -Eeuo pipefail
3
+ :