Esempio n. 1
0
    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))
Esempio n. 2
0
    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())
Esempio n. 3
0
    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))
Esempio n. 4
0
    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))
Esempio n. 5
0
    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)
Esempio n. 6
0
    def test_process_request_no_clicks_or_downloads(self):
        """
        If there were no clicks or downloads since the user's last
        visit, update their last_visit date and do not log a message.
        """
        self.request.user = UserFactory.create(userprofile__last_visit=aware_date(2014, 1, 1))

        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_(not self.messages.info.called)

        profile = UserProfile.objects.get(user=self.request.user)
        eq_(profile.last_visit, aware_date(2014, 1, 3))
Esempio n. 7
0
    def test_process_request_no_clicks_or_downloads(self):
        """
        If there were no clicks or downloads since the user's last
        visit, update their last_visit date and do not log a message.
        """
        self.request.user = UserFactory.create(
            userprofile__last_visit=aware_date(2014, 1, 1))

        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_(not self.messages.info.called)

        profile = UserProfile.objects.get(user=self.request.user)
        eq_(profile.last_visit, aware_date(2014, 1, 3))
Esempio n. 8
0
    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())
Esempio n. 9
0
    def test_default_yesterday(self):
        """When no date is given, fetch data for the two days ago."""
        link1, link2 = LinkFactory.create_batch(2)
        self.service.get_clicks_for_date.return_value = {
            unicode(link1.pk): '4',
            unicode(link2.pk): '7'
        }
        two_days_ago = aware_date(2014, 1, 1)

        with patch.object(collect_ga_data, 'timezone') as mock_timezone:
            mock_timezone.now.return_value = aware_date(2014, 1, 3)
            self.command.execute()

        self.service.get_clicks_for_date.assert_called_with(two_days_ago)
        eq_(link1.datapoint_set.get(date=two_days_ago).link_clicks, 4)
        eq_(link2.datapoint_set.get(date=two_days_ago).link_clicks, 7)
Esempio n. 10
0
    def test_default_yesterday(self):
        """When no date is given, fetch data for the two days ago."""
        link1, link2 = LinkFactory.create_batch(2)
        self.service.get_clicks_for_date.return_value = {
            unicode(link1.pk): '4',
            unicode(link2.pk): '7'
        }
        two_days_ago = aware_date(2014, 1, 1)

        with patch.object(collect_ga_data, 'timezone') as mock_timezone:
            mock_timezone.now.return_value = aware_date(2014, 1, 3)
            self.command.execute()

        self.service.get_clicks_for_date.assert_called_with(two_days_ago)
        eq_(link1.datapoint_set.get(date=two_days_ago).link_clicks, 4)
        eq_(link2.datapoint_set.get(date=two_days_ago).link_clicks, 7)
Esempio n. 11
0
    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))
Esempio n. 12
0
 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)
Esempio n. 13
0
 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)
Esempio n. 14
0
    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)
Esempio n. 15
0
    def test_metric_milestone_have_date_of_last_milestone(self):
        """
        If the aggregate total is less than the previous milestone
        (meaning we know the date of the last milestone), show the date
        the last milestone was achieved with the achieved_milestone
        message.
        """
        # (aggregate == 4) + (prev_milestone == 5) = Last milestone
        # happened within datapoint range and we have a date.
        # milestone_date returns the date when it happened.
        self.user.metric_aggregate_total = Mock(return_value=4)
        self.user.metric_total = Mock(return_value=18)
        self.display.surrounding_milestones.return_value = (5, 20)
        self.display.close_to_milestone.return_value = False
        self.display.milestone_date.return_value = aware_date(2014, 1, 1)

        milestone = self.display.metric_milestone('metric', self.messages)
        eq_(milestone, (aware_date(2014, 1, 1), 'achieved_milestone_5'))

        self.display.milestone_date.assert_called_with('metric', 5, 4)
Esempio n. 16
0
    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)
Esempio n. 17
0
    def test_metric_milestone_have_date_of_last_milestone(self):
        """
        If the aggregate total is less than the previous milestone
        (meaning we know the date of the last milestone), show the date
        the last milestone was achieved with the achieved_milestone
        message.
        """
        # (aggregate == 4) + (prev_milestone == 5) = Last milestone
        # happened within datapoint range and we have a date.
        # milestone_date returns the date when it happened.
        self.user.metric_aggregate_total = Mock(return_value=4)
        self.user.metric_total = Mock(return_value=18)
        self.display.surrounding_milestones.return_value = (5, 20)
        self.display.close_to_milestone.return_value = False
        self.display.milestone_date.return_value = aware_date(2014, 1, 1)

        milestone = self.display.metric_milestone('metric', self.messages)
        eq_(milestone, (aware_date(2014, 1, 1), 'achieved_milestone_5'))

        self.display.milestone_date.assert_called_with('metric', 5, 4)
Esempio n. 18
0
    def test_process_request_less_than_one_day(self):
        """
        If it has been less than one day since the user's last visit,
        return None and do not log a message.
        """
        self.request.user = UserFactory.create(userprofile__last_visit=aware_date(2014, 1, 1))

        with patch("affiliates.links.middleware.timezone.now") as mock_now:
            mock_now.return_value = aware_datetime(2014, 1, 1)
            eq_(self.middleware.process_request(self.request), None)
            ok_(not self.messages.info.called)
Esempio n. 19
0
    def test_process_request_less_than_one_day(self):
        """
        If it has been less than one day since the user's last visit,
        return None and do not log a message.
        """
        self.request.user = UserFactory.create(
            userprofile__last_visit=aware_date(2014, 1, 1))

        with patch('affiliates.links.middleware.timezone.now') as mock_now:
            mock_now.return_value = aware_datetime(2014, 1, 1)
            eq_(self.middleware.process_request(self.request), None)
            ok_(not self.messages.info.called)
