def test_update_from_backfill_with_previous_data(self):
     monthly_stats = MonthlyStats()
     # this is a collection resource
     monthly_stats.add(datetime(2018, 10, 1), u'resource1', 20)
     # this isn't a collection resource
     monthly_stats.add(datetime(2018, 10, 1), u'resource2', 15)
     # update from the backfill
     monthly_stats.update_from_backfill(
         u'10', u'2018', {
             u'collections': {
                 u'download_events': 3,
                 u'records': 10004,
             },
             u'research': {
                 u'download_events': 5829,
                 u'records': 32894932,
             }
         })
     assert monthly_stats.stats[u'10/2018'][u'collections'][
         u'download_events'] == 3 + 1
     assert monthly_stats.stats[u'10/2018'][u'collections'][
         u'records'] == 10004 + 20
     assert monthly_stats.stats[u'10/2018'][u'research'][
         u'download_events'] == 5829 + 1
     assert monthly_stats.stats[u'10/2018'][u'research'][
         u'records'] == 32894932 + 15
 def test_add(self):
     # check that add just calls add_all
     monthly_stats = MonthlyStats()
     monthly_stats.add_all = MagicMock()
     date = MagicMock()
     resource_id = MagicMock()
     count = MagicMock()
     monthly_stats.add(date, resource_id, count)
     assert monthly_stats.add_all.called
     assert monthly_stats.add_all.call_args == call(date,
                                                    {resource_id: count})
    def test_as_dict(self):
        monthly_stats = MonthlyStats()
        monthly_stats.add(datetime(2017, 4, 1), u'resource1', 29)
        monthly_stats.add(datetime(2017, 4, 20), u'resource1', 39)
        monthly_stats.add(datetime(2013, 6, 18), u'resource2', 1000)
        monthly_stats.add(datetime(2013, 1, 8), u'resource5', 55)
        monthly_stats.add(datetime(2013, 11, 4), u'resource2', 1000)
        monthly_stats.add(datetime(2020, 10, 10), u'resource4', 92)

        stats = monthly_stats.as_dict()
        assert isinstance(stats, OrderedDict)
        assert list(stats.keys()) == [
            u'1/2013', u'6/2013', u'11/2013', u'4/2017', u'10/2020'
        ]