

@patch.object(kitsune.kpi.cron, '_get_top_docs')
@patch.object(googleanalytics, 'visitors_by_locale')
def test_update_l10n_metric_cron(self, visitors_by_locale, _get_top_docs):
    'Verify the cron job creates the correct metric.'
    l10n_kind = MetricKindFactory(code=L10N_METRIC_CODE)
    doc = DocumentFactory()
    rev = ApprovedRevisionFactory(document=doc, significance=MEDIUM_SIGNIFICANCE, is_ready_for_localization=True)
    es_doc = DocumentFactory(parent=doc, locale='es')
    ApprovedRevisionFactory(document=es_doc, based_on=rev)
    DocumentFactory(parent=doc, locale='de')
    visitors_by_locale.return_value = {
        'en-US': 50,
        'de': 20,
        'es': 25,
        'fr': 5,
    }
    _get_top_docs.return_value = [doc]
    update_l10n_metric()
    metrics = Metric.objects.filter(kind=l10n_kind)
    eq_(1, len(metrics))
    eq_(75, metrics[0].value)
    ApprovedRevisionFactory(document=doc, significance=TYPO_SIGNIFICANCE, is_ready_for_localization=True)
    Metric.objects.all().delete()
    update_l10n_metric()
    metrics = Metric.objects.filter(kind=l10n_kind)
    eq_(1, len(metrics))
    eq_(75, metrics[0].value)
    m1 = ApprovedRevisionFactory(document=doc, significance=MEDIUM_SIGNIFICANCE, is_ready_for_localization=True)
    Metric.objects.all().delete()
    update_l10n_metric()
    metrics = Metric.objects.filter(kind=l10n_kind)
    eq_(1, len(metrics))
    eq_(62, metrics[0].value)
    m2 = ApprovedRevisionFactory(document=doc, significance=MEDIUM_SIGNIFICANCE, is_ready_for_localization=True)
    Metric.objects.all().delete()
    update_l10n_metric()
    metrics = Metric.objects.filter(kind=l10n_kind)
    eq_(1, len(metrics))
    eq_(50, metrics[0].value)
    m1.delete()
    m2.delete()
    ApprovedRevisionFactory(document=doc, significance=MAJOR_SIGNIFICANCE, is_ready_for_localization=True)
    Metric.objects.all().delete()
    update_l10n_metric()
    metrics = Metric.objects.filter(kind=l10n_kind)
    eq_(1, len(metrics))
    eq_(50, metrics[0].value)

dataset/ETHPy150Open mozilla/kitsune/kitsune/kpi/tests/test_cron.py CronJobTests.test_update_l10n_metric_cron/original