Simplified database layer and consolidated Markov writes into a single transaction.
This commit is contained in:
+42
-90
@@ -15,16 +15,14 @@
|
||||
"""Unit tests for the Database class.
|
||||
|
||||
Tests cover Database.connect (pragmas, schema), markov start word and
|
||||
transition CRUD, image storage, flag CRUD, channel ingestion tracking,
|
||||
and the auto-commit write-batching mechanism.
|
||||
transition CRUD, image storage, flag CRUD, and channel ingestion tracking.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import pytest
|
||||
|
||||
from crabstero.database import AUTO_COMMIT_WRITE_THRESHOLD, Database
|
||||
from crabstero.database import Database
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from pathlib import Path
|
||||
@@ -33,13 +31,6 @@ if TYPE_CHECKING:
|
||||
class TestConnect:
|
||||
"""Database.connect creates a configured SQLite database."""
|
||||
|
||||
async def test_wal_mode(self, db: Database) -> None:
|
||||
"""Journal mode is set to WAL."""
|
||||
async with db._connection.execute("PRAGMA journal_mode") as cursor:
|
||||
row = await cursor.fetchone()
|
||||
assert row is not None
|
||||
assert row[0] == "wal"
|
||||
|
||||
async def test_synchronous_normal(self, db: Database) -> None:
|
||||
"""Synchronous mode is set to NORMAL."""
|
||||
async with db._connection.execute("PRAGMA synchronous") as cursor:
|
||||
@@ -62,42 +53,34 @@ class TestConnect:
|
||||
]
|
||||
|
||||
|
||||
class TestMarkovStartWords:
|
||||
"""Start word storage and random retrieval."""
|
||||
class TestAddMarkovData:
|
||||
"""Markov start word and transition storage via add_markov_data."""
|
||||
|
||||
async def test_add_and_retrieve(self, db: Database) -> None:
|
||||
async def test_stores_start_word(self, db: Database) -> None:
|
||||
"""Inserted start word can be retrieved by channel."""
|
||||
await db.add_start_words_batch([(1, 100, "Hello")])
|
||||
await db.commit()
|
||||
await db.add_markov_data([(1, 100, "Hello")], [])
|
||||
result = await db.get_random_start_word(1)
|
||||
assert result == "Hello"
|
||||
|
||||
async def test_returns_none_when_empty(self, db: Database) -> None:
|
||||
"""Returns None for a channel with no start words."""
|
||||
result = await db.get_random_start_word(999)
|
||||
assert result is None
|
||||
|
||||
async def test_empty_batch_stores_nothing(self, db: Database) -> None:
|
||||
"""An empty batch does not store any start words."""
|
||||
await db.add_start_words_batch([])
|
||||
await db.commit()
|
||||
assert await db.get_random_start_word(1) is None
|
||||
|
||||
|
||||
class TestMarkovTransitions:
|
||||
"""Word transition storage and random retrieval."""
|
||||
|
||||
async def test_add_and_retrieve(self, db: Database) -> None:
|
||||
async def test_stores_transition(self, db: Database) -> None:
|
||||
"""Inserted transition can be retrieved by channel and word."""
|
||||
await db.add_transitions_batch([(1, 100, "Hello", "world.")])
|
||||
await db.commit()
|
||||
await db.add_markov_data([], [(1, 100, "Hello", "world.")])
|
||||
result = await db.get_random_next_word(1, "Hello")
|
||||
assert result == "world."
|
||||
|
||||
async def test_returns_none_when_empty(self, db: Database) -> None:
|
||||
"""Returns None when no transitions exist for the word."""
|
||||
result = await db.get_random_next_word(1, "nonexistent")
|
||||
assert result is None
|
||||
async def test_stores_both_in_single_call(self, db: Database) -> None:
|
||||
"""Start words and transitions are stored in a single call."""
|
||||
await db.add_markov_data(
|
||||
[(1, 100, "Hello")],
|
||||
[(1, 100, "Hello", "world.")],
|
||||
)
|
||||
assert await db.get_random_start_word(1) == "Hello"
|
||||
assert await db.get_random_next_word(1, "Hello") == "world."
|
||||
|
||||
async def test_empty_lists_store_nothing(self, db: Database) -> None:
|
||||
"""Empty lists do not store any data."""
|
||||
await db.add_markov_data([], [])
|
||||
assert await db.get_random_start_word(1) is None
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"completing_word",
|
||||
@@ -105,36 +88,38 @@ class TestMarkovTransitions:
|
||||
pytest.param("world.", id="period"),
|
||||
pytest.param("world!", id="exclamation"),
|
||||
pytest.param("world?", id="question"),
|
||||
pytest.param("world§", id="section-sign"),
|
||||
pytest.param("world\u00a7", id="section-sign"),
|
||||
],
|
||||
)
|
||||
async def test_completing_word_filters_punctuation(
|
||||
self, db: Database, completing_word: str
|
||||
) -> None:
|
||||
"""get_random_completing_next_word only returns sentence-ending words."""
|
||||
await db.add_transitions_batch(
|
||||
await db.add_markov_data(
|
||||
[],
|
||||
[
|
||||
(1, 100, "Hello", "beautiful"),
|
||||
(1, 100, "Hello", completing_word),
|
||||
]
|
||||
],
|
||||
)
|
||||
await db.commit()
|
||||
for _ in range(100):
|
||||
result = await db.get_random_completing_next_word(1, "Hello")
|
||||
assert result == completing_word
|
||||
|
||||
async def test_completing_returns_none_without_match(self, db: Database) -> None:
|
||||
"""Returns None when no transitions end with sentence punctuation."""
|
||||
await db.add_transitions_batch([(1, 100, "Hello", "beautiful")])
|
||||
await db.commit()
|
||||
await db.add_markov_data([], [(1, 100, "Hello", "beautiful")])
|
||||
result = await db.get_random_completing_next_word(1, "Hello")
|
||||
assert result is None
|
||||
|
||||
async def test_empty_batch_stores_nothing(self, db: Database) -> None:
|
||||
"""An empty batch does not store any transitions."""
|
||||
await db.add_transitions_batch([])
|
||||
await db.commit()
|
||||
assert await db.get_random_next_word(1, "Hello") is None
|
||||
|
||||
class TestMarkovReadMethods:
|
||||
"""Read behavior when no data has been stored."""
|
||||
|
||||
async def test_returns_none_when_empty(self, db: Database) -> None:
|
||||
"""Returns None for channels with no data."""
|
||||
assert await db.get_random_start_word(999) is None
|
||||
assert await db.get_random_next_word(1, "nonexistent") is None
|
||||
|
||||
|
||||
class TestImages:
|
||||
@@ -143,7 +128,6 @@ class TestImages:
|
||||
async def test_add_and_retrieve(self, db: Database) -> None:
|
||||
"""Inserted image URL can be retrieved by channel."""
|
||||
await db.add_image(1, 100, "https://example.com/cat.png")
|
||||
await db.commit()
|
||||
result = await db.get_random_image(1)
|
||||
assert result == "https://example.com/cat.png"
|
||||
|
||||
@@ -159,7 +143,6 @@ class TestFlags:
|
||||
async def test_set_and_check(self, db: Database) -> None:
|
||||
"""A set flag is reported as set."""
|
||||
await db.set_flag("channel", "123", "noReply")
|
||||
await db.commit()
|
||||
assert await db.is_flag_set("channel", "123", "noReply") is True
|
||||
|
||||
async def test_unset_flag_is_false(self, db: Database) -> None:
|
||||
@@ -169,16 +152,13 @@ class TestFlags:
|
||||
async def test_clear_flag(self, db: Database) -> None:
|
||||
"""A cleared flag is no longer reported as set."""
|
||||
await db.set_flag("channel", "123", "noReply")
|
||||
await db.commit()
|
||||
await db.clear_flag("channel", "123", "noReply")
|
||||
await db.commit()
|
||||
assert await db.is_flag_set("channel", "123", "noReply") is False
|
||||
|
||||
async def test_set_idempotent(self, db: Database) -> None:
|
||||
"""Setting the same flag twice does not raise."""
|
||||
await db.set_flag("channel", "123", "noReply")
|
||||
await db.set_flag("channel", "123", "noReply")
|
||||
await db.commit()
|
||||
assert await db.is_flag_set("channel", "123", "noReply") is True
|
||||
|
||||
|
||||
@@ -188,7 +168,6 @@ class TestChannelIngestion:
|
||||
async def test_mark_and_check(self, db: Database) -> None:
|
||||
"""A marked channel is reported as ingested."""
|
||||
await db.mark_channel_ingested(42)
|
||||
await db.commit()
|
||||
assert await db.is_channel_ingested(42) is True
|
||||
|
||||
async def test_not_ingested_by_default(self, db: Database) -> None:
|
||||
@@ -199,47 +178,20 @@ class TestChannelIngestion:
|
||||
"""Marking the same channel twice does not raise."""
|
||||
await db.mark_channel_ingested(42)
|
||||
await db.mark_channel_ingested(42)
|
||||
await db.commit()
|
||||
assert await db.is_channel_ingested(42) is True
|
||||
|
||||
|
||||
class TestAutoCommit:
|
||||
"""Write batching and automatic commit behavior."""
|
||||
class TestWriteDurability:
|
||||
"""Writes persist across close and reopen."""
|
||||
|
||||
async def test_commits_after_threshold(self, db: Database) -> None:
|
||||
"""Pending writes reset to zero after reaching the write threshold."""
|
||||
for i in range(AUTO_COMMIT_WRITE_THRESHOLD):
|
||||
await db.add_start_words_batch([(1, 100, f"word{i}")])
|
||||
assert db._pending_writes == 0
|
||||
assert await db.get_random_start_word(1) is not None
|
||||
|
||||
async def test_close_commits_pending(self, tmp_path: Path) -> None:
|
||||
"""close() commits any pending writes before closing."""
|
||||
db_path = str(tmp_path / "close_test.db")
|
||||
async def test_markov_data_survives_reopen(self, tmp_path: Path) -> None:
|
||||
"""Data written via add_markov_data is durable after close/reopen."""
|
||||
db_path = str(tmp_path / "durability.db")
|
||||
db = await Database.connect(db_path)
|
||||
await db.add_start_words_batch([(1, 100, "hello")])
|
||||
assert db._pending_writes > 0
|
||||
await db.add_markov_data([(1, 100, "Hello")], [(1, 100, "Hello", "world.")])
|
||||
await db.close()
|
||||
|
||||
# Reopen and verify data persisted.
|
||||
db2 = await Database.connect(db_path)
|
||||
result = await db2.get_random_start_word(1)
|
||||
assert await db2.get_random_start_word(1) == "Hello"
|
||||
assert await db2.get_random_next_word(1, "Hello") == "world."
|
||||
await db2.close()
|
||||
assert result == "hello"
|
||||
|
||||
async def test_close_without_pending_writes(self, tmp_path: Path) -> None:
|
||||
"""close() succeeds when there are no pending writes."""
|
||||
db = await Database.connect(str(tmp_path / "clean_close.db"))
|
||||
await db.close()
|
||||
|
||||
async def test_flush_timer_commits(
|
||||
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
"""Background flush timer commits pending writes after timeout."""
|
||||
monkeypatch.setattr("crabstero.database.AUTO_COMMIT_TIMEOUT_SECONDS", 0.05)
|
||||
db = await Database.connect(str(tmp_path / "timer_test.db"))
|
||||
await db.add_start_words_batch([(1, 100, "hello")])
|
||||
assert db._pending_writes > 0
|
||||
await asyncio.sleep(0.1)
|
||||
assert db._pending_writes == 0
|
||||
await db.close()
|
||||
|
||||
@@ -103,7 +103,6 @@ class TestSetClearCheck:
|
||||
async def test_set_then_check(self, db: Database, flag: Flag) -> None:
|
||||
"""A set flag is reported as set."""
|
||||
await set_flag(db, 1, EntityType.CHANNEL, flag)
|
||||
await db.commit()
|
||||
assert await is_flag_set(db, 1, EntityType.CHANNEL, flag) is True
|
||||
|
||||
async def test_unset_returns_false(self, db: Database) -> None:
|
||||
@@ -113,7 +112,5 @@ class TestSetClearCheck:
|
||||
async def test_clear_removes_flag(self, db: Database) -> None:
|
||||
"""A cleared flag is no longer reported as set."""
|
||||
await set_flag(db, 1, EntityType.CHANNEL, Flag.NO_REPLY)
|
||||
await db.commit()
|
||||
await clear_flag(db, 1, EntityType.CHANNEL, Flag.NO_REPLY)
|
||||
await db.commit()
|
||||
assert await is_flag_set(db, 1, EntityType.CHANNEL, Flag.NO_REPLY) is False
|
||||
|
||||
+33
-35
@@ -61,42 +61,42 @@ class TestIngestSentence:
|
||||
async def test_stores_start_word(self, db: Database) -> None:
|
||||
"""First word of the sentence is stored as a start word."""
|
||||
await _ingest_sentence(db, 1, 100, "Hello world.")
|
||||
await db.commit()
|
||||
|
||||
result = await db.get_random_start_word(1)
|
||||
assert result == "Hello"
|
||||
|
||||
async def test_stores_transitions(self, db: Database) -> None:
|
||||
"""Adjacent words create transitions."""
|
||||
await _ingest_sentence(db, 1, 100, "Hello world.")
|
||||
await db.commit()
|
||||
|
||||
result = await db.get_random_next_word(1, "Hello")
|
||||
assert result == "world."
|
||||
|
||||
async def test_appends_sentence_end_if_missing(self, db: Database) -> None:
|
||||
"""Unpunctuated sentence gets the default sentence-end marker."""
|
||||
await _ingest_sentence(db, 1, 100, "Hello world")
|
||||
await db.commit()
|
||||
|
||||
result = await db.get_random_next_word(1, "Hello")
|
||||
assert result == f"world{DEFAULT_SENTENCE_END}"
|
||||
|
||||
async def test_preserves_existing_punctuation(self, db: Database) -> None:
|
||||
"""Already-punctuated sentence keeps its terminator."""
|
||||
await _ingest_sentence(db, 1, 100, "Hello world!")
|
||||
await db.commit()
|
||||
|
||||
result = await db.get_random_next_word(1, "Hello")
|
||||
assert result == "world!"
|
||||
|
||||
async def test_single_word_stores_nothing(self, db: Database) -> None:
|
||||
"""A single-word sentence produces no start words or transitions."""
|
||||
await _ingest_sentence(db, 1, 100, "Hello.")
|
||||
await db.commit()
|
||||
|
||||
assert await db.get_random_start_word(1) is None
|
||||
assert await db.get_random_next_word(1, "Hello.") is None
|
||||
|
||||
async def test_stores_all_transitions(self, db: Database) -> None:
|
||||
"""All adjacent word pairs create transitions."""
|
||||
await _ingest_sentence(db, 1, 100, "A B C.")
|
||||
await db.commit()
|
||||
|
||||
assert await db.get_random_next_word(1, "A") == "B"
|
||||
assert await db.get_random_next_word(1, "B") == "C."
|
||||
|
||||
@@ -107,13 +107,13 @@ class TestIngest:
|
||||
async def test_single_sentence(self, db: Database) -> None:
|
||||
"""A single sentence paragraph is ingested."""
|
||||
await ingest(db, 1, 100, "Hello world.")
|
||||
await db.commit()
|
||||
|
||||
assert await db.get_random_start_word(1) == "Hello"
|
||||
|
||||
async def test_multiple_sentences(self, db: Database) -> None:
|
||||
"""Multiple sentences are split and ingested individually."""
|
||||
await ingest(db, 1, 100, "Hello world. Goodbye world!")
|
||||
await db.commit()
|
||||
|
||||
# Both "Hello" and "Goodbye" should appear as start words.
|
||||
async with db._connection.execute(
|
||||
"SELECT DISTINCT word FROM markov_start_words WHERE channel_id = ?",
|
||||
@@ -125,14 +125,14 @@ class TestIngest:
|
||||
async def test_normalizes_whitespace(self, db: Database) -> None:
|
||||
"""Extra spaces and newlines are collapsed."""
|
||||
await ingest(db, 1, 100, "Hello world.\nGoodbye world!")
|
||||
await db.commit()
|
||||
|
||||
result = await db.get_random_next_word(1, "Hello")
|
||||
assert result == "world."
|
||||
|
||||
async def test_appends_default_end(self, db: Database) -> None:
|
||||
"""Unpunctuated paragraph gets the default sentence-end marker."""
|
||||
await ingest(db, 1, 100, "Hello world")
|
||||
await db.commit()
|
||||
|
||||
result = await db.get_random_next_word(1, "Hello")
|
||||
assert result == f"world{DEFAULT_SENTENCE_END}"
|
||||
|
||||
@@ -148,13 +148,13 @@ class TestIngest:
|
||||
) -> None:
|
||||
"""Empty or whitespace-only input does not store any data."""
|
||||
await ingest(db, 1, 100, paragraph)
|
||||
await db.commit()
|
||||
|
||||
assert await db.get_random_start_word(1) is None
|
||||
|
||||
async def test_splits_on_punctuation_followed_by_space(self, db: Database) -> None:
|
||||
"""Punctuation followed by a space splits into separate sentences."""
|
||||
await ingest(db, 1, 100, "Dr. Smith likes cats")
|
||||
await db.commit()
|
||||
|
||||
# "Dr." splits off as a single-word sentence (stores nothing).
|
||||
# "Smith likes cats" becomes a sentence with "Smith" as start word.
|
||||
assert await db.get_random_start_word(1) == "Smith"
|
||||
@@ -162,7 +162,7 @@ class TestIngest:
|
||||
async def test_no_split_without_space_after_punctuation(self, db: Database) -> None:
|
||||
"""Punctuation not followed by a space keeps words together."""
|
||||
await ingest(db, 1, 100, "Hello.World is here")
|
||||
await db.commit()
|
||||
|
||||
assert await db.get_random_start_word(1) == "Hello.World"
|
||||
|
||||
|
||||
@@ -177,14 +177,14 @@ class TestGenerate:
|
||||
async def test_generates_from_ingested_data(self, db: Database) -> None:
|
||||
"""Generated text uses words from ingested data."""
|
||||
await ingest(db, 1, 100, "The quick brown fox.")
|
||||
await db.commit()
|
||||
|
||||
result = await generate(db, 1)
|
||||
assert result == "The quick brown fox."
|
||||
|
||||
async def test_strips_section_sign(self, db: Database) -> None:
|
||||
"""The internal section sign marker never appears in output."""
|
||||
await ingest(db, 1, 100, "Hello world")
|
||||
await db.commit()
|
||||
|
||||
result = await generate(db, 1)
|
||||
assert result == "Hello world"
|
||||
|
||||
@@ -193,7 +193,7 @@ class TestGenerate:
|
||||
words = [f"w{i}" for i in range(100)]
|
||||
text = " ".join(words)
|
||||
await ingest(db, 1, 100, text)
|
||||
await db.commit()
|
||||
|
||||
result = await generate(db, 1, soft_limit=10, hard_limit=49)
|
||||
assert result == "w0 w1 w2 w3 w4 w5 w6 w7 w8 w9 w10 w11 w12 w13 w14"
|
||||
|
||||
@@ -204,8 +204,8 @@ class TestGenerate:
|
||||
# the loop falls back to get_random_next_word for each. D has both a
|
||||
# continuing ("E") and completing ("end.") transition, so
|
||||
# get_random_completing_next_word deterministically picks "end.".
|
||||
await db.add_start_words_batch([(1, 100, "A")])
|
||||
await db.add_transitions_batch(
|
||||
await db.add_markov_data(
|
||||
[(1, 100, "A")],
|
||||
[
|
||||
(1, 100, "A", "B"),
|
||||
(1, 100, "B", "C"),
|
||||
@@ -215,24 +215,24 @@ class TestGenerate:
|
||||
(1, 100, "D", "G"),
|
||||
(1, 100, "D", "H"),
|
||||
(1, 100, "D", "end."),
|
||||
]
|
||||
],
|
||||
)
|
||||
await db.commit()
|
||||
|
||||
for _ in range(100):
|
||||
result = await generate(db, 1, soft_limit=1, hard_limit=1000)
|
||||
assert result == "A B C D end."
|
||||
|
||||
async def test_start_word_already_ends_sentence(self, db: Database) -> None:
|
||||
"""Generation stops immediately when the start word is sentence-ending."""
|
||||
await db.add_start_words_batch([(1, 100, "Yes.")])
|
||||
await db.commit()
|
||||
await db.add_markov_data([(1, 100, "Yes.")], [])
|
||||
|
||||
result = await generate(db, 1)
|
||||
assert result == "Yes."
|
||||
|
||||
async def test_chain_dead_end(self, db: Database) -> None:
|
||||
"""Generation stops when no next word exists (dead-end chain)."""
|
||||
await db.add_start_words_batch([(1, 100, "Hello")])
|
||||
await db.commit()
|
||||
await db.add_markov_data([(1, 100, "Hello")], [])
|
||||
|
||||
# "Hello" has no transitions, so the loop breaks immediately.
|
||||
result = await generate(db, 1)
|
||||
assert result == "Hello"
|
||||
@@ -240,14 +240,14 @@ class TestGenerate:
|
||||
async def test_hard_limit_strips_section_sign(self, db: Database) -> None:
|
||||
"""Section sign at the truncation boundary is stripped."""
|
||||
# Build a chain: "A" -> "B" -> "C§".
|
||||
await db.add_start_words_batch([(1, 100, "A")])
|
||||
await db.add_transitions_batch(
|
||||
await db.add_markov_data(
|
||||
[(1, 100, "A")],
|
||||
[
|
||||
(1, 100, "A", "B"),
|
||||
(1, 100, "B", f"C{DEFAULT_SENTENCE_END}"),
|
||||
]
|
||||
],
|
||||
)
|
||||
await db.commit()
|
||||
|
||||
# hard_limit=5 truncates "A B C§" (length 6) to "A B C".
|
||||
result = await generate(db, 1, soft_limit=100, hard_limit=5)
|
||||
assert result == "A B C"
|
||||
@@ -258,17 +258,15 @@ class TestGenerate:
|
||||
"""After soft_limit, falls back when no completing word exists."""
|
||||
# "A" -> "B" (no completing transition). Past soft_limit,
|
||||
# get_random_completing_next_word returns None, falls back to "B".
|
||||
await db.add_start_words_batch([(1, 100, "A")])
|
||||
await db.add_transitions_batch([(1, 100, "A", "B")])
|
||||
await db.commit()
|
||||
await db.add_markov_data([(1, 100, "A")], [(1, 100, "A", "B")])
|
||||
|
||||
result = await generate(db, 1, soft_limit=1, hard_limit=1000)
|
||||
assert result == "A B"
|
||||
|
||||
async def test_hard_limit_truncates_mid_word(self, db: Database) -> None:
|
||||
"""Hard limit slices output even when it falls inside a word."""
|
||||
await db.add_start_words_batch([(1, 100, "AB")])
|
||||
await db.add_transitions_batch([(1, 100, "AB", "CDEF")])
|
||||
await db.commit()
|
||||
await db.add_markov_data([(1, 100, "AB")], [(1, 100, "AB", "CDEF")])
|
||||
|
||||
# "AB CDEF" is 7 chars; hard_limit=5 truncates to "AB CD".
|
||||
result = await generate(db, 1, soft_limit=100, hard_limit=5)
|
||||
assert result == "AB CD"
|
||||
@@ -277,7 +275,7 @@ class TestGenerate:
|
||||
"""Data ingested into one channel does not leak into another."""
|
||||
await ingest(db, 1, 100, "Channel one data.")
|
||||
await ingest(db, 2, 100, "Channel two data.")
|
||||
await db.commit()
|
||||
|
||||
# Channel 3 has no data; should get the fallback.
|
||||
result = await generate(db, 3)
|
||||
assert result == "Hello world!"
|
||||
|
||||
Reference in New Issue
Block a user