Пример #1
0
class AccountStatsTestCase(MongoTestCase):

    def setUp(self):

        from analytics.models import AccountAnalytics, OverallAnalytics
        from accounts.tests import setUpAccounts
        setUpAccounts(self)

        account = self.bob
        account2 = self.alice
        self.analytics = AccountAnalytics(account, redis_db=15)
        self.analytics2 = AccountAnalytics(account2, redis_db=15)
        self.overall_analytics = OverallAnalytics(redis_db=15)
        self.redis = self.analytics.conn
        self.redis.flushdb()

    def test_tag(self):
        # what tags are being searched for

        from datetime import datetime

        # Add 30
        for i in range(1, 31):
            dt = datetime(2011, 11, i)
            self.analytics.increment_tags("Sport and fitness",
                date_instance=dt, count=i)

        # Add 25
        for i in range(1, 26):
            dt = datetime(2011, 11, i)
            self.analytics.increment_tags("Mental Health",
                date_instance=dt, count=i)

        # Add 15
        for i in range(11, 26):
            dt = datetime(2011, 11, i)
            self.analytics.increment_tags("Hobbies, arts and crafts",
                date_instance=dt, count=i)

        start_date = datetime(2011, 11, 1)
        end_date = datetime(2011, 11, 30)

        result = self.analytics.top_tags(start_date, end_date)

        # Create a report for the full month, should contain everything we
        # added
        expected_result = [
            ('Sport and fitness', 30),
            ('Mental Health', 25),
            ('Hobbies, arts and crafts', 15),
        ]

        self.assertEqual(result, expected_result)

        # Create another report, but only for one day - should only contain
        # the expected result below.
        start_date = datetime(2011, 11, 1)
        end_date = datetime(2011, 11, 1)

        result = self.analytics.top_tags(start_date, end_date)

        expected_result = [
            ('Mental Health', 1),
            ('Sport and fitness', 1),
        ]

        self.assertEqual(result, expected_result)

    def test_overall_analytics(self):
        """
        Incremement the tag stat counter for two different accounts. Check the
        values are right in the summary for those accounts and then check that
        the overall stats are correctly the sum of the accounts.
        """

        from string import uppercase
        from datetime import datetime

        dt = datetime(2011, 11, 20)

        # Increment A-K for both
        for i in range(0, 11):
            self.analytics.increment_tags(uppercase[i], date_instance=dt, count=i)
            self.analytics2.increment_tags(uppercase[i], date_instance=dt, count=i)

        # Increment A-F again for account 1
        for i in range(0, 6):
            self.analytics.increment_tags(uppercase[i], date_instance=dt, count=i)

        # Increment G-K again for account 2
        for i in range(6, 11):
            self.analytics2.increment_tags(uppercase[i], date_instance=dt, count=i)

        account1_result = self.analytics.top_tags(dt, dt)
        account2_result = self.analytics2.top_tags(dt, dt)
        overall_result = self.overall_analytics.top_tags(dt, dt)

        account1_expected = [('A', 2), ('B', 2), ('C', 2), ('D', 2), ('E', 2),
            ('F', 2), ('G', 1), ('H', 1), ('I', 1), ('J', 1), ('K', 1), ]
        account2_expected = [('G', 2), ('H', 2), ('I', 2), ('J', 2), ('K', 2),
            ('A', 1), ('B', 1), ('C', 1), ('D', 1), ('E', 1), ('F', 1), ]
        overall_expected = [('A', 3), ('B', 3), ('C', 3), ('D', 3), ('E', 3),
            ('F', 3), ('G', 3), ('H', 3), ('I', 3), ('J', 3), ('K', 3), ]

        self.assertEqual(account1_result, account1_expected)
        self.assertEqual(account2_result, account2_expected)
        self.assertEqual(overall_expected, overall_result)
