|
@@ -122,4 +122,181 @@ class DataSourceTestCase(TestCase):
|
122
|
122
|
mock_insert.assert_has_calls(expected_calls, any_order = False)
|
123
|
123
|
mock_utils_query.assert_has_calls(expected_utils_query_calls)
|
124
|
124
|
|
|
125
|
+ def test_select_leobject(self):
|
|
126
|
+ """ Test select method on leobject without relational filters """
|
|
127
|
+ from dyncode import Article, Personne, Rubrique, LeObject
|
|
128
|
+
|
|
129
|
+ # Utils var and stuff to make tests queries write easier
|
|
130
|
+
|
|
131
|
+ # lodel_id field name
|
|
132
|
+ lodel_id = db_utils.field_lodel_id
|
|
133
|
+ # pre-fetch tables name to make the tests_queries
|
|
134
|
+ table_names = {
|
|
135
|
+ LeObject: db_utils.objects_table_name,
|
|
136
|
+ Article: db_utils.get_table_name_from_class(Article._leclass.__name__),
|
|
137
|
+ }
|
|
138
|
+ # lodel_id name to be use in joins (leobject side)
|
|
139
|
+ join_lodel_id = db_utils.column_prefix(table_names[LeObject], lodel_id)
|
|
140
|
+ # lodel_id name to be use in joins (leclass side)
|
|
141
|
+ def cls_lodel_id(cls): return db_utils.column_prefix(table_names[cls], lodel_id)
|
125
|
142
|
|
|
143
|
+ tests_queries = [
|
|
144
|
+ # call leobject.select(fields = ['lodel_id', 'string'], filters = [])
|
|
145
|
+ (
|
|
146
|
+ #field_list details
|
|
147
|
+ {
|
|
148
|
+ 'leobject': [lodel_id, 'string'],
|
|
149
|
+ 'leclass': [],
|
|
150
|
+ },
|
|
151
|
+ #Select args
|
|
152
|
+ {
|
|
153
|
+ 'target_cls': LeObject,
|
|
154
|
+ 'filters': [],
|
|
155
|
+ 'rel_filters': [],
|
|
156
|
+ },
|
|
157
|
+ # expt args
|
|
158
|
+ {
|
|
159
|
+ 'where':{},
|
|
160
|
+ 'joins':None, #Expected call on Query.__call__ (called when we call left_join)
|
|
161
|
+ }
|
|
162
|
+ ),
|
|
163
|
+ # call leobject.select(fields = ['lodel_id', 'string'], filters = ['lodel_id = 42', 'string = "hello"', 'rank > 1'])
|
|
164
|
+ (
|
|
165
|
+ {
|
|
166
|
+ 'leobject': [lodel_id, 'string'],
|
|
167
|
+ 'leclass': [],
|
|
168
|
+ },
|
|
169
|
+ {
|
|
170
|
+ 'target_cls': LeObject,
|
|
171
|
+ 'filters': [
|
|
172
|
+ (lodel_id,'=', 42),
|
|
173
|
+ ('string', '=', 'Hello'),
|
|
174
|
+ ('modification_date', '>', 1),
|
|
175
|
+ ],
|
|
176
|
+ 'rel_filters': [],
|
|
177
|
+ },
|
|
178
|
+ {
|
|
179
|
+ 'where':{
|
|
180
|
+ (
|
|
181
|
+ db_utils.column_prefix(table_names[LeObject], lodel_id),
|
|
182
|
+ '='
|
|
183
|
+ ): 42,
|
|
184
|
+ (
|
|
185
|
+ db_utils.column_prefix(table_names[LeObject], 'string'),
|
|
186
|
+ '='
|
|
187
|
+ ): 'Hello',
|
|
188
|
+ (
|
|
189
|
+ db_utils.column_prefix(table_names[LeObject], 'modification_date'),
|
|
190
|
+ '>'
|
|
191
|
+ ): 1
|
|
192
|
+ },
|
|
193
|
+ 'joins':None, #Expected call on Query.__call__ (called when we call left_join)
|
|
194
|
+ }
|
|
195
|
+ ),
|
|
196
|
+ # call Article.select(fields = ['lodel_id', 'titre'], filters = ['lodel_id = 42'])
|
|
197
|
+ (
|
|
198
|
+ {
|
|
199
|
+ 'leobject': [lodel_id],
|
|
200
|
+ 'leclass': ['titre'],
|
|
201
|
+ },
|
|
202
|
+ {
|
|
203
|
+ 'target_cls': Article,
|
|
204
|
+ 'filters': [ (lodel_id, '=', 42) ],
|
|
205
|
+ 'rel_filters': [],
|
|
206
|
+ },
|
|
207
|
+ {
|
|
208
|
+ 'where': {
|
|
209
|
+ (
|
|
210
|
+ db_utils.column_prefix(table_names[LeObject], lodel_id),
|
|
211
|
+ '=',
|
|
212
|
+ ): 42
|
|
213
|
+ },
|
|
214
|
+ 'joins': call(
|
|
215
|
+ table_names[Article],
|
|
216
|
+ {join_lodel_id: cls_lodel_id(Article)}
|
|
217
|
+ ),
|
|
218
|
+ }
|
|
219
|
+ ),
|
|
220
|
+ # call Article.select(fields = ['lodel_id', 'titre'], filters = ['lodel_id = 42', 'soustitre = "foobar"'])
|
|
221
|
+ (
|
|
222
|
+ {
|
|
223
|
+ 'leobject': [lodel_id],
|
|
224
|
+ 'leclass': ['titre'],
|
|
225
|
+ },
|
|
226
|
+ {
|
|
227
|
+ 'target_cls': Article,
|
|
228
|
+ 'filters': [
|
|
229
|
+ (lodel_id, '=', 42),
|
|
230
|
+ ('soustitre', '=', 'foobar'),
|
|
231
|
+ ],
|
|
232
|
+ 'rel_filters': [],
|
|
233
|
+ },
|
|
234
|
+ {
|
|
235
|
+ 'where': {
|
|
236
|
+ (
|
|
237
|
+ db_utils.column_prefix(table_names[LeObject], lodel_id),
|
|
238
|
+ '=',
|
|
239
|
+ ): 42,
|
|
240
|
+ (
|
|
241
|
+ db_utils.column_prefix(table_names[Article], 'soustitre'),
|
|
242
|
+ '=',
|
|
243
|
+ ): 'foobar',
|
|
244
|
+ },
|
|
245
|
+ 'joins': call(
|
|
246
|
+ table_names[Article],
|
|
247
|
+ {join_lodel_id: cls_lodel_id(Article)}
|
|
248
|
+ ),
|
|
249
|
+ }
|
|
250
|
+ ),
|
|
251
|
+ ]
|
|
252
|
+
|
|
253
|
+ # mock the database module to avoid connection tries
|
|
254
|
+ dbmodule_mock = Mock()
|
|
255
|
+
|
|
256
|
+ datasource = DataSource(module=dbmodule_mock, conn_args = {})
|
|
257
|
+ sql_query = 'SELECT foo FROM LeObject' # Fake return of mosql.select()
|
|
258
|
+
|
|
259
|
+ for fields_details, select_args, expt_args in tests_queries:
|
|
260
|
+ mosql_query_return_value = sql_query
|
|
261
|
+ with patch.object(mosql_Query, '__call__', return_value=mosql_query_return_value) as mock_select:
|
|
262
|
+ with patch.object(db_utils, 'query') as mock_utils_query:
|
|
263
|
+ # building the field_list argument
|
|
264
|
+ select_args['field_list'] = copy.copy(fields_details['leobject'])
|
|
265
|
+ select_args['field_list'] += fields_details['leclass']
|
|
266
|
+ # call
|
|
267
|
+ datasource.select(**select_args)
|
|
268
|
+
|
|
269
|
+ # building the select expected argument
|
|
270
|
+ select_arg = [ db_utils.column_prefix(db_utils.objects_table_name, fname) for fname in fields_details['leobject'] ]
|
|
271
|
+ if len(fields_details['leclass']) > 0:
|
|
272
|
+ leclass = select_args['target_cls']
|
|
273
|
+ leclass = leclass._leclass if leclass.is_letype() else leclass
|
|
274
|
+ table_name = db_utils.get_table_name_from_class(leclass.__name__)
|
|
275
|
+ select_arg += [ db_utils.column_prefix(table_name, field_name) for field_name in fields_details['leclass'] ]
|
|
276
|
+
|
|
277
|
+ if expt_args['joins'] is None:
|
|
278
|
+ # If the call is made with LeObject as target class no joins call is made
|
|
279
|
+ mock_select.assert_called_once_with(
|
|
280
|
+ db_utils.objects_table_name,
|
|
281
|
+ select= select_arg,
|
|
282
|
+ where=expt_args['where'],
|
|
283
|
+ joins=[]
|
|
284
|
+ )
|
|
285
|
+ else:
|
|
286
|
+ # Else there has to be 2 calls, 1 for the join another for the select
|
|
287
|
+ expected_calls = [
|
|
288
|
+ expt_args['joins'],
|
|
289
|
+ call(
|
|
290
|
+ db_utils.objects_table_name,
|
|
291
|
+ select= select_arg,
|
|
292
|
+ where=expt_args['where'],
|
|
293
|
+ joins=[mosql_query_return_value]
|
|
294
|
+ )
|
|
295
|
+ ]
|
|
296
|
+ mock_select.assert_has_calls(expected_calls, any_order=False)
|
|
297
|
+
|
|
298
|
+ # Tests that the query is really sent to the query method
|
|
299
|
+ mock_utils_query.assert_called_once_with(
|
|
300
|
+ datasource.connection,
|
|
301
|
+ sql_query
|
|
302
|
+ )
|