11import operator
22from unittest import TestCase as _TestCase
33
4+ from pypika_tortoise .context import DEFAULT_SQL_CONTEXT
5+
46from tests .testmodels import CharFields , IntFields
57from tortoise .contrib .test import TestCase
68from tortoise .exceptions import OperationalError
@@ -134,58 +136,64 @@ def setUp(self) -> None:
134136 def test_q_basic (self ):
135137 q = Q (id = 8 )
136138 r = q .resolve (self .int_fields_context )
137- self .assertEqual (r .where_criterion .get_sql (), '"id"=8' )
139+ self .assertEqual (r .where_criterion .get_sql (DEFAULT_SQL_CONTEXT ), '"id"=8' )
138140
139141 def test_q_basic_and (self ):
140142 q = Q (join_type = "AND" , id = 8 )
141143 r = q .resolve (self .int_fields_context )
142- self .assertEqual (r .where_criterion .get_sql (), '"id"=8' )
144+ self .assertEqual (r .where_criterion .get_sql (DEFAULT_SQL_CONTEXT ), '"id"=8' )
143145
144146 def test_q_basic_or (self ):
145147 q = Q (join_type = "OR" , id = 8 )
146148 r = q .resolve (self .int_fields_context )
147- self .assertEqual (r .where_criterion .get_sql (), '"id"=8' )
149+ self .assertEqual (r .where_criterion .get_sql (DEFAULT_SQL_CONTEXT ), '"id"=8' )
148150
149151 def test_q_multiple_and (self ):
150152 q = Q (join_type = "AND" , id__gt = 8 , id__lt = 10 )
151153 r = q .resolve (self .int_fields_context )
152- self .assertEqual (r .where_criterion .get_sql (), '"id">8 AND "id"<10' )
154+ self .assertEqual (r .where_criterion .get_sql (DEFAULT_SQL_CONTEXT ), '"id">8 AND "id"<10' )
153155
154156 def test_q_multiple_or (self ):
155157 q = Q (join_type = "OR" , id__gt = 8 , id__lt = 10 )
156158 r = q .resolve (self .int_fields_context )
157- self .assertEqual (r .where_criterion .get_sql (), '"id">8 OR "id"<10' )
159+ self .assertEqual (r .where_criterion .get_sql (DEFAULT_SQL_CONTEXT ), '"id">8 OR "id"<10' )
158160
159161 def test_q_multiple_and2 (self ):
160162 q = Q (join_type = "AND" , id = 8 , intnum = 80 )
161163 r = q .resolve (self .int_fields_context )
162- self .assertEqual (r .where_criterion .get_sql (), '"id"=8 AND "intnum"=80' )
164+ self .assertEqual (r .where_criterion .get_sql (DEFAULT_SQL_CONTEXT ), '"id"=8 AND "intnum"=80' )
163165
164166 def test_q_multiple_or2 (self ):
165167 q = Q (join_type = "OR" , id = 8 , intnum = 80 )
166168 r = q .resolve (self .int_fields_context )
167- self .assertEqual (r .where_criterion .get_sql (), '"id"=8 OR "intnum"=80' )
169+ self .assertEqual (r .where_criterion .get_sql (DEFAULT_SQL_CONTEXT ), '"id"=8 OR "intnum"=80' )
168170
169171 def test_q_complex_int (self ):
170172 q = Q (Q (intnum = 80 ), Q (id__lt = 5 , id__gt = 50 , join_type = "OR" ), join_type = "AND" )
171173 r = q .resolve (self .int_fields_context )
172- self .assertEqual (r .where_criterion .get_sql (), '"intnum"=80 AND ("id"<5 OR "id">50)' )
174+ self .assertEqual (
175+ r .where_criterion .get_sql (DEFAULT_SQL_CONTEXT ), '"intnum"=80 AND ("id"<5 OR "id">50)'
176+ )
173177
174178 def test_q_complex_int2 (self ):
175179 q = Q (Q (intnum = "80" ), Q (Q (id__lt = "5" ), Q (id__gt = "50" ), join_type = "OR" ), join_type = "AND" )
176180 r = q .resolve (self .int_fields_context )
177- self .assertEqual (r .where_criterion .get_sql (), '"intnum"=80 AND ("id"<5 OR "id">50)' )
181+ self .assertEqual (
182+ r .where_criterion .get_sql (DEFAULT_SQL_CONTEXT ), '"intnum"=80 AND ("id"<5 OR "id">50)'
183+ )
178184
179185 def test_q_complex_int3 (self ):
180186 q = Q (Q (id__lt = 5 , id__gt = 50 , join_type = "OR" ), join_type = "AND" , intnum = 80 )
181187 r = q .resolve (self .int_fields_context )
182- self .assertEqual (r .where_criterion .get_sql (), '"intnum"=80 AND ("id"<5 OR "id">50)' )
188+ self .assertEqual (
189+ r .where_criterion .get_sql (DEFAULT_SQL_CONTEXT ), '"intnum"=80 AND ("id"<5 OR "id">50)'
190+ )
183191
184192 def test_q_complex_char (self ):
185193 q = Q (Q (char_null = 80 ), ~ Q (char__lt = 5 , char__gt = 50 , join_type = "OR" ), join_type = "AND" )
186194 r = q .resolve (self .char_fields_context )
187195 self .assertEqual (
188- r .where_criterion .get_sql (),
196+ r .where_criterion .get_sql (DEFAULT_SQL_CONTEXT ),
189197 "\" char_null\" ='80' AND NOT (\" char\" <'5' OR \" char\" >'50')" ,
190198 )
191199
@@ -197,47 +205,47 @@ def test_q_complex_char2(self):
197205 )
198206 r = q .resolve (self .char_fields_context )
199207 self .assertEqual (
200- r .where_criterion .get_sql (),
208+ r .where_criterion .get_sql (DEFAULT_SQL_CONTEXT ),
201209 "\" char_null\" ='80' AND NOT (\" char\" <'5' OR \" char\" >'50')" ,
202210 )
203211
204212 def test_q_complex_char3 (self ):
205213 q = Q (~ Q (char__lt = 5 , char__gt = 50 , join_type = "OR" ), join_type = "AND" , char_null = 80 )
206214 r = q .resolve (self .char_fields_context )
207215 self .assertEqual (
208- r .where_criterion .get_sql (),
216+ r .where_criterion .get_sql (DEFAULT_SQL_CONTEXT ),
209217 "\" char_null\" ='80' AND NOT (\" char\" <'5' OR \" char\" >'50')" ,
210218 )
211219
212220 def test_q_with_blank_and (self ):
213221 q = Q (Q (id__gt = 5 ), Q (), join_type = Q .AND )
214222 r = q .resolve (self .char_fields_context )
215- self .assertEqual (r .where_criterion .get_sql (), '"id">5' )
223+ self .assertEqual (r .where_criterion .get_sql (DEFAULT_SQL_CONTEXT ), '"id">5' )
216224
217225 def test_q_with_blank_or (self ):
218226 q = Q (Q (id__gt = 5 ), Q (), join_type = Q .OR )
219227 r = q .resolve (self .char_fields_context )
220- self .assertEqual (r .where_criterion .get_sql (), '"id">5' )
228+ self .assertEqual (r .where_criterion .get_sql (DEFAULT_SQL_CONTEXT ), '"id">5' )
221229
222230 def test_q_with_blank_and2 (self ):
223231 q = Q (id__gt = 5 ) & Q ()
224232 r = q .resolve (self .char_fields_context )
225- self .assertEqual (r .where_criterion .get_sql (), '"id">5' )
233+ self .assertEqual (r .where_criterion .get_sql (DEFAULT_SQL_CONTEXT ), '"id">5' )
226234
227235 def test_q_with_blank_or2 (self ):
228236 q = Q (id__gt = 5 ) | Q ()
229237 r = q .resolve (self .char_fields_context )
230- self .assertEqual (r .where_criterion .get_sql (), '"id">5' )
238+ self .assertEqual (r .where_criterion .get_sql (DEFAULT_SQL_CONTEXT ), '"id">5' )
231239
232240 def test_q_with_blank_and3 (self ):
233241 q = Q () & Q (id__gt = 5 )
234242 r = q .resolve (self .char_fields_context )
235- self .assertEqual (r .where_criterion .get_sql (), '"id">5' )
243+ self .assertEqual (r .where_criterion .get_sql (DEFAULT_SQL_CONTEXT ), '"id">5' )
236244
237245 def test_q_with_blank_or3 (self ):
238246 q = Q () | Q (id__gt = 5 )
239247 r = q .resolve (self .char_fields_context )
240- self .assertEqual (r .where_criterion .get_sql (), '"id">5' )
248+ self .assertEqual (r .where_criterion .get_sql (DEFAULT_SQL_CONTEXT ), '"id">5' )
241249
242250 def test_annotations_resolved (self ):
243251 q = Q (id__gt = 5 ) | Q (annotated__lt = 5 )
@@ -255,4 +263,4 @@ def test_annotations_resolved(self):
255263 },
256264 )
257265 )
258- self .assertEqual (r .where_criterion .get_sql (), '"id">5 OR "intnum"<5' )
266+ self .assertEqual (r .where_criterion .get_sql (DEFAULT_SQL_CONTEXT ), '"id">5 OR "intnum"<5' )
0 commit comments