def test_backup_and_restore(pg): with temp_db(pg) as db_name: fp = tempfile.NamedTemporaryFile() pg.dump(db_name, fp.name) pg.drop(db_name) assert not pg.exists(db_name) fp.seek(0) pg.restore(db_name, fp.name) assert pg.exists(db_name)
def test_temp_db_failed_create(): class FakeDB(object): def create(self, name): pass def exists(self, name): return False with pytest.raises(DatabaseError): with temp_db(FakeDB()): pass
def test_temp_db(): class FakeStatefulDB(object): _exists = False def create(self, name): self._exists = True def drop(self, name): self._exists = False def exists(self, name): return self._exists db = FakeStatefulDB() with temp_db(db, 'foo') as name: assert name == 'foo' assert db.exists(name) assert not db.exists(name) with temp_db(db) as name: assert name.startswith('db')
def test_temp_db_failed_drop(): class FakeDB(object): _exists = None def create(self, name): self._exists = True def drop(self, name): self._exists = True def exists(self, name): return self._exists with pytest.raises(DatabaseError): with temp_db(FakeDB()): pass