addons_daily/tests/test_telemetry.py

491 строка
15 KiB
Python
Исходник Обычный вид История

2019-05-31 02:18:56 +03:00
from addons_daily.utils.telemetry_data import *
from addons_daily.addons_report import expand_addons
from addons_daily.utils.helpers import is_same
from pyspark.sql.types import *
from pyspark.sql import Row
import pytest
import json
import os
BASE_DATE = "20190515"
def df_to_json(df):
return [i.asDict() for i in df.collect()]
2019-06-04 20:32:25 +03:00
def load_test_data(prefix, spark):
2019-05-31 02:18:56 +03:00
root = os.path.dirname(__file__)
2019-06-04 20:32:25 +03:00
schema_path = os.path.join(root, "resources", "{}_schema.json".format(prefix))
2019-05-31 02:18:56 +03:00
with open(schema_path) as f:
d = json.load(f)
schema = StructType.fromJson(d)
2019-06-04 20:32:25 +03:00
rows_path = os.path.join(root, "resources", "{}.json".format(prefix))
2019-05-31 02:18:56 +03:00
# FAILFAST causes us to abort early if the data doesn't match
# the given schema. Without this there was as very annoying
# problem where dataframe.collect() would return an empty set.
frame = spark.read.json(rows_path, schema, mode="FAILFAST")
return frame
2019-06-04 20:32:25 +03:00
@pytest.fixture()
def spark():
spark_session = SparkSession.builder.appName("addons_daily_tests").getOrCreate()
return spark_session
@pytest.fixture()
def main_summary(spark):
return load_test_data("main_summary", spark)
@pytest.fixture()
def events(spark):
return load_test_data("events", spark)
2019-05-31 02:18:56 +03:00
@pytest.fixture()
def addons_expanded(main_summary):
return expand_addons(main_summary)
2019-05-31 21:08:46 +03:00
@pytest.fixture()
def main_summary_day(main_summary):
return main_summary.filter("submission_date_s3 = '{}'".format(BASE_DATE))
2019-05-31 02:18:56 +03:00
@pytest.fixture()
def addons_expanded_day(addons_expanded):
return addons_expanded.filter("submission_date_s3 = '{}'".format(BASE_DATE))
2019-06-01 00:21:35 +03:00
def test_addon_names(addons_expanded_day, spark):
output = df_to_json(get_top_addon_names(addons_expanded_day))
expected = [
{
"addon_id": u"baidu-code-update@mozillaonline.com",
"name": u"Baidu Search Update",
},
{"addon_id": u"screenshots@mozilla.org", "name": u"Firefox Screenshots"},
{"addon_id": u"non-system-addon1", "name": u"Non systemn Addon 1"},
{
"addon_id": u"hotfix-update-xpi-intermediate@mozilla.com",
"name": u"hotfix-update-xpi-intermediate",
},
{"addon_id": u"fxmonitor@mozilla.org", "name": u"Firefox Monitor"},
{"addon_id": u"non-system-addon2", "name": u"Non System Addon 2"},
{"addon_id": u"formautofill@mozilla.org", "name": u"Form Autofill"},
{"addon_id": u"webcompat@mozilla.org", "name": u"Web Compat"},
]
assert output == expected
2019-05-31 21:08:46 +03:00
def test_browser_metrics(addons_expanded_day, spark):
2019-05-31 02:18:56 +03:00
"""
Given a dataframe of some actual sampled data, ensure that
the get_pct_tracking_enabled outputs the correct dataframe
:param addons_expanded: pytest fixture defined above
:return: assertion whether the expected output indeed matches the true output
"""
2019-05-31 21:08:46 +03:00
output = df_to_json(get_browser_metrics(addons_expanded_day))
expected = [
{
"addon_id": u"baidu-code-update@mozillaonline.com",
"avg_bookmarks": None,
"avg_tabs": None,
"avg_toolbox_opened_count": None,
"avg_uri": 33.0,
"pct_w_tracking_prot_enabled": 0.0,
},
{
"addon_id": u"screenshots@mozilla.org",
"avg_bookmarks": None,
"avg_tabs": None,
"avg_toolbox_opened_count": None,
"avg_uri": 33.0,
"pct_w_tracking_prot_enabled": 0.0,
},
2019-06-01 00:21:35 +03:00
{
"addon_id": u"non-system-addon1",
"avg_bookmarks": None,
"avg_tabs": None,
"avg_toolbox_opened_count": None,
"avg_uri": 33.0,
"pct_w_tracking_prot_enabled": 0.0,
},
2019-05-31 21:08:46 +03:00
{
"addon_id": u"hotfix-update-xpi-intermediate@mozilla.com",
"avg_bookmarks": None,
"avg_tabs": None,
"avg_toolbox_opened_count": None,
"avg_uri": 33.0,
"pct_w_tracking_prot_enabled": 0.0,
},
{
"addon_id": u"fxmonitor@mozilla.org",
"avg_bookmarks": None,
"avg_tabs": None,
"avg_toolbox_opened_count": None,
"avg_uri": 33.0,
"pct_w_tracking_prot_enabled": 0.0,
},
2019-06-01 00:21:35 +03:00
{
"addon_id": u"non-system-addon2",
"avg_bookmarks": None,
"avg_tabs": None,
"avg_toolbox_opened_count": None,
"avg_uri": 33.0,
"pct_w_tracking_prot_enabled": 0.0,
},
2019-05-31 21:08:46 +03:00
{
"addon_id": u"formautofill@mozilla.org",
"avg_bookmarks": None,
"avg_tabs": None,
"avg_toolbox_opened_count": None,
"avg_uri": 33.0,
"pct_w_tracking_prot_enabled": 0.0,
},
{
"addon_id": u"webcompat@mozilla.org",
"avg_bookmarks": None,
"avg_tabs": None,
"avg_toolbox_opened_count": None,
"avg_uri": 33.0,
"pct_w_tracking_prot_enabled": 0.0,
},
]
2019-05-31 02:18:56 +03:00
assert output == expected
def test_user_demo_metrics(addons_expanded_day, spark):
output = df_to_json(get_user_demo_metrics(addons_expanded_day))
expected = [
{
"addon_id": u"baidu-code-update@mozillaonline.com",
"country_dist": {u"GB": 1.0},
"os_dist": {u"Windows_NT": 1.0},
},
{
"addon_id": u"screenshots@mozilla.org",
"country_dist": {u"GB": 1.0},
"os_dist": {u"Windows_NT": 1.0},
},
2019-06-01 00:21:35 +03:00
{
"addon_id": u"non-system-addon1",
"country_dist": {u"GB": 1.0},
"os_dist": {u"Windows_NT": 1.0},
},
2019-05-31 02:18:56 +03:00
{
"addon_id": u"hotfix-update-xpi-intermediate@mozilla.com",
"country_dist": {u"GB": 1.0},
"os_dist": {u"Windows_NT": 1.0},
},
{
"addon_id": u"fxmonitor@mozilla.org",
"country_dist": {u"GB": 1.0},
"os_dist": {u"Windows_NT": 1.0},
},
2019-06-01 00:21:35 +03:00
{
"addon_id": u"non-system-addon2",
"country_dist": {u"GB": 1.0},
"os_dist": {u"Windows_NT": 1.0},
},
2019-05-31 02:18:56 +03:00
{
"addon_id": u"formautofill@mozilla.org",
"country_dist": {u"GB": 1.0},
"os_dist": {u"Windows_NT": 1.0},
},
{
"addon_id": u"webcompat@mozilla.org",
"country_dist": {u"GB": 1.0},
"os_dist": {u"Windows_NT": 1.0},
},
]
print("output", output)
print("expected", expected)
assert output == expected
def test_trend_metrics(addons_expanded, spark):
output = df_to_json(get_trend_metrics(addons_expanded, BASE_DATE))
expected_output = [
{
2019-06-04 03:24:08 +03:00
"addon_id": "baidu-code-update@mozillaonline.com",
2019-05-31 02:18:56 +03:00
"mau": 1,
"wau": 1,
2019-06-04 03:24:08 +03:00
"dau": 1,
"dau_prop": 1.0,
2019-05-31 02:18:56 +03:00
},
{
2019-06-04 03:24:08 +03:00
"addon_id": "tls13-version-fallback-rollout-bug1462099@mozilla.org",
"mau": 1,
"wau": None,
2019-05-31 02:18:56 +03:00
"dau": None,
2019-06-04 03:24:08 +03:00
"dau_prop": None,
},
{
"addon_id": "screenshots@mozilla.org",
"mau": 2,
"wau": 1,
"dau": 1,
"dau_prop": 1.0,
},
{
"addon_id": "non-system-addon1",
"mau": 1,
"wau": 1,
"dau": 1,
"dau_prop": 1.0,
},
{
"addon_id": "firefox@getpocket.com",
2019-05-31 02:18:56 +03:00
"mau": 1,
"wau": None,
2019-06-04 03:24:08 +03:00
"dau": None,
"dau_prop": None,
2019-05-31 02:18:56 +03:00
},
{
2019-06-04 03:24:08 +03:00
"addon_id": "hotfix-update-xpi-intermediate@mozilla.com",
"mau": 1,
"wau": 1,
2019-05-31 02:18:56 +03:00
"dau": 1,
2019-06-04 03:24:08 +03:00
"dau_prop": 1.0,
},
{
"addon_id": "fxmonitor@mozilla.org",
2019-05-31 02:18:56 +03:00
"mau": 1,
"wau": 1,
2019-06-04 03:24:08 +03:00
"dau": 1,
"dau_prop": 1.0,
},
{
"addon_id": "aushelper@mozilla.org",
"mau": 1,
"wau": None,
"dau": None,
"dau_prop": None,
2019-05-31 02:18:56 +03:00
},
{
2019-06-04 03:24:08 +03:00
"addon_id": "onboarding@mozilla.org",
"mau": 1,
"wau": None,
2019-05-31 02:18:56 +03:00
"dau": None,
2019-06-04 03:24:08 +03:00
"dau_prop": None,
},
{
"addon_id": "activity-stream@mozilla.org",
"mau": 1,
"wau": None,
"dau": None,
"dau_prop": None,
},
{
"addon_id": "non-system-addon2",
"mau": 1,
"wau": 1,
"dau": 1,
"dau_prop": 1.0,
},
{
"addon_id": "followonsearch@mozilla.com",
2019-05-31 02:18:56 +03:00
"mau": 1,
"wau": None,
2019-06-04 03:24:08 +03:00
"dau": None,
"dau_prop": None,
},
{
"addon_id": "formautofill@mozilla.org",
"mau": 2,
"wau": 1,
"dau": 1,
"dau_prop": 1.0,
},
{
"addon_id": "webcompat@mozilla.org",
"mau": 2,
"wau": 1,
"dau": 1,
"dau_prop": 1.0,
2019-05-31 02:18:56 +03:00
},
]
2019-06-04 03:24:08 +03:00
2019-05-31 02:18:56 +03:00
assert output == expected_output
2019-06-01 00:21:35 +03:00
def test_top_ten_coinstalls(addons_expanded_day, spark):
2019-05-31 02:18:56 +03:00
"""
Given a dataframe of some actual sampled data, ensure that
the get_pct_tracking_enabled outputs the correct dataframe
:param main_summary_tto: pytest fixture defined above, sample data from main_summary
:return: assertion whether the expected output indeed matches the true output
"""
2019-06-01 00:21:35 +03:00
output = df_to_json(get_top_10_coinstalls(addons_expanded_day))
expected = [
{
"addon_id": u"baidu-code-update@mozillaonline.com",
"top_10_coinstalls": {
u"0": u"non-system-addon2",
u"1": u"non-system-addon1",
},
},
{
"addon_id": u"screenshots@mozilla.org",
"top_10_coinstalls": {
u"0": u"hotfix-update-xpi-intermediate@mozilla.com",
u"1": u"non-system-addon1",
},
},
{
"addon_id": u"non-system-addon1",
"top_10_coinstalls": {
u"0": u"non-system-addon1",
u"1": u"non-system-addon2",
},
},
{
"addon_id": u"hotfix-update-xpi-intermediate@mozilla.com",
"top_10_coinstalls": {
u"0": u"non-system-addon1",
u"1": u"non-system-addon2",
},
},
{
"addon_id": u"fxmonitor@mozilla.org",
"top_10_coinstalls": {
u"0": u"non-system-addon1",
u"1": u"hotfix-update-xpi-intermediate@mozilla.com",
},
},
{
"addon_id": u"non-system-addon2",
"top_10_coinstalls": {
u"0": u"non-system-addon2",
u"1": u"non-system-addon1",
},
},
{
"addon_id": u"formautofill@mozilla.org",
"top_10_coinstalls": {
u"0": u"non-system-addon1",
u"1": u"non-system-addon2",
},
},
{
"addon_id": u"webcompat@mozilla.org",
"top_10_coinstalls": {
u"0": u"non-system-addon1",
u"1": u"non-system-addon2",
},
},
]
2019-05-31 02:18:56 +03:00
def test_engagement_metrics(addons_expanded_day, main_summary_day, spark):
"""
Given a dataframe of some actual sampled data, ensure that
the get_pct_tracking_enabled outputs the correct dataframe
:param addons_expanded: pytest fixture defined above
:return: assertion whether the expected output indeed matches the true output
"""
2019-05-31 21:08:46 +03:00
output = df_to_json(get_engagement_metrics(addons_expanded_day, main_summary_day))
2019-05-31 02:18:56 +03:00
expected_output = [
{
"active_hours": 0.18194444444444444,
"addon_id": u"baidu-code-update@mozillaonline.com",
"avg_time_total": 747.0,
"disabled": None,
},
{
"active_hours": 0.18194444444444444,
"addon_id": u"screenshots@mozilla.org",
"avg_time_total": 747.0,
"disabled": None,
},
2019-06-01 00:21:35 +03:00
{
"active_hours": 0.18194444444444444,
"addon_id": u"non-system-addon1",
"avg_time_total": 747.0,
"disabled": None,
},
2019-05-31 02:18:56 +03:00
{
"active_hours": 0.18194444444444444,
"addon_id": u"hotfix-update-xpi-intermediate@mozilla.com",
"avg_time_total": 747.0,
"disabled": None,
},
{
"active_hours": 0.18194444444444444,
"addon_id": u"fxmonitor@mozilla.org",
"avg_time_total": 747.0,
"disabled": None,
},
2019-06-01 00:21:35 +03:00
{
"active_hours": 0.18194444444444444,
"addon_id": u"non-system-addon2",
"avg_time_total": 747.0,
"disabled": None,
},
2019-05-31 02:18:56 +03:00
{
"active_hours": 0.18194444444444444,
"addon_id": u"formautofill@mozilla.org",
"avg_time_total": 747.0,
"disabled": None,
},
{
"active_hours": 0.18194444444444444,
"addon_id": u"webcompat@mozilla.org",
"avg_time_total": 747.0,
"disabled": None,
},
]
assert output == expected_output
2019-06-04 20:32:25 +03:00
def test_install_flows(events):
output = df_to_json(install_flow_events(events))
expected_output = [
{
"addon_id": "screenshots@mozilla.org",
"avg_download_time": None,
"installs": {"amo": 2, "unknown": 0},
"uninstalls": {"system-addon": 1},
},
{
"addon_id": "screenshots@mozilla.org",
"avg_download_time": 584.5,
"installs": {"amo": 2, "unknown": 0},
"uninstalls": {"system-addon": 1},
},
{
"addon_id": "fxmonitor@mozilla.org",
"avg_download_time": None,
"installs": None,
"uninstalls": {"system-addon": 1},
},
{
"addon_id": "jid1-h4Ke2h5q31uuK7@jetpack",
"avg_download_time": 1704.0,
"installs": {"amo": 1, "unknown": 0},
"uninstalls": None,
},
{
"addon_id": "{87e997f4-ae0e-42e6-a780-ff73977188c5}",
"avg_download_time": 3015.0,
"installs": {"amo": 1, "unknown": 0},
"uninstalls": None,
},
{
"addon_id": "{08cc31c0-b1cb-461c-8ba2-95edd9e76a02}",
"avg_download_time": 998.0,
"installs": {"amo": 1, "unknown": 0},
"uninstalls": None,
},
{
"addon_id": "Directions_Found_mVBuOLkFzz@www.directionsfoundnt.com",
"avg_download_time": 572.0,
"installs": {"amo": 0, "unknown": 1},
"uninstalls": None,
},
]