How to use apply_filters method in avocado

Best Python code snippet using avocado_python

test_filters.py

Source:test_filters.py Github

copy

Full Screen

...69class TestFiltersNotApplied:70 def test_no_filters_provided(self, session):71 stmt = select(Bar)72 filters = []73 filtered_query = apply_filters(stmt, filters)74 assert stmt == filtered_query75 @pytest.mark.parametrize('filter_', ['some text', 1, ''])76 def test_wrong_filters_format(self, session, filter_):77 stmt = select(Bar)78 filters = [filter_]79 with pytest.raises(BadFilterFormat) as err:80 apply_filters(stmt, filters)81 expected_error = 'Filter spec `{}` should be a dictionary.'.format(82 filter_83 )84 assert expected_error == err.value.args[0]85 def test_invalid_operator(self, session):86 stmt = select(Bar)87 filters = [{'field': 'name', 'op': 'op_not_valid', 'value': 'name_1'}]88 with pytest.raises(BadFilterFormat) as err:89 apply_filters(stmt, filters)90 assert 'Operator `op_not_valid` not valid.' == err.value.args[0]91 @pytest.mark.usefixtures('multiple_bars_inserted')92 def test_no_operator_provided(self, session):93 stmt = select(Bar)94 filters = [{'field': 'name', 'value': 'name_1'}]95 filtered_query = apply_filters(stmt, filters)96 result = session.execute(filtered_query).scalars().all()97 assert len(result) == 298 assert result[0].id == 199 assert result[1].id == 3100 def test_no_field_provided(self, session):101 stmt = select(Bar)102 filters = [{'op': '==', 'value': 'name_1'}]103 with pytest.raises(BadFilterFormat) as err:104 apply_filters(stmt, filters)105 expected_error = '`field` is a mandatory filter attribute.'106 assert expected_error == err.value.args[0]107 # TODO: replace this test once we add the option to compare against108 # another field109 def test_no_value_provided(self, session):110 stmt = select(Bar)111 filters = [{'field': 'name', 'op': '==', }]112 with pytest.raises(BadFilterFormat) as err:113 apply_filters(stmt, filters)114 assert '`value` must be provided.' == err.value.args[0]115 def test_invalid_field(self, session):116 stmt = select(Bar)117 filters = [{'field': 'invalid_field', 'op': '==', 'value': 'name_1'}]118 with pytest.raises(FieldNotFound) as err:119 apply_filters(stmt, filters)120 expected_error = (121 "Model <class 'test.models.Bar'> has no column `invalid_field`."122 )123 assert expected_error == err.value.args[0]124 @pytest.mark.parametrize('attr_name', [125 'metadata', # model attribute126 'foos', # model relationship127 ])128 def test_invalid_field_but_valid_model_attribute(self, session, attr_name):129 stmt = select(Bar)130 filters = [{'field': attr_name, 'op': '==', 'value': 'name_1'}]131 with pytest.raises(FieldNotFound) as err:132 apply_filters(stmt, filters)133 expected_error = (134 "Model <class 'test.models.Bar'> has no column `{}`.".format(135 attr_name136 )137 )138 assert expected_error == err.value.args[0]139class TestMultipleModels:140 # TODO: multi-model should be tested for each filter type141 @pytest.mark.usefixtures('multiple_bars_inserted')142 @pytest.mark.usefixtures('multiple_quxs_inserted')143 def test_multiple_models(self, session):144 stmt = select(Bar, Qux)145 filters = [146 {'model': 'Bar', 'field': 'name', 'op': '==', 'value': 'name_1'},147 {'model': 'Qux', 'field': 'name', 'op': '==', 'value': 'name_1'},148 ]149 filtered_query = apply_filters(stmt, filters)150 result = session.execute(filtered_query).unique().all()151 assert len(result) == 4152 bars, quxs = zip(*result)153 assert set(map(type, bars)) == {Bar}154 assert {bar.id for bar in bars} == {1, 3}155 assert {bar.name for bar in bars} == {"name_1"}156 assert set(map(type, quxs)) == {Qux}157 assert {qux.id for qux in quxs} == {1, 3}158 assert {qux.name for qux in quxs} == {"name_1"}159class TestAutoJoin:160 @pytest.mark.usefixtures('multiple_foos_inserted')161 def test_auto_join(self, session):162 stmt = select(Foo)163 filters = [164 {'field': 'name', 'op': '==', 'value': 'name_1'},165 {'model': 'Bar', 'field': 'count', 'op': 'is_null'},166 ]167 filtered_query = apply_filters(stmt, filters)168 result = session.execute(filtered_query).scalars().all()169 assert len(result) == 1170 assert result[0].id == 3171 assert result[0].bar_id == 3172 assert result[0].bar.count is None173 @pytest.mark.usefixtures('multiple_foos_inserted')174 def test_do_not_auto_join(self, session):175 stmt = select(Foo)176 filters = [177 {'field': 'name', 'op': '==', 'value': 'name_1'},178 {'model': 'Bar', 'field': 'count', 'op': 'is_null'},179 ]180 with pytest.raises(BadSpec) as exc:181 apply_filters(stmt, filters, do_auto_join=False)182 assert 'The query does not contain model `Bar`' in str(exc)183 @pytest.mark.usefixtures('multiple_foos_inserted')184 def test_noop_if_query_contains_named_models(self, session):185 stmt = select(Foo).join(Bar)186 filters = [187 {'model': 'Foo', 'field': 'name', 'op': '==', 'value': 'name_1'},188 {'model': 'Bar', 'field': 'count', 'op': 'is_null'},189 ]190 filtered_query = apply_filters(stmt, filters)191 result = session.execute(filtered_query).scalars().all()192 assert len(result) == 1193 assert result[0].id == 3194 assert result[0].bar_id == 3195 assert result[0].bar.count is None196 @pytest.mark.usefixtures('multiple_foos_inserted')197 def test_auto_join_to_invalid_model(self, session):198 stmt = select(Foo)199 filters = [200 {'field': 'name', 'op': '==', 'value': 'name_1'},201 {'model': 'Bar', 'field': 'count', 'op': 'is_null'},202 {'model': 'Qux', 'field': 'created_at', 'op': 'is_not_null'}203 ]204 with pytest.raises(BadSpec) as err:205 apply_filters(stmt, filters)206 assert 'The query does not contain model `Qux`.' == err.value.args[0]207 @pytest.mark.usefixtures('multiple_foos_inserted')208 def test_ambiguous_query(self, session):209 stmt = select(Foo).join(Bar)210 filters = [211 {'field': 'name', 'op': '==', 'value': 'name_1'}, # ambiguous212 {'model': 'Bar', 'field': 'count', 'op': 'is_null'},213 ]214 with pytest.raises(BadSpec) as err:215 apply_filters(stmt, filters)216 assert 'Ambiguous spec. Please specify a model.' == err.value.args[0]217 @pytest.mark.usefixtures('multiple_foos_inserted')218 def test_eager_load(self, session):219 # behaves as if the joinedload wasn't present220 stmt = select(Foo).options(joinedload(Foo.bar))221 filters = [222 {'field': 'name', 'op': '==', 'value': 'name_1'},223 {'model': 'Bar', 'field': 'count', 'op': 'is_null'},224 ]225 filtered_query = apply_filters(stmt, filters)226 result = session.execute(filtered_query).scalars().all()227 assert len(result) == 1228 assert result[0].id == 3229 assert result[0].bar_id == 3230 assert result[0].bar.count is None231class TestApplyIsNullFilter:232 @pytest.mark.usefixtures('multiple_bars_inserted')233 def test_filter_field_with_null_values(self, session):234 stmt = select(Bar)235 filters = [{'field': 'count', 'op': 'is_null'}]236 filtered_query = apply_filters(stmt, filters)237 result = session.execute(filtered_query).scalars().all()238 assert len(result) == 1239 assert result[0].id == 3240 @pytest.mark.usefixtures('multiple_bars_inserted')241 def test_filter_field_with_no_null_values(self, session):242 stmt = select(Bar)243 filters = [{'field': 'name', 'op': 'is_null'}]244 filtered_query = apply_filters(stmt, filters)245 result = session.execute(filtered_query).scalars().all()246 assert len(result) == 0247class TestApplyIsNotNullFilter:248 @pytest.mark.usefixtures('multiple_bars_inserted')249 def test_filter_field_with_null_values(self, session):250 stmt = select(Bar)251 filters = [{'field': 'count', 'op': 'is_not_null'}]252 filtered_query = apply_filters(stmt, filters)253 result = session.execute(filtered_query).scalars().all()254 assert len(result) == 3255 assert result[0].id == 1256 assert result[1].id == 2257 assert result[2].id == 4258 @pytest.mark.usefixtures('multiple_bars_inserted')259 def test_filter_field_with_no_null_values(self, session):260 stmt = select(Bar)261 filters = [{'field': 'name', 'op': 'is_not_null'}]262 filtered_query = apply_filters(stmt, filters)263 result = session.execute(filtered_query).scalars().all()264 assert len(result) == 4265 assert result[0].id == 1266 assert result[1].id == 2267 assert result[2].id == 3268 assert result[3].id == 4269class TestApplyFiltersMultipleTimes:270 @pytest.mark.usefixtures('multiple_bars_inserted')271 def test_concatenate_queries(self, session):272 stmt = select(Bar)273 filters = [{'field': 'name', 'op': '==', 'value': 'name_1'}]274 filtered_query = apply_filters(stmt, filters)275 result = session.execute(filtered_query).scalars().all()276 assert len(result) == 2277 assert result[0].id == 1278 assert result[0].name == 'name_1'279 assert result[1].id == 3280 assert result[1].name == 'name_1'281 filters = [{'field': 'id', 'op': '==', 'value': 3}]282 filtered_query = apply_filters(filtered_query, filters)283 result = session.execute(filtered_query).scalars().all()284 assert len(result) == 1285 assert result[0].id == 3286 assert result[0].name == 'name_1'287class TestApplyFilterWithoutList:288 @pytest.mark.usefixtures('multiple_bars_inserted')289 def test_a_single_dict_can_be_supplied_as_filters(self, session):290 stmt = select(Bar)291 filters = {'field': 'name', 'op': '==', 'value': 'name_1'}292 filtered_query = apply_filters(stmt, filters)293 result = session.execute(filtered_query).scalars().all()294 assert len(result) == 2295 assert result[0].id == 1296 assert result[0].name == 'name_1'297 assert result[1].id == 3298 assert result[1].name == 'name_1'299class TestApplyFilterOnFieldBasedQuery:300 @pytest.mark.usefixtures('multiple_bars_inserted')301 def test_apply_filter_on_single_field_query(self, session):302 stmt = select(Bar.id)303 filters = [{'field': 'name', 'op': '==', 'value': 'name_1'}]304 filtered_query = apply_filters(stmt, filters)305 result = session.execute(filtered_query).all()306 assert len(result) == 2307 assert result[0] == (1,)308 assert result[1] == (3,)309 @pytest.mark.usefixtures('multiple_bars_inserted')310 def test_apply_filter_on_aggregate_query(self, session):311 stmt = select(func.count(Bar.id))312 filters = [{'field': 'name', 'op': '==', 'value': 'name_1'}]313 filtered_query = apply_filters(stmt, filters)314 result = session.execute(filtered_query).all()315 assert len(result) == 1316 assert result[0] == (2,)317class TestApplyEqualToFilter:318 @pytest.mark.parametrize('operator', ['==', 'eq'])319 @pytest.mark.usefixtures('multiple_bars_inserted')320 def test_one_filter_applied_to_a_single_model(self, session, operator):321 stmt = select(Bar)322 filters = [{'field': 'name', 'op': operator, 'value': 'name_1'}]323 filtered_query = apply_filters(stmt, filters)324 result = session.execute(filtered_query).scalars().all()325 assert len(result) == 2326 assert result[0].id == 1327 assert result[0].name == 'name_1'328 assert result[1].id == 3329 assert result[1].name == 'name_1'330 @pytest.mark.parametrize(331 'filters', [332 [ # filters using `==` in a list333 {'field': 'name', 'op': '==', 'value': 'name_1'},334 {'field': 'id', 'op': '==', 'value': 3}335 ],336 ( # filters using `eq` in a tuple337 {'field': 'name', 'op': 'eq', 'value': 'name_1'},338 {'field': 'id', 'op': 'eq', 'value': 3}339 )340 ]341 )342 @pytest.mark.usefixtures('multiple_bars_inserted')343 def test_multiple_filters_applied_to_a_single_model(344 self, session, filters345 ):346 stmt = select(Bar)347 filtered_query = apply_filters(stmt, filters)348 result = session.execute(filtered_query).scalars().all()349 assert len(result) == 1350 assert result[0].id == 3351 assert result[0].name == 'name_1'352class TestApplyNotEqualToFilter:353 @pytest.mark.parametrize('operator', ['!=', 'ne'])354 @pytest.mark.usefixtures('multiple_bars_inserted')355 def test_one_filter_applied_to_a_single_model(self, session, operator):356 stmt = select(Bar)357 filters = [{'field': 'name', 'op': operator, 'value': 'name_1'}]358 filtered_query = apply_filters(stmt, filters)359 result = session.execute(filtered_query).scalars().all()360 assert len(result) == 2361 assert result[0].id == 2362 assert result[0].name == 'name_2'363 assert result[1].id == 4364 assert result[1].name == 'name_4'365 @pytest.mark.parametrize('operator', ['!=', 'ne'])366 @pytest.mark.usefixtures('multiple_bars_inserted')367 def test_multiple_filters_applied_to_a_single_model(368 self, session, operator369 ):370 stmt = select(Bar)371 filters = [372 {'field': 'name', 'op': operator, 'value': 'name_2'},373 {'field': 'id', 'op': operator, 'value': 3}374 ]375 filtered_query = apply_filters(stmt, filters)376 result = session.execute(filtered_query).scalars().all()377 assert len(result) == 2378 assert result[0].id == 1379 assert result[0].name == 'name_1'380 assert result[1].id == 4381 assert result[1].name == 'name_4'382class TestApplyGreaterThanFilter:383 @pytest.mark.parametrize('operator', ['>', 'gt'])384 @pytest.mark.usefixtures('multiple_bars_inserted')385 def test_one_filter_applied_to_a_single_model(self, session, operator):386 stmt = select(Bar)387 filters = [{'field': 'count', 'op': operator, 'value': '5'}]388 filtered_query = apply_filters(stmt, filters)389 result = session.execute(filtered_query).scalars().all()390 assert len(result) == 2391 assert result[0].id == 2392 assert result[1].id == 4393 @pytest.mark.parametrize('operator', ['>', 'gt'])394 @pytest.mark.usefixtures('multiple_bars_inserted')395 def test_multiple_filters_applied_to_a_single_model(396 self, session, operator397 ):398 stmt = select(Bar)399 filters = [400 {'field': 'count', 'op': operator, 'value': '5'},401 {'field': 'id', 'op': operator, 'value': 2},402 ]403 filtered_query = apply_filters(stmt, filters)404 result = session.execute(filtered_query).scalars().all()405 assert len(result) == 1406 assert result[0].id == 4407class TestApplyLessThanFilter:408 @pytest.mark.parametrize('operator', ['<', 'lt'])409 @pytest.mark.usefixtures('multiple_bars_inserted')410 def test_one_filter_applied_to_a_single_model(self, session, operator):411 stmt = select(Bar)412 filters = [{'field': 'count', 'op': operator, 'value': '7'}]413 filtered_query = apply_filters(stmt, filters)414 result = session.execute(filtered_query).scalars().all()415 assert len(result) == 1416 assert result[0].id == 1417 @pytest.mark.parametrize('operator', ['<', 'lt'])418 @pytest.mark.usefixtures('multiple_bars_inserted')419 def test_multiple_filters_applied_to_a_single_model(420 self, session, operator421 ):422 stmt = select(Bar)423 filters = [424 {'field': 'count', 'op': operator, 'value': '7'},425 {'field': 'id', 'op': operator, 'value': 1},426 ]427 filtered_query = apply_filters(stmt, filters)428 result = session.execute(filtered_query).scalars().all()429 assert len(result) == 0430class TestApplyGreaterOrEqualThanFilter:431 @pytest.mark.parametrize('operator', ['>=', 'ge'])432 @pytest.mark.usefixtures('multiple_bars_inserted')433 def test_one_filter_applied_to_a_single_model(self, session, operator):434 stmt = select(Bar)435 filters = [{'field': 'count', 'op': operator, 'value': '5'}]436 filtered_query = apply_filters(stmt, filters)437 result = session.execute(filtered_query).scalars().all()438 assert len(result) == 3439 assert result[0].id == 1440 assert result[1].id == 2441 assert result[2].id == 4442 @pytest.mark.parametrize('operator', ['>=', 'ge'])443 @pytest.mark.usefixtures('multiple_bars_inserted')444 def test_multiple_filters_applied_to_a_single_model(445 self, session, operator446 ):447 stmt = select(Bar)448 filters = [449 {'field': 'count', 'op': operator, 'value': '5'},450 {'field': 'id', 'op': operator, 'value': 4},451 ]452 filtered_query = apply_filters(stmt, filters)453 result = session.execute(filtered_query).scalars().all()454 assert len(result) == 1455 assert result[0].id == 4456class TestApplyLessOrEqualThanFilter:457 @pytest.mark.parametrize('operator', ['<=', 'le'])458 @pytest.mark.usefixtures('multiple_bars_inserted')459 def test_one_filter_applied_to_a_single_model(self, session, operator):460 stmt = select(Bar)461 filters = [{'field': 'count', 'op': operator, 'value': '15'}]462 filtered_query = apply_filters(stmt, filters)463 result = session.execute(filtered_query).scalars().all()464 assert len(result) == 3465 assert result[0].id == 1466 assert result[1].id == 2467 assert result[2].id == 4468 @pytest.mark.parametrize('operator', ['<=', 'le'])469 @pytest.mark.usefixtures('multiple_bars_inserted')470 def test_multiple_filters_applied_to_a_single_model(471 self, session, operator472 ):473 stmt = select(Bar)474 filters = [475 {'field': 'count', 'op': operator, 'value': '15'},476 {'field': 'id', 'op': operator, 'value': 1},477 ]478 filtered_query = apply_filters(stmt, filters)479 result = session.execute(filtered_query).scalars().all()480 assert len(result) == 1481 assert result[0].id == 1482class TestApplyLikeFilter:483 @pytest.mark.usefixtures('multiple_bars_inserted')484 def test_one_filter_applied_to_a_single_model(self, session):485 stmt = select(Bar)486 filters = [{'field': 'name', 'op': 'like', 'value': '%me_1'}]487 filtered_query = apply_filters(stmt, filters)488 result = session.execute(filtered_query).scalars().all()489 assert len(result) == 2490 assert result[0].id == 1491 assert result[1].id == 3492class TestApplyILikeFilter:493 @pytest.mark.usefixtures('multiple_bars_inserted')494 def test_one_filter_applied_to_a_single_model(self, session):495 stmt = session.query(Bar)496 filters = [{'field': 'name', 'op': 'ilike', 'value': '%ME_1'}]497 filtered_query = apply_filters(stmt, filters)498 result = session.execute(filtered_query).scalars().all()499 assert len(result) == 2500 assert result[0].id == 1501 assert result[1].id == 3502class TestApplyNotILikeFilter:503 @pytest.mark.usefixtures('multiple_bars_inserted')504 def test_one_filter_applied_to_a_single_model(self, session):505 stmt = select(Bar)506 filters = [{'field': 'name', 'op': 'not_ilike', 'value': '%ME_1'}]507 filtered_query = apply_filters(stmt, filters)508 result = session.execute(filtered_query).scalars().all()509 assert len(result) == 2510 assert result[0].id == 2511 assert result[1].id == 4512class TestApplyInFilter:513 @pytest.mark.usefixtures('multiple_bars_inserted')514 def test_field_not_in_value_list(self, session):515 stmt = select(Bar)516 filters = [{'field': 'count', 'op': 'in', 'value': [1, 2, 3]}]517 filtered_query = apply_filters(stmt, filters)518 result = session.execute(filtered_query).all()519 assert len(result) == 0520 @pytest.mark.usefixtures('multiple_bars_inserted')521 def test_field_in_value_list(self, session):522 stmt = select(Bar)523 filters = [{'field': 'count', 'op': 'in', 'value': [15, 2, 3]}]524 filtered_query = apply_filters(stmt, filters)525 result = session.execute(filtered_query).scalars().all()526 assert len(result) == 1527 assert result[0].id == 4528class TestApplyNotInFilter:529 @pytest.mark.usefixtures('multiple_bars_inserted')530 def test_field_not_in_value_list(self, session):531 stmt = select(Bar)532 filters = [{'field': 'count', 'op': 'not_in', 'value': [1, 2, 3]}]533 filtered_query = apply_filters(stmt, filters)534 result = session.execute(filtered_query).scalars().all()535 assert len(result) == 3536 assert result[0].id == 1537 assert result[1].id == 2538 assert result[2].id == 4539 @pytest.mark.usefixtures('multiple_bars_inserted')540 def test_field_in_value_list(self, session):541 stmt = select(Bar)542 filters = [{'field': 'count', 'op': 'not_in', 'value': [15, 2, 3]}]543 filtered_query = apply_filters(stmt, filters)544 result = session.execute(filtered_query).scalars().all()545 assert len(result) == 2546 assert result[0].id == 1547 assert result[1].id == 2548class TestDateFields:549 @pytest.mark.parametrize(550 'value',551 [552 datetime.date(2016, 7, 14),553 datetime.date(2016, 7, 14).isoformat()554 ]555 )556 @pytest.mark.usefixtures('multiple_quxs_inserted')557 def test_filter_date_equality(self, session, value):558 stmt = select(Qux)559 filters = [{560 'field': 'created_at',561 'op': '==',562 'value': value563 }]564 filtered_query = apply_filters(stmt, filters)565 result = session.execute(filtered_query).scalars().all()566 assert len(result) == 1567 assert result[0].created_at == datetime.date(2016, 7, 14)568 @pytest.mark.parametrize(569 'value',570 [571 datetime.date(2016, 7, 13),572 datetime.date(2016, 7, 13).isoformat()573 ]574 )575 @pytest.mark.usefixtures('multiple_quxs_inserted')576 def test_filter_multiple_dates(self, session, value):577 stmt = select(Qux)578 filters = [{579 'field': 'created_at',580 'op': '>=',581 'value': value582 }]583 filtered_query = apply_filters(stmt, filters)584 result = session.execute(filtered_query).scalars().all()585 assert len(result) == 2586 assert result[0].created_at == datetime.date(2016, 7, 13)587 assert result[1].created_at == datetime.date(2016, 7, 14)588 @pytest.mark.usefixtures('multiple_quxs_inserted')589 def test_null_date(self, session):590 stmt = session.query(Qux)591 filters = [{'field': 'created_at', 'op': 'is_null'}]592 filtered_query = apply_filters(stmt, filters)593 result = session.execute(filtered_query).scalars().all()594 assert len(result) == 1595 assert result[0].created_at is None596class TestTimeFields:597 @pytest.mark.parametrize(598 'value',599 [600 datetime.time(3, 5, 9),601 datetime.time(3, 5, 9).isoformat() # '03:05:09'602 ]603 )604 @pytest.mark.usefixtures('multiple_quxs_inserted')605 def test_filter_time_equality(self, session, is_sqlite, value):606 if isinstance(value, str) and is_sqlite:607 pytest.skip(STRING_DATE_TIME_NOT_SUPPORTED)608 stmt = select(Qux)609 filters = [{'field': 'expiration_time', 'op': '==', 'value': value}]610 filtered_query = apply_filters(stmt, filters)611 result = session.execute(filtered_query).scalars().all()612 assert len(result) == 1613 assert result[0].expiration_time == datetime.time(3, 5, 9)614 @pytest.mark.parametrize(615 'value',616 [617 datetime.time(2, 5, 9),618 datetime.time(2, 5, 9).isoformat() # '02:05:09'619 ]620 )621 @pytest.mark.usefixtures('multiple_quxs_inserted')622 def test_filter_multiple_times(self, session, is_sqlite, value):623 if isinstance(value, str) and is_sqlite:624 pytest.skip(STRING_DATE_TIME_NOT_SUPPORTED)625 stmt = select(Qux)626 filters = [{627 'field': 'expiration_time',628 'op': '>=',629 'value': value630 }]631 filtered_query = apply_filters(stmt, filters)632 result = session.execute(filtered_query).scalars().all()633 assert len(result) == 2634 assert result[0].expiration_time == datetime.time(2, 5, 9)635 assert result[1].expiration_time == datetime.time(3, 5, 9)636 @pytest.mark.usefixtures('multiple_quxs_inserted')637 def test_null_time(self, session):638 stmt = select(Qux)639 filters = [{'field': 'expiration_time', 'op': 'is_null'}]640 filtered_query = apply_filters(stmt, filters)641 result = session.execute(filtered_query).scalars().all()642 assert len(result) == 1643 assert result[0].expiration_time is None644class TestDateTimeFields:645 @pytest.mark.parametrize(646 'value',647 [648 datetime.datetime(2016, 7, 14, 3, 5, 9),649 # '2016-07-14T03:05:09'650 datetime.datetime(2016, 7, 14, 3, 5, 9).isoformat()651 ]652 )653 @pytest.mark.usefixtures('multiple_quxs_inserted')654 def test_filter_datetime_equality(self, session, is_sqlite, value):655 if isinstance(value, str) and is_sqlite:656 pytest.skip(STRING_DATE_TIME_NOT_SUPPORTED)657 stmt = select(Qux)658 filters = [{659 'field': 'execution_time',660 'op': '==',661 'value': value662 }]663 filtered_query = apply_filters(stmt, filters)664 result = session.execute(filtered_query).scalars().all()665 assert len(result) == 1666 assert result[0].execution_time == datetime.datetime(667 2016, 7, 14, 3, 5, 9668 )669 @pytest.mark.parametrize(670 'value',671 [672 datetime.datetime(2016, 7, 13, 2, 5, 9),673 # '2016-07-13T02:05:09'674 datetime.datetime(2016, 7, 13, 2, 5, 9).isoformat()675 ]676 )677 @pytest.mark.usefixtures('multiple_quxs_inserted')678 def test_filter_multiple_datetimes(self, session, is_sqlite, value):679 if isinstance(value, str) and is_sqlite:680 pytest.skip(STRING_DATE_TIME_NOT_SUPPORTED)681 stmt = select(Qux)682 filters = [{683 'field': 'execution_time',684 'op': '>=',685 'value': value686 }]687 filtered_query = apply_filters(stmt, filters)688 result = session.execute(filtered_query).scalars().all()689 assert len(result) == 2690 assert result[0].execution_time == datetime.datetime(691 2016, 7, 13, 2, 5, 9692 )693 assert result[1].execution_time == datetime.datetime(694 2016, 7, 14, 3, 5, 9695 )696 @pytest.mark.usefixtures('multiple_quxs_inserted')697 def test_null_datetime(self, session):698 stmt = select(Qux)699 filters = [{'field': 'execution_time', 'op': 'is_null'}]700 filtered_query = apply_filters(stmt, filters)701 result = session.execute(filtered_query).scalars().all()702 assert len(result) == 1703 assert result[0].execution_time is None704class TestApplyBooleanFunctions:705 @pytest.mark.usefixtures('multiple_bars_inserted')706 def test_or(self, session):707 stmt = select(Bar)708 filters = [709 {'or': [710 {'field': 'id', 'op': '==', 'value': 1},711 {'field': 'id', 'op': '==', 'value': 3},712 ]},713 ]714 filtered_query = apply_filters(stmt, filters)715 result = session.execute(filtered_query).scalars().all()716 assert len(result) == 2717 assert result[0].id == 1718 assert result[1].id == 3719 @pytest.mark.usefixtures('multiple_bars_inserted')720 def test_or_with_one_arg(self, session):721 stmt = select(Bar)722 filters = [723 {'or': [724 {'field': 'id', 'op': '==', 'value': 1},725 ]},726 ]727 filtered_query = apply_filters(stmt, filters)728 result = session.execute(filtered_query).scalars().all()729 assert len(result) == 1730 assert result[0].id == 1731 @pytest.mark.usefixtures('multiple_bars_inserted')732 def test_or_with_three_args(self, session):733 stmt = select(Bar)734 filters = [735 {'or': [736 {'field': 'id', 'op': '==', 'value': 1},737 {'field': 'id', 'op': '==', 'value': 3},738 {'field': 'id', 'op': '==', 'value': 4},739 ]},740 ]741 filtered_query = apply_filters(stmt, filters)742 result = session.execute(filtered_query).scalars().all()743 assert len(result) == 3744 assert result[0].id == 1745 assert result[1].id == 3746 assert result[2].id == 4747 @pytest.mark.parametrize(748 ('or_args', 'expected_error'), [749 (750 [],751 '`or` must have one or more arguments'752 ),753 (754 {},755 '`or` value must be an iterable across the function arguments'756 ),757 (758 'hello',759 '`or` value must be an iterable across the function arguments'760 ),761 ]762 )763 @pytest.mark.usefixtures('multiple_bars_inserted')764 def test_or_with_bad_format(self, session, or_args, expected_error):765 stmt = select(Bar)766 filters = [{'or': or_args}]767 with pytest.raises(BadFilterFormat) as exc:768 apply_filters(stmt, filters)769 assert expected_error in str(exc)770 @pytest.mark.usefixtures('multiple_bars_inserted')771 def test_and(self, session):772 stmt = select(Bar)773 filters = [774 {'and': [775 {'field': 'id', 'op': '<=', 'value': 2},776 {'field': 'count', 'op': '>=', 'value': 6},777 ]},778 ]779 filtered_query = apply_filters(stmt, filters)780 result = session.execute(filtered_query).scalars().all()781 assert len(result) == 1782 assert result[0].id == 2783 @pytest.mark.usefixtures('multiple_bars_inserted')784 def test_and_with_one_arg(self, session):785 stmt = select(Bar)786 filters = [787 {'and': [788 {'field': 'id', 'op': '==', 'value': 3},789 ]},790 ]791 filtered_query = apply_filters(stmt, filters)792 result = session.execute(filtered_query).scalars().all()793 assert len(result) == 1794 assert result[0].id == 3795 @pytest.mark.usefixtures('multiple_bars_inserted')796 def test_and_with_three_args(self, session):797 stmt = select(Bar)798 filters = [799 {'and': [800 {'field': 'id', 'op': '<=', 'value': 3},801 {'field': 'name', 'op': '==', 'value': 'name_1'},802 {'field': 'count', 'op': 'is_not_null'},803 ]},804 ]805 filtered_query = apply_filters(stmt, filters)806 result = session.execute(filtered_query).scalars().all()807 assert len(result) == 1808 assert result[0].id == 1809 @pytest.mark.parametrize(810 ('and_args', 'expected_error'), [811 (812 [],813 '`and` must have one or more arguments'814 ),815 (816 {},817 '`and` value must be an iterable across the function arguments'818 ),819 (820 'hello',821 '`and` value must be an iterable across the function arguments'822 ),823 ]824 )825 @pytest.mark.usefixtures('multiple_bars_inserted')826 def test_and_with_bad_format(self, session, and_args, expected_error):827 stmt = select(Bar)828 filters = [{'and': and_args}]829 with pytest.raises(BadFilterFormat) as exc:830 apply_filters(stmt, filters)831 assert expected_error in str(exc)832 @pytest.mark.usefixtures('multiple_bars_inserted')833 def test_not(self, session):834 stmt = select(Bar)835 filters = [836 {'not': [837 {'field': 'id', 'op': '==', 'value': 3},838 ]},839 ]840 filtered_query = apply_filters(stmt, filters)841 result = session.execute(filtered_query).scalars().all()842 assert len(result) == 3843 assert result[0].id == 1844 assert result[1].id == 2845 assert result[2].id == 4846 @pytest.mark.parametrize(847 ('not_args', 'expected_error'), [848 (849 [{'field': 'id', 'op': '==', 'value': 1},850 {'field': 'id', 'op': '==', 'value': 2}],851 '`not` must have one argument'852 ),853 (854 [],855 '`not` must have one argument'856 ),857 (858 {},859 '`not` value must be an iterable across the function arguments'860 ),861 (862 'hello',863 '`not` value must be an iterable across the function arguments'864 ),865 ]866 )867 @pytest.mark.usefixtures('multiple_bars_inserted')868 def test_not_with_bad_format(self, session, not_args, expected_error):869 stmt = select(Bar)870 filters = [{'not': not_args}]871 with pytest.raises(BadFilterFormat) as exc:872 apply_filters(stmt, filters)873 assert expected_error in str(exc)874 @pytest.mark.usefixtures('multiple_bars_inserted')875 def test_complex(self, session):876 stmt = select(Bar)877 filters = [878 {879 'and': [880 {881 'or': [882 {'field': 'id', 'op': '==', 'value': 2},883 {'field': 'id', 'op': '==', 'value': 3},884 ]885 },886 {887 'not': [888 {'field': 'name', 'op': '==', 'value': 'name_2'}889 ]890 },891 ],892 }893 ]894 filtered_query = apply_filters(stmt, filters)895 result = session.execute(filtered_query).scalars().all()896 assert len(result) == 1897 assert result[0].id == 3898 @pytest.mark.usefixtures('multiple_bars_inserted')899 def test_complex_using_tuples(self, session):900 stmt = select(Bar)901 filters = (902 {903 'and': (904 {905 'or': (906 {'field': 'id', 'op': '==', 'value': 2},907 {'field': 'id', 'op': '==', 'value': 3},908 )909 },910 {911 'not': (912 {'field': 'name', 'op': '==', 'value': 'name_2'},913 )914 },915 ),916 },917 )918 filtered_query = apply_filters(stmt, filters)919 result = session.execute(filtered_query).scalars().all()920 assert len(result) == 1921 assert result[0].id == 3922class TestApplyArrayFilters:923 @pytest.mark.usefixtures('multiple_corges_inserted')924 def test_any_value_in_array(self, session, is_postgresql):925 if not is_postgresql:926 pytest.skip(ARRAY_NOT_SUPPORTED)927 stmt = select(Corge)928 filters = [{'field': 'tags', 'op': 'any', 'value': 'foo'}]929 filtered_query = apply_filters(stmt, filters)930 result = session.execute(filtered_query).scalars().all()931 assert len(result) == 2932 assert result[0].id == 2933 assert result[1].id == 3934 @pytest.mark.usefixtures('multiple_corges_inserted')935 def test_not_any_values_in_array(self, session, is_postgresql):936 if not is_postgresql:937 pytest.skip(ARRAY_NOT_SUPPORTED)938 stmt = select(Corge)939 filters = [{'field': 'tags', 'op': 'not_any', 'value': 'foo'}]940 filtered_query = apply_filters(stmt, filters)941 result = session.execute(filtered_query).scalars().all()942 assert len(result) == 2943 assert result[0].id == 1944 assert result[1].id == 4945class TestHybridAttributes:946 @pytest.mark.usefixtures('multiple_bars_inserted')947 @pytest.mark.parametrize(948 ('field, expected_error'),949 [950 ('foos', "Model <class 'test.models.Bar'> has no column `foos`."),951 (952 '__mapper__',953 "Model <class 'test.models.Bar'> has no column `__mapper__`.",954 ),955 (956 'not_valid',957 "Model <class 'test.models.Bar'> has no column `not_valid`.",958 ),959 ]960 )961 def test_orm_descriptors_not_valid_hybrid_attributes(962 self, session, field, expected_error963 ):964 stmt = select(Bar)965 filters = [966 {967 'model': 'Bar',968 'field': field,969 'op': '==',970 'value': 100971 }972 ]973 with pytest.raises(FieldNotFound) as exc:974 apply_filters(stmt, filters)975 assert expected_error in str(exc)976 @pytest.mark.usefixtures('multiple_bars_inserted')977 @pytest.mark.usefixtures('multiple_quxs_inserted')978 def test_filter_by_hybrid_properties(self, session):979 stmt = select(Bar, Qux)980 filters = [981 {982 'model': 'Bar',983 'field': 'count_square',984 'op': '==',985 'value': 100986 },987 {988 'model': 'Qux',989 'field': 'count_square',990 'op': '>=',991 'value': 26992 },993 ]994 filtered_query = apply_filters(stmt, filters)995 result = session.execute(filtered_query).all()996 assert len(result) == 2997 bars, quxs = zip(*result)998 assert set(map(type, bars)) == {Bar}999 assert {bar.id for bar in bars} == {2}1000 assert {bar.count_square for bar in bars} == {100}1001 assert set(map(type, quxs)) == {Qux}1002 assert {qux.id for qux in quxs} == {2, 4}1003 assert {qux.count_square for qux in quxs} == {100, 225}1004 @pytest.mark.usefixtures('multiple_bars_inserted')1005 @pytest.mark.usefixtures('multiple_quxs_inserted')1006 def test_filter_by_hybrid_methods(self, session):1007 stmt = select(Bar, Qux)1008 filters = [1009 {1010 'model': 'Bar',1011 'field': 'three_times_count',1012 'op': '==',1013 'value': 301014 },1015 {1016 'model': 'Qux',1017 'field': 'three_times_count',1018 'op': '>=',1019 'value': 311020 },1021 ]1022 filtered_query = apply_filters(stmt, filters)1023 result = session.execute(filtered_query).all()1024 assert len(result) == 11025 bars, quxs = zip(*result)1026 assert set(map(type, bars)) == {Bar}1027 assert {bar.id for bar in bars} == {2}1028 assert {bar.three_times_count() for bar in bars} == {30}1029 assert set(map(type, quxs)) == {Qux}1030 assert {qux.id for qux in quxs} == {4}1031 assert {qux.three_times_count() for qux in quxs} == {45}1032class TestQueryObject:1033 @pytest.mark.usefixtures('multiple_foos_inserted')1034 def test_auto_join(self, session):1035 query = session.query(Foo)1036 filters = [1037 {'field': 'name', 'op': '==', 'value': 'name_1'},1038 {'model': 'Bar', 'field': 'count', 'op': 'is_null'},1039 ]1040 filtered_query = apply_filters(query, filters)1041 result = filtered_query.all()1042 assert len(result) == 11043 assert result[0].id == 31044 assert result[0].bar_id == 3...

