🚧 chore(conftest.py): add fixtures for database testing

 test(database.py): add tests for CRUD operations on Flow model
The new fixtures added to conftest.py are session and client fixtures. These fixtures are used to create a test database and a test client for testing the database. The tests added to test_database.py test the CRUD operations on the Flow model. The tests include creating a flow, reading all flows, reading a single flow, updating a flow, and deleting a flow. These tests ensure that the database is working as expected and that the API endpoints for the Flow model are functioning correctly.
This commit is contained in:
Gabriel Almeida 2023-05-30 21:24:23 -03:00
commit 6cacfced09
2 changed files with 93 additions and 0 deletions

View file

@ -1,10 +1,13 @@
import json
from pathlib import Path
from typing import AsyncGenerator
from langflow.api.database import get_session
import pytest
from fastapi.testclient import TestClient
from httpx import AsyncClient
from sqlmodel import SQLModel, Session, create_engine
from sqlmodel.pool import StaticPool
def pytest_configure():
@ -76,3 +79,35 @@ def complex_graph():
@pytest.fixture
def openapi_graph():
return get_graph("openapi")
@pytest.fixture
def json_flow():
with open(pytest.BASIC_EXAMPLE_PATH, "r") as f:
return f.read()
@pytest.fixture(name="session") #
def session_fixture(): #
engine = create_engine(
"sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool
)
SQLModel.metadata.create_all(engine)
with Session(engine) as session:
yield session
@pytest.fixture(name="client") #
def client_fixture(session: Session): #
def get_session_override(): #
return session
from langflow.main import create_app
app = create_app()
app.dependency_overrides[get_session] = get_session_override #
client = TestClient(app) #
yield client #
app.dependency_overrides.clear() #

58
tests/test_database.py Normal file
View file

@ -0,0 +1,58 @@
from langflow.database.models.flow import FlowCreate
def test_create_flow(client, json_flow):
flow = FlowCreate(name="Test Flow", flow=json_flow)
response = client.post("/flows/", json=flow.dict())
assert response.status_code == 200
assert response.json()["name"] == flow.name
assert response.json()["flow"] == flow.flow
def test_read_flows(client, json_flow):
flow = FlowCreate(name="Test Flow", flow=json_flow)
response = client.post("/flows/", json=flow.dict())
assert response.status_code == 200
assert response.json()["name"] == flow.name
assert response.json()["flow"] == flow.flow
flow = FlowCreate(name="Test Flow", flow=json_flow)
response = client.post("/flows/", json=flow.dict())
assert response.status_code == 200
assert response.json()["name"] == flow.name
assert response.json()["flow"] == flow.flow
response = client.get("/flows/")
assert response.status_code == 200
assert len(response.json()) > 0
def test_read_flow(client, json_flow):
flow = FlowCreate(name="Test Flow", flow=json_flow)
response = client.post("/flows/", json=flow.dict())
flow_id = response.json()["id"]
response = client.get(f"/flows/{flow_id}")
assert response.status_code == 200
assert response.json()["name"] == flow.name
assert response.json()["flow"] == flow.flow
def test_update_flow(client, json_flow):
flow = FlowCreate(name="Test Flow", flow=json_flow)
response = client.post("/flows/", json=flow.dict())
flow_id = response.json()["id"]
updated_flow = FlowCreate(
name="Updated Flow", flow=json_flow.replace("BasicExample", "Updated Flow")
)
response = client.put(f"/flows/{flow_id}", json=updated_flow.dict())
assert response.status_code == 200
assert response.json()["name"] == updated_flow.name
assert response.json()["flow"] == updated_flow.flow
def test_delete_flow(client, json_flow):
flow = FlowCreate(name="Test Flow", flow=json_flow)
response = client.post("/flows/", json=flow.dict())
flow_id = response.json()["id"]
response = client.delete(f"/flows/{flow_id}")
assert response.status_code == 200
assert response.json()["message"] == "Flow deleted successfully"