Add test for new records in DB

This commit is contained in:
SimonSanich
2026-03-27 14:47:45 +02:00
parent 65f767d38e
commit 7d75a15de1
3 changed files with 48 additions and 3 deletions

View File

@@ -2,7 +2,7 @@ from sqlalchemy import MetaData
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, declarative_base
from config import POSTGRES_USER, POSTGRES_PASSWORD, POSTGRES_HOST, POSTGRES_PORT, POSTGRES_DB
from .config import POSTGRES_USER, POSTGRES_PASSWORD, POSTGRES_HOST, POSTGRES_PORT, POSTGRES_DB
DATABASE_URL = f"postgresql+psycopg2://{POSTGRES_USER}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}:{POSTGRES_PORT}/{POSTGRES_DB}"

View File

@@ -12,8 +12,8 @@ from sqlalchemy import (
)
from sqlalchemy.sql import select
from database import metadata, SessionLocal
from schemas import ProcessedAgentData, ProcessedAgentDataInDB
from .database import metadata, SessionLocal
from .schemas import ProcessedAgentData, ProcessedAgentDataInDB
# FastAPI app setup
app = FastAPI()

45
store/test_db.py Normal file
View File

@@ -0,0 +1,45 @@
from fastapi.testclient import TestClient
from .main import app, processed_agent_data
from .database import SessionLocal
from sqlalchemy import select
from datetime import datetime
client = TestClient(app)
def test_create_processed_agent_data():
db = SessionLocal()
before = db.execute(select(processed_agent_data)).fetchall()
before_count = len(before)
payload = {
"user_id": 123,
"data": [
{
"road_state": "good",
"agent_data": {
"user_id": 999,
"accelerometer": {
"x": 1.1,
"y": 2.2,
"z": 3.3
},
"gps": {
"latitude": 50.45,
"longitude": 30.52
},
"timestamp": datetime.now().isoformat()
}
}
]
}
response = client.post("/processed_agent_data/", json=payload)
assert response.status_code == 200
after = db.execute(select(processed_agent_data)).fetchall()
after_count = len(after)
assert after_count == before_count + 1
db.close()