Esempio n. 20
0
    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)
Esempio n. 21
0
    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)
Esempio n. 22
0
    def test_process_request_no_last_visit(self):
        """
        If we haven't logged a last visit for the user, set their
        last_visit and return None and do not log a message.
        """
        self.request.user = UserFactory.create(userprofile__last_visit=None)

        with patch('affiliates.links.middleware.timezone.now') as mock_now:
            mock_now.return_value = aware_datetime(2014, 1, 1)
            eq_(self.middleware.process_request(self.request), None)
            ok_(not self.messages.info.called)

        profile = UserProfile.objects.get(user=self.request.user)
        eq_(profile.last_visit, aware_date(2014, 1, 1))
Esempio n. 23
0
    def test_process_request_no_last_visit(self):
        """
        If we haven't logged a last visit for the user, set their
        last_visit and return None and do not log a message.
        """
        self.request.user = UserFactory.create(userprofile__last_visit=None)

        with patch("affiliates.links.middleware.timezone.now") as mock_now:
            mock_now.return_value = aware_datetime(2014, 1, 1)
            eq_(self.middleware.process_request(self.request), None)
            ok_(not self.messages.info.called)

        profile = UserProfile.objects.get(user=self.request.user)
        eq_(profile.last_visit, aware_date(2014, 1, 1))
Esempio n. 24
0
    def test_creation_milestone_no_previous_links_created(self):
        """
        If there is no previous milestone, but the user has created at
        least one link (which is normally impossible, as the default
        milestones start at 1), show when their last link was created.
        """
        links = LinkFactory.create_batch(2, banner_type='test', user=self.user)
        links[0].created = aware_datetime(2014, 1, 1, 5)
        links[1].created = aware_datetime(2014, 1, 2, 8)
        for link in links:
            link.save()

        self.display.surrounding_milestones.return_value = (None, 5)

        milestone = self.display.creation_milestone('test', self.messages)
        eq_(milestone, (aware_date(2014, 1, 2), 'link_created'))
Esempio n. 25
0
    def test_creation_milestone_no_next_milestone(self):
        """
        If there is no next milestone, show the date of their last
        milestone.
        """
        links = LinkFactory.create_batch(4, banner_type='test', user=self.user)
        links[0].created = aware_datetime(2014, 1, 1, 5)
        links[1].created = aware_datetime(2014, 1, 1, 8)
        links[2].created = aware_datetime(2014, 1, 2, 5)  # Winner!
        links[3].created = aware_datetime(2014, 1, 3, 5)
        for link in links:
            link.save()

        self.display.surrounding_milestones.return_value = (3, None)

        milestone = self.display.creation_milestone('test', self.messages)
        eq_(milestone, (aware_date(2014, 1, 2), 'achieved_milestone_3'))
Esempio n. 26
0
    def test_creation_milestone_far_from_next_milestone(self):
        """
        If the user isn't close to the next milestone, show the date of
        their last milestone.
        """
        variation = TextBannerVariationFactory.create()
        links = LinkFactory.create_batch(4, banner_variation=variation, user=self.user)
        links[0].created = aware_datetime(2014, 1, 1, 5)
        links[1].created = aware_datetime(2014, 1, 1, 8)
        links[2].created = aware_datetime(2014, 1, 2, 5)  # Winner!
        links[3].created = aware_datetime(2014, 1, 3, 5)
        for link in links:
            link.save()

        self.display.surrounding_milestones.return_value = (3, 10)

        milestone = self.display.creation_milestone(TextBanner, self.messages)
        eq_(milestone, (aware_date(2014, 1, 2), 'achieved_milestone_3'))
Esempio n. 27
0
    def test_creation_milestone_no_previous_links_created(self):
        """
        If there is no previous milestone, but the user has created at
        least one link (which is normally impossible, as the default
        milestones start at 1), show when their last link was created.
        """
        variation = TextBannerVariationFactory.create()
        links = LinkFactory.create_batch(2,
                                         banner_variation=variation,
                                         user=self.user)
        links[0].created = aware_datetime(2014, 1, 1, 5)
        links[1].created = aware_datetime(2014, 1, 2, 8)
        for link in links:
            link.save()

        self.display.surrounding_milestones.return_value = (None, 5)

        milestone = self.display.creation_milestone(TextBanner, self.messages)
        eq_(milestone, (aware_date(2014, 1, 2), 'link_created'))
Esempio n. 28
0
    def test_creation_milestone_no_next_milestone(self):
        """
        If there is no next milestone, show the date of their last
        milestone.
        """
        variation = TextBannerVariationFactory.create()
        links = LinkFactory.create_batch(4,
                                         banner_variation=variation,
                                         user=self.user)
        links[0].created = aware_datetime(2014, 1, 1, 5)
        links[1].created = aware_datetime(2014, 1, 1, 8)
        links[2].created = aware_datetime(2014, 1, 2, 5)  # Winner!
        links[3].created = aware_datetime(2014, 1, 3, 5)
        for link in links:
            link.save()

        self.display.surrounding_milestones.return_value = (3, None)

        milestone = self.display.creation_milestone(TextBanner, self.messages)
        eq_(milestone, (aware_date(2014, 1, 2), 'achieved_milestone_3'))
Esempio n. 29
0
def _milestone(*args, **kwargs):
    return aware_date(*args, **kwargs), 'milestone'
Esempio n. 30
0
def _milestone(*args, **kwargs):
    return aware_date(*args, **kwargs), 'milestone'