|
5 | 5 | from pathlib import Path |
6 | 6 |
|
7 | 7 | import click |
| 8 | +import yaml |
8 | 9 | from jinja2 import Environment, FileSystemLoader |
9 | 10 |
|
10 | 11 | from bigquery_etl.cli.utils import use_cloud_function_option |
11 | 12 | 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 |
13 | 14 |
|
14 | 15 | 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 | + |
15 | 23 | TABLE_NAME = os.path.basename(os.path.normpath(THIS_PATH)) |
16 | 24 | BASE_NAME = "_".join(TABLE_NAME.split("_")[:-1]) |
17 | 25 | DATASET_FOR_UNIONED_VIEWS = "telemetry" |
|
50 | 58 | "klar_ios": [ |
51 | 59 | {"table": "`moz-fx-data-shared-prod.org_mozilla_ios_klar_live.baseline_v1`"} |
52 | 60 | ], |
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`"} |
55 | 63 | ], |
56 | 64 | } |
57 | 65 |
|
| 66 | + |
58 | 67 | class Browsers(Enum): |
59 | 68 | """Enumeration with browser names and equivalent dataset names.""" |
60 | 69 |
|
61 | | - firefox_desktop = "Firefox Desktop" |
62 | 70 | fenix = "Fenix" |
63 | 71 | focus_ios = "Focus iOS" |
64 | 72 | focus_android = "Focus Android" |
@@ -111,7 +119,109 @@ def generate(target_project, output_dir, use_cloud_function): |
111 | 119 | # checks templates for BigEye |
112 | 120 | bigeye_checks_template = env.get_template("bigconfig.yml") |
113 | 121 |
|
| 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 | + |
114 | 173 | 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. |
115 | 225 | if browser.name == "firefox_desktop": |
116 | 226 | query_sql = reformat( |
117 | 227 | desktop_query_template.render( |
@@ -140,7 +250,7 @@ def generate(target_project, output_dir, use_cloud_function): |
140 | 250 | table_schema_template = mobile_table_schema_template |
141 | 251 | view_schema_template = mobile_view_schema_template |
142 | 252 |
|
143 | | - # create checks_sql |
| 253 | + # Create checks_sql |
144 | 254 | if browser.name == "firefox_desktop": |
145 | 255 | checks_sql = desktop_checks_template.render( |
146 | 256 | project_id=target_project, |
@@ -176,13 +286,13 @@ def generate(target_project, output_dir, use_cloud_function): |
176 | 286 | full_table_id=f"{target_project}.{browser.name}_derived.{TABLE_NAME}", |
177 | 287 | basename="metadata.yaml", |
178 | 288 | 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 | + ), |
186 | 296 | skip_existing=False, |
187 | 297 | ) |
188 | 298 |
|
@@ -288,8 +398,8 @@ def generate(target_project, output_dir, use_cloud_function): |
288 | 398 | full_table_id=f"{target_project}.{browser.name}_derived.{TABLE_NAME}", |
289 | 399 | basename="bigconfig.yml", |
290 | 400 | sql=bigeye_checks_template.render( |
291 | | - app_name=browser.name, |
292 | | - format=False, |
| 401 | + app_name=browser.name, |
| 402 | + format=False, |
293 | 403 | ), |
294 | 404 | skip_existing=False, |
295 | 405 | ) |
0 commit comments