Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hbcarlos committed Oct 13, 2023
1 parent 3ae1368 commit c1e4991
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
18 changes: 9 additions & 9 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
from asyncio import Event, sleep
from datetime import datetime
from logging import getLogger
from typing import Any

import nbformat
Expand Down Expand Up @@ -170,9 +171,7 @@ def rtc_create_SQLite_store(jp_serverapp):
setattr(SQLiteYStore, k, v)

async def _inner(type: str, path: str, content: str) -> DocumentRoom:
room_id = f"{type}:{path}"
db = SQLiteYStore()
await db.start()
db = SQLiteYStore(log=getLogger(__name__))
await db.initialize()

if type == "notebook":
Expand All @@ -182,8 +181,8 @@ async def _inner(type: str, path: str, content: str) -> DocumentRoom:

doc.source = content

await db.create(room_id, 0)
await db.encode_state_as_update(room_id, doc.ydoc)
await db.create(path, 0)
await db.encode_state_as_update(path, doc.ydoc)

return db

Expand All @@ -193,22 +192,23 @@ async def _inner(type: str, path: str, content: str) -> DocumentRoom:
@pytest.fixture
def rtc_create_mock_document_room():
def _inner(
id: str,
room_id: str,
path_id: str,
path: str,
content: str,
last_modified: datetime | None = None,
save_delay: float | None = None,
store: SQLiteYStore | None = None,
) -> tuple[FakeContentsManager, FileLoader, DocumentRoom]:
paths = {id: path}
paths = {path_id: path}

if last_modified is None:
cm = FakeContentsManager({"content": content})
else:
cm = FakeContentsManager({"last_modified": datetime.now(), "content": content})

loader = FileLoader(
id,
path_id,
FakeFileIDManager(paths),
cm,
poll_interval=0.1,
Expand All @@ -218,7 +218,7 @@ def _inner(
cm,
loader,
DocumentRoom(
"test-room", "text", "file", loader, FakeEventLogger(), store, None, save_delay
room_id, "text", "file", loader, FakeEventLogger(), store, None, save_delay
),
)

Expand Down
27 changes: 17 additions & 10 deletions tests/test_rooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@pytest.mark.asyncio
async def test_should_initialize_document_room_without_store(rtc_create_mock_document_room):
content = "test"
_, _, room = rtc_create_mock_document_room("test-id", "test.txt", content)
_, _, room = rtc_create_mock_document_room("test-id", "test_path", "test.txt", content)

await room.initialize()
assert room._document.source == content
Expand All @@ -29,10 +29,11 @@ async def test_should_initialize_document_room_from_store(
# If the content from the store is different than the content from disk,
# the room will initialize with the content from disk and overwrite the document

id = "test-id"
room_id = "test-id"
path_id = "test_path"
content = "test"
store = await rtc_create_SQLite_store("file", id, content)
_, _, room = rtc_create_mock_document_room("test-id", "test.txt", content, store=store)
store = await rtc_create_SQLite_store("file", room_id, content)
_, _, room = rtc_create_mock_document_room(room_id, path_id, "test.txt", content, store=store)

await room.initialize()
assert room._document.source == content
Expand All @@ -43,13 +44,13 @@ async def test_should_overwrite_the_store(rtc_create_SQLite_store, rtc_create_mo
id = "test-id"
content = "test"
store = await rtc_create_SQLite_store("file", id, "whatever")
_, _, room = rtc_create_mock_document_room("test-id", "test.txt", content, store=store)
_, _, room = rtc_create_mock_document_room(id, "test_path", "test.txt", content, store=store)

await room.initialize()
assert room._document.source == content

doc = YUnicode()
await store.apply_updates(doc.ydoc)
await store.apply_updates(id, doc.ydoc)

assert doc.source == content

Expand All @@ -59,7 +60,9 @@ async def test_defined_save_delay_should_save_content_after_document_change(
rtc_create_mock_document_room,
):
content = "test"
cm, _, room = rtc_create_mock_document_room("test-id", "test.txt", content, save_delay=0.01)
cm, _, room = rtc_create_mock_document_room(
"test-id", "test_path", "test.txt", content, save_delay=0.01
)

await room.initialize()
room._document.source = "Test 2"
Expand All @@ -75,7 +78,9 @@ async def test_undefined_save_delay_should_not_save_content_after_document_chang
rtc_create_mock_document_room,
):
content = "test"
cm, _, room = rtc_create_mock_document_room("test-id", "test.txt", content, save_delay=None)
cm, _, room = rtc_create_mock_document_room(
"test-id", "test_path", "test.txt", content, save_delay=None
)

await room.initialize()
room._document.source = "Test 2"
Expand All @@ -92,7 +97,7 @@ async def test_should_reload_content_from_disk(rtc_create_mock_document_room):
last_modified = datetime.now()

cm, loader, room = rtc_create_mock_document_room(
"test-id", "test.txt", "whatever", last_modified
"test-id", "test_path", "test.txt", "whatever", last_modified
)

await room.initialize()
Expand All @@ -114,7 +119,9 @@ async def test_should_not_reload_content_from_disk(rtc_create_mock_document_room
content = "test"
last_modified = datetime.now()

cm, loader, room = rtc_create_mock_document_room("test-id", "test.txt", content, last_modified)
cm, loader, room = rtc_create_mock_document_room(
"test-id", "test_path", "test.txt", content, last_modified
)

await room.initialize()

Expand Down

0 comments on commit c1e4991

Please sign in to comment.