Skip to content

Commit

Permalink
Pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hbcarlos committed Sep 29, 2023
1 parent a887b64 commit 5a1adb7
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 159 deletions.
91 changes: 48 additions & 43 deletions tests/test_file_store.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import pytest

import time
import anyio
import struct
import time
from pathlib import Path

from ypy_websocket.stores import FileYStore, DocExists
import anyio
import pytest

from ypy_websocket.stores import DocExists, FileYStore
from ypy_websocket.yutils import Decoder, write_var_uint


@pytest.fixture
def create_store():
async def _inner(path: str, version: int) -> None:
await anyio.Path(path).mkdir(parents=True, exist_ok=True)
version_path = Path(path, "__version__")
async with await anyio.open_file(version_path, "wb") as f:
version_bytes = str(version).encode()
await f.write(version_bytes)
version_bytes = str(version).encode()
await f.write(version_bytes)

return _inner


@pytest.fixture
def add_document():
async def _inner(path: str, doc_path: str, version: int, data: bytes = b"") -> None:
Expand All @@ -36,23 +38,25 @@ async def _inner(path: str, doc_path: str, version: int, data: bytes = b"") -> N
timestamp = struct.pack("<d", time.time())
timestamp_len = write_var_uint(len(timestamp))
await f.write(timestamp_len + timestamp)

return _inner


@pytest.mark.anyio
async def test_initialization(tmp_path):
path = tmp_path / "tmp"
store = FileYStore(str(path))
await store.start()
await store.initialize()

assert store.initialized == True
assert store.initialized

version_path = Path(path / "__version__")
async with await anyio.open_file(version_path, "rb") as f:
version = int(await f.readline())
assert FileYStore.version == version


@pytest.mark.anyio
async def test_initialization_with_old_store(tmp_path, create_store):
path = tmp_path / "tmp"
Expand All @@ -64,13 +68,14 @@ async def test_initialization_with_old_store(tmp_path, create_store):
await store.start()
await store.initialize()

assert store.initialized == True
assert store.initialized

version_path = Path(path / "__version__")
async with await anyio.open_file(version_path, "rb") as f:
version = int(await f.readline())
assert FileYStore.version == version


@pytest.mark.anyio
async def test_initialization_with_existing_store(tmp_path, create_store, add_document):
path = tmp_path / "tmp"
Expand All @@ -84,16 +89,16 @@ async def test_initialization_with_existing_store(tmp_path, create_store, add_do
await store.start()
await store.initialize()

assert store.initialized == True
assert store.initialized

version_path = Path(path / "__version__")
async with await anyio.open_file(version_path, "rb") as f:
version = int(await f.readline())
assert FileYStore.version == version

file_path = Path(path / (doc_path + ".y"))
res = await anyio.Path(file_path).exists()
assert res == True
assert await anyio.Path(file_path).exists()


@pytest.mark.anyio
async def test_exists(tmp_path, create_store, add_document):
Expand All @@ -108,13 +113,12 @@ async def test_exists(tmp_path, create_store, add_document):
await store.start()
await store.initialize()

assert store.initialized == True
assert store.initialized

res = await store.exists(doc_path)
assert res == True
assert await store.exists(doc_path)

assert not await store.exists("random.path")

res = await store.exists("random.path")
assert res == False

@pytest.mark.anyio
async def test_list(tmp_path, create_store, add_document):
Expand All @@ -131,15 +135,16 @@ async def test_list(tmp_path, create_store, add_document):
await store.start()
await store.initialize()

assert store.initialized == True
assert store.initialized

count = 0
async for doc in store.list():
count += 1
#assert doc in [doc1, doc2]
# assert doc in [doc1, doc2]

assert count == 2


@pytest.mark.anyio
async def test_get(tmp_path, create_store, add_document):
path = tmp_path / "tmp"
Expand All @@ -153,14 +158,15 @@ async def test_get(tmp_path, create_store, add_document):
await store.start()
await store.initialize()

assert store.initialized == True
assert store.initialized

res = await store.get(doc_path)
assert res["path"] == doc_path
assert res["version"] == 0

res = await store.get("random.doc")
assert res == None
assert res is None


@pytest.mark.anyio
async def test_create(tmp_path, create_store, add_document):
Expand All @@ -175,7 +181,7 @@ async def test_create(tmp_path, create_store, add_document):
await store.start()
await store.initialize()

assert store.initialized == True
assert store.initialized

new_doc = "new_doc.path"
await store.create(new_doc, 0)
Expand All @@ -184,14 +190,15 @@ async def test_create(tmp_path, create_store, add_document):
async with await anyio.open_file(file_path, "rb") as f:
header = await f.read(8)
assert header == b"VERSION:"

version = int(await f.readline())
assert version == 0

with pytest.raises(DocExists) as e:
with pytest.raises(DocExists) as e:
await store.create(doc_path, 0)
assert str(e.value) == f"The document {doc_path} already exists."



@pytest.mark.anyio
async def test_remove(tmp_path, create_store, add_document):
path = tmp_path / "tmp"
Expand All @@ -205,19 +212,17 @@ async def test_remove(tmp_path, create_store, add_document):
await store.start()
await store.initialize()

assert store.initialized == True
assert store.initialized

await store.remove(doc_path)
res = await store.exists(doc_path)
assert res == False
assert not await store.exists(doc_path)

new_doc = "new_doc.path"
res = await store.exists(new_doc)
assert res == False
assert not await store.exists(new_doc)

await store.remove(new_doc)
res = await store.exists(new_doc)
assert res == False
assert not await store.exists(new_doc)


@pytest.mark.anyio
async def test_read(tmp_path, create_store, add_document):
Expand All @@ -233,15 +238,16 @@ async def test_read(tmp_path, create_store, add_document):
await store.start()
await store.initialize()

assert store.initialized == True
assert store.initialized

count = 0
async for u,_,_ in store.read(doc_path):
async for u, _, _ in store.read(doc_path):
count += 1
assert update == u

assert count == 1


@pytest.mark.anyio
async def test_write(tmp_path, create_store, add_document):
path = tmp_path / "tmp"
Expand All @@ -255,16 +261,16 @@ async def test_write(tmp_path, create_store, add_document):
await store.start()
await store.initialize()

assert store.initialized == True
assert store.initialized

update = b"foo"
await store.write(doc_path, update)

file_path = Path(path / (doc_path + ".y"))
async with await anyio.open_file(file_path, "rb") as f:
header = await f.read(8)
assert header == b"VERSION:"

version = int(await f.readline())
assert version == 0

Expand All @@ -278,6 +284,5 @@ async def test_write(tmp_path, create_store, add_document):
# The fixture add_document inserts an empty update
assert u in [b"", update]
i = (i + 1) % 3

assert count == 2

Loading

0 comments on commit 5a1adb7

Please sign in to comment.