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_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(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_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_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_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_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_run(self):
     """Test that json run calls are plumbed to datasource correctly."""
     result = self.driver.run('unaggregated', 'company', '{}', '["zzz"]')
     self.assertEqual('aaa', result)
     self.assertEqual(
         [['run', 'unaggregated', 'company',
           ContactsFilter(), ['zzz']]], self.ds.calls)
 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_date_filters(self):
     """Test that date filters are built properly."""
     spec = '{"date": ["09/01/2015", "09/30/2015"]}'
     result = self.driver.build_filter(spec)
     expected = ContactsFilter(date=DateRangeFilter(
         [datetime(2015, 9, 1), datetime(2015, 9, 30)]))
     self.assertEquals(expected, 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_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_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_plain_filter_unlinked(self):
     """Convert unlinked filter to plain object."""
     filt = ContactsFilter(tags=UnlinkedFilter())
     self.assertEqual({
         'tags': {
             'nolink': True
         },
     }, plain_filter(filt))
 def test_empty_list_filters(self):
     """
     Test that an empty list is interpreted as no filter.
     """
     spec = '{"partner": [], "partner_tags": []}'
     result = self.driver.build_filter(spec)
     expected = ContactsFilter()
     self.assertEquals(expected, result)
 def test_help_partner(self):
     """Check partner help works at all."""
     ds = ContactsDataSource()
     recs = ds.help_partner(self.company, ContactsFilter(), "A")
     self.assertEqual([{
         'value': self.partner_a.pk,
         'display': 'aaa'
     }], recs)
 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_unlinked_filters(self):
     """
     Test that we can build an unlinked filter
     """
     spec = '{"partner": {"nolink": true}}'
     result = self.driver.build_filter(spec)
     expected = ContactsFilter(partner=UnlinkedFilter())
     self.assertEquals(expected, result)
    def test_help_partner_tags(self):
        """
        Check partner tags help works at all.

        """
        ds = ContactsDataSource()
        recs = ds.help_partner_tags(self.company, ContactsFilter(), "t")
        actual = {r['value'] for r in recs}
        self.assertEqual({'left', 'right'}, actual)
 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__plain_filter_date_range(self):
     """Convert date range to plain object."""
     filt = ContactsFilter(date=DateRangeFilter(
         [datetime(2015, 9, 1), datetime(2015, 9, 30)]))
     self.assertEqual(
         {
             'date': [datetime(2015, 9, 1),
                      datetime(2015, 9, 30)],
         }, plain_filter(filt))
 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_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_run_unfiltered(self):
        """
        Make sure we only get data for this user.

        """
        ds = ContactsDataSource()
        recs = ds.run_unaggregated(self.company, ContactsFilter(), [])
        names = {r['name'] for r in recs}
        expected = {self.sue.name, self.john.name}
        self.assertEqual(expected, names)
 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_default_filter(self):
     """should produce a populated filter object."""
     ds = ContactsDataSource()
     default_filter = ds.get_default_filter(None, self.company)
     self.assertEquals(datetime.now().year,
                       default_filter.date.dates[1].year)
     # Take out value dated today. Too hard to run through assertEquals.
     default_filter.date.dates[1] = None
     expected = ContactsFilter(
         date=DateRangeFilter([datetime(2014, 1, 1), None]))
     self.assertEquals(expected, default_filter)
    def test_filter_by_partner_tags_untagged(self):
        """
        Check that we can find a record attached to an untagged partner.

        """
        self.partner_b.tags.clear()
        ds = ContactsDataSource()
        recs = ds.run_unaggregated(
            self.company, ContactsFilter(partner_tags=UnlinkedFilter()), [])
        subjects = {r['name'] for r in recs}
        expected = {self.sue.name}
        self.assertEqual(expected, subjects)
    def test_filter_untagged(self):
        """
        This indicates that the member selected to filter by untagged.

        """
        self.sue.tags.clear()
        ds = ContactsDataSource()
        recs = ds.run_unaggregated(self.company,
                                   ContactsFilter(tags=UnlinkedFilter()), [])
        names = {r['name'] for r in recs}
        expected = {self.sue.name}
        self.assertEqual(expected, names)
    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)
    def test_filter_by_date_after(self):
        """
        Should show only contact with last_action_time after date.

        """
        ds = ContactsDataSource()
        recs = ds.run_unaggregated(
            self.company,
            ContactsFilter(
                date=DateRangeFilter([datetime(2015, 10, 1), None])), [])
        names = {r['name'] for r in recs}
        expected = {self.john.name}
        self.assertEqual(expected, names)
    def test_filter_by_date_before(self):
        """
        Should show only contact with last_action_time before date.

        """
        ds = ContactsDataSource()
        recs = ds.run_unaggregated(
            self.company,
            ContactsFilter(
                date=DateRangeFilter([None, datetime(2015, 9, 30)])), [])
        names = {r['name'] for r in recs}
        expected = {self.sue.name}
        self.assertEqual(expected, names)
 def test_passthrough_missing_composite_and(self):
     """
     Pass through  when all branches of a CompositeAndFilter are not indexed
     by the data source.
     """
     result = adorn_filter(
         None, self.ds,
         ContactsFilter(partner=CompositeAndFilter({
             'a': MatchFilter(1),
             'b': MatchFilter(2)
         })))
     self.assertEqual([{
         'partner': {
             'a': [1],
             'b': [2]
         },
     }], self.ds.adorn_calls)
     self.assertEqual(
         ContactsFilter(partner=CompositeAndFilter({
             'a': MatchFilter(1),
             'b': MatchFilter(2)
         })), result)
 def test_clone_without_empty(self):
     """Cloning empty filters shouldn't crash."""
     filter = ContactsFilter()
     self.assertEqual(ContactsFilter(), filter.clone_without_city())
     self.assertEqual(ContactsFilter(), filter.clone_without_state())