Add test for new records in DB
This commit is contained in:
@@ -2,7 +2,7 @@ from sqlalchemy import MetaData
|
|||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
from sqlalchemy.orm import sessionmaker, declarative_base
|
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}"
|
DATABASE_URL = f"postgresql+psycopg2://{POSTGRES_USER}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}:{POSTGRES_PORT}/{POSTGRES_DB}"
|
||||||
|
|||||||
45
store/test_db.py
Normal file
45
store/test_db.py
Normal 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()
|
||||||
Reference in New Issue
Block a user