| 1 |
import json |
| 2 |
import logging |
| 3 |
import os |
| 4 |
|
| 5 |
import avro |
| 6 |
import pytest |
| 7 |
|
| 8 |
from pbcommand.models import PipelinePreset, PipelineDataStoreViewRules |
| 9 |
from pbcommand.models.report import Report, ReportSpec |
| 10 |
from pbcommand.pb_io import (load_pipeline_presets_from, |
| 11 |
load_pipeline_datastore_view_rules_from_json, |
| 12 |
load_report_spec_from_json) |
| 13 |
from pbcommand.schemas import (validate_presets, |
| 14 |
validate_datastore_view_rules, |
| 15 |
validate_report_spec) |
| 16 |
from pbcommand.utils import walker |
| 17 |
from base_utils import DATA_DIR_PRESETS, DATA_DIR_DSVIEW, DATA_DIR_REPORT_SPECS |
| 18 |
|
| 19 |
log = logging.getLogger(__name__) |
| 20 |
|
| 21 |
|
| 22 |
def _to_json(path): |
| 23 |
with open(path, 'r') as f: |
| 24 |
d = json.loads(f.read()) |
| 25 |
return d |
| 26 |
|
| 27 |
|
| 28 |
def json_filter(path): |
| 29 |
return path.endswith(".json") |
| 30 |
|
| 31 |
|
| 32 |
def _to_assertion(path, schema_validate_func): |
| 33 |
def test_is_validate(self): |
| 34 |
d = _to_json(path) |
| 35 |
|
| 36 |
log.info("Attempting to validate '{}'".format(path)) |
| 37 |
is_valid = schema_validate_func(d) |
| 38 |
log.info(" is-valid? {i} {p}".format(i=is_valid, p=path)) |
| 39 |
if not is_valid: |
| 40 |
print("{p} is not valid with the avro schema".format(p=path)) |
| 41 |
assert is_valid |
| 42 |
return test_is_validate |
| 43 |
|
| 44 |
|
| 45 |
class ValidatePipelinePreset: |
| 46 |
def test_validate_pipeline_presets(self): |
| 47 |
for path in walker(DATA_DIR_PRESETS, json_filter): |
| 48 |
f = _to_assertion(path, validate_presets) |
| 49 |
f(self) |
| 50 |
assert isinstance(load_pipeline_presets_from(path), PipelinePreset) |
| 51 |
|
| 52 |
|
| 53 |
class ValidateDataStoreViewRules: |
| 54 |
def test_validate_pipeline_datastore_view_rules(self): |
| 55 |
for path in walker(DATA_DIR_DSVIEW, json_filter): |
| 56 |
f = _to_assertion(path, validate_datastore_view_rules) |
| 57 |
f(self) |
| 58 |
assert isinstance( |
| 59 |
load_pipeline_datastore_view_rules_from_json(path), |
| 60 |
PipelineDataStoreViewRules) |
| 61 |
|
| 62 |
|
| 63 |
class ValidateReportSpec: |
| 64 |
def test_validate_report_spec(self): |
| 65 |
for path in walker(DATA_DIR_REPORT_SPECS, json_filter): |
| 66 |
if os.path.basename(path).startswith("report-specs"): |
| 67 |
f = _to_assertion(path, validate_report_spec) |
| 68 |
f(self) |
| 69 |
assert isinstance(load_report_spec_from_json(path), |
| 70 |
ReportSpec) |