def setUp(self): (self.mongo, self.col) = create_mock_for_db() self.service = SaveReportStatsService(self.mongo) self.service._now = MockDatetime() self.empty_report_request = {'name': 'report', 'keywords': []} self.expected_stats_report = {'name': 'report', 'stats': [ {'date': '2014-1-1', 'threads': '1', 'msgs': '1', 'blogs': '0'}]}
class TestSaveReportStatsService(unittest.TestCase): def create_report_stat(self): report_stats = ReportStats() report_stats.inc_msgs() report_stats.inc_threads() return report_stats def setUp(self): (self.mongo, self.col) = create_mock_for_db() self.service = SaveReportStatsService(self.mongo) self.service._now = MockDatetime() self.empty_report_request = {'name': 'report', 'keywords': []} self.expected_stats_report = {'name': 'report', 'stats': [ {'date': '2014-1-1', 'threads': '1', 'msgs': '1', 'blogs': '0'}]} def test_find_stat_with_date_no_delete(self): stats = {u'stats': [{u'date': u'2014-3-22', u'blogs': u'0', u'threads': u'0', u'msgs': u'0'}], u'name': u'HootBoardGame'} stats_len = 1 self.service._delete_with_date_now(stats) self.assertEqual(len(stats['stats']), stats_len) def test_find_stat_with_date_delete(self): stats = {u'stats': [{u'date': u'2014-1-1', u'blogs': u'0', u'threads': u'0', u'msgs': u'0'}], u'name': u'HootBoardGame'} self.service._delete_with_date_now(stats) self.assertEqual(len(stats['stats']), 0) def test_when_stats_for_a_report_does_not_exist_then_create_it(self): when(self.col).find_one('name', 'report').thenReturn(None) self.service._save_report_stats(self.empty_report_request['name'], self.create_report_stat()) verify(self.col).save(self.expected_stats_report) def test_saving_report_stats(self): when(self.col).find_one('name', 'report').thenReturn({'name': 'report', 'stats': []}) self.service._save_report_stats('report', self.create_report_stat()) verify(self.col).save(self.expected_stats_report)