mirror of
https://github.com/sortedcord/alemno-payments.git
synced 2026-07-22 12:12:49 +05:30
add test config and basic tests for csv_parser
This commit is contained in:
60
tests/conftest.py
Normal file
60
tests/conftest.py
Normal file
@@ -0,0 +1,60 @@
|
||||
import asyncio
|
||||
from typing import AsyncGenerator
|
||||
import pytest
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
|
||||
from app.core.config import settings
|
||||
from app.core.dependencies import get_db
|
||||
from app.main import app
|
||||
# Use a test database suffix or a separate database for testing
|
||||
TEST_DATABASE_URL = settings.DATABASE_URL.replace(settings.POSTGRES_DB, f"test_{settings.POSTGRES_DB}")
|
||||
# Create async engine for test db
|
||||
test_engine = create_async_engine(
|
||||
TEST_DATABASE_URL,
|
||||
echo=False,
|
||||
future=True,
|
||||
)
|
||||
|
||||
TestAsyncSessionLocal = async_sessionmaker(
|
||||
bind=test_engine,
|
||||
class_=AsyncSession,
|
||||
expire_on_commit=False,
|
||||
)
|
||||
|
||||
# Global flag to track if test DB is accessible
|
||||
_db_offline = False
|
||||
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def initialize_test_db():
|
||||
"""Create test tables and clean them up after all tests finish."""
|
||||
global _db_offline
|
||||
import asyncio
|
||||
try:
|
||||
loop = asyncio.get_event_loop_policy().get_event_loop()
|
||||
except RuntimeError:
|
||||
loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(loop)
|
||||
|
||||
async def setup_db():
|
||||
async with test_engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.drop_all)
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
|
||||
async def teardown_db():
|
||||
async with test_engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.drop_all)
|
||||
|
||||
try:
|
||||
loop.run_until_complete(setup_db())
|
||||
except Exception as e:
|
||||
_db_offline = True
|
||||
print(f"\nSkipping DB initialization (Postgres is likely offline): {e}\n")
|
||||
|
||||
yield
|
||||
|
||||
if not _db_offline:
|
||||
try:
|
||||
loop.run_until_complete(teardown_db())
|
||||
except Exception:
|
||||
pass
|
||||
49
tests/services/test_csv_parser.py
Normal file
49
tests/services/test_csv_parser.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import pytest
|
||||
from app.core.exceptions import InvalidCSVException
|
||||
from app.utils.csv_parser import parse_and_validate_csv
|
||||
|
||||
|
||||
def test_parse_valid_csv():
|
||||
valid_csv = (
|
||||
"transaction_id,date,amount,category,description\n"
|
||||
"TX1001,2026-06-25,120.50,Groceries,Weekly grocery shop\n"
|
||||
"TX1002,06/26/2026,15.00,Coffee,Morning latte\n"
|
||||
)
|
||||
result = parse_and_validate_csv(valid_csv.encode("utf-8"))
|
||||
assert len(result) == 2
|
||||
assert result[0]["transaction_id"] == "TX1001"
|
||||
assert result[0]["amount"] == 120.50
|
||||
assert result[0]["date"] == "2026-06-25"
|
||||
assert result[1]["transaction_id"] == "TX1002"
|
||||
assert result[1]["amount"] == 15.00
|
||||
assert result[1]["date"] == "2026-06-26"
|
||||
|
||||
|
||||
def test_parse_missing_headers():
|
||||
invalid_csv = (
|
||||
"transaction_id,amount,category,description\n"
|
||||
"TX1001,120.50,Groceries,Weekly grocery shop\n"
|
||||
)
|
||||
with pytest.raises(InvalidCSVException) as exc_info:
|
||||
parse_and_validate_csv(invalid_csv.encode("utf-8"))
|
||||
assert "Missing required CSV columns" in str(exc_info.value)
|
||||
|
||||
|
||||
def test_parse_invalid_amount():
|
||||
invalid_csv = (
|
||||
"transaction_id,date,amount,category,description\n"
|
||||
"TX1001,2026-06-25,abc,Groceries,Weekly grocery shop\n"
|
||||
)
|
||||
with pytest.raises(InvalidCSVException) as exc_info:
|
||||
parse_and_validate_csv(invalid_csv.encode("utf-8"))
|
||||
assert "Invalid amount" in str(exc_info.value)
|
||||
|
||||
|
||||
def test_parse_invalid_date():
|
||||
invalid_csv = (
|
||||
"transaction_id,date,amount,category,description\n"
|
||||
"TX1001,invalid-date,120.50,Groceries,Weekly grocery shop\n"
|
||||
)
|
||||
with pytest.raises(InvalidCSVException) as exc_info:
|
||||
parse_and_validate_csv(invalid_csv.encode("utf-8"))
|
||||
assert "Invalid date format" in str(exc_info.value)
|
||||
Reference in New Issue
Block a user