def test_pruneDanglingPlaceholders_isDangling(self):
        # Arrange
        dangling_placeholder_person = generate_person()
        dangling_placeholder_sg = generate_sentence_group()

        # Act
        pruned_person = \
            prune_dangling_placeholders_from_tree(dangling_placeholder_person)
        pruned_sentence_group = \
            prune_dangling_placeholders_from_tree(dangling_placeholder_sg)

        # Assert
        self.assertIsNone(pruned_person)
        self.assertIsNone(pruned_sentence_group)
Esempio n. 2
0
    def test_pruneDanglingPlaceholders_placeholderHasNonPlaceholderChildren(self):
        # Arrange
        non_placeholder_sg = generate_sentence_group(external_id="external_id")
        placeholder_person = generate_person(sentence_groups=[non_placeholder_sg])

        expected_non_placeholder_sg = generate_sentence_group(external_id="external_id")
        expected_placeholder_person = generate_person(
            sentence_groups=[expected_non_placeholder_sg]
        )

        # Act
        pruned_tree = prune_dangling_placeholders_from_tree(placeholder_person)

        # Assert
        self.assertIsNotNone(pruned_tree)
        self.assertEqual(
            attr.evolve(self.to_entity(pruned_tree)),
            attr.evolve(self.to_entity(expected_placeholder_person)),
        )
    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)