async def test_CRUD(ha: HaSource, time, interceptor: RequestInterceptor, data_cache: DataCache) -> None: snapshot: HASnapshot = await ha.create( CreateOptions(time.now(), "Test Name")) assert snapshot.name() == "Test Name" assert type(snapshot) is HASnapshot assert not snapshot.retained() assert snapshot.snapshotType() == "full" assert not snapshot.protected() assert snapshot.name() == "Test Name" assert snapshot.source() == SOURCE_HA assert not snapshot.ignore() assert snapshot.madeByTheAddon() assert "pending" not in data_cache.snapshots # read the item directly, its metadata should match from_ha = await ha.harequests.snapshot(snapshot.slug()) assert from_ha.size() == snapshot.size() assert from_ha.slug() == snapshot.slug() assert from_ha.source() == SOURCE_HA snapshots = await ha.get() assert len(snapshots) == 1 assert snapshot.slug() in snapshots full = DummySnapshot(from_ha.name(), from_ha.date(), from_ha.size(), from_ha.slug(), "dummy") full.addSource(snapshot) # download the item, its bytes should match up download = await ha.read(full) await download.setup() direct_download = await ha.harequests.download(snapshot.slug()) await direct_download.setup() while True: from_file = await direct_download.read(1024 * 1024) from_download = await download.read(1024 * 1024) if len(from_file.getbuffer()) == 0: assert len(from_download.getbuffer()) == 0 break assert from_file.getbuffer() == from_download.getbuffer() # update retention assert not snapshot.retained() await ha.retain(full, True) assert (await ha.get())[full.slug()].retained() await ha.retain(full, False) assert not (await ha.get())[full.slug()].retained() # Delete the item, make sure its gone await ha.delete(full) assert full.getSource(ha.name()) is None snapshots = await ha.get() assert len(snapshots) == 0
async def test_retained_on_finish(ha: HaSource, server, time, config: Config, supervisor: SimulatedSupervisor): async with supervisor._snapshot_inner_lock: retention = {ha.name(): True} config.override(Setting.NEW_SNAPSHOT_TIMEOUT_SECONDS, 0.0001) pending = await ha.create( CreateOptions(time.now(), "Test Name", retention)) results = await ha.get() assert pending.name() == "Test Name" assert results == {pending.slug(): pending} assert type(pending) == PendingSnapshot assert not ha._pending_snapshot_task.done() await asyncio.wait({ha._pending_snapshot_task}) results = list((await ha.get()).values()) assert len(results) == 1 assert results[0].name() == "Test Name" assert type(results[0]) == HASnapshot assert results[0].retained() assert config.isRetained(results[0].slug())