Skip to content

Commit e61a912

Browse files
rverkRob Verkuijlenclaude
authored
Feature: test coverage for DB writers, readers, rate limit and db init (v1.3.0) (#25)
Feature: add test coverage for DB writers, readers, rate limit and db init (v1.3.0) Covers six previously untested modules: KvKVestigingenWriter, VestigingsProfielWriter, SignaalWriter, SignaalReader, global_rate_limit, and ensure_database_initialized. Total project coverage rises to 95%. Co-authored-by: Rob Verkuijlen <rob.verkuijlen@outlook.com> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 9c1bd2e commit e61a912

File tree

8 files changed

+1170
-2
lines changed

8 files changed

+1170
-2
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "kvk-connect"
3-
version = "1.2.1"
3+
version = "1.3.0"
44
description = "Client + domain mapping for KVK API"
55
readme = "README.md"
66
requires-python = ">=3.13"

tests/db/test_db_init.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""Tests for ensure_database_initialized."""
2+
3+
from __future__ import annotations
4+
5+
import logging
6+
7+
import pytest
8+
from sqlalchemy import create_engine, inspect, text
9+
10+
from kvk_connect.db.init import ensure_database_initialized
11+
from kvk_connect.models.orm.base import Base
12+
13+
logger = logging.getLogger(__name__)
14+
15+
16+
@pytest.fixture
17+
def fresh_engine():
18+
"""Create a fresh in-memory SQLite engine (no tables yet)."""
19+
engine = create_engine("sqlite:///:memory:", connect_args={"check_same_thread": False})
20+
yield engine
21+
engine.dispose()
22+
23+
24+
class TestEnsureDatabaseInitialized:
25+
"""Test suite for ensure_database_initialized."""
26+
27+
def test_creates_tables_on_first_call(self, fresh_engine) -> None:
28+
"""Tables are created after the first call."""
29+
inspector = inspect(fresh_engine)
30+
assert inspector.get_table_names() == []
31+
32+
ensure_database_initialized(fresh_engine, Base)
33+
34+
table_names = inspect(fresh_engine).get_table_names()
35+
assert len(table_names) > 0
36+
37+
def test_creates_expected_tables(self, fresh_engine) -> None:
38+
"""Known ORM tables are present after initialization."""
39+
ensure_database_initialized(fresh_engine, Base)
40+
41+
table_names = set(inspect(fresh_engine).get_table_names())
42+
assert "basisprofielen" in table_names
43+
assert "vestigingen" in table_names
44+
assert "vestigingsprofielen" in table_names
45+
assert "signalen" in table_names
46+
47+
def test_idempotent_second_call_does_not_raise(self, fresh_engine) -> None:
48+
"""Calling twice does not raise an error (create_all is idempotent)."""
49+
ensure_database_initialized(fresh_engine, Base)
50+
ensure_database_initialized(fresh_engine, Base) # should not raise
51+
52+
table_names = inspect(fresh_engine).get_table_names()
53+
assert len(table_names) > 0
54+
55+
def test_table_count_matches_metadata(self, fresh_engine) -> None:
56+
"""Number of created tables matches number of tables in Base.metadata."""
57+
ensure_database_initialized(fresh_engine, Base)
58+
59+
inspector = inspect(fresh_engine)
60+
created = {t for t in inspector.get_table_names() if t in Base.metadata.tables}
61+
assert len(created) == len(Base.metadata.tables)

0 commit comments

Comments
 (0)