def test_add_all_resource_filter_mixed(self):
     date1 = datetime(2019, 1, 1)
     resource_counts1 = {
         u'resource1': 10002,
         u'cresource2': 51,
     }
     date2 = datetime(2020, 1, 1)
     resource_counts2 = {
         u'resource1': 29,
         u'cresource2': 38290,
     }
     monthly_stats = MonthlyStats(resource_id=u'resource1')
     monthly_stats.add_all(date1, resource_counts1)
     monthly_stats.add_all(date2, resource_counts2)
     assert monthly_stats.stats[u'1/2019'][u'research'][u'records'] == 10002
     assert monthly_stats.stats[u'1/2019'][u'research'][
         u'download_events'] == 1
     assert monthly_stats.stats[u'1/2019'][u'collections'][u'records'] == 0
     assert monthly_stats.stats[u'1/2019'][u'collections'][
         u'download_events'] == 0
     assert monthly_stats.stats[u'1/2020'][u'research'][u'records'] == 29
     assert monthly_stats.stats[u'1/2020'][u'research'][
         u'download_events'] == 1
     assert monthly_stats.stats[u'1/2020'][u'collections'][u'records'] == 0
     assert monthly_stats.stats[u'1/2020'][u'collections'][
         u'download_events'] == 0
 def test_update_from_gbif(self):
     monthly_stats = MonthlyStats()
     monthly_stats.update_from_gbif(10, 2018, 83, 4)
     monthly_stats.update_from_gbif(10, 2018, 18, 5)
     assert monthly_stats.stats[u'10/2018'][u'gbif'][
         u'download_events'] == 4 + 5
     assert monthly_stats.stats[u'10/2018'][u'gbif'][u'records'] == 83 + 18
 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_update_from_gbif_with_filters(self):
     monthly_stats = MonthlyStats(month=10, year=2018)
     monthly_stats.update_from_gbif(10, 2019, 2389, 223)
     monthly_stats.update_from_gbif(4, 2012, 100, 28)
     monthly_stats.update_from_gbif(10, 2018, 8344, 40)
     monthly_stats.update_from_gbif(10, 2018, 40, 1)
     assert monthly_stats.stats[u'10/2018'][u'gbif'][
         u'download_events'] == 40 + 1
     assert monthly_stats.stats[u'10/2018'][u'gbif'][
         u'records'] == 8344 + 40
     assert u'10/2019' not in monthly_stats.stats
     assert u'4/2012' not in monthly_stats.stats
 def test_month_year_format(self):
     # currently the month/year keys are not zero padded so we might as well test that that is
     # still the case in case a developer gets refactor happy and changes it (I did this so maybe
     # this test is for me...)
     monthly_stats = MonthlyStats()
     for month in range(1, 12):
         date = datetime(2017, month, 1)
         monthly_stats.add_all(date, resource_counts=dict(resource1=10))
         month_year = u'{}/2017'.format(month)
         assert monthly_stats.stats[month_year][u'research'][
             u'records'] == 10
         assert monthly_stats.stats[month_year][u'research'][
             u'download_events'] == 1
 def test_add_all_no_filters_mixed(self):
     date = datetime(2020, 1, 1)
     resource_counts = {
         u'resource1': 10002,
         u'cresource2': 51,
     }
     monthly_stats = MonthlyStats()
     monthly_stats.add_all(date, resource_counts)
     assert monthly_stats.stats[u'1/2020'][u'collections'][u'records'] == 51
     assert monthly_stats.stats[u'1/2020'][u'collections'][
         u'download_events'] == 1
     assert monthly_stats.stats[u'1/2020'][u'research'][u'records'] == 10002
     assert monthly_stats.stats[u'1/2020'][u'research'][
         u'download_events'] == 1
 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_all_complex_filter_mixed(self):
     date = datetime(2019, 6, 1)
     resource_counts = {
         u'resource1': 10,
         u'cresource2': 51,
         u'resource2': 19,
     }
     monthly_stats = MonthlyStats(month=6,
                                  year=2019,
                                  resource_id=u'resource2')
     monthly_stats.add_all(date, resource_counts)
     assert monthly_stats.stats[u'6/2019'][u'research'][u'records'] == 0
     assert monthly_stats.stats[u'6/2019'][u'research'][
         u'download_events'] == 0
     assert monthly_stats.stats[u'6/2019'][u'collections'][u'records'] == 19
     assert monthly_stats.stats[u'6/2019'][u'collections'][
         u'download_events'] == 1
 def test_update_from_backfill_no_previous_data(self):
     monthly_stats = MonthlyStats()
     monthly_stats.update_from_backfill(
         u'10', u'2018', {
             u'collections': {
                 u'download_events': 102,
                 u'records': 1029
             },
             u'research': {
                 u'download_events': 4,
                 u'records': 902832
             }
         })
     assert monthly_stats.stats[u'10/2018'][u'collections'][
         u'download_events'] == 102
     assert monthly_stats.stats[u'10/2018'][u'collections'][
         u'records'] == 1029
     assert monthly_stats.stats[u'10/2018'][u'research'][
         u'download_events'] == 4
     assert monthly_stats.stats[u'10/2018'][u'research'][
         u'records'] == 902832
 def test_update_from_backfill_with_filters(self):
     monthly_stats = MonthlyStats(month=10, year=2018)
     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,
             }
         })
     monthly_stats.update_from_backfill(
         u'11', u'2018', {
             u'collections': {
                 u'download_events': 3,
                 u'records': 10004,
             },
             u'research': {
                 u'download_events': 5829,
                 u'records': 32894932,
             }
         })
     monthly_stats.update_from_backfill(
         u'11', u'2017', {
             u'collections': {
                 u'download_events': 3,
                 u'records': 10004,
             },
             u'research': {
                 u'download_events': 5829,
                 u'records': 32894932,
             }
         })
     assert u'10/2018' in monthly_stats.stats
     assert u'11/2018' not in monthly_stats.stats
     assert u'11/2017' not in monthly_stats.stats
    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'
        ]