Full Screen

Full Screen

test_tools.py

Source:test_tools.py Github

copy

Full Screen

...23 assert extract_filters({'filter_foo': 'bar'}) == ({'foo': 'bar'}, {})24 assert extract_filters({'exclude_foo': 'bar'}) == ({}, {'foo': 'bar'})25 assert extract_filters({'filter_foo': ''}) == ({'foo': ''}, {})26 assert extract_filters({'exclude_foo': ''}) == ({}, {'foo': ''})27def test_apply_filters():28 assert apply_filters({}, {}, {}) == {}29 assert apply_filters({'foo': 'bar'}, {}, {}) == {'foo': 'bar'}30 assert apply_filters({'foo': 'bar'}, {'foo': 'bar'}, {}) == {'foo': 'bar'}31 assert apply_filters({'foo': 'bar'}, {}, {'foo': 'bar'}) is None32 assert apply_filters({'foo': 'bar'}, {'baz': 'quux'}, {}) is None33 assert apply_filters({'foo': 'bar'}, {'foo': 'quux'}, {}) is None34 assert apply_filters({'foo': 'bar'}, {}, {'foo': 'quux'}) == {'foo': 'bar'}35 assert apply_filters({'foo': 'bar'}, {}, {'baz': 'quux'}) == {'foo': 'bar'}36def test_apply_filters_with_domains():37 assert apply_filters(38 {'checked-url': 'http://example.com/'}, {'domain': 'example.org'},39 {}) is None40 input = {'checked-url': 'http://example.org/'}41 assert apply_filters(input, {'domain': 'example.org'}, {}) == input42 assert apply_filters(input, {}, {'domain': 'example.org'}) is None43 input = {'checked-url': 'http://example.org/'}44 assert apply_filters(input, {'domain': 'example.com'}, {}) is None45 assert apply_filters(input, {}, {'domain': 'example.com'}) == input46 input = {47 'checked-url': 'http://example.com/',48 'status': '200',49 }50 assert apply_filters(51 input, {'domain': 'example.org', 'status': '200'}, {}) is None52 assert apply_filters(53 input, {'domain': 'example.org'}, {'status': '500'}) is None54 input = {55 'checked-url': 'http://example.org/',56 'status': '200',57 }58 assert apply_filters(59 input, {'domain': 'example.org', 'status': '200'}, {}) == input60 assert apply_filters(61 input, {'domain': 'example.org', 'status': '400'}, {}) is None62 assert apply_filters(63 input, {'domain': 'example.org'}, {'status': '200'}) is None64 input = {65 'checked-url': 'http://example.org/',66 'status': '200',67 'content-type': 'text/html',68 }69 assert apply_filters(70 input, {71 'domain': 'example.org',72 'status': '200',73 'content-type': 'text/html'74 }, {}) == input75 assert apply_filters(76 input,77 {'domain': 'example.org', 'status': '200'},78 {'content-type': 'text/html'}) is None79 assert apply_filters(80 input,81 {'domain': 'example.org', 'status': '500'},82 {'content-type': 'application/csv'}) is None83 assert apply_filters(84 input,85 {'domain': 'example.org'},86 {'content-type': 'application/csv', 'status': '500'}) == input87 assert apply_filters(88 input,89 {'domain': 'example.org', 'status': '200'},90 {'content-type': 'application/csv'}) == input91 assert apply_filters(92 input, {93 'status': '200',94 'content-type': 'text/html'95 }, {'domain': 'example.com'}) == input96 assert apply_filters(97 input,98 {'status': '200'},99 {'domain': 'example.org', 'content-type': 'application/csv'}) is None100 assert apply_filters(101 input,102 {'status': '500'},103 {'domain': 'example.com', 'content-type': 'application/csv'}) is None104 assert apply_filters(105 input,106 {}, {107 'domain': 'example.com',108 'content-type': 'application/csv',109 'status': '500'110 }) == input111 assert apply_filters(112 input,113 {'status': '200'},...

Full Screen

Full Screen

filters.py

Source:filters.py Github

copy

Full Screen

...45 session[key]['user'] = '*'46 if 'paid' in filters:47 session[key]['paid'] = '*'48 session.save()49def apply_filters(request, key, filters = {'date'}):50 if key not in request.session or 'date_from' not in request.session[key] or 'date_to' not in request.session[key]:51 save_default_values(request, key, filters)52 for k in filters:53 if k != 'date' and k not in request.session[key]:54 save_default_values(request, key, filters)55 break56 if request.method == 'POST' and request.POST.get('date_filter_button', '') == 'add_filter':57 apply_filters = {}58 if 'date' in filters:59 date_from = request.POST.get('date_from', '').strip()60 date_to = request.POST.get('date_to', '').strip()61 try:62 datetime.strptime(date_from, date_filter_format())63 datetime.strptime(date_to, date_filter_format())...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run avocado automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful