Skip to content

Commit f9bd8f3

Browse files
committed
Refactor test_sync_hub_node_backfill: streamline imports and utilize shared DB helpers
1 parent 89139fe commit f9bd8f3

File tree

2 files changed

+19
-25
lines changed

2 files changed

+19
-25
lines changed

.github/skills/code-standards/SKILL.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ Use sanitizers from `server/helper.py` before storing user input. MAC addresses
7474
- Everything is already writable
7575
- If permissions needed, fix `.devcontainer/scripts/setup.sh`
7676

77+
## Test Helpers — No Duplicate Mocks
78+
79+
Reuse shared mocks and factories from `test/db_test_helpers.py`. Never redefine `DummyDB`, `make_db`, or inline DDL in individual test files.
80+
81+
```python
82+
import sys, os
83+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
84+
from db_test_helpers import make_db, DummyDB, insert_device, minutes_ago
85+
```
86+
87+
If a helper you need doesn't exist yet, add it to `db_test_helpers.py` — not locally in the test file.
88+
7789
## Path Hygiene
7890

7991
- Use environment variables for runtime paths

test/scan/test_sync_hub_node_backfill.py

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,19 @@
11
"""Tests for update_sync_hub_node backfill."""
22

3-
import sqlite3
3+
import sys
4+
import os
45
from unittest.mock import patch
56

6-
from server.scan import device_handling
7-
8-
9-
class DummyDB:
10-
"""Minimal DB wrapper compatible with device_handling helpers."""
11-
12-
def __init__(self, conn):
13-
self.sql = conn.cursor()
14-
self._conn = conn
7+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
8+
from db_test_helpers import make_db, DummyDB # noqa: E402
159

16-
def commitDB(self):
17-
self._conn.commit()
10+
from server.scan import device_handling
1811

1912

2013
def _make_db(devices):
21-
"""Create an in-memory DB with a Devices table and seed rows."""
22-
conn = sqlite3.connect(":memory:")
23-
conn.row_factory = sqlite3.Row
14+
"""Create an in-memory DB with full schema and seed rows."""
15+
conn = make_db()
2416
cur = conn.cursor()
25-
26-
cur.execute(
27-
"""
28-
CREATE TABLE Devices (
29-
devMac TEXT PRIMARY KEY,
30-
devSyncHubNode TEXT
31-
)
32-
"""
33-
)
34-
3517
cur.executemany(
3618
"INSERT INTO Devices (devMac, devSyncHubNode) VALUES (?, ?)",
3719
devices,

0 commit comments

Comments
 (0)