def setUp(self): super(ContactRecordFormTests, self).setUp() self.contact_record = ContactRecordFactory(contact=self.contact, partner=self.partner) # The contact record form has a lot of required fields. self.data = { 'contact_type': self.contact_record.contact_type, 'contact': self.contact_record.contact.pk, 'contact_email': self.contact_record.contact_email, 'length_0': "00", 'length_1': "30", 'date_time_0': "Aug", 'date_time_1': "24", 'date_time_2': "2015", 'date_time_3': "03", 'date_time_4': "10", 'date_time_5': "PM", 'notes': self.contact_record.notes, 'partner': self.contact_record.partner.pk, 'company': self.contact_record.partner.owner } self.form = ContactRecordForm(instance=self.contact_record, partner=self.partner, data=self.data) # add a few contact records so that we have some to select from ContactRecordFactory.create_batch(3, partner=self.partner, contact=self.contact, contact_type='phone') self.client.path = reverse( "partner_edit_record") + "?partner=%s" % self.partner.pk
def test_filtering_on_foreign_key(self): """Test the ability to filter on a model's foreign key fields.""" PartnerFactory.create_batch(5, name='Test Partner', owner=self.company) ContactRecordFactory.create_batch(5, partner=self.partner, contact__name='Jane Doe') self.client.path += '/partner' filters = json.dumps({ 'name': { 'icontains': 'Test Partner', }, 'contactrecord': { 'contact': { 'name': { 'icontains': 'Jane Doe' } } } }) response = self.client.post(data={'filters': filters}) output = json.loads(response.content) self.assertEqual(response.status_code, 200) # We look for distinct records self.assertEqual(len(output), 1)
def test_contact_record_report_numbers(self): """ Contact records have properties which represent various aggregated values. This test ensures that given a number of contact records, those aggregated numbers are correct. """ email_record = ContactRecordFactory(contact_type="email", partner=self.partner, contact=self.contact) job_record = ContactRecordFactory(contact_type="job", partner=self.partner, contact=self.contact, job_applications=10, job_interviews=6, job_hires=5) phone_record = ContactRecordFactory(contact_type="phone", partner=self.partner, contact=ContactFactory(name="Joe")) records = ContactRecord.objects.all() self.assertEqual(len(records), 3) self.assertEqual(len(records.contacts), 2) # job follow ups don't count as comm activity self.assertEqual(len(records.communication_activity), 2) # only job follow ups count as referrals self.assertEqual(records.referrals, 1) self.assertEqual(records.applications, 10) self.assertEqual(records.interviews, 6) self.assertEqual(records.hires, 5) self.assertEqual(records.emails, 1) self.assertEqual(records.calls, 1)
def setUp(self): super(TestRegenerate, self).setUp() self.client = TestClient(path=reverse('reports', kwargs={ 'app': 'mypartners', 'model': 'contactrecord'})) self.client.login_user(self.user) ContactRecordFactory.create_batch(10, partner__owner=self.company)
def setUp(self): super(TestDownloads, self).setUp() self.client = TestClient(path=reverse('downloads'), HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.client.login_user(self.user) ContactRecordFactory.create_batch(10, partner__owner=self.company)
def setUp(self): super(ContactRecordFormTests, self).setUp() self.contact_record = ContactRecordFactory( contact=self.contact, partner=self.partner) # The contact record form has a lot of required fields. self.data = { 'contact_type': self.contact_record.contact_type, 'contact': self.contact_record.contact.pk, 'contact_email': self.contact_record.contact_email, 'length_0': "00", 'length_1': "30", 'date_time_0': "Aug", 'date_time_1': "24", 'date_time_2': "2015", 'date_time_3': "03", 'date_time_4': "10", 'date_time_5': "PM", 'notes': self.contact_record.notes, 'partner': self.contact_record.partner.pk, 'company': self.contact_record.partner.owner } self.form = ContactRecordForm( instance=self.contact_record, partner=self.partner, data=self.data) # add a few contact records so that we have some to select from ContactRecordFactory.create_batch(3, partner=self.partner, contact=self.contact, contact_type='phone') self.client.path = reverse( "partner_edit_record") + "?partner=%s" % self.partner.pk
def test_filtering_on_foreign_key(self): """Test the ability to filter on a model's foreign key fields.""" PartnerFactory.create_batch(5, name='Test Partner', owner=self.company) ContactRecordFactory.create_batch( 5, partner=self.partner, contact__name='Jane Doe') self.client.path += '/partner' filters = json.dumps({ 'name': { 'icontains': 'Test Partner', }, 'contactrecord': { 'contact': { 'name': { 'icontains': 'Jane Doe' } } } }) response = self.client.post(data={'filters': filters}) output = json.loads(response.content) self.assertEqual(response.status_code, 200) # We look for distinct records self.assertEqual(len(output), 1)
def setUp(self): super(TestViewRecords, self).setUp() self.client = TestClient(path='/reports/ajax/mypartners', HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.client.login_user(self.user) ContactRecordFactory.create_batch( 10, partner=self.partner, contact__name='Joe Shmoe')
def setUp(self): super(TestViewRecords, self).setUp() self.client = TestClient(path='/reports/ajax/mypartners', HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.client.login_user(self.user) ContactRecordFactory.create_batch(10, partner=self.partner, contact__name='Joe Shmoe')
def setUp(self): super(TestHelpers, self).setUp() tags = [TagFactory(name=name, company=self.company) for name in ["test", "stuff", "working"]] # Returns a list rather than a QuerySet, which is what the helper # functions use, so saving this to a variable isn't really helpful ContactRecordFactory.create_batch(10, partner=self.partner, contact__name="Joe Shmoe", tags=tags) self.records = ContactRecord.objects.all()
def setUp(self): super(TestRegenerate, self).setUp() self.client = TestClient(path=reverse('reports', kwargs={ 'app': 'mypartners', 'model': 'contactrecord' })) self.client.login_user(self.user) ContactRecordFactory.create_batch(10, partner__owner=self.company)
def test_json_output(self): """Test that filtering contact records through ajax works properly.""" # records to be filtered out ContactRecordFactory.create_batch(10, contact__name="John Doe") self.client.path += "/contactrecord" response = self.client.get(data={"contact__name": "Joe Shmoe"}) output = json.loads(response.content) self.assertEqual(response.status_code, 200) self.assertEqual(len(output), 10)
def test_only_user_results_returned(self): """Results should only contain records user has access to.""" # records not owned by user partner = PartnerFactory(name="Wrong Partner") ContactRecordFactory.create_batch(10, partner=partner) self.client.path += '/contactrecord' response = self.client.post() output = json.loads(response.content) self.assertEqual(response.status_code, 200) self.assertEqual(len(output), 10)
def test_json_output(self): """Test that filtering contact records through ajax works properly.""" # records to be filtered out ContactRecordFactory.create_batch(10, contact__name='John Doe') self.client.path += '/contactrecord' filters = json.dumps({'contact': {'name': {'icontains': 'Joe Shmoe'}}}) response = self.client.post(data={'filters': filters}) output = json.loads(response.content) self.assertEqual(response.status_code, 200) self.assertEqual(len(output), 10)
def setUp(self): super(TestHelpers, self).setUp() tags = [ TagFactory(name=name, company=self.company) for name in ['test', 'stuff', 'working'] ] # Returns a list rather than a QuerySet, which is what the helper # functions use, so saving this to a variable isn't really helpful ContactRecordFactory.create_batch(10, partner=self.partner, contact__name='Joe Shmoe', tags=tags) self.records = ContactRecord.objects.all()
def test_dynamic_partners_report_comm_per_month(self): """Run the comm_rec per month per partner report.""" self.client.login_user(self.user) partner = PartnerFactory(owner=self.company) partner.tags.add(TagFactory.create(name="this")) contact = ContactFactory.create(name='somename', partner=partner) for i in range(0, 20): # unicode here to push through report generation/download ContactRecordFactory(partner=partner, contact=contact, date_time=datetime(2015, 2, 4), subject=u"subject-%s \u2019" % i) report_data = self.find_report_data( 'partners', data_type="count_comm_rec_per_month") resp = self.client.post(reverse('run_dynamic_report'), data={ 'report_data_id': report_data.pk, 'name': 'The Report', 'filter': json.dumps({ 'tags': [['this']], }), }) self.assertEqual(200, resp.status_code) report_id = json.loads(resp.content)['id'] resp = self.client.get(reverse('list_dynamic_reports')) self.assertEqual(200, resp.status_code) self.assertEqual( { 'reports': [ { 'id': report_id, 'name': 'The Report', 'report_type': 'partners' }, ] }, json.loads(resp.content)) report_presentation = self.find_report_presentation( report_data, 'json_pass') data = { 'id': report_id, 'report_presentation_id': report_presentation.pk, } resp = self.client.get(reverse('download_dynamic_report'), data) self.assertEquals(200, resp.status_code) response_data = json.loads(resp.content) self.assertEquals(12, len(response_data['records'])) january = response_data['records'][0] self.assertEqual('1', january['Month']) self.assertEqual('0', january['Communication Record Count']) february = response_data['records'][1] self.assertEqual('2', february['Month']) self.assertEqual('20', february['Communication Record Count'])
def test_filtering_on_foreign_key(self): """Test the ability to filter on a model's foreign key fields.""" PartnerFactory.create_batch(5, name="Test Partner", owner=self.company) ContactRecordFactory.create_batch(5, partner=self.partner, contact__name="Jane Doe") self.client.path += "/partner" filters = json.dumps( {"name": {"icontains": "Test Partner"}, "contactrecord": {"contact": {"name": {"icontains": "Jane Doe"}}}} ) response = self.client.post(data={"filters": filters}) output = json.loads(response.content) self.assertEqual(response.status_code, 200) # We look for distinct records self.assertEqual(len(output), 1)
def test_models_approved(self): """ By default, new partners, contacts, and contactrecords should be approved. """ contactrecord = ContactRecordFactory(partner=self.partner) for instance in (self.contact, self.partner, contactrecord): self.assertEqual(instance.approval_status.code, Status.APPROVED)
def test_json_output(self): """Test that filtering contact records through ajax works properly.""" # records to be filtered out ContactRecordFactory.create_batch(10, contact__name='John Doe') self.client.path += '/contactrecord' filters = json.dumps({ 'contact': { 'name': { 'icontains': 'Joe Shmoe' } } }) response = self.client.post(data={'filters': filters}) output = json.loads(response.content) self.assertEqual(response.status_code, 200) self.assertEqual(len(output), 10)
def test_contact_record_counts_vs_list(self): """ ContactRecord counts for Communication Records and Referals should match summed counts from contacts. """ contacts = ContactFactory.create_batch(4) contacts[0].name = 'Other name' contacts[1].email = '*****@*****.**' contacts[2].partner = PartnerFactory(name='Other Partner') for contact in contacts: ContactRecordFactory.create(contact_type="job", contact=contact, partner=contact.partner) ContactRecordFactory.create(contact_type = 'email', contact=contact, partner=contact.partner) contacts[0].email = '*****@*****.**' ContactRecordFactory.create(contact_type = 'email', contact=contacts[0], partner=contact.partner) queryset = ContactRecord.objects.all() self.assertEqual(queryset.count(), 9) contacts = list(queryset.contacts) sum_referrals = sum([contact['referrals'] for contact in contacts]) sum_records = sum([contact['records'] for contact in contacts]) self.assertEqual(sum_referrals, queryset.referrals) self.assertEqual(sum_records, queryset.communication_activity.count())
def test_contact_record_counts_vs_list(self): """ ContactRecord counts for Communication Records and Referals should match summed counts from contacts. """ contacts = ContactFactory.create_batch(4) contacts[0].name = 'Other name' contacts[1].email = '*****@*****.**' contacts[2].partner = PartnerFactory(name='Other Partner') for contact in contacts: ContactRecordFactory.create(contact_type="job", contact=contact, partner=contact.partner) ContactRecordFactory.create(contact_type='email', contact=contact, partner=contact.partner) contacts[0].email = '*****@*****.**' ContactRecordFactory.create(contact_type='email', contact=contacts[0], partner=contact.partner) queryset = ContactRecord.objects.all() self.assertEqual(queryset.count(), 9) contacts = list(queryset.contacts) sum_referrals = sum([contact['referrals'] for contact in contacts]) sum_records = sum([contact['records'] for contact in contacts]) self.assertEqual(sum_referrals, queryset.referrals) self.assertEqual(sum_records, queryset.communication_activity.count())
def setUp(self): super(TestDownloadReport, self).setUp() self.client = TestClient(path=reverse("download_report")) self.client.login_user(self.user) ContactRecordFactory.create_batch(5, partner__owner=self.company) ContactRecordFactory.create_batch(5, contact_type="job", job_applications=1, partner__owner=self.company) ContactRecordFactory.create_batch(5, contact_type="job", job_hires=1, partner__owner=self.company)
def setUp(self): super(TestReportView, self).setUp() self.client = TestClient(path=reverse("reports", kwargs={"app": "mypartners", "model": "contactrecord"})) self.client.login_user(self.user) ContactRecordFactory.create_batch(5, partner__owner=self.company) ContactRecordFactory.create_batch(5, contact_type="job", job_applications=1, partner__owner=self.company) ContactRecordFactory.create_batch(5, contact_type="job", job_hires=1, partner__owner=self.company)
def test_dynamic_comm_records_report(self): """Create some test data, run, list, and download a commrec report.""" self.client.login_user(self.user) partner = PartnerFactory(owner=self.company) contact = ContactFactory.create(name='somename', partner=partner) for i in range(0, 20): # unicode here to push through report generation/download ContactRecordFactory(partner=partner, contact=contact, subject=u"subject-%s \u2019" % i) report_data = self.find_report_data('comm_records') resp = self.client.post(reverse('run_dynamic_report'), data={ 'report_data_id': report_data.pk, 'name': 'The Report', }) self.assertEqual(200, resp.status_code) report_id = json.loads(resp.content)['id'] resp = self.client.get(reverse('list_dynamic_reports')) self.assertEqual(200, resp.status_code) self.assertEqual( { 'reports': [ { 'id': report_id, 'name': 'The Report', 'report_type': 'communication-records' }, ] }, json.loads(resp.content)) report_presentation = self.find_report_presentation( report_data, 'json_pass') data = { 'id': report_id, 'report_presentation_id': report_presentation.pk, } resp = self.client.get(reverse('download_dynamic_report'), data) self.assertEquals(200, resp.status_code) response_data = json.loads(resp.content) self.assertEquals(20, len(response_data['records'])) last_subject = response_data['records'][-1]['Subject'] expected_subject = u'subject-19 \u2019' self.assertEqual(expected_subject, last_subject)
def test_run_count_comm_rec_per_month_no_partners(self): """Trip over a bug in the mysql client driver.""" for (subject, itx) in enumerate(['a', 'b', 'c']): for month in [2, 3, 4]: self.sue.contactrecord_set.add( ContactRecordFactory(subject=subject, date_time=datetime(2015, month, 1))) ds = PartnersDataSource() partners_filter = PartnersFilter( tags=AndGroupFilter([OrGroupFilter([MatchFilter('zzz')])])) recs = ds.run_count_comm_rec_per_month(self.company, partners_filter, ['name', 'year', '-month']) self.assertEqual(0, len(recs))
def test_run_count_comm_rec_per_month_one_partner(self): """Trip over a bug in the mysql client driver.""" for (subject, itx) in enumerate(['a', 'b', 'c']): for year in [2013, 2015, 2014]: for month in [2, 3, 4]: self.sue.contactrecord_set.add( ContactRecordFactory(subject=subject, date_time=datetime( year, month, 1))) ds = PartnersDataSource() partners_filter = PartnersFilter( tags=AndGroupFilter([OrGroupFilter([MatchFilter('west')])])) recs = ds.run_count_comm_rec_per_month(self.company, partners_filter, ['name', 'year', '-month']) data = [(r['name'], r['year'], r['month'], r['comm_rec_count']) for r in recs] self.assertEqual(36, len(data))
def setUp(self): super(TestReportView, self).setUp() self.client = TestClient(path=reverse('reports', kwargs={ 'app': 'mypartners', 'model': 'contactrecord'})) self.client.login_user(self.user) ContactRecordFactory.create_batch(5, partner__owner=self.company) ContactRecordFactory.create_batch( 5, contact_type='job', job_applications=1, partner__owner=self.company) ContactRecordFactory.create_batch( 5, contact_type='job', job_hires=1, partner__owner=self.company)
def setUp(self): super(TestDownloadReport, self).setUp() self.client = TestClient(path=reverse('download_report')) self.client.login_user(self.user) ContactRecordFactory.create_batch(5, partner__owner=self.company) ContactRecordFactory.create_batch(5, contact_type='job', job_applications=1, partner__owner=self.company) ContactRecordFactory.create_batch(5, contact_type='job', job_hires=1, partner__owner=self.company)
def test_run_count_comm_rec_per_month(self): """Make sure we only get data for this user.""" for (subject, itx) in enumerate(['a', 'b', 'c']): for month in [2, 3, 4]: self.sue.contactrecord_set.add( ContactRecordFactory(subject=subject, date_time=datetime(2015, month, 1))) ds = PartnersDataSource() recs = ds.run_count_comm_rec_per_month(self.company, PartnersFilter(), []) data = [(r['name'], r['year'], r['month'], r['comm_rec_count']) for r in recs] expected = [ (self.partner_a.name, 2015, 1, 0), (self.partner_a.name, 2015, 2, 0), (self.partner_a.name, 2015, 3, 0), (self.partner_a.name, 2015, 4, 0), (self.partner_a.name, 2015, 5, 0), (self.partner_a.name, 2015, 6, 0), (self.partner_a.name, 2015, 7, 0), (self.partner_a.name, 2015, 8, 0), (self.partner_a.name, 2015, 9, 0), (self.partner_a.name, 2015, 10, 0), (self.partner_a.name, 2015, 11, 0), (self.partner_a.name, 2015, 12, 0), (self.partner_b.name, 2015, 1, 0), (self.partner_b.name, 2015, 2, 3), (self.partner_b.name, 2015, 3, 3), (self.partner_b.name, 2015, 4, 3), (self.partner_b.name, 2015, 5, 0), (self.partner_b.name, 2015, 6, 0), (self.partner_b.name, 2015, 7, 0), (self.partner_b.name, 2015, 8, 0), (self.partner_b.name, 2015, 9, 0), (self.partner_b.name, 2015, 10, 0), (self.partner_b.name, 2015, 11, 0), (self.partner_b.name, 2015, 12, 0), ] self.assertEqual(expected, data)
def test_get_form_delta(self): """Tests that form changes are properly captured in a delta.""" request = Mock() request.configure_mock(user=self.staff_user, impersonator=None) contact_record = ContactRecordFactory( contact=self.contact, partner=self.partner) attachment = PRMAttachmentFactory(contact_record=contact_record) # The contact record form has a lot of required fields. data=dict( contact_type=contact_record.contact_type, contact=contact_record.contact.pk, contact_email=contact_record.contact_email, # contact_phone=contact_record.contact_phone, length_0="00", length_1="30", date_time_0="Aug", date_time_1="24", date_time_2="2015", date_time_3="03", date_time_4="10", date_time_5="PM", notes=contact_record.notes, partner=contact_record.partner.pk, company=contact_record.partner.owner, attach_delete=[attachment.pk] ) form = ContactRecordForm( instance=contact_record, partner=contact_record.partner, data=data) # Before 8/25/2015, saving this form with such changes would cause an # error form.save(request, contact_record.partner) delta = json.loads(ContactLogEntry.objects.last().delta) # ensure a change is logged self.assertEqual(delta['attach_delete']['new'], [str(attachment.pk)]) # ensure things that aren't changes aren't logged self.assertNotIn("notes", delta.keys())
def test_date_filters(self): """ Filter by activity start and end date. """ end_date = datetime.now().date() # randomly create partners and assign them contact records ranging from # 60 days ago to now. partners = [PartnerFactory(owner=self.company) for i in range(3)] partners.append(self.partner) # we want the contact records to exist before the tests, hence the # second for loop for days in [60, 30, 1, 0]: ContactRecordFactory(partner=random.choice(partners), date_time=end_date - timedelta(days)) for days in [60, 30, 1, 0]: start_date = (datetime.now() - timedelta(days)).date() request = self.request_factory.get( 'prm/view/partner-library', dict( company=self.company.id, start_date=start_date.strftime("%m/%d/%Y"), end_date=end_date.strftime("%m/%d/%Y"))) request.user = self.staff_user partners = helpers.filter_partners(request) for partner in partners: date_times = [c.date_time.date() for c in partner.contactrecord_set.all()] # ensure that for each partner, at least one contact record is # within range if partner.contactrecord_set.all(): self.assertTrue(filter( lambda x: start_date <= x <= end_date , date_times))
def test_tag_added_to_taggable_models(self): tag = TagFactory(company=self.company) tag.save() tag2 = TagFactory(name="bar", company=self.company) tag2.save() cr = ContactRecordFactory(partner=self.partner) # Add tag to models cr.tags.add(tag) self.partner.tags.add(tag) self.partner.save() self.contact.tags.add(tag) self.contact.save() # Check to make sure it was added self.assertEquals(1, len(cr.tags.all())) self.assertEquals(1, len(self.partner.tags.all())) self.assertEquals(1, len(self.contact.tags.all())) # Add a 2nd tag and check self.partner.tags.add(tag2) self.partner.save() self.assertEquals(2, len(self.partner.tags.all()))
def setUp(self): super(TestReportView, self).setUp() self.client = TestClient(path=reverse('reports', kwargs={ 'app': 'mypartners', 'model': 'contactrecord' })) self.client.login_user(self.user) ContactRecordFactory.create_batch(5, partner=self.partner) ContactRecordFactory.create_batch(5, contact_type='job', job_applications="1", job_interviews="0", job_hires="0", partner=self.partner) ContactRecordFactory.create_batch(5, contact_type='job', job_applications="0", job_interviews="0", job_hires="1", partner=self.partner)
def setUp(self): super(TestCommRecordsDataSource, self).setUp() # A company to work with self.company = CompanyFactory(name='right') self.company.save() # A separate company that should not show up in results. self.other_company = CompanyFactory(name='wrong') self.other_company.save() self.partner_a = PartnerFactory( owner=self.company, uri='http://www.example.com/', data_source="zap", name="aaa") self.partner_b = PartnerFactory( owner=self.company, uri='http://www.asdf.com/', data_source="bcd", name="bbb") # An unapproved parther. Associated data should be filtered out. self.partner_unapp = PartnerFactory( owner=self.company, name="unapproved", approval_status__code=Status.UNPROCESSED) # An archived parther. Associated data should be filtered out. self.partner_archived = PartnerFactory(owner=self.company) self.east_tag = TagFactory.create(name='east', hex_color="aaaaaa") self.west_tag = TagFactory.create(name='west', hex_color="bbbbbb") self.bad_tag = TagFactory.create(name='bad', hex_color="cccccc") self.john_user = UserFactory(email="*****@*****.**") self.john = ContactFactory( partner=self.partner_a, name='john adams', user=self.john_user, email="*****@*****.**", last_action_time='2015-10-03') self.john.locations.add( LocationFactory.create( city="Indianapolis", state="IN")) self.john.locations.add( LocationFactory.create( city="Chicago", state="IL")) self.sue_user = UserFactory(email="*****@*****.**") self.sue = ContactFactory( partner=self.partner_b, name='Sue Baxter', user=self.sue_user, email="*****@*****.**", last_action_time='2015-09-30 13:23') self.sue.locations.add( LocationFactory.create( address_line_one="123", city="Los Angeles", state="CA")) self.sue.locations.add( LocationFactory.create( address_line_one="234", city="Los Angeles", state="CA")) self.partner_a.primary_contact = self.john self.partner_b.primary_contact = self.sue self.partner_a.save() self.partner_b.save() self.record_1 = ContactRecordFactory( subject='record 1', date_time='2015-09-30 13:23', contact=self.john, contact_type="Email", partner=self.partner_a, location='Indianapolis, IN', tags=[self.east_tag]) self.record_2 = ContactRecordFactory( subject='record 2', date_time='2015-01-01', contact=self.john, contact_type="Meeting Or Event", partner=self.partner_a, location='Indianapolis, IN', tags=[self.east_tag]) self.record_3 = ContactRecordFactory( subject='record 3', date_time='2015-10-03', contact=self.sue, contact_type="Phone", partner=self.partner_b, location='Los Angeles, CA', tags=[self.west_tag]) # Archive archived data here. Doing this earlier in the set up results # in odd exceptions. self.partner_archived.archive()
class TestCommRecordsDataSource(MyJobsBase): def setUp(self): super(TestCommRecordsDataSource, self).setUp() # A company to work with self.company = CompanyFactory(name='right') self.company.save() # A separate company that should not show up in results. self.other_company = CompanyFactory(name='wrong') self.other_company.save() self.partner_a = PartnerFactory( owner=self.company, uri='http://www.example.com/', data_source="zap", name="aaa") self.partner_b = PartnerFactory( owner=self.company, uri='http://www.asdf.com/', data_source="bcd", name="bbb") # An unapproved parther. Associated data should be filtered out. self.partner_unapp = PartnerFactory( owner=self.company, name="unapproved", approval_status__code=Status.UNPROCESSED) # An archived parther. Associated data should be filtered out. self.partner_archived = PartnerFactory(owner=self.company) self.east_tag = TagFactory.create(name='east', hex_color="aaaaaa") self.west_tag = TagFactory.create(name='west', hex_color="bbbbbb") self.bad_tag = TagFactory.create(name='bad', hex_color="cccccc") self.john_user = UserFactory(email="*****@*****.**") self.john = ContactFactory( partner=self.partner_a, name='john adams', user=self.john_user, email="*****@*****.**", last_action_time='2015-10-03') self.john.locations.add( LocationFactory.create( city="Indianapolis", state="IN")) self.john.locations.add( LocationFactory.create( city="Chicago", state="IL")) self.sue_user = UserFactory(email="*****@*****.**") self.sue = ContactFactory( partner=self.partner_b, name='Sue Baxter', user=self.sue_user, email="*****@*****.**", last_action_time='2015-09-30 13:23') self.sue.locations.add( LocationFactory.create( address_line_one="123", city="Los Angeles", state="CA")) self.sue.locations.add( LocationFactory.create( address_line_one="234", city="Los Angeles", state="CA")) self.partner_a.primary_contact = self.john self.partner_b.primary_contact = self.sue self.partner_a.save() self.partner_b.save() self.record_1 = ContactRecordFactory( subject='record 1', date_time='2015-09-30 13:23', contact=self.john, contact_type="Email", partner=self.partner_a, location='Indianapolis, IN', tags=[self.east_tag]) self.record_2 = ContactRecordFactory( subject='record 2', date_time='2015-01-01', contact=self.john, contact_type="Meeting Or Event", partner=self.partner_a, location='Indianapolis, IN', tags=[self.east_tag]) self.record_3 = ContactRecordFactory( subject='record 3', date_time='2015-10-03', contact=self.sue, contact_type="Phone", partner=self.partner_b, location='Los Angeles, CA', tags=[self.west_tag]) # Archive archived data here. Doing this earlier in the set up results # in odd exceptions. self.partner_archived.archive() def test_run_unfiltered(self): """Make sure we only get data for this user.""" ds = CommRecordsDataSource() recs = ds.run_unaggregated(self.company, CommRecordsFilter(), []) subjects = set([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_filter_by_date_range(self): """Should show only commrec with last_action_time in range.""" ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter( date_time=[datetime(2015, 9, 1), datetime(2015, 9, 30)]), []) subjects = set([r['subject'] for r in recs]) expected = {self.record_1.subject} self.assertEqual(expected, subjects) def test_filter_by_date_before(self): """Should show only commrec with last_action_time before date.""" ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter( date_time=[None, datetime(2015, 9, 30)]), []) subjects = set([r['subject'] for r in recs]) expected = {self.record_1.subject, self.record_2.subject} self.assertEqual(expected, subjects) def test_filter_by_date_after(self): """Should show only commrec with last_action_time after date.""" ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter( date_time=[datetime(2015, 10, 1), None]), []) subjects = set([r['subject'] for r in recs]) expected = {self.record_3.subject} self.assertEqual(expected, subjects) def test_filter_by_state(self): """Should show only commrecs with correct state.""" ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter( locations={ 'state': 'CA' }), []) subjects = set([r['subject'] for r in recs]) expected = {self.record_3.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={ 'city': 'Los Angeles' }), []) subjects = set([r['subject'] for r in recs]) expected = {self.record_3.subject} self.assertEqual(expected, subjects) def test_filter_by_tags(self): """Show only commrec with correct tags.""" ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter(tags=[['EaSt']]), []) subjects = set([r['subject'] for r in recs]) expected = {self.record_1.subject, self.record_2.subject} self.assertEqual(expected, subjects) 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=[['EaSt', 'wEsT']]), []) subjects = set([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_filter_by_tags_and(self): """Show only commrec with correct tags in 'and' configuration.""" ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter(tags=[['EaSt'], ['wEsT']]), []) subjects = set([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=[['EaSt'], ['wEsT']]), []) subjects = set([r['subject'] for r in recs]) expected = {self.record_1.subject} self.assertEqual(expected, subjects) def test_filter_by_empty_things(self): """Empty filters should not filter, just like missing filters.""" ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter( locations={'city': '', 'state': ''}), []) subjects = set([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_filter_by_communication_type(self): """Check communication_type filter works at all.""" ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter(communication_type='Email'), []) subjects = set([r['subject'] for r in recs]) expected = {self.record_1.subject} self.assertEqual(expected, subjects) def test_filter_by_partner(self): """Check partner filter works at all.""" ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter(partner=[self.partner_a.pk]), []) subjects = set([r['subject'] for r in recs]) expected = {self.record_1.subject, self.record_2.subject} self.assertEqual(expected, subjects) def test_filter_by_contact(self): """Check partner filter works at all.""" ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter(contact=[self.sue.pk]), []) subjects = set([r['subject'] for r in recs]) expected = {self.record_3.subject} self.assertEqual(expected, subjects) def test_help_city(self): """Check city help works and ignores current city filter.""" ds = CommRecordsDataSource() recs = ds.help_city( self.company, CommRecordsFilter(locations={'city': "zz"}), "angel") actual = set([r['key'] for r in recs]) self.assertEqual({'Los Angeles'}, actual) def test_help_state(self): """Check state help works and ignores current state filter.""" ds = CommRecordsDataSource() recs = ds.help_state( self.company, CommRecordsFilter(locations={'state': "zz"}), "i") actual = set([r['key'] for r in recs]) self.assertEqual({'IL', 'IN'}, actual) def test_help_tags(self): """Check tags help works at all.""" ds = CommRecordsDataSource() recs = ds.help_tags(self.company, CommRecordsFilter(), "E") actual = set([r['key'] for r in recs]) self.assertEqual({'east', 'west'}, actual) def test_help_tags_colors(self): """Tags should have colors""" ds = CommRecordsDataSource() recs = ds.help_tags(self.company, CommRecordsFilter(), "east") self.assertEqual("aaaaaa", recs[0]['hexColor']) def test_help_communication_types(self): """Check communication_types help works at all.""" ds = CommRecordsDataSource() recs = ds.help_communication_type( self.company, CommRecordsFilter(), "ph") actual = set([r['key'] for r in recs]) self.assertEqual({'Phone'}, actual) def test_help_partner(self): """Check partner help works at all.""" ds = CommRecordsDataSource() recs = ds.help_partner(self.company, CommRecordsFilter(), "A") self.assertEqual( [{'key': self.partner_a.pk, 'display': self.partner_a.name}], recs) def test_help_contact(self): """Check contact help works at all.""" ds = CommRecordsDataSource() recs = ds.help_contact(self.company, CommRecordsFilter(), "U") self.assertEqual( [{'key': self.sue.pk, 'display': self.sue.name}], recs) def test_order(self): """Check ordering results works at all.""" ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter(), ["-subject"]) subjects = [r['subject'] for r in recs] expected = [ self.record_3.subject, self.record_2.subject, self.record_1.subject, ] self.assertEqual(expected, subjects)
class TestCommRecordsDataSource(MyJobsBase): def setUp(self): super(TestCommRecordsDataSource, self).setUp() # A company to work with self.company = CompanyFactory(name='right') self.company.save() # A separate company that should not show up in results. self.other_company = CompanyFactory(name='wrong') self.other_company.save() self.partner_a = PartnerFactory( owner=self.company, uri='http://www.example.com/', data_source="zap", name="aaa") self.partner_b = PartnerFactory( owner=self.company, uri='http://www.asdf.com/', data_source="bcd", name="bbb") # An unapproved parther. Associated data should be filtered out. self.partner_unapp = PartnerFactory( owner=self.company, name="unapproved", approval_status__code=Status.UNPROCESSED) # An archived parther. Associated data should be filtered out. self.partner_archived = PartnerFactory(owner=self.company) self.east_tag = TagFactory.create( company=self.company, name='east', hex_color="aaaaaa") self.west_tag = TagFactory.create( company=self.company, name='west', hex_color="bbbbbb") self.north_tag = TagFactory.create( company=self.company, name='north', hex_color="cccccc") self.south_tag = TagFactory.create( company=self.company, name='south', hex_color="dddddd") self.left_tag = TagFactory.create( company=self.company, name='left', hex_color="eeeeee") self.right_tag = TagFactory.create( company=self.company, name='right', hex_color="ffffff") self.bad_tag = TagFactory.create( company=self.company, name='bad', hex_color="cccccc") self.partner_a.tags.add(self.left_tag) self.partner_b.tags.add(self.right_tag) self.john_user = UserFactory(email="*****@*****.**") self.john = ContactFactory( partner=self.partner_a, name='john adams', user=self.john_user, email="*****@*****.**", last_action_time='2015-10-03') self.john.locations.add( LocationFactory.create( city="Indianapolis", state="IN")) self.john.locations.add( LocationFactory.create( city="Chicago", state="IL")) self.john.tags.add(self.north_tag) self.sue_user = UserFactory(email="*****@*****.**") self.sue = ContactFactory( partner=self.partner_b, name='Sue Baxter', user=self.sue_user, email="*****@*****.**", last_action_time='2015-09-30 13:23') self.sue.locations.add( LocationFactory.create( address_line_one="123", city="Los Angeles", state="CA")) self.sue.locations.add( LocationFactory.create( address_line_one="234", city="Los Angeles", state="CA")) self.sue.tags.add(self.south_tag) self.partner_a.primary_contact = self.john self.partner_b.primary_contact = self.sue self.partner_a.save() self.partner_b.save() self.record_1 = ContactRecordFactory( subject='record 1', date_time='2015-09-30 13:23', contact=self.john, contact_type="Email", partner=self.partner_a, location='Indianapolis, IN', tags=[self.east_tag]) self.record_2 = ContactRecordFactory( subject='record 2', date_time='2015-01-01', contact=self.john, contact_type="Meeting Or Event", partner=self.partner_a, location='Indianapolis, IN', tags=[self.east_tag]) self.record_3 = ContactRecordFactory( subject='record 3', date_time='2015-10-03', contact=self.sue, contact_type="Phone", partner=self.partner_b, location='Los Angeles, CA', tags=[self.west_tag]) # Archive archived data here. Doing this earlier in the set up results # in odd exceptions. self.partner_archived.archive() def test_run_unfiltered(self): """Make sure we only get data for this user.""" ds = CommRecordsDataSource() recs = ds.run_unaggregated(self.company, CommRecordsFilter(), []) 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_filter_by_date_range(self): """Should show only commrec with last_action_time in range.""" ds = CommRecordsDataSource() date_range = DateRangeFilter([ datetime(2015, 9, 1), datetime(2015, 9, 30)]) recs = ds.run_unaggregated( self.company, CommRecordsFilter(date_time=date_range), []) subjects = {r['subject'] for r in recs} expected = {self.record_1.subject} self.assertEqual(expected, subjects) def test_filter_by_date_before(self): """Should show only commrec with last_action_time before date.""" ds = CommRecordsDataSource() date_range = DateRangeFilter( [None, datetime(2015, 9, 30)]) recs = ds.run_unaggregated( self.company, CommRecordsFilter(date_time=date_range), []) subjects = {r['subject'] for r in recs} expected = {self.record_1.subject, self.record_2.subject} self.assertEqual(expected, subjects) def test_filter_by_date_after(self): """Should show only commrec with last_action_time after date.""" ds = CommRecordsDataSource() date_range = DateRangeFilter( [datetime(2015, 10, 1), None]) recs = ds.run_unaggregated( self.company, CommRecordsFilter(date_time=date_range), []) subjects = {r['subject'] for r in recs} expected = {self.record_3.subject} self.assertEqual(expected, subjects) def test_filter_by_state(self): """Should show only commrecs with correct state.""" ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter( locations=CompositeAndFilter({'state': MatchFilter('CA')})), []) subjects = {r['subject'] for r in recs} expected = {self.record_3.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_filter_by_tags(self): """ Show only commrec with correct tags. """ ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter(tags=AndGroupFilter([ OrGroupFilter([MatchFilter('EaSt')])])), []) subjects = {r['subject'] for r in recs} expected = {self.record_1.subject, self.record_2.subject} self.assertEqual(expected, subjects) def test_filter_by_tags_unlinked(self): """ Only return untagged commrecs. """ self.record_1.tags.clear() ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter(tags=UnlinkedFilter()), []) subjects = {r['subject'] for r in recs} expected = {self.record_1.subject} self.assertEqual(expected, subjects) 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_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_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_partner(self): """ Check partner filter works at all. """ ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter( partner=OrGroupFilter([MatchFilter(self.partner_a.pk)])), []) subjects = {r['subject'] for r in recs} expected = {self.record_1.subject, self.record_2.subject} self.assertEqual(expected, subjects) def test_filter_by_contact(self): """ Check partner filter works at all. """ ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter( contact=OrGroupFilter([MatchFilter(self.sue.pk)])), []) subjects = {r['subject'] for r in recs} expected = {self.record_3.subject} self.assertEqual(expected, subjects) def test_filter_by_contact_tags(self): """ Test that we can filter by contact tags. """ ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter( contact_tags=AndGroupFilter([ OrGroupFilter([MatchFilter('sOuTh')])])), []) subjects = {r['subject'] for r in recs} expected = {self.record_3.subject} self.assertEqual(expected, subjects) def test_filter_by_contact_tags_untagged(self): """ Check that we can find a record attached to an untagged contact. """ self.sue.tags.clear() ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter(contact_tags=UnlinkedFilter()), []) subjects = {r['subject'] for r in recs} expected = {self.record_3.subject} self.assertEqual(expected, subjects) def test_filter_by_partner_tags(self): """ Test that we can filter by partner tags. """ ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter( partner_tags=AndGroupFilter([ OrGroupFilter([MatchFilter('rigHt')])])), []) subjects = {r['subject'] for r in recs} expected = {self.record_3.subject} self.assertEqual(expected, subjects) 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 = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter(partner_tags=UnlinkedFilter()), []) subjects = {r['subject'] for r in recs} expected = {self.record_3.subject} self.assertEqual(expected, subjects) def test_help_city(self): """ Check city help works and ignores current city filter. """ ds = CommRecordsDataSource() recs = ds.help_city( self.company, CommRecordsFilter( locations=CompositeAndFilter({ 'city': MatchFilter("zz")})), "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 = CommRecordsDataSource() recs = ds.help_state( self.company, CommRecordsFilter( locations=CompositeAndFilter({ 'state': MatchFilter("zz")})), "i") actual = {r['value'] for r in recs} self.assertEqual({'IL', 'IN'}, actual) def test_help_tags(self): """ Check tags help works at all. """ ds = CommRecordsDataSource() recs = ds.help_tags(self.company, CommRecordsFilter(), "E") actual = {r['value'] for r in recs} self.assertEqual({'east', 'west'}, actual) def test_help_contact_tags(self): """ Check contact tags help works at all. """ ds = CommRecordsDataSource() recs = ds.help_contact_tags(self.company, CommRecordsFilter(), "O") actual = {r['value'] for r in recs} self.assertEqual({'north', 'south'}, actual) def test_help_partner_tags(self): """ Check partner tags help works at all. """ ds = CommRecordsDataSource() recs = ds.help_partner_tags(self.company, CommRecordsFilter(), "t") actual = {r['value'] for r in recs} self.assertEqual({'left', 'right'}, actual) def test_help_tags_colors(self): """ Tags should have colors """ ds = CommRecordsDataSource() recs = ds.help_tags(self.company, CommRecordsFilter(), "east") self.assertEqual("aaaaaa", recs[0]['hexColor']) def test_help_communication_types(self): """ Check communication_types help works at all. """ ds = CommRecordsDataSource() recs = ds.help_communication_type( self.company, CommRecordsFilter(), "ph") actual = {r['value'] for r in recs} expected = {'phone', 'job', 'meetingorevent', 'email', 'pssemail'} self.assertEqual(expected, actual) def test_help_partner(self): """ Check partner help works at all. """ ds = CommRecordsDataSource() recs = ds.help_partner(self.company, CommRecordsFilter(), "A") self.assertEqual( [{'value': self.partner_a.pk, 'display': self.partner_a.name}], recs) def test_help_contact(self): """ Check contact help works at all. """ ds = CommRecordsDataSource() recs = ds.help_contact(self.company, CommRecordsFilter(), "U") self.assertEqual( [{'value': self.sue.pk, 'display': self.sue.name}], recs) def test_values(self): """ Check limiting values works at all """ self.maxDiff = 10000 ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter(), ["partner", "subject"]) expected = [ {'partner': u'aaa', 'subject': u'record 1'}, {'partner': u'aaa', 'subject': u'record 2'}, {'partner': u'bbb', 'subject': u'record 3'}, ] self.assertEqual(expected, recs) def test_adorn_filter(self): self.maxDiff = 10000 found_filter_items = { 'tags': ['east', 'west'], 'communication_type': ['Email'], 'partner': [str(self.partner_a.pk)], 'contact': [str(self.sue.pk)], 'contact_tags': ['nOrth', 'south'], 'partner_tags': ['lEft', 'riGht'], } expected = { u'partner': { self.partner_a.pk: {'value': self.partner_a.pk, 'display': u'aaa'}, }, u'contact': { self.sue.pk: {'value': self.sue.pk, 'display': u'Sue Baxter'}, }, u'tags': { u'east': { 'value': u'east', 'display': u'east', 'hexColor': u'aaaaaa', }, u'west': { 'value': u'west', 'display': u'west', 'hexColor': u'bbbbbb', }, }, u'communication_type': { 'email': {'value': 'email', 'display': 'Email'} }, u'contact_tags': { u'north': { 'value': u'north', 'display': u'north', 'hexColor': u'cccccc', }, u'south': { 'value': u'south', 'display': u'south', 'hexColor': u'dddddd', }, }, u'partner_tags': { u'left': { 'value': u'left', 'display': u'left', 'hexColor': u'eeeeee', }, u'right': { 'value': u'right', 'display': u'right', 'hexColor': u'ffffff', }, }, } ds = CommRecordsDataSource() result = ds.adorn_filter_items(self.company, found_filter_items) self.assertEqual(expected, result) def test_default_filter(self): """ should produce a populated filter object. """ ds = CommRecordsDataSource() default_filter = ds.get_default_filter(None, self.company) self.assertEquals( datetime.now().year, default_filter.date_time.dates[1].year) # Take out value dated today. Too hard to run through assertEquals. default_filter.date_time.dates[1] = None expected = CommRecordsFilter( date_time=DateRangeFilter([datetime(2014, 1, 1), None])) self.assertEquals(expected, default_filter)
class TestCommRecordsDataSource(MyJobsBase): def setUp(self): super(TestCommRecordsDataSource, self).setUp() # A company to work with self.company = CompanyFactory(name='right') self.company.save() # A separate company that should not show up in results. self.other_company = CompanyFactory(name='wrong') self.other_company.save() self.partner_a = PartnerFactory(owner=self.company, uri='http://www.example.com/', data_source="zap", name="aaa") self.partner_b = PartnerFactory(owner=self.company, uri='http://www.asdf.com/', data_source="bcd", name="bbb") # An unapproved parther. Associated data should be filtered out. self.partner_unapp = PartnerFactory( owner=self.company, name="unapproved", approval_status__code=Status.UNPROCESSED) # An archived parther. Associated data should be filtered out. self.partner_archived = PartnerFactory(owner=self.company) self.east_tag = TagFactory.create(company=self.company, name='east', hex_color="aaaaaa") self.west_tag = TagFactory.create(company=self.company, name='west', hex_color="bbbbbb") self.north_tag = TagFactory.create(company=self.company, name='north', hex_color="cccccc") self.south_tag = TagFactory.create(company=self.company, name='south', hex_color="dddddd") self.left_tag = TagFactory.create(company=self.company, name='left', hex_color="eeeeee") self.right_tag = TagFactory.create(company=self.company, name='right', hex_color="ffffff") self.bad_tag = TagFactory.create(company=self.company, name='bad', hex_color="cccccc") self.partner_a.tags.add(self.left_tag) self.partner_b.tags.add(self.right_tag) self.john_user = UserFactory(email="*****@*****.**") self.john = ContactFactory(partner=self.partner_a, name='john adams', user=self.john_user, email="*****@*****.**", last_action_time='2015-10-03') self.john.locations.add( LocationFactory.create(city="Indianapolis", state="IN")) self.john.locations.add( LocationFactory.create(city="Chicago", state="IL")) self.john.tags.add(self.north_tag) self.sue_user = UserFactory(email="*****@*****.**") self.sue = ContactFactory(partner=self.partner_b, name='Sue Baxter', user=self.sue_user, email="*****@*****.**", last_action_time='2015-09-30 13:23') self.sue.locations.add( LocationFactory.create(address_line_one="123", city="Los Angeles", state="CA")) self.sue.locations.add( LocationFactory.create(address_line_one="234", city="Los Angeles", state="CA")) self.sue.tags.add(self.south_tag) self.partner_a.primary_contact = self.john self.partner_b.primary_contact = self.sue self.partner_a.save() self.partner_b.save() self.record_1 = ContactRecordFactory(subject='record 1', date_time='2015-09-30 13:23', contact=self.john, contact_type="Email", partner=self.partner_a, location='Indianapolis, IN', tags=[self.east_tag]) self.record_2 = ContactRecordFactory(subject='record 2', date_time='2015-01-01', contact=self.john, contact_type="Meeting Or Event", partner=self.partner_a, location='Indianapolis, IN', tags=[self.east_tag]) self.record_3 = ContactRecordFactory(subject='record 3', date_time='2015-10-03', contact=self.sue, contact_type="Phone", partner=self.partner_b, location='Los Angeles, CA', tags=[self.west_tag]) # Archive archived data here. Doing this earlier in the set up results # in odd exceptions. self.partner_archived.archive() def test_run_unfiltered(self): """Make sure we only get data for this user.""" ds = CommRecordsDataSource() recs = ds.run_unaggregated(self.company, CommRecordsFilter(), []) 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_filter_by_date_range(self): """Should show only commrec with last_action_time in range.""" ds = CommRecordsDataSource() date_range = DateRangeFilter( [datetime(2015, 9, 1), datetime(2015, 9, 30)]) recs = ds.run_unaggregated(self.company, CommRecordsFilter(date_time=date_range), []) subjects = {r['subject'] for r in recs} expected = {self.record_1.subject} self.assertEqual(expected, subjects) def test_filter_by_date_before(self): """Should show only commrec with last_action_time before date.""" ds = CommRecordsDataSource() date_range = DateRangeFilter([None, datetime(2015, 9, 30)]) recs = ds.run_unaggregated(self.company, CommRecordsFilter(date_time=date_range), []) subjects = {r['subject'] for r in recs} expected = {self.record_1.subject, self.record_2.subject} self.assertEqual(expected, subjects) def test_filter_by_date_after(self): """Should show only commrec with last_action_time after date.""" ds = CommRecordsDataSource() date_range = DateRangeFilter([datetime(2015, 10, 1), None]) recs = ds.run_unaggregated(self.company, CommRecordsFilter(date_time=date_range), []) subjects = {r['subject'] for r in recs} expected = {self.record_3.subject} self.assertEqual(expected, subjects) def test_filter_by_state(self): """Should show only commrecs with correct state.""" ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter( locations=CompositeAndFilter({'state': MatchFilter('CA')})), []) subjects = {r['subject'] for r in recs} expected = {self.record_3.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_filter_by_tags(self): """ Show only commrec with correct tags. """ ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter( tags=AndGroupFilter([OrGroupFilter([MatchFilter('EaSt')])])), []) subjects = {r['subject'] for r in recs} expected = {self.record_1.subject, self.record_2.subject} self.assertEqual(expected, subjects) def test_filter_by_tags_unlinked(self): """ Only return untagged commrecs. """ self.record_1.tags.clear() ds = CommRecordsDataSource() recs = ds.run_unaggregated(self.company, CommRecordsFilter(tags=UnlinkedFilter()), []) subjects = {r['subject'] for r in recs} expected = {self.record_1.subject} self.assertEqual(expected, subjects) 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_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_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_partner(self): """ Check partner filter works at all. """ ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter( partner=OrGroupFilter([MatchFilter(self.partner_a.pk)])), []) subjects = {r['subject'] for r in recs} expected = {self.record_1.subject, self.record_2.subject} self.assertEqual(expected, subjects) def test_filter_by_contact(self): """ Check partner filter works at all. """ ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter( contact=OrGroupFilter([MatchFilter(self.sue.pk)])), []) subjects = {r['subject'] for r in recs} expected = {self.record_3.subject} self.assertEqual(expected, subjects) def test_filter_by_contact_tags(self): """ Test that we can filter by contact tags. """ ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter(contact_tags=AndGroupFilter( [OrGroupFilter([MatchFilter('sOuTh')])])), []) subjects = {r['subject'] for r in recs} expected = {self.record_3.subject} self.assertEqual(expected, subjects) def test_filter_by_contact_tags_untagged(self): """ Check that we can find a record attached to an untagged contact. """ self.sue.tags.clear() ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter(contact_tags=UnlinkedFilter()), []) subjects = {r['subject'] for r in recs} expected = {self.record_3.subject} self.assertEqual(expected, subjects) def test_filter_by_partner_tags(self): """ Test that we can filter by partner tags. """ ds = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter(partner_tags=AndGroupFilter( [OrGroupFilter([MatchFilter('rigHt')])])), []) subjects = {r['subject'] for r in recs} expected = {self.record_3.subject} self.assertEqual(expected, subjects) 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 = CommRecordsDataSource() recs = ds.run_unaggregated( self.company, CommRecordsFilter(partner_tags=UnlinkedFilter()), []) subjects = {r['subject'] for r in recs} expected = {self.record_3.subject} self.assertEqual(expected, subjects) def test_help_city(self): """ Check city help works and ignores current city filter. """ ds = CommRecordsDataSource() recs = ds.help_city( self.company, CommRecordsFilter( locations=CompositeAndFilter({'city': MatchFilter("zz")})), "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 = CommRecordsDataSource() recs = ds.help_state( self.company, CommRecordsFilter( locations=CompositeAndFilter({'state': MatchFilter("zz")})), "i") actual = {r['value'] for r in recs} self.assertEqual({'IL', 'IN'}, actual) def test_help_tags(self): """ Check tags help works at all. """ ds = CommRecordsDataSource() recs = ds.help_tags(self.company, CommRecordsFilter(), "E") actual = {r['value'] for r in recs} self.assertEqual({'east', 'west'}, actual) def test_help_contact_tags(self): """ Check contact tags help works at all. """ ds = CommRecordsDataSource() recs = ds.help_contact_tags(self.company, CommRecordsFilter(), "O") actual = {r['value'] for r in recs} self.assertEqual({'north', 'south'}, actual) def test_help_partner_tags(self): """ Check partner tags help works at all. """ ds = CommRecordsDataSource() recs = ds.help_partner_tags(self.company, CommRecordsFilter(), "t") actual = {r['value'] for r in recs} self.assertEqual({'left', 'right'}, actual) def test_help_tags_colors(self): """ Tags should have colors """ ds = CommRecordsDataSource() recs = ds.help_tags(self.company, CommRecordsFilter(), "east") self.assertEqual("aaaaaa", recs[0]['hexColor']) def test_help_communication_types(self): """ Check communication_types help works at all. """ ds = CommRecordsDataSource() recs = ds.help_communication_type(self.company, CommRecordsFilter(), "ph") actual = {r['value'] for r in recs} expected = {'phone', 'job', 'meetingorevent', 'email', 'pssemail'} self.assertEqual(expected, actual) def test_help_partner(self): """ Check partner help works at all. """ ds = CommRecordsDataSource() recs = ds.help_partner(self.company, CommRecordsFilter(), "A") self.assertEqual([{ 'value': self.partner_a.pk, 'display': self.partner_a.name }], recs) def test_help_contact(self): """ Check contact help works at all. """ ds = CommRecordsDataSource() recs = ds.help_contact(self.company, CommRecordsFilter(), "U") self.assertEqual([{ 'value': self.sue.pk, 'display': self.sue.name }], recs) def test_values(self): """ Check limiting values works at all """ self.maxDiff = 10000 ds = CommRecordsDataSource() recs = ds.run_unaggregated(self.company, CommRecordsFilter(), ["partner", "subject"]) expected = [ { 'partner': u'aaa', 'subject': u'record 1' }, { 'partner': u'aaa', 'subject': u'record 2' }, { 'partner': u'bbb', 'subject': u'record 3' }, ] self.assertEqual(expected, recs) def test_adorn_filter(self): self.maxDiff = 10000 found_filter_items = { 'tags': ['east', 'west'], 'communication_type': ['Email'], 'partner': [str(self.partner_a.pk)], 'contact': [str(self.sue.pk)], 'contact_tags': ['nOrth', 'south'], 'partner_tags': ['lEft', 'riGht'], } expected = { u'partner': { self.partner_a.pk: { 'value': self.partner_a.pk, 'display': u'aaa' }, }, u'contact': { self.sue.pk: { 'value': self.sue.pk, 'display': u'Sue Baxter' }, }, u'tags': { u'east': { 'value': u'east', 'display': u'east', 'hexColor': u'aaaaaa', }, u'west': { 'value': u'west', 'display': u'west', 'hexColor': u'bbbbbb', }, }, u'communication_type': { 'email': { 'value': 'email', 'display': 'Email' } }, u'contact_tags': { u'north': { 'value': u'north', 'display': u'north', 'hexColor': u'cccccc', }, u'south': { 'value': u'south', 'display': u'south', 'hexColor': u'dddddd', }, }, u'partner_tags': { u'left': { 'value': u'left', 'display': u'left', 'hexColor': u'eeeeee', }, u'right': { 'value': u'right', 'display': u'right', 'hexColor': u'ffffff', }, }, } ds = CommRecordsDataSource() result = ds.adorn_filter_items(self.company, found_filter_items) self.assertEqual(expected, result) def test_default_filter(self): """ should produce a populated filter object. """ ds = CommRecordsDataSource() default_filter = ds.get_default_filter(None, self.company) self.assertEquals(datetime.now().year, default_filter.date_time.dates[1].year) # Take out value dated today. Too hard to run through assertEquals. default_filter.date_time.dates[1] = None expected = CommRecordsFilter( date_time=DateRangeFilter([datetime(2014, 1, 1), None])) self.assertEquals(expected, default_filter)
def setUp(self): super(TestCommRecordsDataSource, self).setUp() # A company to work with self.company = CompanyFactory(name='right') self.company.save() # A separate company that should not show up in results. self.other_company = CompanyFactory(name='wrong') self.other_company.save() self.partner_a = PartnerFactory(owner=self.company, uri='http://www.example.com/', data_source="zap", name="aaa") self.partner_b = PartnerFactory(owner=self.company, uri='http://www.asdf.com/', data_source="bcd", name="bbb") # An unapproved parther. Associated data should be filtered out. self.partner_unapp = PartnerFactory( owner=self.company, name="unapproved", approval_status__code=Status.UNPROCESSED) # An archived parther. Associated data should be filtered out. self.partner_archived = PartnerFactory(owner=self.company) self.east_tag = TagFactory.create(company=self.company, name='east', hex_color="aaaaaa") self.west_tag = TagFactory.create(company=self.company, name='west', hex_color="bbbbbb") self.north_tag = TagFactory.create(company=self.company, name='north', hex_color="cccccc") self.south_tag = TagFactory.create(company=self.company, name='south', hex_color="dddddd") self.left_tag = TagFactory.create(company=self.company, name='left', hex_color="eeeeee") self.right_tag = TagFactory.create(company=self.company, name='right', hex_color="ffffff") self.bad_tag = TagFactory.create(company=self.company, name='bad', hex_color="cccccc") self.partner_a.tags.add(self.left_tag) self.partner_b.tags.add(self.right_tag) self.john_user = UserFactory(email="*****@*****.**") self.john = ContactFactory(partner=self.partner_a, name='john adams', user=self.john_user, email="*****@*****.**", last_action_time='2015-10-03') self.john.locations.add( LocationFactory.create(city="Indianapolis", state="IN")) self.john.locations.add( LocationFactory.create(city="Chicago", state="IL")) self.john.tags.add(self.north_tag) self.sue_user = UserFactory(email="*****@*****.**") self.sue = ContactFactory(partner=self.partner_b, name='Sue Baxter', user=self.sue_user, email="*****@*****.**", last_action_time='2015-09-30 13:23') self.sue.locations.add( LocationFactory.create(address_line_one="123", city="Los Angeles", state="CA")) self.sue.locations.add( LocationFactory.create(address_line_one="234", city="Los Angeles", state="CA")) self.sue.tags.add(self.south_tag) self.partner_a.primary_contact = self.john self.partner_b.primary_contact = self.sue self.partner_a.save() self.partner_b.save() self.record_1 = ContactRecordFactory(subject='record 1', date_time='2015-09-30 13:23', contact=self.john, contact_type="Email", partner=self.partner_a, location='Indianapolis, IN', tags=[self.east_tag]) self.record_2 = ContactRecordFactory(subject='record 2', date_time='2015-01-01', contact=self.john, contact_type="Meeting Or Event", partner=self.partner_a, location='Indianapolis, IN', tags=[self.east_tag]) self.record_3 = ContactRecordFactory(subject='record 3', date_time='2015-10-03', contact=self.sue, contact_type="Phone", partner=self.partner_b, location='Los Angeles, CA', tags=[self.west_tag]) # Archive archived data here. Doing this earlier in the set up results # in odd exceptions. self.partner_archived.archive()
def setUp(self): super(TestRegenerate, self).setUp() self.client = TestClient(path=reverse("reports", kwargs={"app": "mypartners", "model": "contactrecord"})) self.client.login_user(self.user) ContactRecordFactory.create_batch(10, partner__owner=self.company)