From f9bd8f35f5477ada5dbc21bb7a0ed2bf9ca60232 Mon Sep 17 00:00:00 2001 From: "Jokob @NetAlertX" <96159884+jokob-sk@users.noreply.github.com> Date: Wed, 15 Apr 2026 12:48:01 +0000 Subject: [PATCH] Refactor test_sync_hub_node_backfill: streamline imports and utilize shared DB helpers --- .github/skills/code-standards/SKILL.md | 12 +++++++++ test/scan/test_sync_hub_node_backfill.py | 32 ++++++------------------ 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/.github/skills/code-standards/SKILL.md b/.github/skills/code-standards/SKILL.md index 37e6932b..faf583d6 100644 --- a/.github/skills/code-standards/SKILL.md +++ b/.github/skills/code-standards/SKILL.md @@ -74,6 +74,18 @@ Use sanitizers from `server/helper.py` before storing user input. MAC addresses - Everything is already writable - If permissions needed, fix `.devcontainer/scripts/setup.sh` +## Test Helpers — No Duplicate Mocks + +Reuse shared mocks and factories from `test/db_test_helpers.py`. Never redefine `DummyDB`, `make_db`, or inline DDL in individual test files. + +```python +import sys, os +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) +from db_test_helpers import make_db, DummyDB, insert_device, minutes_ago +``` + +If a helper you need doesn't exist yet, add it to `db_test_helpers.py` — not locally in the test file. + ## Path Hygiene - Use environment variables for runtime paths diff --git a/test/scan/test_sync_hub_node_backfill.py b/test/scan/test_sync_hub_node_backfill.py index 74587737..f32fcdac 100644 --- a/test/scan/test_sync_hub_node_backfill.py +++ b/test/scan/test_sync_hub_node_backfill.py @@ -1,37 +1,19 @@ """Tests for update_sync_hub_node backfill.""" -import sqlite3 +import sys +import os from unittest.mock import patch +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) +from db_test_helpers import make_db, DummyDB # noqa: E402 + from server.scan import device_handling -class DummyDB: - """Minimal DB wrapper compatible with device_handling helpers.""" - - def __init__(self, conn): - self.sql = conn.cursor() - self._conn = conn - - def commitDB(self): - self._conn.commit() - - def _make_db(devices): - """Create an in-memory DB with a Devices table and seed rows.""" - conn = sqlite3.connect(":memory:") - conn.row_factory = sqlite3.Row + """Create an in-memory DB with full schema and seed rows.""" + conn = make_db() cur = conn.cursor() - - cur.execute( - """ - CREATE TABLE Devices ( - devMac TEXT PRIMARY KEY, - devSyncHubNode TEXT - ) - """ - ) - cur.executemany( "INSERT INTO Devices (devMac, devSyncHubNode) VALUES (?, ?)", devices,