def test_load_repository_returns_error_if_url_not_recognized(self) -> None: """ Given: Nothing When: load_repository is called with an unrecognized url Then: An error is raised """ with pytest.raises(ValueError, match="Database URL: .* not recognized."): load_repository(database_url="inexistent://path/to/file.db")
def test_load_repository_loads_models(self) -> None: """ Given: Nothing When: load_repository is called with the models. Then: a warning is raised not to use it """ with pytest.warns(UserWarning, match="In 2022-12-10.*deprecated"): load_repository(models=[Author]) # act
def test_load_repository_set_search_exception_false(self) -> None: """ Given: Nothing When: loading the repository with search_exception not None Then: a warning is raised not to use it See ADR 005 for more info. """ with pytest.warns(UserWarning, match="In 2022-12-10.*deprecated"): load_repository(search_exception=False) # act
def test_load_repository_loads_fake_with_fake_urls(self) -> None: """ Given: Nothing When: load_repository is called without argument Then: a working FakeRepository instance is returned """ result = load_repository(database_url="fake://fake.db") assert isinstance(result, FakeRepository)
def test_load_repository_loads_fake_by_default(self) -> None: """ Given: Nothing When: load_repository is called without argument Then: a working FakeRepository instance is returned """ result = load_repository() assert isinstance(result, FakeRepository)
def test_load_repository_loads_pypika_with_sqlite_urls( self, db_sqlite: Tuple[str, sqlite3.Cursor]) -> None: """ Given: Nothing When: load_repository is called with a pypika compatible url Then: a working PypikaRepository instance is returned """ result = load_repository(database_url=db_sqlite[0]) assert isinstance(result, PypikaRepository)
def test_load_repository_loads_tinydb_with_sqlite_urls( self, db_tinydb: Tuple[str, TinyDB]) -> None: """ Given: Nothing When: load_repository is called with a tinydb compatible url Then: a working TinyDBRepository instance is returned """ result = load_repository(database_url=db_tinydb[0]) assert isinstance(result, TinyDBRepository) result.close()
def test_load_repository_loads_models(self) -> None: """ Given: Nothing When: load_repository is called with the models. Then: they are saved """ models = [Author] with pytest.warns(UserWarning, match="In 2022-06-10.*deprecated"): result = load_repository(models=models) assert result.models == models
def test_load_repository_set_search_exception_false(self) -> None: """ Given: loading the repository with search_exception False When: running search on a criteria that returns no results Then: an empty list is returned instead of an exception. See ADR 005 for more info. """ repo = load_repository(search_exception=False) result = repo.search({"id_": 1}, Author) assert result == []
def test_search_doesnt_raise_exception_if_search_exception_false( self, repo: Repository ) -> None: """ Given: A repository with search_exception False When: running search on a criteria that returns no results Then: an empty list is returned instead of an exception. See ADR 005 for more info. """ repo = load_repository(search_exception=False) result = repo.search({"id_": "inexistent"}, Author) assert result == []
def repo_e2e(config: Config) -> Repository: """Configure the end to end repository.""" return load_repository([Task, RecurrentTask], config["database_url"])
from repository_orm import Entity, load_repository class Author(Entity): first_name: str last_name: str country: str repo = load_repository() author = Author(first_name="Brandon", last_name="Sanderson", country="US") # Add entities repo.add(author) repo.commit() # Retrieve entities by their ID brandon = repo.get(0, Author) assert brandon == author # Search entities brandon = repo.search({"first_name": "Brandon"}, Author)[0] assert brandon == author # Delete entities repo.delete(brandon) repo.commit() assert len(repo.all(Author)) == 0 # Close the connection
def greet(database_url: str) -> None: repo = load_repository(database_url) print(create_greeting(repo)) repo.close()
def get_repo(config: Config) -> Repository: """Configure the Repository.""" log.debug("Initializing the repository") repo = load_repository([Task, RecurrentTask], config["database_url"]) return repo