def test_basic(self): """ Aggregate any datapoints older than 90 days into the totals stored on their links. """ link1 = LinkFactory.create(aggregate_link_clicks=7, aggregate_firefox_downloads=10) link1_old_datapoint = DataPointFactory.create(link=link1, date=aware_date(2014, 1, 1), link_clicks=8, firefox_downloads=4) link1_new_datapoint = DataPointFactory.create(link=link1, date=aware_date(2014, 3, 1), link_clicks=2, firefox_downloads=7) link2 = LinkFactory.create(aggregate_link_clicks=7, aggregate_firefox_downloads=10) link2_old_datapoint1 = DataPointFactory.create(link=link2, date=aware_date(2014, 1, 1), link_clicks=8, firefox_downloads=4) link2_old_datapoint2 = DataPointFactory.create(link=link2, date=aware_date(2013, 12, 30), link_clicks=2, firefox_downloads=7) self.command.handle() # link1 should have 7+8=15 clicks, 10+4=14 downloads, and the # new datapoint should still exist. link1 = Link.objects.get(pk=link1.pk) eq_(link1.aggregate_link_clicks, 15) eq_(link1.aggregate_firefox_downloads, 14) ok_(not DataPoint.objects.filter(pk=link1_old_datapoint.pk).exists()) ok_(DataPoint.objects.filter(pk=link1_new_datapoint.pk).exists()) # link2 should have 7+8+2=17 clicks, 10+4+7=21 downloads, and the # old datapoints should not exist. link2 = Link.objects.get(pk=link2.pk) eq_(link2.aggregate_link_clicks, 17) eq_(link2.aggregate_firefox_downloads, 21) ok_(not DataPoint.objects.filter(pk=link2_old_datapoint1.pk).exists()) ok_(not DataPoint.objects.filter(pk=link2_old_datapoint2.pk).exists())
def _link_with_clicks(self, user, aggregate_link_clicks, link_click_counts): """ Create a link with a specific number of aggregate links and datapoints with the given click counts. """ start_date = aware_date(2014, 4, 1) link = LinkFactory.create(user=user, aggregate_link_clicks=aggregate_link_clicks) for link_clicks in link_click_counts: DataPointFactory.create(link=link, link_clicks=link_clicks, date=start_date) start_date += timedelta(1)
def test_milestone_date_not_reached(self): """ If the milestone hasn't been reached by the user, return None. """ user = UserFactory.create() display = MilestoneDisplay(user) DataPointFactory.create(link_clicks=4, date=aware_date(2014, 1, 1), link__user=user) eq_(display.milestone_date('link_clicks', 8, 2), None)
def test_milestone_date_invalid_metric(self): """ If an invalid metric name is given, raise an AttributeError. """ user = UserFactory.create() display = MilestoneDisplay(user) DataPointFactory.create(link_clicks=4, date=aware_date(2014, 1, 1), link__user=user) with self.assertRaises(AttributeError): display.milestone_date('invalid', 2, 1)
def test_basic(self): """ Aggregate any datapoints older than 90 days into the totals stored on their links. """ link1 = LinkFactory.create(aggregate_link_clicks=7, aggregate_firefox_downloads=10) link1_old_datapoint = DataPointFactory.create(link=link1, date=aware_date( 2014, 1, 1), link_clicks=8, firefox_downloads=4) link1_new_datapoint = DataPointFactory.create(link=link1, date=aware_date( 2014, 3, 1), link_clicks=2, firefox_downloads=7) link2 = LinkFactory.create(aggregate_link_clicks=7, aggregate_firefox_downloads=10) link2_old_datapoint1 = DataPointFactory.create(link=link2, date=aware_date( 2014, 1, 1), link_clicks=8, firefox_downloads=4) link2_old_datapoint2 = DataPointFactory.create(link=link2, date=aware_date( 2013, 12, 30), link_clicks=2, firefox_downloads=7) self.command.handle() # link1 should have 7+8=15 clicks, 10+4=14 downloads, and the # new datapoint should still exist. link1 = Link.objects.get(pk=link1.pk) eq_(link1.aggregate_link_clicks, 15) eq_(link1.aggregate_firefox_downloads, 14) ok_(not DataPoint.objects.filter(pk=link1_old_datapoint.pk).exists()) ok_(DataPoint.objects.filter(pk=link1_new_datapoint.pk).exists()) # link2 should have 7+8+2=17 clicks, 10+4+7=21 downloads, and the # old datapoints should not exist. link2 = Link.objects.get(pk=link2.pk) eq_(link2.aggregate_link_clicks, 17) eq_(link2.aggregate_firefox_downloads, 21) ok_(not DataPoint.objects.filter(pk=link2_old_datapoint1.pk).exists()) ok_(not DataPoint.objects.filter(pk=link2_old_datapoint2.pk).exists())
def test_not_authenticated(self): """ If the current user isn't authenticated, render the home page with the correct stats. """ request = self.factory.get('/') request.user = Mock() request.user.is_authenticated.return_value = False # User count = 27 + 8 autogenerated via related factory # Click count = 64 # Link count = 3 + 5 autogenerated via related factory UserFactory.create_batch(27) for clicks in (4, 6, 9, 10): DataPointFactory.create(link_clicks=clicks, date=date(2014, 4, 26)) for clicks in (25, 5, 5): LinkFactory.create(aggregate_link_clicks=clicks) # Create a link with multiple datapoints to test for a faulty # join that would screw up the totals. link = LinkFactory.create() DataPointFactory.create(link_clicks=7, link=link, date=date(2014, 4, 26)) DataPointFactory.create(link_clicks=7, link=link, date=date(2014, 4, 27)) with patch('affiliates.base.views.render') as render: eq_(views.home(request), render.return_value) render.assert_called_with(request, 'base/home.html', { 'affiliate_count': 35, 'link_count': 8, 'click_count': 78 })
def test_process_request_clicks_and_downloads(self): """ If there were any clicks or downloads since the user's last visit, update their last_visit date and log a message. """ self.request.user = UserFactory.create(userprofile__last_visit=aware_date(2014, 1, 1)) # Date of last visit not included. DataPointFactory.create( link__user=self.request.user, date=aware_date(2014, 1, 1), link_clicks=3, firefox_downloads=9 ) # Current date and dates between are included. DataPointFactory.create( link__user=self.request.user, date=aware_date(2014, 1, 2), link_clicks=4, firefox_downloads=7 ) DataPointFactory.create( link__user=self.request.user, date=aware_date(2014, 1, 3), link_clicks=1, firefox_downloads=2 ) with patch("affiliates.links.middleware.timezone.now") as mock_now: mock_now.return_value = aware_datetime(2014, 1, 3) eq_(self.middleware.process_request(self.request), None) ok_(self.messages.info.called) message = self.messages.info.call_args[0][1] ok_("5" in message and "9" in message) profile = UserProfile.objects.get(user=self.request.user) eq_(profile.last_visit, aware_date(2014, 1, 3))
def test_process_request_clicks_and_downloads(self): """ If there were any clicks or downloads since the user's last visit, update their last_visit date and log a message. """ self.request.user = UserFactory.create( userprofile__last_visit=aware_date(2014, 1, 1)) # Date of last visit not included. DataPointFactory.create(link__user=self.request.user, date=aware_date(2014, 1, 1), link_clicks=3, firefox_downloads=9) # Current date and dates between are included. DataPointFactory.create(link__user=self.request.user, date=aware_date(2014, 1, 2), link_clicks=4, firefox_downloads=7) DataPointFactory.create(link__user=self.request.user, date=aware_date(2014, 1, 3), link_clicks=1, firefox_downloads=2) with patch('affiliates.links.middleware.timezone.now') as mock_now: mock_now.return_value = aware_datetime(2014, 1, 3) eq_(self.middleware.process_request(self.request), None) ok_(self.messages.info.called) message = self.messages.info.call_args[0][1] ok_('5' in message and '9' in message) profile = UserProfile.objects.get(user=self.request.user) eq_(profile.last_visit, aware_date(2014, 1, 3))
def test_link_exists_datapoint_exists(self): """ If a datapoint exists for the given date, increment it's link_clicks value. """ link = LinkFactory.create() datapoint = DataPointFactory.create(link=link, date=date(2014, 1, 1), link_clicks=7) add_click(link.id, date(2014, 1, 1)) datapoint = DataPoint.objects.get(pk=datapoint.pk) eq_(datapoint.link_clicks, 8)
def test_date_argument(self): """ If a date argument is given, parse it as DD-MM-YYYY and use it as the query date. """ link1, link2 = LinkFactory.create_batch(2) self.service.get_clicks_for_date.return_value = { unicode(link1.pk): '4', unicode(link2.pk): '7' } query_date = aware_datetime(2014, 1, 1).date() # Create pre-existing data to check that it is replaced. DataPointFactory.create(link=link1, date=query_date, link_clicks=18) DataPointFactory.create(link=link2, date=query_date, link_clicks=14) self.command.execute('01-01-2014') # There must only be one datapoint for the query date, and the # link_clicks must match the new data. self.service.get_clicks_for_date.assert_called_with(query_date) eq_(link1.datapoint_set.get(date=query_date).link_clicks, 4) eq_(link2.datapoint_set.get(date=query_date).link_clicks, 7)
def test_milestone_date(self): user = UserFactory.create() display = MilestoneDisplay(user) DataPointFactory.create(link_clicks=4, date=aware_date(2014, 1, 1), link__user=user) DataPointFactory.create(link_clicks=3, date=aware_date(2014, 1, 2), link__user=user) DataPointFactory.create(link_clicks=2, date=aware_date(2014, 1, 3), link__user=user) eq_(display.milestone_date('link_clicks', 10, 4), aware_date(2014, 1, 2))
def test_get_metric_total(self): """ _get_metric_total should combine the aggregate data on the link and the data stored in multiple data points. """ link = LinkFactory.create(aggregate_link_clicks=58) DataPointFactory.create(link=link, link_clicks=5, date=aware_date(2014, 1, 1)) DataPointFactory.create(link=link, link_clicks=2, date=aware_date(2014, 1, 2)) DataPointFactory.create(link=link, link_clicks=87, date=aware_date(2014, 1, 3)) eq_(link._get_metric_total('link_clicks'), 58 + 5 + 2 + 87)
def test_manager_total_link_clicks(self): for clicks in (4, 6, 9, 10): # = 29 clicks DataPointFactory.create(link_clicks=clicks, date=date(2014, 4, 26)) for clicks in (25, 5, 5): # = 35 clicks LinkFactory.create(aggregate_link_clicks=clicks) # Create a link with multiple datapoints to test for a faulty # join that would screw up the totals. link = LinkFactory.create() DataPointFactory.create(link_clicks=7, link=link, date=date(2014, 4, 26)) DataPointFactory.create(link_clicks=7, link=link, date=date(2014, 4, 27)) # 29 + 35 + 7 + 7 = 78 clicks eq_(Link.objects.total_link_clicks(), 78)
def test_link_exists_datapoint_exists(self): """ If a datapoint exists for the given date, increment it's link_clicks value. """ link = LinkFactory.create() datapoint = DataPointFactory.create(link=link, date=date(2014, 1, 1), link_clicks=7) call_command('denormalize_metrics') # Ensure denormalized data. add_click(link.id, date(2014, 1, 1)) datapoint = DataPoint.objects.get(pk=datapoint.pk) eq_(datapoint.link_clicks, 8) link = datapoint.link eq_(link.link_clicks, 8) banner = link.banner eq_(banner.link_clicks, 8) category = banner.category eq_(category.link_clicks, 8)
def _datapoint(self, link, clicks): DataPointFactory.create(link_clicks=clicks, link=link, date=self._date) self._date += timedelta(days=1)