|
| 1 | +"""Tests for DbRegistry SQLite-backed storage.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import json |
| 5 | +import sqlite3 |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from openkb.state import DbRegistry |
| 11 | + |
| 12 | + |
| 13 | +def test_db_registry_creates_database_file(tmp_path): |
| 14 | + """DbRegistry should create a .db file on init.""" |
| 15 | + db_path = tmp_path / "hashes.db" |
| 16 | + registry = DbRegistry(db_path) |
| 17 | + assert db_path.exists() |
| 18 | + |
| 19 | + |
| 20 | +def test_db_registry_creates_table(tmp_path): |
| 21 | + """DbRegistry should create the registry table.""" |
| 22 | + db_path = tmp_path / "hashes.db" |
| 23 | + registry = DbRegistry(db_path) |
| 24 | + |
| 25 | + conn = sqlite3.connect(str(db_path)) |
| 26 | + cursor = conn.execute( |
| 27 | + "SELECT name FROM sqlite_master WHERE type='table' AND name='registry'" |
| 28 | + ) |
| 29 | + result = cursor.fetchone() |
| 30 | + conn.close() |
| 31 | + assert result is not None |
| 32 | + |
| 33 | + |
| 34 | +def test_db_empty_registry_is_known_false(tmp_path): |
| 35 | + """Empty DbRegistry should return False for is_known.""" |
| 36 | + registry = DbRegistry(tmp_path / "hashes.db") |
| 37 | + assert registry.is_known("abc123") is False |
| 38 | + |
| 39 | + |
| 40 | +def test_db_empty_registry_get_returns_none(tmp_path): |
| 41 | + """Empty DbRegistry should return None for get.""" |
| 42 | + registry = DbRegistry(tmp_path / "hashes.db") |
| 43 | + assert registry.get("abc123") is None |
| 44 | + |
| 45 | + |
| 46 | +def test_db_add_and_is_known(tmp_path): |
| 47 | + """After add, is_known should return True.""" |
| 48 | + registry = DbRegistry(tmp_path / "hashes.db") |
| 49 | + registry.add("deadbeef", {"filename": "test.pdf"}) |
| 50 | + assert registry.is_known("deadbeef") is True |
| 51 | + |
| 52 | + |
| 53 | +def test_db_add_and_get(tmp_path): |
| 54 | + """After add, get should return the metadata.""" |
| 55 | + registry = DbRegistry(tmp_path / "hashes.db") |
| 56 | + metadata = {"filename": "doc.pdf", "pages": 10} |
| 57 | + registry.add("cafebabe", metadata) |
| 58 | + assert registry.get("cafebabe") == metadata |
| 59 | + |
| 60 | + |
| 61 | +def test_db_persistence_across_instances(tmp_path): |
| 62 | + """Data should persist across DbRegistry instances.""" |
| 63 | + db_path = tmp_path / "hashes.db" |
| 64 | + r1 = DbRegistry(db_path) |
| 65 | + r1.add("hash1", {"file": "a.pdf"}) |
| 66 | + |
| 67 | + r2 = DbRegistry(db_path) |
| 68 | + assert r2.is_known("hash1") is True |
| 69 | + assert r2.get("hash1") == {"file": "a.pdf"} |
| 70 | + |
| 71 | + |
| 72 | +def test_db_all_entries_returns_all(tmp_path): |
| 73 | + """all_entries should return all hash -> metadata mappings.""" |
| 74 | + registry = DbRegistry(tmp_path / "hashes.db") |
| 75 | + registry.add("h1", {"name": "one"}) |
| 76 | + registry.add("h2", {"name": "two"}) |
| 77 | + entries = registry.all_entries() |
| 78 | + assert "h1" in entries |
| 79 | + assert "h2" in entries |
| 80 | + assert entries["h1"] == {"name": "one"} |
| 81 | + assert entries["h2"] == {"name": "two"} |
| 82 | + |
| 83 | + |
| 84 | +def test_db_all_entries_empty(tmp_path): |
| 85 | + """all_entries on empty registry should return empty dict.""" |
| 86 | + registry = DbRegistry(tmp_path / "hashes.db") |
| 87 | + assert registry.all_entries() == {} |
| 88 | + |
| 89 | + |
| 90 | +def test_db_hash_file_unchanged(tmp_path): |
| 91 | + """DbRegistry.hash_file should work same as HashRegistry.""" |
| 92 | + f = tmp_path / "sample.txt" |
| 93 | + f.write_text("hello world") |
| 94 | + digest = DbRegistry.hash_file(f) |
| 95 | + assert len(digest) == 64 |
| 96 | + assert all(c in "0123456789abcdef" for c in digest) |
| 97 | + |
| 98 | + |
| 99 | +def test_db_update_existing_hash(tmp_path): |
| 100 | + """Adding same hash twice should update metadata.""" |
| 101 | + registry = DbRegistry(tmp_path / "hashes.db") |
| 102 | + registry.add("hash1", {"version": 1}) |
| 103 | + registry.add("hash1", {"version": 2}) |
| 104 | + assert registry.get("hash1") == {"version": 2} |
| 105 | + |
| 106 | + |
| 107 | +def test_db_metadata_with_nested_dict(tmp_path): |
| 108 | + """Metadata can contain nested dictionaries.""" |
| 109 | + registry = DbRegistry(tmp_path / "hashes.db") |
| 110 | + metadata = { |
| 111 | + "name": "doc.pdf", |
| 112 | + "stats": {"pages": 10, "words": 5000}, |
| 113 | + } |
| 114 | + registry.add("hash1", metadata) |
| 115 | + assert registry.get("hash1") == metadata |
| 116 | + |
| 117 | + |
| 118 | +def test_db_wal_mode_enabled(tmp_path): |
| 119 | + """Database should use WAL mode for concurrency.""" |
| 120 | + db_path = tmp_path / "hashes.db" |
| 121 | + DbRegistry(db_path) |
| 122 | + |
| 123 | + conn = sqlite3.connect(str(db_path)) |
| 124 | + cursor = conn.execute("PRAGMA journal_mode") |
| 125 | + result = cursor.fetchone() |
| 126 | + conn.close() |
| 127 | + assert result[0].lower() == "wal" |
| 128 | + |
| 129 | + |
| 130 | +def test_migrate_from_json(tmp_path): |
| 131 | + """DbRegistry should migrate existing JSON data on first access.""" |
| 132 | + json_path = tmp_path / "hashes.json" |
| 133 | + existing_data = { |
| 134 | + "hash1": {"name": "doc1.pdf", "pages": 10}, |
| 135 | + "hash2": {"name": "doc2.pdf", "pages": 20}, |
| 136 | + } |
| 137 | + json_path.write_text(json.dumps(existing_data), encoding="utf-8") |
| 138 | + |
| 139 | + db_path = tmp_path / "hashes.db" |
| 140 | + registry = DbRegistry(db_path, migrate_from=json_path) |
| 141 | + |
| 142 | + assert registry.is_known("hash1") |
| 143 | + assert registry.is_known("hash2") |
| 144 | + assert registry.get("hash1") == {"name": "doc1.pdf", "pages": 10} |
| 145 | + assert registry.get("hash2") == {"name": "doc2.pdf", "pages": 20} |
| 146 | + |
| 147 | + |
| 148 | +def test_migrate_only_once(tmp_path): |
| 149 | + """Migration should only happen once, not on subsequent loads.""" |
| 150 | + json_path = tmp_path / "hashes.json" |
| 151 | + existing_data = {"hash1": {"name": "doc1.pdf"}} |
| 152 | + json_path.write_text(json.dumps(existing_data), encoding="utf-8") |
| 153 | + |
| 154 | + db_path = tmp_path / "hashes.db" |
| 155 | + |
| 156 | + r1 = DbRegistry(db_path, migrate_from=json_path) |
| 157 | + assert r1.is_known("hash1") |
| 158 | + |
| 159 | + existing_data["hash2"] = {"name": "doc2.pdf"} |
| 160 | + json_path.write_text(json.dumps(existing_data), encoding="utf-8") |
| 161 | + |
| 162 | + r2 = DbRegistry(db_path, migrate_from=json_path) |
| 163 | + assert r2.is_known("hash1") |
| 164 | + assert not r2.is_known("hash2") |
| 165 | + |
| 166 | + |
| 167 | +def test_migrate_optional(tmp_path): |
| 168 | + """DbRegistry should work without migration.""" |
| 169 | + db_path = tmp_path / "hashes.db" |
| 170 | + registry = DbRegistry(db_path) |
| 171 | + registry.add("hash1", {"name": "doc.pdf"}) |
| 172 | + assert registry.is_known("hash1") |
0 commit comments