Skip to content

Commit 7749ba6

Browse files
Add unitttests for active_users_aggregates Mobile (#8616)
* Remove Desktop v3 from sql_generators. Add unitttests for Mobile. * Improve tests descriptions. * Remove comment.
1 parent 01bdd56 commit 7749ba6

37 files changed

+1190
-16
lines changed

sql_generators/active_users_aggregates_v3/__init__.py

Lines changed: 124 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@
55
from pathlib import Path
66

77
import click
8+
import yaml
89
from jinja2 import Environment, FileSystemLoader
910

1011
from bigquery_etl.cli.utils import use_cloud_function_option
1112
from bigquery_etl.format_sql.formatter import reformat
12-
from bigquery_etl.util.common import render, write_sql
13+
from bigquery_etl.util.common import render, write_sql, get_table_dir
1314

1415
THIS_PATH = Path(os.path.dirname(__file__))
16+
MOBILE_UNITTESTS_PATH = THIS_PATH / "templates/unittests/mobile"
17+
DESKTOP_UNITTESTS_PATH = THIS_PATH / "templates/unittests/desktop"
18+
MOBILE_UNITTESTS_TEMPLATES = THIS_PATH / "templates" / "mobile_unittests_templates.yaml"
19+
DESKTOP_UNITTESTS_TEMPLATES = (
20+
THIS_PATH / "templates" / "desktop_unittests_templates.yaml"
21+
)
22+
1523
TABLE_NAME = os.path.basename(os.path.normpath(THIS_PATH))
1624
BASE_NAME = "_".join(TABLE_NAME.split("_")[:-1])
1725
DATASET_FOR_UNIONED_VIEWS = "telemetry"
@@ -50,15 +58,15 @@
5058
"klar_ios": [
5159
{"table": "`moz-fx-data-shared-prod.org_mozilla_ios_klar_live.baseline_v1`"}
5260
],
53-
"klar_android" : [
54-
{"table" : "`moz-fx-data-shared-prod.org_mozilla_klar_live.baseline_v1`"}
61+
"klar_android": [
62+
{"table": "`moz-fx-data-shared-prod.org_mozilla_klar_live.baseline_v1`"}
5563
],
5664
}
5765

66+
5867
class Browsers(Enum):
5968
"""Enumeration with browser names and equivalent dataset names."""
6069

61-
firefox_desktop = "Firefox Desktop"
6270
fenix = "Fenix"
6371
focus_ios = "Focus iOS"
6472
focus_android = "Focus Android"
@@ -111,7 +119,109 @@ def generate(target_project, output_dir, use_cloud_function):
111119
# checks templates for BigEye
112120
bigeye_checks_template = env.get_template("bigconfig.yml")
113121

122+
def output_unittest_templates(
123+
dataset, app_name, templates, template_type, source, test_name=None
124+
):
125+
"""Load, render and output unitest template."""
126+
test_folder = ""
127+
parts = 2
128+
output_path = Path("tests") / output_dir
129+
for group in templates[template_type]:
130+
try:
131+
for file_name, file_template in group[source].items():
132+
if template_type == "test_data":
133+
parts = 3
134+
test_folder = (
135+
"/desktop" if app_name == "firefox_desktop" else "/mobile"
136+
) + f"/{test_name}"
137+
_dataset = (
138+
"telemetry"
139+
if (
140+
dataset == "firefox_desktop"
141+
and file_name == "desktop_active_users"
142+
)
143+
else dataset
144+
)
145+
_file = (
146+
file_template
147+
if file_name in ["expect", "query_params"]
148+
else f"{target_project}.{_dataset}.{file_template}"
149+
)
150+
151+
unittest_template = env.get_template(
152+
f"unittests{test_folder}/{file_template}"
153+
)
154+
full_table_id = (
155+
f"{target_project}.{dataset}_derived.{TABLE_NAME}"
156+
+ (f".{test_name}" if template_type == "test_data" else "")
157+
)
158+
d = get_table_dir(output_path, full_table_id, parts)
159+
d.mkdir(parents=True, exist_ok=True)
160+
target = d / _file
161+
click.echo(f"INFO:Writing {target}")
162+
with target.open("w") as f:
163+
f.write(
164+
unittest_template.render(
165+
app_name=app_name,
166+
format=False,
167+
)
168+
)
169+
f.write("\n")
170+
except KeyError:
171+
continue
172+
114173
for browser in Browsers:
174+
# Load list of unittests schemas and data templates.
175+
templates_yaml_path = (
176+
DESKTOP_UNITTESTS_TEMPLATES
177+
if browser.name == "firefox_desktop"
178+
else MOBILE_UNITTESTS_TEMPLATES
179+
)
180+
tests_path = (
181+
DESKTOP_UNITTESTS_PATH
182+
if browser.name == "firefox_desktop"
183+
else MOBILE_UNITTESTS_PATH
184+
)
185+
186+
with open(templates_yaml_path) as f:
187+
templates = yaml.safe_load(f)
188+
189+
# Write unittests test schemas.
190+
output_unittest_templates(
191+
dataset=browser.name,
192+
app_name=browser.value,
193+
templates=templates,
194+
template_type="test_schemas",
195+
source="common",
196+
)
197+
output_unittest_templates(
198+
dataset=browser.name,
199+
app_name=browser.value,
200+
templates=templates,
201+
template_type="test_schemas",
202+
source=browser.name,
203+
)
204+
205+
# Write unittests test data.
206+
for test in next(os.walk(tests_path))[1]:
207+
output_unittest_templates(
208+
dataset=browser.name,
209+
app_name=browser.value,
210+
templates=templates,
211+
template_type="test_data",
212+
source="common",
213+
test_name=test,
214+
)
215+
output_unittest_templates(
216+
dataset=browser.name,
217+
app_name=browser.value,
218+
templates=templates,
219+
template_type="test_data",
220+
source=browser.name,
221+
test_name=test,
222+
)
223+
224+
# Write queries.
115225
if browser.name == "firefox_desktop":
116226
query_sql = reformat(
117227
desktop_query_template.render(
@@ -140,7 +250,7 @@ def generate(target_project, output_dir, use_cloud_function):
140250
table_schema_template = mobile_table_schema_template
141251
view_schema_template = mobile_view_schema_template
142252

143-
# create checks_sql
253+
# Create checks_sql
144254
if browser.name == "firefox_desktop":
145255
checks_sql = desktop_checks_template.render(
146256
project_id=target_project,
@@ -176,13 +286,13 @@ def generate(target_project, output_dir, use_cloud_function):
176286
full_table_id=f"{target_project}.{browser.name}_derived.{TABLE_NAME}",
177287
basename="metadata.yaml",
178288
sql=render(
179-
metadata_template,
180-
template_folder=THIS_PATH / "templates",
181-
app_value=browser.value,
182-
app_name=browser.name,
183-
table_name=TABLE_NAME,
184-
format=False,
185-
),
289+
metadata_template,
290+
template_folder=THIS_PATH / "templates",
291+
app_value=browser.value,
292+
app_name=browser.name,
293+
table_name=TABLE_NAME,
294+
format=False,
295+
),
186296
skip_existing=False,
187297
)
188298

@@ -288,8 +398,8 @@ def generate(target_project, output_dir, use_cloud_function):
288398
full_table_id=f"{target_project}.{browser.name}_derived.{TABLE_NAME}",
289399
basename="bigconfig.yml",
290400
sql=bigeye_checks_template.render(
291-
app_name=browser.name,
292-
format=False,
401+
app_name=browser.name,
402+
format=False,
293403
),
294404
skip_existing=False,
295405
)

sql_generators/active_users_aggregates_v3/templates/mobile_query.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ WITH
77
adjust_network,
88
install_source
99
FROM
10-
fenix.firefox_android_clients
10+
`{{ project_id }}.fenix.firefox_android_clients`
1111
),
1212
{% endif %}
1313
{% if app_name == "firefox_ios"%}
@@ -17,7 +17,7 @@ WITH
1717
adjust_network,
1818
CAST(NULL AS STRING) install_source
1919
FROM
20-
firefox_ios.firefox_ios_clients
20+
`{{ project_id }}.firefox_ios.firefox_ios_clients`
2121
),
2222
{% endif %}
2323
baseline AS (
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
test_data:
2+
- common:
3+
expect: expect.yaml
4+
query_params: query_params.yaml
5+
active_users: active_users.yaml
6+
metrics_clients_last_seen: metrics_clients_last_seen.yaml
7+
fenix:
8+
firefox_android_clients: firefox_android_clients.yaml
9+
firefox_ios:
10+
firefox_ios_clients: firefox_ios_clients.yaml
11+
test_schemas:
12+
- common:
13+
active_users: active_users.schema.json
14+
metrics_clients_last_seen: metrics_clients_last_seen.schema.json
15+
fenix:
16+
firefox_android_clients: firefox_android_clients.schema.json
17+
firefox_ios:
18+
firefox_ios_clients: firefox_ios_clients.schema.json

0 commit comments

Comments
 (0)