Esempio n. 1
0
    def test_repository_all_is_idempotent(
        self,
        repo: Repository,
        inserted_entities: List[Entity],
    ) -> None:
        """
        Given: A repository that has already used the all method.
        When: all is called again.
        Then: it returns the same results.
        """
        entity_type = type(inserted_entities[0])
        expected_results = repo.all(entity_type)

        result = repo.all(entity_type)

        assert result == expected_results
Esempio n. 2
0
def test_empty_removes_all_entities(repo: Repository) -> None:
    """
    Given: A full repository
    When: calling empty
    Then: No entity remains in the repository
    """
    books = BookFactory.batch(25)
    genres = GenreFactory.batch(20)
    repo.add(books)
    repo.add(genres)
    repo.commit()

    result = repo.empty()

    assert result is None
    assert repo.all(Book) == []
    assert repo.all(Genre) == []
Esempio n. 3
0
    def test_repository_all_returns_empty_list_if_there_are_no_entities_of_a_type(
        self,
        repo: Repository,
    ) -> None:
        """
        Given: An empty repo
        When: all is used with an entity type
        Then: An empty list is returned
        """
        result: List[Entity] = repo.all(Author)

        assert result == []
Esempio n. 4
0
    def test_repository_can_retrieve_all_objects_of_an_entity_type(
        self,
        repo: Repository,
        inserted_entities: List[Entity],
    ) -> None:
        """Given an entity type all the entity objects that match should be returned."""
        entity_type = type(inserted_entities[0])

        result = repo.all(entity_type)

        assert result == inserted_entities
        assert len(result) == 3
        assert result[0].id_ == inserted_entities[0].id_
Esempio n. 5
0
    def test_repository_can_delete_an_entity(
        self,
        repo: Repository,
        inserted_entities: List[Entity],
    ) -> None:
        """
        Given: a full repository.
        When: an entity is deleted.
        Then: the entity is not longer in the repository.
        """
        entity_to_delete = inserted_entities[1]
        repo.delete(entity_to_delete)

        repo.commit()  # act

        remaining_entities = repo.all(type(entity_to_delete))
        assert entity_to_delete not in remaining_entities
Esempio n. 6
0
    def test_repository_can_save_an_two_entities_without_id_in_empty_repo(
            self, repo: Repository, int_entity: Entity, merge: bool) -> None:
        """
        Given: An empty repository and an entity whose id_ type is an int
        When: adding two entity without id
        Then: the ids 0 and 1 are set
        """
        first_entity = int_entity.__class__(name="First entity without id")
        second_entity = int_entity.__class__(name="Second entity without id")
        repo.add([first_entity, second_entity], merge=merge)

        repo.commit()  # act

        entities = repo.all(type(int_entity))
        assert len(entities) == 2
        assert entities[0].id_ == 0
        assert entities[0].name == "First entity without id"
        assert entities[1].id_ == 1
        assert entities[1].name == "Second entity without id"
Esempio n. 7
0
    def test_repository_can_save_an_entity_without_id_in_empty_repo(
        self,
        repo: Repository,
        int_entity: Entity,
        merge: bool,
    ) -> None:
        """
        Given: An empty repository and an entity whose id_ type is an int
        When: adding an entity without id
        Then: the id 0 is set
        """
        entity = int_entity.__class__(name="Entity without id")
        added_entity = repo.add(entity, merge=merge)

        repo.commit()  # act

        entities = repo.all(type(entity))
        assert len(entities) == 1
        assert added_entity.id_ == 0
        assert entities[0].id_ == 0
Esempio n. 8
0
    def test_repository_can_retrieve_all_objects_of_a_list_of_entity_types(
        self,
        repo: Repository,
        inserted_entities: List[Entity],
    ) -> None:
        """
        Given: Three entities of a type and another of other type.
        When: all is called using a list of entities
        Then: all elements are returned ordered by ID, we need it so that we know for
            sure that the results are always ordered.
        """
        other_entity = OtherEntity(id_=0, name="Other entity")
        repo.add(other_entity)
        repo.commit()
        entity_types: List[Type[Entity]] = [type(inserted_entities[0]), OtherEntity]
        repo.models = entity_types  # type: ignore
        with pytest.warns(UserWarning, match="In 2022-06-10.*deprecated"):

            result = repo.all(entity_types)

        assert result == [other_entity] + inserted_entities  # type: ignore
        assert len(result) == 4
Esempio n. 9
0
    def test_repository_can_save_two_entities_without_id_full_repo(
        self, repo: Repository, inserted_int_entity: Entity, merge: bool
    ) -> None:
        """
        Given: A repository with an entity whose id_ type is an int
        When: adding two entities without id
        Then: the id of the new entities is one unit greater than the last one.
        """
        first_entity = inserted_int_entity.__class__(name="First entity without id")
        second_entity = inserted_int_entity.__class__(name="Second entity without id")
        repo.add([first_entity, second_entity], merge=merge)  # type: ignore

        repo.commit()  # act

        entities = repo.all(type(inserted_int_entity))
        assert len(entities) == 3
        # ignore: we know that the entities have an int id_
        assert entities[1].id_ == inserted_int_entity.id_ + 1  # type: ignore
        assert entities[1].name == "First entity without id"
        # ignore: we know that the entities have an int id_
        assert entities[2].id_ == inserted_int_entity.id_ + 2  # type: ignore
        assert entities[2].name == "Second entity without id"
Esempio n. 10
0
    def test_repository_can_retrieve_all(
        self,
        repo: Repository,
        inserted_entities: List[Entity],
    ) -> None:
        """
        Given: A repository with inserted entities
        When: all is called
        Then: all entities are returned and saved to the repo cache.
            The defined_values of all entities are empty, otherwise the merge fails.
        """
        entity_types: List[Type[Entity]] = [type(inserted_entities[0]), OtherEntity]
        repo.models = entity_types  # type: ignore
        with pytest.warns(UserWarning, match="In 2022-06-10.*deprecated"):

            result: List[Entity] = repo.all()

        assert result == inserted_entities
        assert len(result) == 3
        assert result[0] < result[1]
        assert result[1] < result[2]
        for entity in result:
            assert repo.cache.get(entity) == entity
            assert entity.defined_values == {}
Esempio n. 11
0
def create_greeting(repo: Repository) -> str:
    first_author = repo.all(Author)[0]
    return f"Hi {first_author.first_name}, you're the first author!"