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_mismatches(outcomes): expected: List[ValueMismatch] = [ ValueMismatch("entity_2", "period_1", "/temporal_root_text", "Text", "GHI", "GGG"), ValueMismatch("entity_2", "immutable", "/immutable_root_text", "Text", "MNO", "MMM"), ] actual: List[ValueMismatch] = list(outcomes.mismatches) assert actual == expected
def test_each_has_different_mismatch_not_equal(): p: Outcome = Outcome() p_mismatch: ValueMismatch = ValueMismatch("the_entity_id", "the_period", "/path/to/var", "Text", "foo", "bar") p.mismatches.append(p_mismatch) q: Outcome = Outcome() q_mismatch: ValueMismatch = ValueMismatch("the_entity_id", "the_period", "/path/to/var", "Text", "foo", "BAR") q.mismatches.append(q_mismatch) assert p != q
def test_both_have_same_mismatch_equal(): p: Outcome = Outcome() p_mismatch: ValueMismatch = ValueMismatch("the_entity_id", "the_period", "/path/to/var", "Text", "foo", "bar") p.mismatches.append(p_mismatch) q: Outcome = Outcome() q_mismatch: ValueMismatch = ValueMismatch("the_entity_id", "the_period", "/path/to/var", "Text", "foo", "bar") q.mismatches.append(q_mismatch) assert p == q
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_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_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_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 _record_mismatch(self, path: ListType, data_type: str, expected: Optional[Any], actual: Optional[Any]) -> None: if not _is_simple_value(expected): expected = json.dumps(expected, sort_keys=True) if not _is_simple_value(actual): actual = json.dumps(actual, sort_keys=True) path_str = nesteddicts.path_to_str(path) mismatch: ValueMismatch = ValueMismatch(self.entity_id, self.label, path_str, data_type, expected, actual) self.outcome.mismatches.append(mismatch)
def test_one_matches_one_doesnt_not_equal(): p: Outcome = Outcome() p_mismatch: ValueMismatch = ValueMismatch("the_entity_id", "the_period", "/path/to/var", "Text", "foo", "bar") p.mismatches.append(p_mismatch) 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_mismatch_complex(schema, entity_id, period): """All same except for one item.""" fixture: Dict = { "outer": [ { "the_folder": { "inner": [{ "some_text": "foo" }] } }, { "the_folder": { "inner": [{ "some_text": "bar" }] } }, ] } observed: Dict = { "outer": [ { "the_folder": { "inner": [{ "some_text": "123" }] } }, { "the_folder": { "inner": [{ "some_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_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
def test_mismatch_simple(schema, period, entity_id): """All same except for one item.""" fixture: Dict = { "some_multiple_text": ["x", "y"], "outer": { "some_multiple_text": ["xx", "yy"], "inner": { "some_multiple_text": ["xxx", "yyy"], } } } observation: Dict = { "some_multiple_text": ["x", "y"], "outer": { "some_multiple_text": ["aa"], "inner": { "some_multiple_text": ["xxx", "yyy"], } } } # 2 matches, 1 mismatch 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/inner/some_multiple_text", "MultipleText", json.dumps( fixture["outer"]["inner"]["some_multiple_text"]))) expected.mismatches.append( ValueMismatch(entity_id, period, "/outer/some_multiple_text", "MultipleText", json.dumps(fixture["outer"]["some_multiple_text"]), json.dumps(["aa"]))) actual: Outcome = Outcome() crawl: CrawlPeriod = CrawlPeriod(entity_id, schema, fixture, observation, actual, period) crawl() assert expected == actual
def test_list_na_present_as_none_mismatch(complex_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, complex_track) fixture: Dict = { "outer": [ { "the_folder": { "inner": [ {"some_multiple_text": ["foo", "bar"]}, {"some_multiple_text": POLYTROPOS_NA} ], } } ] } observed: Dict = { "outer": [ { "the_folder": { "inner": [ {"some_multiple_text": ["foo", "bar"]}, {"some_multiple_text": None} ], } } ] } expected: Outcome = Outcome() expected.mismatches.append(ValueMismatch(entity_id, "immutable", "/outer", "List", json.dumps(fixture["outer"]), json.dumps(observed["outer"]))) actual: Outcome = Outcome() crawl: CrawlImmutable = CrawlImmutable(entity_id, schema, fixture, observed, actual) crawl() assert expected == actual
def test_not_tested_simple(schema, period, entity_id): """One mismatch, one field omitted from fixture.""" fixture: Dict = { "some_multiple_text": ["x", "y"], "outer": { "inner": { "some_multiple_text": ["xxx", "yyy"], } } } observation: Dict = { "some_multiple_text": ["x", "y"], "outer": { "some_multiple_text": ["xx", "yy"], "inner": { "some_multiple_text": ["aaa"], } } } # 1 match, 1 mismatch expected: Outcome = Outcome() expected.matches.append( ValueMatch(entity_id, period, "/some_multiple_text", "MultipleText", json.dumps(fixture["some_multiple_text"]))) expected.mismatches.append( ValueMismatch( entity_id, period, "/outer/inner/some_multiple_text", "MultipleText", json.dumps(fixture["outer"]["inner"]["some_multiple_text"]), json.dumps(["aaa"]))) actual: Outcome = Outcome() crawl: CrawlPeriod = CrawlPeriod(entity_id, schema, fixture, observation, actual, period) crawl() assert expected == actual
def _record_mismatch(self, path: ListType, data_type: str, expected: Optional[Any], actual: Optional[Any]) -> None: path_str = nesteddicts.path_to_str(path) mismatch: ValueMismatch = ValueMismatch(self.entity_id, self.label, path_str, data_type, expected, actual) self.outcome.mismatches.append(mismatch)