feat: project skeleton with config, dependencies, and test setup

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
walkpan
2026-03-29 21:26:00 +08:00
parent ce10907619
commit 8b925263a2
8 changed files with 143 additions and 0 deletions

42
tests/conftest.py Normal file
View File

@@ -0,0 +1,42 @@
import pytest
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
from sqlalchemy.pool import StaticPool
from mqtt_home.database import Base
from mqtt_home.config import Settings
TEST_DATABASE_URL = "sqlite+aiosqlite:///:memory:"
@pytest.fixture
def settings():
return Settings(
mqtt_host="localhost",
mqtt_port=1883,
emqx_api_url="http://localhost:18083/api/v5",
emqx_api_key="test-key",
emqx_api_secret="test-secret",
database_url=TEST_DATABASE_URL,
)
@pytest.fixture
async def engine():
engine = create_async_engine(
TEST_DATABASE_URL,
connect_args={"check_same_thread": False},
poolclass=StaticPool,
)
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
yield engine
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
await engine.dispose()
@pytest.fixture
async def db_session(engine):
async_session = async_sessionmaker(engine, expire_on_commit=False)
async with async_session() as session:
yield session

17
tests/test_config.py Normal file
View File

@@ -0,0 +1,17 @@
import os
from mqtt_home.config import Settings
def test_default_settings():
s = Settings()
assert s.mqtt_host == "localhost"
assert s.mqtt_port == 1883
assert s.web_port == 8000
def test_settings_from_env(monkeypatch):
monkeypatch.setenv("MQTT_HOST", "192.168.1.1")
monkeypatch.setenv("MQTT_PORT", "1884")
s = Settings()
assert s.mqtt_host == "192.168.1.1"
assert s.mqtt_port == 1884