def test_mismatch_simple(schema, period, entity_id): """All same except for one item.""" fixture: Dict = { "some_text": "foo", "outer": { "some_text": "bar", "inner": { "some_text": "baz" } } } observation: Dict = { "some_text": "foo", "outer": { "some_text": "123", "inner": { "some_text": "baz" } } } # 2 matches, 1 mismatch expected: Outcome = Outcome() expected.matches.append(ValueMatch(entity_id, period, "/some_text", "Text", "foo")) expected.matches.append(ValueMatch(entity_id, period, "/outer/inner/some_text", "Text", "baz")) expected.mismatches.append(ValueMismatch(entity_id, period, "/outer/some_text", "Text", "bar", "123")) actual: Outcome = Outcome() crawl: CrawlPeriod = CrawlPeriod(entity_id, schema, fixture, observation, actual, period) crawl() assert expected == actual
def test_identical_complex(schema, period, entity_id): """Everything should be reported as matches.""" fixture: Dict = { "outer": [ { "the_folder": { "inner": [{ "some_text": "foo" }] } }, { "the_folder": { "inner": [{ "some_text": "bar" }] } }, ] } observed: Dict = copy.deepcopy(fixture) expected: Outcome = Outcome() expected.matches.append( ValueMatch(entity_id, period, "/outer", "List", json.dumps(fixture["outer"]))) actual: Outcome = Outcome() crawl: CrawlPeriod = CrawlPeriod(entity_id, schema, fixture, observed, actual, period) crawl() assert expected == actual
def test_null_actual(schema, period, entity_id): """Everything should be reported as missing.""" fixture: Dict = { "some_multiple_text": ["x", "y"], "outer": { "some_multiple_text": ["xx", "yy"], "inner": { "some_multiple_text": ["xxx", "yyy"], } } } expected: Outcome = Outcome() expected.missings.append( MissingValue(entity_id, period, "/some_multiple_text", "MultipleText", json.dumps(fixture["some_multiple_text"]))) expected.missings.append( MissingValue(entity_id, period, "/outer/some_multiple_text", "MultipleText", json.dumps(fixture["outer"]["some_multiple_text"]))) expected.missings.append( MissingValue( entity_id, period, "/outer/inner/some_multiple_text", "MultipleText", json.dumps(fixture["outer"]["inner"]["some_multiple_text"]))) actual: Outcome = Outcome() crawl: CrawlPeriod = CrawlPeriod(entity_id, schema, fixture, None, actual, period) crawl() assert expected == actual
def test_list_na_absent_match(complex_track, empty_track, entity_id): """If the fixture has an explicit |--|NA|--| and the item is missing in the actual, count it as a match.""" schema: Schema = Schema(empty_track, complex_track) fixture: Dict = { "outer": [{ "the_folder": { "inner": [{ "some_text": "foo" }, { "some_text": POLYTROPOS_NA }], } }] } observed: Dict = { "outer": [{ "the_folder": { "inner": [{ "some_text": "foo" }, {}], } }] } expected: Outcome = Outcome() expected.matches.append( ValueMatch(entity_id, "immutable", "/outer", "List", json.dumps(fixture["outer"]))) actual: Outcome = Outcome() crawl: CrawlImmutable = CrawlImmutable(entity_id, schema, fixture, observed, actual) crawl() assert expected == actual
def test_identical_simple(schema, entity_id): """Everything should be reported as matches.""" fixture: Dict = { "some_multiple_text": ["foo"], "outer": { "some_multiple_text": ["bar", "baq"], "inner": { "some_multiple_text": ["baz"] } } } observation: Dict = copy.deepcopy(fixture) # 3 Matches expected: Outcome = Outcome() expected.matches.append( ValueMatch(entity_id, "immutable", "/some_multiple_text", "MultipleText", json.dumps(["foo"]))) expected.matches.append( ValueMatch(entity_id, "immutable", "/outer/some_multiple_text", "MultipleText", json.dumps(["bar", "baq"]))) expected.matches.append( ValueMatch(entity_id, "immutable", "/outer/inner/some_multiple_text", "MultipleText", json.dumps(["baz"]))) actual: Outcome = Outcome() crawl: CrawlImmutable = CrawlImmutable(entity_id, schema, fixture, observation, actual) crawl() assert expected == actual
def test_identical_simple(schema, period, entity_id): """Everything should be reported as matches.""" fixture: Dict = { "some_text": "foo", "outer": { "some_text": "bar", "inner": { "some_text": "baz" } } } observation: Dict = copy.deepcopy(fixture) # 3 Matches expected: Outcome = Outcome() expected.matches.append(ValueMatch(entity_id, period, "/some_text", "Text", "foo")) expected.matches.append(ValueMatch(entity_id, period, "/outer/some_text", "Text", "bar")) expected.matches.append(ValueMatch(entity_id, period, "/outer/inner/some_text", "Text", "baz")) actual: Outcome = Outcome() crawl: CrawlPeriod = CrawlPeriod(entity_id, schema, fixture, observation, actual, period) crawl() assert expected == actual
def test_almost_same_not_equal(): p: Outcome = Outcome() p_match: ValueMatch = ValueMatch("the_entity_id", "the_period", "/path/to/var", "Text", "foo") p.matches.append(p_match) p_mismatch: ValueMismatch = ValueMismatch("the_entity_id", "the_period", "/path/to/var", "Text", "foo", "bar") p.mismatches.append(p_mismatch) p_missing: MissingValue = MissingValue("the_entity_id", "the_period", "/path/to/var", "Text", None) p.missings.append(p_missing) q: Outcome = Outcome() q_mismatch: ValueMismatch = ValueMismatch("the_entity_id", "the_period", "/path/to/var", "Text", "foo", "bar") q.mismatches.append(q_mismatch) q_missing: MissingValue = MissingValue("the_entity_id", "the_period", "/path/to/var", "Text", None) q.missings.append(q_missing) assert p != q
def test_empty_actual(schema, entity_id): """Everything should be reported as missing.""" fixture: Dict = { "some_multiple_text": ["foo"], "outer": { "some_multiple_text": ["bar", "baq"], "inner": { "some_multiple_text": ["baz"] } } } observed: Dict = {} expected: Outcome = Outcome() expected.missings.append( MissingValue(entity_id, "immutable", "/some_multiple_text", "MultipleText", json.dumps(["foo"]))) expected.missings.append( MissingValue(entity_id, "immutable", "/outer/some_multiple_text", "MultipleText", json.dumps(["bar", "baq"]))) expected.missings.append( MissingValue(entity_id, "immutable", "/outer/inner/some_multiple_text", "MultipleText", json.dumps(["baz"]))) actual: Outcome = Outcome() crawl: CrawlImmutable = CrawlImmutable(entity_id, schema, fixture, observed, actual) crawl() assert expected == actual
def test_folder_na_present_as_none_mismatch(simple_track, empty_track, entity_id): """If the fixture has an explicit |--|NA|--| and the item is present in the actual with the value None, count it as a mismatch.""" schema: Schema = Schema(empty_track, simple_track) fixture: Dict = { "some_text": "bar", "outer": { "some_text": POLYTROPOS_NA, } } observation: Dict = { "some_text": "bar", "outer": { "some_text": None, } } expected: Outcome = Outcome() expected.mismatches.append( ValueMismatch(entity_id, "immutable", "/outer/some_text", "Text", POLYTROPOS_NA, None)) expected.matches.append( ValueMatch(entity_id, "immutable", "/some_text", "Text", "bar")) actual: Outcome = Outcome() crawl: CrawlImmutable = CrawlImmutable(entity_id, schema, fixture, observation, actual) crawl() assert expected == actual
def test_not_tested_complex(schema, entity_id, period): """All tested fields same, but one field is omitted from fixture.""" fixture: Dict = {} observed: Dict = { "outer": [ { "the_folder": { "inner": [{ "some_text": "foo" }] } }, { "the_folder": { "inner": [{ "some_text": "bar" }] } }, ] } expected: Outcome = Outcome() actual: Outcome = Outcome() crawl: CrawlPeriod = CrawlPeriod(entity_id, schema, fixture, observed, actual, period) crawl() assert expected == actual
def test_missing_complex(schema, entity_id, period): """All same except for one missing item.""" fixture: Dict = { "outer": [ { "the_folder": { "inner": [{ "some_text": "foo" }] } }, { "the_folder": { "inner": [{ "some_text": "bar" }] } }, ] } observed: Dict = {} expected: Outcome = Outcome() expected.missings.append( MissingValue(entity_id, period, "/outer", "List", json.dumps(fixture["outer"]))) actual: Outcome = Outcome() crawl: CrawlPeriod = CrawlPeriod(entity_id, schema, fixture, observed, actual, period) crawl() assert expected == actual
def test_identical_simple(schema, period, entity_id): """Everything should be reported as matches.""" fixture: Dict = { "some_multiple_text": ["x", "y"], "outer": { "some_multiple_text": ["xx", "yy"], "inner": { "some_multiple_text": ["xxx", "yyy"], } } } observation: Dict = copy.deepcopy(fixture) # 3 Matches expected: Outcome = Outcome() expected.matches.append( ValueMatch(entity_id, period, "/some_multiple_text", "MultipleText", json.dumps(fixture["some_multiple_text"]))) expected.matches.append( ValueMatch(entity_id, period, "/outer/some_multiple_text", "MultipleText", json.dumps(fixture["outer"]["some_multiple_text"]))) expected.matches.append( ValueMatch(entity_id, period, "/outer/inner/some_multiple_text", "MultipleText", json.dumps( fixture["outer"]["inner"]["some_multiple_text"]))) actual: Outcome = Outcome() crawl: CrawlPeriod = CrawlPeriod(entity_id, schema, fixture, observation, actual, period) crawl() assert expected == actual
def test_null_actual(schema, period, entity_id): """Everything should be reported as missing.""" fixture: Dict = { "some_text": "foo", "outer": { "some_text": "bar", "inner": { "some_text": "baz" } } } expected: Outcome = Outcome() expected.missings.append( MissingValue(entity_id, period, "/some_text", "Text", "foo")) expected.missings.append( MissingValue(entity_id, period, "/outer/some_text", "Text", "bar")) expected.missings.append( MissingValue(entity_id, period, "/outer/inner/some_text", "Text", "baz")) actual: Outcome = Outcome() crawl: CrawlPeriod = CrawlPeriod(entity_id, schema, fixture, None, actual, period) crawl() assert expected == actual
def test_simple_na_present_with_value_mismatch(simple_track, empty_track, entity_id): """If the fixture has an explicit |--|NA|--| and the item is present in the actual with a non-null value, count it as a mismatch.""" schema: Schema = Schema(empty_track, simple_track) fixture: Dict = { "some_multiple_text": POLYTROPOS_NA, "outer": { "some_multiple_text": ["foo", "bar"] } } observation: Dict = { "some_multiple_text": ["123"], "outer": { "some_multiple_text": ["foo", "bar"] } } expected: Outcome = Outcome() expected.mismatches.append(ValueMismatch(entity_id, "immutable", "/some_multiple_text", "MultipleText", POLYTROPOS_NA, json.dumps(["123"]))) expected.matches.append(ValueMatch(entity_id, "immutable", "/outer/some_multiple_text", "MultipleText", json.dumps(["foo", "bar"]))) actual: Outcome = Outcome() crawl: CrawlImmutable = CrawlImmutable(entity_id, schema, fixture, observation, actual) crawl() assert expected == actual
def test_not_tested_simple(schema, period, entity_id): """One mismatch, one field omitted from fixture.""" fixture: Dict = { "some_text": "foo", "outer": { "inner": { "some_text": "123" } } } observation: Dict = { "some_text": "foo", "outer": { "some_text": "bar", "inner": { "some_text": "baz" } } } # 1 match, 1 mismatch expected: Outcome = Outcome() expected.matches.append(ValueMatch(entity_id, period, "/some_text", "Text", "foo")) expected.mismatches.append(ValueMismatch(entity_id, period, "/outer/inner/some_text", "Text", "123", "baz")) actual: Outcome = Outcome() crawl: CrawlPeriod = CrawlPeriod(entity_id, schema, fixture, observation, actual, period) crawl() assert expected == actual
def test_one_has_missing_not_equal(): p: Outcome = Outcome() p_missing: MissingValue = MissingValue("the_entity_id", "the_period", "/path/to/var", "Text", None) p.missings.append(p_missing) q: Outcome = Outcome() assert p != q
def test_one_has_match_not_equal(): p: Outcome = Outcome() p_match: ValueMatch = ValueMatch("the_entity_id", "the_period", "/path/to/var", "Text", "foo") p.matches.append(p_match) q: Outcome = Outcome() assert p != q
def test_fixture_undefined_records_invalid(schema, period, entity_id, observation): """Fixture contains an undefined field, resulting in an error state.""" fixture: Dict = { "not a thing": "blah blah" } expected: Outcome = Outcome() expected.invalids.append(InvalidPath(entity_id, "/not a thing", "immutable", "blah blah")) actual: Outcome = Outcome() crawl: CrawlPeriod = CrawlPeriod(entity_id, schema, fixture, observation, actual, period) crawl()
def test_both_have_same_match_equal(): p: Outcome = Outcome() p_match: ValueMatch = ValueMatch("the_entity_id", "the_period", "/path/to/var", "Text", "foo") p.matches.append(p_match) q: Outcome = Outcome() q_match: ValueMatch = ValueMatch("the_entity_id", "the_period", "/path/to/var", "Text", "foo") q.matches.append(q_match) assert p == q
def test_empty_both(schema, period, entity_id): """Outcomes should remain empty.""" fixture: Dict = {} observed: Dict = {} expected: Outcome = Outcome() actual: Outcome = Outcome() crawl: CrawlPeriod = CrawlPeriod(entity_id, schema, fixture, observed, actual, period) crawl() assert expected == actual
def test_each_has_different_match_not_equal(): p: Outcome = Outcome() p_match: ValueMatch = ValueMatch("the_entity_id", "the_period", "/path/to/var", "Text", "foo") p.matches.append(p_match) q: Outcome = Outcome() q_match: ValueMatch = ValueMatch("the_entity_id", "a_different_period", "/path/to/var", "Text", "foo") q.matches.append(q_match) assert p != q
def test_both_have_same_missing_equal(): p: Outcome = Outcome() p_missing: MissingValue = MissingValue("the_entity_id", "the_period", "/path/to/var", "Text", None) p.missings.append(p_missing) q: Outcome = Outcome() q_missing: MissingValue = MissingValue("the_entity_id", "the_period", "/path/to/var", "Text", None) q.missings.append(q_missing) assert p == q
def test_each_has_different_missing_not_equal(): p: Outcome = Outcome() p_missing: MissingValue = MissingValue("the_entity_id", "the_period", "/path/to/var", "Text", None) p.missings.append(p_missing) q: Outcome = Outcome() q_missing: MissingValue = MissingValue("the_entity_id", "the_period", "/path/to/var", "Text", "Blah") q.missings.append(q_missing) assert p != q
def test_explicit_null_mismatch(schema, period, entity_id, f_val, o_val): fixture: Dict = {"some_text": f_val} observation: Dict = {"some_text": o_val} expected: Outcome = Outcome() expected.mismatches.append(ValueMismatch(entity_id, period, "/some_text", "Text", f_val, o_val)) actual: Outcome = Outcome() crawl: CrawlPeriod = CrawlPeriod(entity_id, schema, fixture, observation, actual, period) crawl() assert expected == actual
def test_explicit_null_missing(schema, period, entity_id): fixture: Dict = {"some_text": None} observation: Dict = {} expected: Outcome = Outcome() expected.missings.append(MissingValue(entity_id, period, "/some_text", "Text", None)) actual: Outcome = Outcome() crawl: CrawlPeriod = CrawlPeriod(entity_id, schema, fixture, observation, actual, period) crawl() assert expected == actual
def test_mismatch_complex(schema, entity_id, period): """All same except for one item.""" fixture: Dict = { "outer": [ { "the_folder": { "inner": [{ "some_multiple_text": ["foo"] }] } }, { "the_folder": { "inner": [{ "some_multiple_text": ["bar"] }] } }, ] } observed: Dict = { "outer": [ { "the_folder": { "inner": [{ "some_multiple_text": ["123", "456"] }] } }, { "the_folder": { "inner": [{ "some_multiple_text": ["bar"] }] } }, ] } expected: Outcome = Outcome() expected.mismatches.append( ValueMismatch(entity_id, period, "/outer", "List", json.dumps(fixture["outer"]), json.dumps(observed["outer"]))) actual: Outcome = Outcome() crawl: CrawlPeriod = CrawlPeriod(entity_id, schema, fixture, observed, actual, period) crawl() assert expected == actual
def test_fixture_undefined_complex_raises(schema, period, entity_id): """Fixture contains an undefined field, resulting in an error state.""" """All same except for one missing item.""" fixture: Dict = { "outer": [ { "not_a_thing": { "inner": [{ "some_text": "foo" }] } }, { "the_folder": { "inner": [{ "some_text": "bar" }] } }, ] } observed: Dict = copy.deepcopy(fixture) outcome: Outcome = Outcome() crawl: CrawlPeriod = CrawlPeriod(entity_id, schema, fixture, observed, outcome, period) with pytest.raises(ValueError): crawl()
def test_explicit_null_match(schema, period, entity_id): fixture: Dict = {"some_multiple_text": None} observation: Dict = {"some_multiple_text": None} expected: Outcome = Outcome() expected.matches.append( ValueMatch(entity_id, period, "/some_multiple_text", "MultipleText", None)) actual: Outcome = Outcome() crawl: CrawlPeriod = CrawlPeriod(entity_id, schema, fixture, observation, actual, period) crawl() assert expected == actual
def test_list_has_sentinal_value_raises(complex_track, empty_track, entity_id, bad_value): """If the fixture has an explicit |--|NA|--| and the item has the same string, an error is raised because we need to choose a new, truly unique sentinel value for missing data.""" schema: Schema = Schema(empty_track, complex_track) fixture: Dict = { "outer": [{ "the_folder": { "inner": [{ "some_text": "foo" }, { "some_text": POLYTROPOS_NA }], } }] } observed: Dict = { "outer": [{ "the_folder": { "inner": [{ "some_text": "foo" }, { "some_text": bad_value }], } }] } actual: Outcome = Outcome() crawl: CrawlImmutable = CrawlImmutable(entity_id, schema, fixture, observed, actual) with pytest.raises(ValueError): crawl()
def test_explicit_null_mismatch(schema, period, entity_id, f_val, o_val): fixture: Dict = {"some_multiple_text": f_val} observation: Dict = {"some_multiple_text": o_val} expected: Outcome = Outcome() expected.mismatches.append( ValueMismatch(entity_id, period, "/some_multiple_text", "MultipleText", None if f_val is None else json.dumps(f_val), None if o_val is None else json.dumps(o_val))) actual: Outcome = Outcome() crawl: CrawlPeriod = CrawlPeriod(entity_id, schema, fixture, observation, actual, period) crawl() assert expected == actual