def assert_people_match(self,
                         expected_people: List[StatePerson],
                         matched_people: List[schema.StatePerson],
                         debug: bool = False):
     converted_matched = converter.convert_schema_objects_to_entity(
         matched_people)
     db_expected_with_backedges = converter.convert_entity_people_to_schema_people(
         expected_people)
     expected_with_backedges = converter.convert_schema_objects_to_entity(
         db_expected_with_backedges)
     if debug:
         print_visible_header_label('EXPECTED')
         print_entity_trees(expected_with_backedges)
         print_visible_header_label('FINAL')
         print_entity_trees(converted_matched)
     self.assertEqual(expected_with_backedges, converted_matched)
    def _assert_people_match(
            self, expected_people, matched_people, debug=False):
        converted_matched = \
            converter.convert_schema_objects_to_entity(matched_people)
        db_expected_with_backedges = \
            converter.convert_entity_people_to_schema_people(expected_people)
        expected_with_backedges = \
            converter.convert_schema_objects_to_entity(
                db_expected_with_backedges)

        clear_db_ids(converted_matched)
        clear_db_ids(expected_with_backedges)

        if debug:
            print('============== EXPECTED WITH BACKEDGES ==============')
            print_entity_trees(expected_with_backedges)
            print('============== CONVERTED MATCHED ==============')
            print_entity_trees(converted_matched)
        self.assertCountEqual(expected_with_backedges, converted_matched)
예제 #3
0
    def assert_people_match(self,
                            expected_people: List[StatePerson],
                            matched_people: List[schema.StatePerson],
                            debug: bool = True):
        converted_matched = \
            converter.convert_schema_objects_to_entity(matched_people)
        db_expected_with_backedges = \
            converter.convert_entity_people_to_schema_people(expected_people)
        expected_with_backedges = \
            converter.convert_schema_objects_to_entity(
                db_expected_with_backedges)

        if debug:
            print('============== EXPECTED WITH BACKEDGES ==============')
            print_entity_trees(expected_with_backedges)
            print('============== CONVERTED MATCHED ==============')
            print_entity_trees(converted_matched)

        self.assertCountEqual(expected_with_backedges, converted_matched)
    def _run_convert_test(
        self,
        start_entities: List[Entity],
        expected_entities: List[Entity],
        populate_back_edges: bool,
        debug: bool = False,
    ):
        """
        Runs a test that takes in entities,
        converts them to schema objects and back, optionally writing to the DB,
        then checks that the result is expected.

        Args:
            start_entities: A list of entities to convert.
            expected_entities: A list of entities with expected final structure
                after conversion to schema objects and back.
        """

        self.assertEqual(len(start_entities), len(expected_entities))
        expected_length = len(start_entities)

        # Convert entities to schema objects
        schema_objects = StateEntityToSchemaConverter().convert_all(
            start_entities, populate_back_edges
        )
        self.assertEqual(len(schema_objects), expected_length)

        # Get the list of schema objects we want to convert back to entities
        schema_objects_to_convert = schema_objects

        # Convert schema objects back to entities
        result = StateSchemaToEntityConverter().convert_all(
            schema_objects_to_convert, populate_back_edges
        )
        self.assertEqual(len(result), len(expected_entities))
        if debug:
            print("============== EXPECTED WITH BACKEDGES ==============")
            print_entity_trees(expected_entities)
            print("============== CONVERTED MATCHED ==============")
            print_entity_trees(result)

        self.assertCountEqual(result, expected_entities)
    def assert_expected_db_people(
        self,
        expected_db_people: List[StatePerson],
        debug: bool = False,
        single_person_to_debug: Optional[str] = None,
        # TODO(#2492): Once we properly clean up dangling placeholders,
        #  delete this.
        ignore_dangling_placeholders: bool = False,
        print_tree_structure_only: bool = False,
    ) -> None:
        """Asserts that the set of expected people matches all the people that currently exist in the database.

        Args:
            debug: (bool) If true, prints out both the found and expected entity trees.
            single_person_to_debug: (str) A string external_id of a person. If debug=True and this is not None, this
                will only check for equality between the people with that external_id. This should be used for debugging
                only and this function will throw if this value is set in CI.
            ignore_dangling_placeholders: (bool) If True, eliminates dangling placeholder objects (i.e. placeholders
                with no non-placeholder children) from both the result and expected trees before doing a comparison.
            print_tree_structure_only: (bool) If True and debug=True, then the printed result only shows the tree
                structure - external ids and parent-child relationships.
        """

        if debug:
            print("\n\n************** ASSERTING *************")
        session = SessionFactory.for_schema_base(StateBase)
        found_people_from_db = dao.read_people(session)
        found_people = cast(
            List[StatePerson],
            self.convert_and_clear_db_ids(found_people_from_db))

        if ignore_dangling_placeholders:
            pruned_found_people = []
            for person in found_people:
                pruned_person = cast(
                    StatePerson, prune_dangling_placeholders_from_tree(person))
                if pruned_person is not None:
                    pruned_found_people.append(pruned_person)
            found_people = pruned_found_people

            pruned_expected_people: List[StatePerson] = []
            for person in expected_db_people:
                pruned_expected_person = cast(
                    StatePerson, prune_dangling_placeholders_from_tree(person))
                if pruned_expected_person is not None:
                    pruned_expected_people.append(pruned_expected_person)
            expected_db_people = pruned_expected_people

        if debug:
            if is_running_in_ci():
                self.fail(
                    "The |debug| flag should only be used for local debugging."
                )
            if single_person_to_debug is not None:
                found_people = [
                    p for p in found_people
                    if person_has_id(p, single_person_to_debug)
                ]
                expected_db_people = [
                    p for p in expected_db_people
                    if person_has_id(p, single_person_to_debug)
                ]

            print_visible_header_label("FINAL")
            print_entity_trees(
                found_people,
                print_tree_structure_only=print_tree_structure_only)

            print_visible_header_label("EXPECTED")
            print_entity_trees(
                expected_db_people,
                print_tree_structure_only=print_tree_structure_only)

        self.assertCountEqual(found_people, expected_db_people)

        assert_no_unexpected_entities_in_db(found_people_from_db, session)