示例#1
0
    def test_import_hw_monthly_stats(self):
        self.assertEqual(0, models.Article.objects.count())
        day_to_import = datetime(year=2015, month=8, day=11)
        logic.import_hw_metrics('monthly', from_date=day_to_import, to_date=day_to_import)
        # 2015-11-20, HW stats can't be trusted. I don't know why there are suddenly fewer
        # articles on this day now that we have more data...
        #expected_article_count = 1631

        #expected_article_count = 1603
        # 2016-02-16: aaaaand we're back to 1631
        expected_article_count = 1631

        self.assertEqual(expected_article_count, models.Article.objects.count())

        doi = '10.7554/eLife.02993'
        expected_data = {
            'abstract': 2,
            'date': '2015-08',
            'full': 26,
            'pdf': 18,
            'period': 'month',
        }
        metric = models.Metric.objects.get(article__doi=doi, period='month', date='2015-08')
        for attr, val in expected_data.items():
            self.assertEqual(expected_data[attr], getattr(metric, attr))
示例#2
0
    def test_import_hw_daily_stats(self):
        self.assertEqual(0, models.Article.objects.count())
        day_to_import = datetime(year=2015, month=8, day=11)
        logic.import_hw_metrics('daily', from_date=day_to_import, to_date=day_to_import)

        # 2015-11-20, new article present after supporting multiple datasets in elife-hw-metrics
        #expected_article_count = 11
        expected_article_count = 12 
        self.assertEqual(expected_article_count, models.Article.objects.count())

        doi = '10.7554/eLife.02993'
        expected_data = {
            'abstract': 0,
            'date': '2015-08-11',
            'full': 1,
            'pdf': 2,
            'period': 'day',
        }
        metric = models.Metric.objects.get(article__doi=doi, period='day', date='2015-08-11')
        for attr, val in expected_data.items():
            self.assertEqual(expected_data[attr], getattr(metric, attr))
示例#3
0
    def test_mixed_source_data(self):
        "data from multiple sources is served up correctly"        
        from_date = datetime(year=2015, month=9, day=11)
        to_date = from_date + timedelta(days=1)
        logic.import_ga_metrics('daily', from_date, to_date)
        logic.import_hw_metrics('daily', from_date, to_date)
        doi = '10.7554/eLife.09560'


        # hack. 
        yesterday = ymd(datetime.now() - timedelta(days=1))
        day_before = ymd(datetime.now() - timedelta(days=2))
        m1, m2 = models.Metric.objects.filter(article__doi=doi, source=models.GA)
        m1.date = day_before
        m2.date = yesterday
        m1.save()
        m2.save()

        m1, m2 = models.Metric.objects.filter(article__doi=doi, source=models.HW)
        m1.date = day_before
        m2.date = yesterday
        m1.save()
        m2.save()
        
        expected_data = {
            models.GA: {
                doi: {
                    'daily': OrderedDict([
                        (day_before, {
                            'full': 21922,
                            'abstract': 325,
                            'digest': 114,
                            'pdf': 1533,
                        }),
                        (yesterday, { 
                            'full': 9528,
                            'abstract': 110,
                            'digest': 42,
                            'pdf': 489,
                        })
                    ]),
                    'monthly': OrderedDict({}),
                },
            },
            models.HW: {
                doi: {
                    'daily': OrderedDict([
                        (day_before, {
                            'full': 39912,
                            'abstract': 540,
                            'digest': 0,
                            'pdf': 4226,
                        }),
                        (yesterday, {
                            'full': 15800,
                            'abstract': 144,
                            'digest': 0,
                            'pdf': 1132,
                        }),
                    ]),
                    'monthly': OrderedDict({}),
                },
            },
        }
        url = reverse('api-article-metrics-mixed-source', kwargs={'doi': doi})
        resp = self.c.get(url)
        self.assertEqual(200, resp.status_code)
        self.assertEqual(expected_data, resp.data)