def test_or_group(self): """Or group should result in or'ed comparisons.""" qs = MockQuerySet() result = apply_filter_to_queryset( qs, OrGroupFilter([MatchFilter('a'), MatchFilter('b')]), 'zz') self.assert_filters_equals([Q(zz='a') | Q(zz='b')], result.filters)
def test_filter_by_tags_and(self): """ Show only contact with correct tags in 'and' configuration. """ ds = ContactsDataSource() recs = ds.run_unaggregated( self.company, ContactsFilter(tags=AndGroupFilter([ OrGroupFilter([MatchFilter('EaSt')]), OrGroupFilter([MatchFilter('wEsT')]) ])), []) names = {r['name'] for r in recs} expected = set() self.assertEqual(expected, names) # Now try adding another tag. self.john.tags.add(self.west_tag) recs = ds.run_unaggregated( self.company, ContactsFilter(tags=AndGroupFilter([ OrGroupFilter([MatchFilter('EaSt')]), OrGroupFilter([MatchFilter('wEsT')]) ])), []) names = {r['name'] for r in recs} expected = {self.john.name} self.assertEqual(expected, names)
def test_filter_by_tags_and(self): """Show only commrec with correct tags in 'and' configuration.""" ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter(tags=AndGroupFilter([ OrGroupFilter([MatchFilter('EaSt')]), OrGroupFilter([MatchFilter('wEsT')]) ])), []) subjects = {r['subject'] for r in recs} expected = set() self.assertEqual(expected, subjects) # Now try adding another tag. self.record_1.tags.add(self.west_tag) self.record_1.save() recs = ds.run_unaggregated( self.company, CommRecordsFilter(tags=AndGroupFilter([ OrGroupFilter([MatchFilter('EaSt')]), OrGroupFilter([MatchFilter('wEsT')]) ])), []) subjects = {r['subject'] for r in recs} expected = {self.record_1.subject} self.assertEqual(expected, subjects)
def test_composite_and(self): """Test adorning a CompositeAndFilter.""" self.maxDiff = 10000 result = adorn_filter( None, self.ds, ContactsFilter( locations=CompositeAndFilter({ 'city': MatchFilter('indy'), 'state': MatchFilter('IN'), }))) self.assertEqual([{ 'locations': { 'city': ['indy'], 'state': ['IN'], }, }], self.ds.adorn_calls) self.assertEqual( ContactsFilter(locations=CompositeAndFilter( { 'city': MatchFilter({ 'value': 'indy', 'display': 'Indy' }), 'state': MatchFilter({ 'value': 'IN', 'display': 'Indiana' }), })), result)
def test_plain_filter_or(self): """Convert OrGroupFilter to plain object.""" filt = ContactsFilter(partner=OrGroupFilter([ MatchFilter(1), MatchFilter(2), ])) self.assertEqual({'partner': [1, 2]}, plain_filter(filt))
def test_tag_filter(self): """Test that tag filter is built properly.""" result = self.driver.build_filter('{"tags": [[1, 2], [3, 4]]}') self.assertEquals( ContactsFilter(tags=AndGroupFilter([ OrGroupFilter([MatchFilter(1), MatchFilter(2)]), OrGroupFilter([MatchFilter(3), MatchFilter(4)]), ])), result)
def test_clone_without_partners(self): locations_filter = CompositeAndFilter({ 'city': MatchFilter('A'), 'state': MatchFilter('B') }) filter = ContactsFilter(partner=[1, 2, 3], locations=locations_filter) expected_without_partners = ContactsFilter(locations=locations_filter) self.assertEqual(expected_without_partners, filter.clone_without_partner())
def test_filter_by_tags_or(self): """Show only partner with correct tags in 'or' configuration.""" ds = PartnersDataSource() recs = ds.run_unaggregated( self.company, PartnersFilter(tags=AndGroupFilter( [OrGroupFilter([MatchFilter('EaSt'), MatchFilter('wEsT')])])), []) names = {r['name'] for r in recs} expected = {self.partner_a.name, self.partner_b.name} self.assertEqual(expected, names)
def test_passthrough_missing(self): """Pass through items not indexed by the data source.""" result = adorn_filter( None, self.ds, ContactsFilter(partner=OrGroupFilter( [MatchFilter(1), MatchFilter(2)]))) self.assertEqual([{ 'partner': [1, 2], }], self.ds.adorn_calls) self.assertEqual( ContactsFilter(partner=OrGroupFilter( [MatchFilter(1), MatchFilter(2)])), result)
def test_plain_filter_composite_and(self): """Convert CompositeAndFilter to plain object.""" filt = ContactsFilter( locations=CompositeAndFilter({ 'city': MatchFilter('indy'), 'state': MatchFilter('IN'), })) self.assertEqual({ 'locations': { 'city': 'indy', 'state': 'IN', }, }, plain_filter(filt))
def test_plain_filter_and(self): """Convert AndGroupFilter to plain object.""" filt = ContactsFilter(tags=AndGroupFilter([ OrGroupFilter([ MatchFilter(1), MatchFilter(2), ]), OrGroupFilter([ MatchFilter(3), MatchFilter(4), ]) ])) self.assertEqual({'tags': [[1, 2], [3, 4]]}, plain_filter(filt))
def test_composite_and_group(self): """CompositeAndFilter should result in and'ed Q.""" qs = MockQuerySet() filt = CompositeAndFilter({ 'a': MatchFilter('b'), 'c': OrGroupFilter([MatchFilter('d'), MatchFilter('e')]) }) result = apply_filter_to_queryset(qs, filt, {'a': 'aaa', 'c': 'ccc'}) self.assert_filters_equals([ Q(aaa='b') & (Q(ccc='d') | Q(ccc='e')), ], result.filters)
def test_filter_by_tags_or(self): """Show only commrec with correct tags in 'or' configuration.""" ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter(tags=AndGroupFilter( [OrGroupFilter([MatchFilter('EaSt'), MatchFilter('wEsT')])])), []) subjects = {r['subject'] for r in recs} expected = { self.record_1.subject, self.record_2.subject, self.record_3.subject, } self.assertEqual(expected, subjects)
def test_match(self): """Test adorning a bare MatchFilter.""" result = adorn_filter( None, self.ds, ContactsFilter(date=MatchFilter('dt'), tags=MatchFilter('a'))) self.assertEqual([{ 'date': ['dt'], 'tags': ['a'], }], self.ds.adorn_calls) self.assertEqual( ContactsFilter(date=MatchFilter('dt'), tags=MatchFilter({ 'value': 'a', 'display': 'A', 'hexcolor': 'aaaaaa' })), result)
def test_city_filter(self): """Test that city filter is built properly.""" result = self.driver.build_filter('{"locations": {"city": "Indy"}}') self.assertEquals( ContactsFilter( locations=CompositeAndFilter({'city': MatchFilter('Indy')})), result)
def test_and_group(self): """And group should result in a series of comparisons.""" qs = MockQuerySet() result = apply_filter_to_queryset( qs, AndGroupFilter([ OrGroupFilter([MatchFilter('f')]), OrGroupFilter([MatchFilter('g'), MatchFilter('h')]), MatchFilter('i') ]), 'z') self.assert_filters_equals([ Q(z='f'), Q(z='g') | Q(z='h'), Q(z='i'), ], result.filters)
def test_filter_by_data_source(self): """Check datasource filter works at all.""" ds = PartnersDataSource() recs = ds.run_unaggregated( self.company, PartnersFilter(data_source=MatchFilter("zap")), []) names = {r['name'] for r in recs} expected = {self.partner_a.name} self.assertEqual(expected, names)
def test_clone_without_full(self): """Cloning should remove certain fields.""" filter = ContactsFilter(tags=['C'], locations=CompositeAndFilter({ 'city': MatchFilter('A'), 'state': MatchFilter('B') })) expected_with_city = ContactsFilter(tags=['C'], locations=CompositeAndFilter( {'city': MatchFilter('A')})) expected_with_state = ContactsFilter(tags=['C'], locations=CompositeAndFilter( {'state': MatchFilter('B')})) self.assertEqual(expected_with_state, filter.clone_without_city()) self.assertEqual(expected_with_city, filter.clone_without_state())
def test_filter_by_uri(self): """Check uri filter works at all.""" ds = PartnersDataSource() recs = ds.run_unaggregated( self.company, PartnersFilter(uri=MatchFilter("http://www.asdf.com/")), []) names = {r['name'] for r in recs} expected = {self.partner_b.name} self.assertEqual(expected, names)
def test_help_city(self): """Check city help works and ignores current city filter.""" ds = ContactsDataSource() recs = ds.help_city( self.company, ContactsFilter(locations=CompositeAndFilter( {'city': MatchFilter('Los Angeles')})), "angel") actual = {r['value'] for r in recs} self.assertEqual({'Los Angeles'}, actual)
def test_help_state(self): """Check state help works and ignores current state filter.""" ds = ContactsDataSource() recs = ds.help_state( self.company, ContactsFilter( locations=CompositeAndFilter({'state': MatchFilter('zz')})), "i") actual = {r['value'] for r in recs} self.assertEqual({'IL', 'IN'}, actual)
def test_filter_by_partners(self): """Should filter by partners.""" ds = ContactsDataSource() recs = ds.run_unaggregated( self.company, ContactsFilter( partner=OrGroupFilter([MatchFilter(self.partner_a.pk)])), []) subjects = {r['name'] for r in recs} expected = {self.john.name} self.assertEqual(expected, subjects)
def test_filter_by_city(self): """Should show only contacts with correct city.""" ds = ContactsDataSource() recs = ds.run_unaggregated( self.company, ContactsFilter(locations=CompositeAndFilter( {'city': MatchFilter('Los Angeles')})), []) names = [r['name'] for r in recs] expected = [self.sue.name] self.assertEqual(expected, names)
def test_filter_by_communication_type(self): """Check communication_type filter works at all.""" ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter( communication_type=OrGroupFilter([MatchFilter('email')])), []) subjects = {r['subject'] for r in recs} expected = {self.record_1.subject} self.assertEqual(expected, subjects)
def test_filter_by_city(self): """Should show only commrecs with correct city.""" ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter(locations=CompositeAndFilter( {'city': MatchFilter('Los Angeles')})), []) subjects = {r['subject'] for r in recs} expected = {self.record_3.subject} self.assertEqual(expected, subjects)
def test_composite_and_group_missing_db_field(self): """ Failing to pass a db field to a compsite and field should result in no comparison for that field. """ qs = MockQuerySet() filt = CompositeAndFilter({'a': MatchFilter('b'), 'c': NoFilter()}) result = apply_filter_to_queryset(qs, filt, {'a': 'aaa', 'b': 'bbb'}) self.assert_filters_equals([Q(aaa='b')], result.filters) result = apply_filter_to_queryset(qs, filt, {'b', 'bbb'}) self.assert_filters_equals([], result.filters)
def test_filter_by_state(self): """Should show only partners with correct state.""" ds = PartnersDataSource() recs = ds.run_unaggregated( self.company, PartnersFilter( locations=CompositeAndFilter({ 'state': MatchFilter('CA'), })), []) names = [r['name'] for r in recs] expected = [self.partner_b.name] self.assertEqual(expected, names)
def test_or_group(self): """Test adorning an OrGroupFilter.""" self.maxDiff = 10000 result = adorn_filter( None, self.ds, ContactsFilter(tags=OrGroupFilter([ MatchFilter('a'), MatchFilter('b'), MatchFilter('c'), ]))) self.assertEqual([{ 'tags': ['a', 'b', 'c'], }], self.ds.adorn_calls) self.assertEqual( ContactsFilter(tags=OrGroupFilter([ MatchFilter({ 'value': 'a', 'display': 'A', 'hexcolor': 'aaaaaa' }), MatchFilter({ 'value': 'b', 'display': 'B', 'hexcolor': 'bbbbbb' }), MatchFilter('c'), ])), result)
def test_composite_and_missing_in_db(self): """ Pass through branches of a CompositeAndFilter not indexed by the data source. """ self.maxDiff = 10000 result = adorn_filter( None, self.ds, ContactsFilter( locations=CompositeAndFilter({ 'city': MatchFilter('indy'), 'state': MatchFilter('IN'), 'county': MatchFilter('marion'), }))) self.assertEqual([{ 'locations': { 'city': ['indy'], 'state': ['IN'], 'county': ['marion'], }, }], self.ds.adorn_calls) self.assertEqual( ContactsFilter(locations=CompositeAndFilter( { 'city': MatchFilter({ 'value': 'indy', 'display': 'Indy' }), 'state': MatchFilter({ 'value': 'IN', 'display': 'Indiana' }), 'county': MatchFilter('marion'), })), result)
def test_filter_by_partner_tags(self): """ Test that we can filter by partner tags. """ ds = ContactsDataSource() recs = ds.run_unaggregated( self.company, ContactsFilter(partner_tags=AndGroupFilter( [OrGroupFilter([MatchFilter('rigHt')])])), []) subjects = {r['name'] for r in recs} expected = {self.sue.name} self.assertEqual(expected, subjects)