

def test_column_filters():
    (app, db, admin) = setup()
    (Model1, Model2) = create_models(db)
    view = CustomModelView(Model1, db.session, column_filters=['test1'])
    admin.add_view(view)
    client = app.test_client()
    eq_(len(view._filters), 7)
    eq_([(f['index'], f['operation']) for f in view._filter_groups['Test1']], [(0, 'contains'), (1, 'not contains'), (2, 'equals'), (3, 'not equal'), (4, 'empty'), (5, 'in list'), (6, 'not in list')])
    view = CustomModelView(Model2, db.session, column_filters=['model1'])
    eq_([(f['index'], f['operation']) for f in view._filter_groups['Model1 / Test1']], [(0, 'contains'), (1, 'not contains'), (2, 'equals'), (3, 'not equal'), (4, 'empty'), (5, 'in list'), (6, 'not in list')])
    eq_([(f['index'], f['operation']) for f in view._filter_groups['Model1 / Test2']], [(7, 'contains'), (8, 'not contains'), (9, 'equals'), (10, 'not equal'), (11, 'empty'), (12, 'in list'), (13, 'not in list')])
    eq_([(f['index'], f['operation']) for f in view._filter_groups['Model1 / Test3']], [(14, 'contains'), (15, 'not contains'), (16, 'equals'), (17, 'not equal'), (18, 'empty'), (19, 'in list'), (20, 'not in list')])
    eq_([(f['index'], f['operation']) for f in view._filter_groups['Model1 / Test4']], [(21, 'contains'), (22, 'not contains'), (23, 'equals'), (24, 'not equal'), (25, 'empty'), (26, 'in list'), (27, 'not in list')])
    eq_([(f['index'], f['operation']) for f in view._filter_groups['Model1 / Bool Field']], [(28, 'equals'), (29, 'not equal')])
    eq_([(f['index'], f['operation']) for f in view._filter_groups['Model1 / Enum Field']], [(30, 'equals'), (31, 'not equal'), (32, 'empty'), (33, 'in list'), (34, 'not in list')])
    view = CustomModelView(Model2, db.session, column_filters=['model1.bool_field'])
    eq_([(f['index'], f['operation']) for f in view._filter_groups['Model1 / Bool Field']], [(0, 'equals'), (1, 'not equal')])
    view = CustomModelView(Model2, db.session, column_filters=['model1.bool_field', 'string_field'], column_labels={
        'model1.bool_field': 'Test Filter #1',
        'string_field': 'Test Filter #2',
    })
    eq_(list(view._filter_groups.keys()), ['Test Filter #1', 'Test Filter #2'])
    fill_db(db, Model1, Model2)
    rv = client.get('/admin/model1/?flt0_0=test1_val_1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' in data))
    ok_(('test1_val_2' not in data))
    rv = client.get('/admin/model1/?flt0_6=test1_val_1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test1_val_2' in data))
    ok_(('test2_val_1' not in data))
    view = CustomModelView(Model1, db.session, column_filters=['test1'], endpoint='_strings')
    admin.add_view(view)
    eq_([(f['index'], f['operation']) for f in view._filter_groups['Test1']], [(0, 'contains'), (1, 'not contains'), (2, 'equals'), (3, 'not equal'), (4, 'empty'), (5, 'in list'), (6, 'not in list')])
    rv = client.get('/admin/_strings/?flt0_0=test1_val_1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' in data))
    ok_(('test1_val_2' not in data))
    rv = client.get('/admin/_strings/?flt0_1=test1_val_1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' not in data))
    ok_(('test1_val_2' in data))
    rv = client.get('/admin/_strings/?flt0_2=test1_val_1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' in data))
    ok_(('test1_val_2' not in data))
    rv = client.get('/admin/_strings/?flt0_3=test1_val_1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' not in data))
    ok_(('test1_val_2' in data))
    rv = client.get('/admin/_strings/?flt0_4=1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('empty_obj' in data))
    ok_(('test1_val_1' not in data))
    ok_(('test1_val_2' not in data))
    rv = client.get('/admin/_strings/?flt0_4=0')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('empty_obj' not in data))
    ok_(('test1_val_1' in data))
    ok_(('test1_val_2' in data))
    rv = client.get('/admin/_strings/?flt0_5=test1_val_1%2Ctest1_val_2')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' in data))
    ok_(('test2_val_2' in data))
    ok_(('test1_val_3' not in data))
    ok_(('test1_val_4' not in data))
    rv = client.get('/admin/_strings/?flt0_6=test1_val_1%2Ctest1_val_2')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' not in data))
    ok_(('test2_val_2' not in data))
    ok_(('test1_val_3' in data))
    ok_(('test1_val_4' in data))
    view = CustomModelView(Model2, db.session, column_filters=['int_field'])
    admin.add_view(view)
    eq_([(f['index'], f['operation']) for f in view._filter_groups['Int Field']], [(0, 'equals'), (1, 'not equal'), (2, 'greater than'), (3, 'smaller than'), (4, 'empty'), (5, 'in list'), (6, 'not in list')])
    rv = client.get('/admin/model2/?flt0_0=5000')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_3' in data))
    ok_(('test2_val_4' not in data))
    rv = client.get('/admin/model2/?flt0_0=6169453081680413441')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_5' in data))
    ok_(('test2_val_4' not in data))
    rv = client.get('/admin/model2/?flt0_0=badval')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('Invalid Filter Value' in data))
    rv = client.get('/admin/model2/?flt0_1=5000')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_3' not in data))
    ok_(('test2_val_4' in data))
    rv = client.get('/admin/model2/?flt0_2=6000')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_3' not in data))
    ok_(('test2_val_4' in data))
    rv = client.get('/admin/model2/?flt0_3=6000')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_3' in data))
    ok_(('test2_val_4' not in data))
    rv = client.get('/admin/model2/?flt0_4=1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' in data))
    ok_(('test2_val_2' in data))
    ok_(('test2_val_3' not in data))
    ok_(('test2_val_4' not in data))
    rv = client.get('/admin/model2/?flt0_4=0')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' not in data))
    ok_(('test2_val_2' not in data))
    ok_(('test2_val_3' in data))
    ok_(('test2_val_4' in data))
    rv = client.get('/admin/model2/?flt0_5=5000%2C9000')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' not in data))
    ok_(('test2_val_2' not in data))
    ok_(('test2_val_3' in data))
    ok_(('test2_val_4' in data))
    rv = client.get('/admin/model2/?flt0_5=6169453081680413441')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' not in data))
    ok_(('test2_val_5' in data))
    rv = client.get('/admin/model2/?flt0_5=5000%2Cbadval')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('Invalid Filter Value' in data))
    rv = client.get('/admin/model2/?flt0_6=5000%2C9000')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' in data))
    ok_(('test2_val_2' in data))
    ok_(('test2_val_3' not in data))
    ok_(('test2_val_4' not in data))
    view = CustomModelView(Model1, db.session, column_filters=['bool_field'], endpoint='_bools')
    admin.add_view(view)
    eq_([(f['index'], f['operation']) for f in view._filter_groups['Bool Field']], [(0, 'equals'), (1, 'not equal')])
    rv = client.get('/admin/_bools/?flt0_0=1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' in data))
    ok_(('test2_val_2' not in data))
    ok_(('test2_val_3' not in data))
    rv = client.get('/admin/_bools/?flt0_0=0')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' not in data))
    ok_(('test2_val_2' in data))
    ok_(('test2_val_3' in data))
    rv = client.get('/admin/_bools/?flt0_1=1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' not in data))
    ok_(('test2_val_2' in data))
    ok_(('test2_val_3' in data))
    rv = client.get('/admin/_bools/?flt0_1=0')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' in data))
    ok_(('test2_val_2' not in data))
    ok_(('test2_val_3' not in data))
    view = CustomModelView(Model2, db.session, column_filters=['float_field'], endpoint='_float')
    admin.add_view(view)
    eq_([(f['index'], f['operation']) for f in view._filter_groups['Float Field']], [(0, 'equals'), (1, 'not equal'), (2, 'greater than'), (3, 'smaller than'), (4, 'empty'), (5, 'in list'), (6, 'not in list')])
    rv = client.get('/admin/_float/?flt0_0=25.9')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_3' in data))
    ok_(('test2_val_4' not in data))
    rv = client.get('/admin/_float/?flt0_0=badval')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('Invalid Filter Value' in data))
    rv = client.get('/admin/_float/?flt0_1=25.9')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_3' not in data))
    ok_(('test2_val_4' in data))
    rv = client.get('/admin/_float/?flt0_2=60.5')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_3' not in data))
    ok_(('test2_val_4' in data))
    rv = client.get('/admin/_float/?flt0_3=60.5')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_3' in data))
    ok_(('test2_val_4' not in data))
    rv = client.get('/admin/_float/?flt0_4=1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' in data))
    ok_(('test2_val_2' in data))
    ok_(('test2_val_3' not in data))
    ok_(('test2_val_4' not in data))
    rv = client.get('/admin/_float/?flt0_4=0')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' not in data))
    ok_(('test2_val_2' not in data))
    ok_(('test2_val_3' in data))
    ok_(('test2_val_4' in data))
    rv = client.get('/admin/_float/?flt0_5=25.9%2C75.5')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' not in data))
    ok_(('test2_val_2' not in data))
    ok_(('test2_val_3' in data))
    ok_(('test2_val_4' in data))
    rv = client.get('/admin/_float/?flt0_5=25.9%2Cbadval')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('Invalid Filter Value' in data))
    rv = client.get('/admin/_float/?flt0_6=25.9%2C75.5')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' in data))
    ok_(('test2_val_2' in data))
    ok_(('test2_val_3' not in data))
    ok_(('test2_val_4' not in data))
    view = CustomModelView(Model2, db.session, endpoint='_model2', column_filters=['model1.bool_field'], column_list=['string_field', 'model1.id', 'model1.bool_field'])
    admin.add_view(view)
    rv = client.get('/admin/_model2/?flt1_0=1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' in data))
    ok_(('test2_val_2' not in data))
    ok_(('test2_val_3' not in data))
    ok_(('test2_val_4' not in data))
    view = CustomModelView(Model1, db.session, column_filters=['test1'], endpoint='_model3', named_filter_urls=True)
    admin.add_view(view)
    rv = client.get('/admin/_model3/?flt1_test1_equals=test1_val_1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test1_val_1' in data))
    ok_(('test1_val_2' not in data))
    view = CustomModelView(Model1, db.session, column_filters=['date_field', 'datetime_field', 'time_field'], endpoint='_datetime')
    admin.add_view(view)
    eq_([(f['index'], f['operation']) for f in view._filter_groups['Date Field']], [(0, 'equals'), (1, 'not equal'), (2, 'greater than'), (3, 'smaller than'), (4, 'between'), (5, 'not between'), (6, 'empty')])
    eq_([(f['index'], f['operation']) for f in view._filter_groups['Datetime Field']], [(7, 'equals'), (8, 'not equal'), (9, 'greater than'), (10, 'smaller than'), (11, 'between'), (12, 'not between'), (13, 'empty')])
    eq_([(f['index'], f['operation']) for f in view._filter_groups['Time Field']], [(14, 'equals'), (15, 'not equal'), (16, 'greater than'), (17, 'smaller than'), (18, 'between'), (19, 'not between'), (20, 'empty')])
    rv = client.get('/admin/_datetime/?flt0_0=2014-11-17')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('date_obj1' in data))
    ok_(('date_obj2' not in data))
    rv = client.get('/admin/_datetime/?flt0_1=2014-11-17')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('date_obj1' not in data))
    ok_(('date_obj2' in data))
    rv = client.get('/admin/_datetime/?flt0_2=2014-11-16')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('date_obj1' in data))
    ok_(('date_obj2' not in data))
    rv = client.get('/admin/_datetime/?flt0_3=2014-11-16')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('date_obj1' not in data))
    ok_(('date_obj2' in data))
    rv = client.get('/admin/_datetime/?flt0_4=2014-11-13+to+2014-11-20')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('date_obj1' in data))
    ok_(('date_obj2' not in data))
    rv = client.get('/admin/_datetime/?flt0_5=2014-11-13+to+2014-11-20')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('date_obj1' not in data))
    ok_(('date_obj2' in data))
    rv = client.get('/admin/_datetime/?flt0_6=1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test1_val_1' in data))
    ok_(('date_obj1' not in data))
    ok_(('date_obj2' not in data))
    rv = client.get('/admin/_datetime/?flt0_6=0')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test1_val_1' not in data))
    ok_(('date_obj1' in data))
    ok_(('date_obj2' in data))
    rv = client.get('/admin/_datetime/?flt0_7=2014-04-03+01%3A09%3A00')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('datetime_obj1' in data))
    ok_(('datetime_obj2' not in data))
    rv = client.get('/admin/_datetime/?flt0_8=2014-04-03+01%3A09%3A00')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('datetime_obj1' not in data))
    ok_(('datetime_obj2' in data))
    rv = client.get('/admin/_datetime/?flt0_9=2014-04-03+01%3A08%3A00')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('datetime_obj1' in data))
    ok_(('datetime_obj2' not in data))
    rv = client.get('/admin/_datetime/?flt0_10=2014-04-03+01%3A08%3A00')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('datetime_obj1' not in data))
    ok_(('datetime_obj2' in data))
    rv = client.get('/admin/_datetime/?flt0_11=2014-04-02+00%3A00%3A00+to+2014-11-20+23%3A59%3A59')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('datetime_obj1' in data))
    ok_(('datetime_obj2' not in data))
    rv = client.get('/admin/_datetime/?flt0_12=2014-04-02+00%3A00%3A00+to+2014-11-20+23%3A59%3A59')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('datetime_obj1' not in data))
    ok_(('datetime_obj2' in data))
    rv = client.get('/admin/_datetime/?flt0_13=1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test1_val_1' in data))
    ok_(('datetime_obj1' not in data))
    ok_(('datetime_obj2' not in data))
    rv = client.get('/admin/_datetime/?flt0_13=0')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test1_val_1' not in data))
    ok_(('datetime_obj1' in data))
    ok_(('datetime_obj2' in data))
    rv = client.get('/admin/_datetime/?flt0_14=11%3A10%3A09')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('timeonly_obj1' in data))
    ok_(('timeonly_obj2' not in data))
    rv = client.get('/admin/_datetime/?flt0_15=11%3A10%3A09')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('timeonly_obj1' not in data))
    ok_(('timeonly_obj2' in data))
    rv = client.get('/admin/_datetime/?flt0_16=11%3A09%3A09')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('timeonly_obj1' in data))
    ok_(('timeonly_obj2' not in data))
    rv = client.get('/admin/_datetime/?flt0_17=11%3A09%3A09')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('timeonly_obj1' not in data))
    ok_(('timeonly_obj2' in data))
    rv = client.get('/admin/_datetime/?flt0_18=10%3A40%3A00+to+11%3A50%3A59')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('timeonly_obj1' in data))
    ok_(('timeonly_obj2' not in data))
    rv = client.get('/admin/_datetime/?flt0_19=10%3A40%3A00+to+11%3A50%3A59')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('timeonly_obj1' not in data))
    ok_(('timeonly_obj2' in data))
    rv = client.get('/admin/_datetime/?flt0_20=1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test1_val_1' in data))
    ok_(('timeonly_obj1' not in data))
    ok_(('timeonly_obj2' not in data))
    rv = client.get('/admin/_datetime/?flt0_20=0')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test1_val_1' not in data))
    ok_(('timeonly_obj1' in data))
    ok_(('timeonly_obj2' in data))
    view = CustomModelView(Model1, db.session, column_filters=['enum_field'], endpoint='_enumfield')
    admin.add_view(view)
    rv = client.get('/admin/_enumfield/?flt0_0=model1_v1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('enum_obj1' in data))
    ok_(('enum_obj2' not in data))
    rv = client.get('/admin/_enumfield/?flt0_1=model1_v1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('enum_obj1' not in data))
    ok_(('enum_obj2' in data))
    rv = client.get('/admin/_enumfield/?flt0_2=1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test1_val_1' in data))
    ok_(('enum_obj1' not in data))
    ok_(('enum_obj2' not in data))
    rv = client.get('/admin/_enumfield/?flt0_2=0')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test1_val_1' not in data))
    ok_(('enum_obj1' in data))
    ok_(('enum_obj2' in data))
    rv = client.get('/admin/_enumfield/?flt0_3=model1_v1%2Cmodel1_v2')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test1_val_1' not in data))
    ok_(('enum_obj1' in data))
    ok_(('enum_obj2' in data))
    rv = client.get('/admin/_enumfield/?flt0_4=model1_v1%2Cmodel1_v2')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test1_val_1' in data))
    ok_(('enum_obj1' not in data))
    ok_(('enum_obj2' not in data))
    view = CustomModelView(Model2, db.session, column_filters=[filters.FilterEqual(Model1.test1, 'Test1')], endpoint='_relation_test')
    admin.add_view(view)
    rv = client.get('/admin/_relation_test/?flt1_0=test1_val_1')
    data = rv.data.decode('utf-8')
    ok_(('test1_val_1' in data))
    ok_(('test1_val_2' not in data))

dataset/ETHPy150Open flask-admin/flask-admin/flask_admin/tests/sqla/test_basic.py test_column_filters/original