Skip to content

Commit 8f49a38

Browse files
authored
Merge pull request #1 from vijaxx/add-unit-tests
Add unit tests for analytics_db and pincopy
2 parents 9d27200 + 5d397e8 commit 8f49a38

5 files changed

Lines changed: 95 additions & 1 deletion

File tree

.github/workflows/ci.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ jobs:
1818
cache: "pip"
1919

2020
- name: Install dependencies
21-
run: pip install -r requirements.txt
21+
run: pip install -r requirements-dev.txt
2222

2323
- name: Compile check
2424
run: python -m compileall -q .
25+
26+
- name: Run tests
27+
run: python -m pytest tests/ -v

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
__pycache__/
22
*.pyc
3+
.pytest_cache/
34
.DS_Store
45
*.log
56
out/

requirements-dev.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-r requirements.txt
2+
pytest

tests/test_analytics_db.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import sqlite3
2+
import sys
3+
from pathlib import Path
4+
5+
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
6+
7+
import analytics_db as adb
8+
9+
10+
def test_init_db_creates_all_expected_tables(tmp_path):
11+
db_path = str(tmp_path / "test.db")
12+
adb.init_db(db_path)
13+
14+
conn = sqlite3.connect(db_path)
15+
tables = {r[0] for r in conn.execute(
16+
"SELECT name FROM sqlite_master WHERE type='table'")}
17+
conn.close()
18+
19+
assert {"pins", "posts", "analytics", "sales"} <= tables
20+
21+
22+
def test_init_db_is_idempotent(tmp_path):
23+
db_path = str(tmp_path / "test.db")
24+
adb.init_db(db_path)
25+
adb.init_db(db_path) # must not raise on re-init
26+
27+
conn = adb.connect(db_path)
28+
conn.execute("INSERT INTO pins (theme, idx) VALUES ('nature', 1)")
29+
conn.commit()
30+
row = conn.execute("SELECT theme, idx FROM pins").fetchone()
31+
conn.close()
32+
33+
assert row["theme"] == "nature" and row["idx"] == 1
34+
35+
36+
def test_pins_table_rejects_duplicate_theme_idx(tmp_path):
37+
db_path = str(tmp_path / "test.db")
38+
adb.init_db(db_path)
39+
conn = adb.connect(db_path)
40+
conn.execute("INSERT INTO pins (theme, idx) VALUES ('nature', 1)")
41+
conn.commit()
42+
try:
43+
conn.execute("INSERT INTO pins (theme, idx) VALUES ('nature', 1)")
44+
conn.commit()
45+
raised = False
46+
except sqlite3.IntegrityError:
47+
raised = True
48+
finally:
49+
conn.close()
50+
assert raised, "UNIQUE(theme, idx) constraint should reject the duplicate"

tests/test_pincopy.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import sys
2+
from pathlib import Path
3+
4+
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
5+
6+
import pincopy
7+
8+
9+
def test_extract_json_parses_array_embedded_in_prose():
10+
text = 'Sure, here you go:\n[{"a": 1}, {"a": 2}]\nHope that helps!'
11+
assert pincopy.extract_json(text) == [{"a": 1}, {"a": 2}]
12+
13+
14+
def test_extract_json_returns_none_with_no_array():
15+
assert pincopy.extract_json("no json here at all") is None
16+
17+
18+
def test_extract_json_returns_none_on_malformed_array():
19+
assert pincopy.extract_json("[{broken json}]") is None
20+
21+
22+
def test_fallback_returns_five_variants():
23+
variants = pincopy.fallback("Large Print Nature Word Search", 48)
24+
assert len(variants) == 5
25+
assert all(set(v.keys()) == {"title", "description"} for v in variants)
26+
27+
28+
def test_fallback_titles_fit_pinterest_length_limit():
29+
variants = pincopy.fallback("Large Print Nature Word Search", 48)
30+
for v in variants:
31+
assert len(v["title"]) <= 100
32+
33+
34+
def test_fallback_strips_boilerplate_from_short_theme_name():
35+
variants = pincopy.fallback("Large Print Nature Word Search", 48)
36+
# "nature" (the actual theme) should show up; the generic wrapper words shouldn't
37+
# dominate every title once stripped down to the short form.
38+
assert any("nature" in v["title"].lower() for v in variants)

0 commit comments

Comments
 (0)