Skip to content

Commit f315426

Browse files
committed
test: add unit tests for SqliteDataCache backend functionality
1 parent a3107fd commit f315426

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
"""Tests for SqliteDataCache backend."""
2+
3+
from dataclasses import dataclass
4+
from pathlib import Path
5+
6+
import pytest
7+
8+
from robotcode.robot.diagnostics.data_cache import CacheSection, SqliteDataCache
9+
10+
11+
@dataclass
12+
class _SampleData:
13+
name: str
14+
value: int
15+
16+
17+
class TestSqliteDataCache:
18+
def test_save_and_read_roundtrip(self, tmp_path: Path) -> None:
19+
cache = SqliteDataCache(tmp_path / "cache")
20+
data = _SampleData(name="hello", value=42)
21+
22+
cache.save_cache_data(CacheSection.LIBRARY, "entry1", data)
23+
result = cache.read_cache_data(CacheSection.LIBRARY, "entry1", _SampleData)
24+
25+
assert result == data
26+
27+
def test_cache_data_exists_true(self, tmp_path: Path) -> None:
28+
cache = SqliteDataCache(tmp_path / "cache")
29+
cache.save_cache_data(CacheSection.LIBRARY, "exists", "some data")
30+
31+
assert cache.cache_data_exists(CacheSection.LIBRARY, "exists") is True
32+
33+
def test_cache_data_exists_false(self, tmp_path: Path) -> None:
34+
cache = SqliteDataCache(tmp_path / "cache")
35+
36+
assert cache.cache_data_exists(CacheSection.LIBRARY, "missing") is False
37+
38+
def test_read_missing_entry_raises_file_not_found(self, tmp_path: Path) -> None:
39+
cache = SqliteDataCache(tmp_path / "cache")
40+
41+
with pytest.raises(FileNotFoundError):
42+
cache.read_cache_data(CacheSection.LIBRARY, "missing", str)
43+
44+
def test_read_wrong_type_raises_type_error(self, tmp_path: Path) -> None:
45+
cache = SqliteDataCache(tmp_path / "cache")
46+
cache.save_cache_data(CacheSection.LIBRARY, "entry", "a string")
47+
48+
with pytest.raises(TypeError):
49+
cache.read_cache_data(CacheSection.LIBRARY, "entry", int)
50+
51+
def test_overwrite_existing_entry(self, tmp_path: Path) -> None:
52+
cache = SqliteDataCache(tmp_path / "cache")
53+
cache.save_cache_data(CacheSection.LIBRARY, "entry", "first")
54+
cache.save_cache_data(CacheSection.LIBRARY, "entry", "second")
55+
56+
result = cache.read_cache_data(CacheSection.LIBRARY, "entry", str)
57+
assert result == "second"
58+
59+
def test_different_sections_independent(self, tmp_path: Path) -> None:
60+
cache = SqliteDataCache(tmp_path / "cache")
61+
cache.save_cache_data(CacheSection.LIBRARY, "entry", "lib_data")
62+
cache.save_cache_data(CacheSection.RESOURCE, "entry", "res_data")
63+
64+
assert cache.read_cache_data(CacheSection.LIBRARY, "entry", str) == "lib_data"
65+
assert cache.read_cache_data(CacheSection.RESOURCE, "entry", str) == "res_data"
66+
67+
def test_different_entry_names_independent(self, tmp_path: Path) -> None:
68+
cache = SqliteDataCache(tmp_path / "cache")
69+
cache.save_cache_data(CacheSection.LIBRARY, "a", 1)
70+
cache.save_cache_data(CacheSection.LIBRARY, "b", 2)
71+
72+
assert cache.read_cache_data(CacheSection.LIBRARY, "a", int) == 1
73+
assert cache.read_cache_data(CacheSection.LIBRARY, "b", int) == 2
74+
75+
def test_data_persists_after_close_and_reopen(self, tmp_path: Path) -> None:
76+
cache_dir = tmp_path / "cache"
77+
cache = SqliteDataCache(cache_dir)
78+
cache.save_cache_data(CacheSection.NAMESPACE, "entry", {"key": "value"})
79+
cache.close()
80+
81+
cache2 = SqliteDataCache(cache_dir)
82+
result = cache2.read_cache_data(CacheSection.NAMESPACE, "entry", dict)
83+
assert result == {"key": "value"}
84+
cache2.close()
85+
86+
def test_creates_cache_dir_and_gitignore(self, tmp_path: Path) -> None:
87+
cache_dir = tmp_path / "new_cache"
88+
assert not cache_dir.exists()
89+
90+
SqliteDataCache(cache_dir)
91+
92+
assert cache_dir.exists()
93+
gitignore = cache_dir / ".gitignore"
94+
assert gitignore.exists()
95+
assert "*" in gitignore.read_text("utf-8")
96+
97+
def test_creates_db_file(self, tmp_path: Path) -> None:
98+
cache_dir = tmp_path / "cache"
99+
SqliteDataCache(cache_dir)
100+
101+
assert (cache_dir / "cache.db").exists()
102+
103+
def test_complex_data_roundtrip(self, tmp_path: Path) -> None:
104+
cache = SqliteDataCache(tmp_path / "cache")
105+
data = {
106+
"strings": ["a", "b", "c"],
107+
"nested": {"x": 1, "y": [2, 3]},
108+
"none_val": None,
109+
"tuple_as_list": [1, 2, 3],
110+
}
111+
112+
cache.save_cache_data(CacheSection.VARIABLES, "complex", data)
113+
result = cache.read_cache_data(CacheSection.VARIABLES, "complex", dict)
114+
assert result == data

0 commit comments

Comments
 (0)