Пример #2
0
class AccountStatsTestCase(MongoTestCase):
    def setUp(self):

        from analytics.models import AccountAnalytics, OverallAnalytics
        from accounts.tests import setUpAccounts
        setUpAccounts(self)

        account = self.bob
        account2 = self.alice
        self.analytics = AccountAnalytics(account, redis_db=15)
        self.analytics2 = AccountAnalytics(account2, redis_db=15)
        self.overall_analytics = OverallAnalytics(redis_db=15)
        self.redis = self.analytics.conn
        self.redis.flushdb()

    def test_tag(self):
        # what tags are being searched for

        from datetime import datetime

        # Add 30
        for i in range(1, 31):
            dt = datetime(2011, 11, i)
            self.analytics.increment_tags("Sport and fitness",
                                          date_instance=dt,
                                          count=i)

        # Add 25
        for i in range(1, 26):
            dt = datetime(2011, 11, i)
            self.analytics.increment_tags("Mental Health",
                                          date_instance=dt,
                                          count=i)

        # Add 15
        for i in range(11, 26):
            dt = datetime(2011, 11, i)
            self.analytics.increment_tags("Hobbies, arts and crafts",
                                          date_instance=dt,
                                          count=i)

        start_date = datetime(2011, 11, 1)
        end_date = datetime(2011, 11, 30)

        result = self.analytics.top_tags(start_date, end_date)

        # Create a report for the full month, should contain everything we
        # added
        expected_result = [
            ('Sport and fitness', 30),
            ('Mental Health', 25),
            ('Hobbies, arts and crafts', 15),
        ]

        self.assertEqual(result, expected_result)

        # Create another report, but only for one day - should only contain
        # the expected result below.
        start_date = datetime(2011, 11, 1)
        end_date = datetime(2011, 11, 1)

        result = self.analytics.top_tags(start_date, end_date)

        expected_result = [
            ('Mental Health', 1),
            ('Sport and fitness', 1),
        ]

        self.assertEqual(result, expected_result)

    def test_overall_analytics(self):
        """
        Incremement the tag stat counter for two different accounts. Check the
        values are right in the summary for those accounts and then check that
        the overall stats are correctly the sum of the accounts.
        """

        from string import uppercase
        from datetime import datetime

        dt = datetime(2011, 11, 20)

        # Increment A-K for both
        for i in range(0, 11):
            self.analytics.increment_tags(uppercase[i],
                                          date_instance=dt,
                                          count=i)
            self.analytics2.increment_tags(uppercase[i],
                                           date_instance=dt,
                                           count=i)

        # Increment A-F again for account 1
        for i in range(0, 6):
            self.analytics.increment_tags(uppercase[i],
                                          date_instance=dt,
                                          count=i)

        # Increment G-K again for account 2
        for i in range(6, 11):
            self.analytics2.increment_tags(uppercase[i],
                                           date_instance=dt,
                                           count=i)

        account1_result = self.analytics.top_tags(dt, dt)
        account2_result = self.analytics2.top_tags(dt, dt)
        overall_result = self.overall_analytics.top_tags(dt, dt)

        account1_expected = [
            ('A', 2),
            ('B', 2),
            ('C', 2),
            ('D', 2),
            ('E', 2),
            ('F', 2),
            ('G', 1),
            ('H', 1),
            ('I', 1),
            ('J', 1),
            ('K', 1),
        ]
        account2_expected = [
            ('G', 2),
            ('H', 2),
            ('I', 2),
            ('J', 2),
            ('K', 2),
            ('A', 1),
            ('B', 1),
            ('C', 1),
            ('D', 1),
            ('E', 1),
            ('F', 1),
        ]
        overall_expected = [
            ('A', 3),
            ('B', 3),
            ('C', 3),
            ('D', 3),
            ('E', 3),
            ('F', 3),
            ('G', 3),
            ('H', 3),
            ('I', 3),
            ('J', 3),
            ('K', 3),
        ]

        self.assertEqual(account1_result, account1_expected)
        self.assertEqual(account2_result, account2_expected)
        self.assertEqual(overall_expected, overall_result)