This repository was archived by the owner on Jun 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathtest_parser.py
More file actions
277 lines (240 loc) · 10.4 KB
/
Copy pathtest_parser.py
File metadata and controls
277 lines (240 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# Copyright 2020 Google LLC All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import unittest
class TestParser(unittest.TestCase):
skip_condition = sys.version_info[0] < 3
skip_message = "Subtests are not supported in Python 2"
@unittest.skipIf(skip_condition, skip_message)
def test_func(self):
from google.cloud.spanner_dbapi.parser import FUNC
from google.cloud.spanner_dbapi.parser import a_args
from google.cloud.spanner_dbapi.parser import expect
from google.cloud.spanner_dbapi.parser import func
from google.cloud.spanner_dbapi.parser import pyfmt_str
cases = [
("_91())", ")", func("_91", a_args([]))),
("_a()", "", func("_a", a_args([]))),
("___()", "", func("___", a_args([]))),
("abc()", "", func("abc", a_args([]))),
(
"AF112(%s, LOWER(%s, %s), rand(%s, %s, TAN(%s, %s)))",
"",
func(
"AF112",
a_args(
[
pyfmt_str,
func("LOWER", a_args([pyfmt_str, pyfmt_str])),
func(
"rand",
a_args(
[
pyfmt_str,
pyfmt_str,
func("TAN", a_args([pyfmt_str, pyfmt_str])),
]
),
),
]
),
),
),
]
for text, want_unconsumed, want_parsed in cases:
with self.subTest(text=text):
got_unconsumed, got_parsed = expect(text, FUNC)
self.assertEqual(got_parsed, want_parsed)
self.assertEqual(got_unconsumed, want_unconsumed)
@unittest.skipIf(skip_condition, skip_message)
def test_func_fail(self):
from google.cloud.spanner_dbapi.exceptions import ProgrammingError
from google.cloud.spanner_dbapi.parser import FUNC
from google.cloud.spanner_dbapi.parser import expect
cases = [
("", "FUNC: `` does not begin with `a-zA-z` nor a `_`"),
("91", "FUNC: `91` does not begin with `a-zA-z` nor a `_`"),
("_91", "supposed to begin with `\\(`"),
("_91(", "supposed to end with `\\)`"),
("_.()", "supposed to begin with `\\(`"),
("_a.b()", "supposed to begin with `\\(`"),
]
for text, wantException in cases:
with self.subTest(text=text):
self.assertRaisesRegex(
ProgrammingError, wantException, lambda: expect(text, FUNC)
)
def test_func_eq(self):
from google.cloud.spanner_dbapi.parser import func
func1 = func("func1", None)
func2 = func("func2", None)
self.assertFalse(func1 == object)
self.assertFalse(func1 == func2)
func2.name = func1.name
func1.args = 0
func2.args = "0"
self.assertFalse(func1 == func2)
func1.args = [0]
func2.args = [0, 0]
self.assertFalse(func1 == func2)
func2.args = func1.args
self.assertTrue(func1 == func2)
@unittest.skipIf(skip_condition, skip_message)
def test_a_args(self):
from google.cloud.spanner_dbapi.parser import ARGS
from google.cloud.spanner_dbapi.parser import a_args
from google.cloud.spanner_dbapi.parser import expect
from google.cloud.spanner_dbapi.parser import func
from google.cloud.spanner_dbapi.parser import pyfmt_str
cases = [
("()", "", a_args([])),
("(%s)", "", a_args([pyfmt_str])),
("(%s,)", "", a_args([pyfmt_str])),
("(%s),", ",", a_args([pyfmt_str])),
(
"(%s,%s, f1(%s, %s))",
"",
a_args(
[pyfmt_str, pyfmt_str, func("f1", a_args([pyfmt_str, pyfmt_str]))]
),
),
]
for text, want_unconsumed, want_parsed in cases:
with self.subTest(text=text):
got_unconsumed, got_parsed = expect(text, ARGS)
self.assertEqual(got_parsed, want_parsed)
self.assertEqual(got_unconsumed, want_unconsumed)
@unittest.skipIf(skip_condition, skip_message)
def test_a_args_fail(self):
from google.cloud.spanner_dbapi.exceptions import ProgrammingError
from google.cloud.spanner_dbapi.parser import ARGS
from google.cloud.spanner_dbapi.parser import expect
cases = [
("", "ARGS: supposed to begin with `\\(`"),
("(", "ARGS: supposed to end with `\\)`"),
(")", "ARGS: supposed to begin with `\\(`"),
("(%s,%s, f1(%s, %s), %s", "ARGS: supposed to end with `\\)`"),
]
for text, wantException in cases:
with self.subTest(text=text):
self.assertRaisesRegex(
ProgrammingError, wantException, lambda: expect(text, ARGS)
)
def test_a_args_has_expr(self):
from google.cloud.spanner_dbapi.parser import a_args
self.assertFalse(a_args([]).has_expr())
self.assertTrue(a_args([[0]]).has_expr())
def test_a_args_eq(self):
from google.cloud.spanner_dbapi.parser import a_args
a1 = a_args([0])
self.assertFalse(a1 == object())
a2 = a_args([0, 0])
self.assertFalse(a1 == a2)
a1.argv = [0, 1]
self.assertFalse(a1 == a2)
a2.argv = [0, 1]
self.assertTrue(a1 == a2)
def test_a_args_homogeneous(self):
from google.cloud.spanner_dbapi.parser import a_args
from google.cloud.spanner_dbapi.parser import terminal
a_obj = a_args([a_args([terminal(10**i)]) for i in range(10)])
self.assertTrue(a_obj.homogenous())
a_obj = a_args([a_args([[object()]]) for _ in range(10)])
self.assertFalse(a_obj.homogenous())
def test_a_args__is_equal_length(self):
from google.cloud.spanner_dbapi.parser import a_args
a_obj = a_args([])
self.assertTrue(a_obj._is_equal_length())
@unittest.skipIf(skip_condition, "Python 2 has an outdated iterator definition")
@unittest.skipIf(
skip_condition, "Python 2 does not support 0-argument super() calls"
)
def test_values(self):
from google.cloud.spanner_dbapi.parser import a_args
from google.cloud.spanner_dbapi.parser import terminal
from google.cloud.spanner_dbapi.parser import values
a_obj = a_args([a_args([terminal(10**i)]) for i in range(10)])
self.assertEqual(str(values(a_obj)), "VALUES%s" % str(a_obj))
def test_expect(self):
from google.cloud.spanner_dbapi.parser import ARGS
from google.cloud.spanner_dbapi.parser import expect
from google.cloud.spanner_dbapi import exceptions
with self.assertRaises(exceptions.ProgrammingError):
expect(word="", token=ARGS)
with self.assertRaises(exceptions.ProgrammingError):
expect(word="ABC", token=ARGS)
with self.assertRaises(exceptions.ProgrammingError):
expect(word="(", token=ARGS)
with self.assertRaises(exceptions.ProgrammingError):
expect(word="", token="ABC")
@unittest.skipIf(skip_condition, skip_message)
def test_expect_values(self):
from google.cloud.spanner_dbapi.parser import VALUES
from google.cloud.spanner_dbapi.parser import a_args
from google.cloud.spanner_dbapi.parser import expect
from google.cloud.spanner_dbapi.parser import func
from google.cloud.spanner_dbapi.parser import pyfmt_str
from google.cloud.spanner_dbapi.parser import values
cases = [
("VALUES ()", "", values([a_args([])])),
("VALUES", "", values([])),
("VALUES(%s)", "", values([a_args([pyfmt_str])])),
(" VALUES (%s) ", "", values([a_args([pyfmt_str])])),
("VALUES(%s, %s)", "", values([a_args([pyfmt_str, pyfmt_str])])),
(
"VALUES(%s, %s, LOWER(%s, %s))",
"",
values(
[
a_args(
[
pyfmt_str,
pyfmt_str,
func("LOWER", a_args([pyfmt_str, pyfmt_str])),
]
)
]
),
),
(
"VALUES (UPPER(%s)), (%s)",
"",
values(
[a_args([func("UPPER", a_args([pyfmt_str]))]), a_args([pyfmt_str])]
),
),
]
for text, want_unconsumed, want_parsed in cases:
with self.subTest(text=text):
got_unconsumed, got_parsed = expect(text, VALUES)
self.assertEqual(got_parsed, want_parsed)
self.assertEqual(got_unconsumed, want_unconsumed)
@unittest.skipIf(skip_condition, skip_message)
def test_expect_values_fail(self):
from google.cloud.spanner_dbapi.exceptions import ProgrammingError
from google.cloud.spanner_dbapi.parser import VALUES
from google.cloud.spanner_dbapi.parser import expect
cases = [
("", "VALUES: `` does not start with VALUES"),
(
"VALUES(%s, %s, (%s, %s))",
"FUNC: `\\(%s, %s\\)\\)` does not begin with `a-zA-z` nor a `_`",
),
("VALUES(%s),,", "ARGS: supposed to begin with `\\(` in `,`"),
]
for text, wantException in cases:
with self.subTest(text=text):
self.assertRaisesRegex(
ProgrammingError, wantException, lambda: expect(text, VALUES)
)