117 lines
3.5 KiB
Python
117 lines
3.5 KiB
Python
"""SQLite 持久化层测试。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import numpy as np
|
|
import pytest
|
|
|
|
from qbh.melody import Melody
|
|
from qbh.store import SongRecord, Store, hash_password, verify_password
|
|
|
|
|
|
@pytest.fixture
|
|
def store(tmp_path):
|
|
s = Store(tmp_path / "test.db")
|
|
yield s
|
|
s.close()
|
|
|
|
|
|
def test_song_crud(store):
|
|
record = SongRecord(id="s1", title="测试曲", origin="测试", tags=("a", "b"),
|
|
source="test", n_notes=10, duration=5.0, median_pitch=60.0)
|
|
store.upsert_song(record)
|
|
assert store.count_songs() == 1
|
|
|
|
loaded = store.get_song("s1")
|
|
assert loaded is not None
|
|
assert loaded.title == "测试曲"
|
|
assert loaded.tags == ("a", "b")
|
|
|
|
record.title = "改名"
|
|
store.upsert_song(record)
|
|
assert store.get_song("s1").title == "改名"
|
|
|
|
assert store.delete_song("s1") is True
|
|
assert store.count_songs() == 0
|
|
assert store.get_song("s1") is None
|
|
|
|
|
|
def test_melody_storage(store):
|
|
mel = Melody(np.array([60.0, 62.0]), np.array([0.0, 0.5]), np.array([0.5, 0.5]), "s1", "曲")
|
|
store.set_melody(mel)
|
|
loaded = store.get_melody("s1")
|
|
assert loaded is not None
|
|
assert np.allclose(loaded.midis, mel.midis)
|
|
|
|
store.set_melodies([mel])
|
|
assert len(store.all_melodies()) == 1
|
|
|
|
|
|
def test_password_hashing():
|
|
salt, digest = hash_password("secret123")
|
|
assert verify_password("secret123", salt, digest)
|
|
assert not verify_password("wrong", salt, digest)
|
|
# 相同密码、不同盐 → 不同哈希
|
|
salt2, digest2 = hash_password("secret123")
|
|
assert digest != digest2
|
|
|
|
|
|
def test_user_lifecycle(store):
|
|
ok, message = store.create_user("alice", "password1", "a@example.com")
|
|
assert ok, message
|
|
|
|
# 重复注册
|
|
ok, message = store.create_user("alice", "password2")
|
|
assert not ok and "已存在" in message
|
|
|
|
# 密码太短
|
|
ok, message = store.create_user("bob", "123")
|
|
assert not ok and "密码" in message
|
|
|
|
user_id = store.verify_user("alice", "password1")
|
|
assert user_id is not None
|
|
assert store.verify_user("alice", "wrong") is None
|
|
assert store.verify_user("nobody", "password1") is None
|
|
|
|
info = store.get_user(user_id)
|
|
assert info["username"] == "alice"
|
|
|
|
|
|
def test_history_lifecycle(store):
|
|
ok, _ = store.create_user("carol", "password1")
|
|
assert ok
|
|
user_id = store.verify_user("carol", "password1")
|
|
|
|
store.add_history(
|
|
user_id=user_id, source="mic", query_notes=12, query_melody="{}",
|
|
top_song="s1", top_title="曲一", top_score=0.9, elapsed_ms=50.0,
|
|
results=[{"song_id": "s1", "score": 0.9}],
|
|
)
|
|
store.add_history(
|
|
user_id=None, source="demo", query_notes=8, query_melody="{}",
|
|
top_song="s2", top_title="曲二", top_score=0.8, elapsed_ms=40.0, results=[],
|
|
)
|
|
|
|
assert len(store.list_history()) == 2
|
|
mine = store.list_history(user_id=user_id)
|
|
assert len(mine) == 1
|
|
assert mine[0]["top_song"] == "s1"
|
|
assert mine[0]["results"][0]["song_id"] == "s1"
|
|
|
|
assert store.clear_history(user_id=user_id) == 1
|
|
assert len(store.list_history()) == 1
|
|
|
|
|
|
def test_meta_roundtrip(store):
|
|
store.set_meta("k", {"a": 1})
|
|
assert store.get_meta("k") == {"a": 1}
|
|
assert store.get_meta("missing", default=42) == 42
|
|
|
|
|
|
def test_persistence_across_connections(tmp_path):
|
|
path = tmp_path / "persist.db"
|
|
with Store(path) as first:
|
|
first.upsert_song(SongRecord(id="x", title="持久化"))
|
|
with Store(path) as second:
|
|
assert second.get_song("x").title == "持久化"
|