Skip to content

Commit

Permalink
refactor (tests0: resolve failing backend tests due to project struct…
Browse files Browse the repository at this point in the history
…uring
  • Loading branch information
aybruhm committed Sep 25, 2024
1 parent ad738db commit 83ae0cc
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 33 deletions.
6 changes: 2 additions & 4 deletions agenta-backend/agenta_backend/routers/evaluation_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,7 @@ async def fetch_evaluation_results(
"""

try:
evaluation = await db_manager.fetch_evaluation_by_id(
evaluation_id, project_id=request.state.project_id
)
evaluation = await db_manager.fetch_evaluation_by_id(evaluation_id)
if isCloudEE():
has_permission = await check_action_access(
user_uid=request.state.user_id,
Expand Down Expand Up @@ -416,7 +414,7 @@ async def delete_evaluations(
if isCloudEE():
has_permission = await check_action_access(
user_uid=request.state.user_id,
project_id=str(evaluation.project),
project_id=str(evaluation.project_id),
permission=Permission.DELETE_EVALUATION,
)
logger.debug(f"User has permission to delete evaluation: {has_permission}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from agenta_backend.models.db.postgres_engine import db_engine
from agenta_backend.models.shared_models import ConfigDB
from agenta_backend.models.db_models import (
ProjectDB,
AppDB,
UserDB,
DeploymentDB,
Expand Down Expand Up @@ -70,15 +71,20 @@ async def get_first_user_app(get_first_user_object):
user = await get_first_user_object

async with db_engine.get_session() as session:
app = AppDB(app_name="myapp", user_id=user.id)
project = ProjectDB(project_name="default", is_default=True)
session.add(project)
await session.commit()
await session.refresh(project)

app = AppDB(app_name="myapp", project_id=project.id)
session.add(app)
await session.commit()
await session.refresh(app)

db_image = ImageDB(
docker_id="sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
tags="agentaai/templates_v2:local_test_prompt",
user_id=user.id,
project_id=project.id,
)
session.add(db_image)
await session.commit()
Expand All @@ -91,7 +97,7 @@ async def get_first_user_app(get_first_user_object):

db_deployment = DeploymentDB(
app_id=app.id,
user_id=user.id,
project_id=project.id,
container_name="container_a_test",
container_id="w243e34red",
uri="http://localhost/app/w243e34red",
Expand All @@ -102,7 +108,7 @@ async def get_first_user_app(get_first_user_object):
db_base = VariantBaseDB(
base_name="app",
image_id=db_image.id,
user_id=user.id,
project_id=project.id,
app_id=app.id,
deployment_id=db_deployment.id,
)
Expand All @@ -114,7 +120,7 @@ async def get_first_user_app(get_first_user_object):
app_id=app.id,
variant_name="app",
image_id=db_image.id,
user_id=user.id,
project_id=project.id,
config_parameters={},
base_name="app",
config_name="default",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import httpx
import random
import pytest
import logging
from bson import ObjectId
Expand All @@ -9,6 +10,7 @@
from agenta_backend.models.db.postgres_engine import db_engine
from agenta_backend.models.shared_models import ConfigDB
from agenta_backend.models.db_models import (
ProjectDB,
AppDB,
DeploymentDB,
VariantBaseDB,
Expand Down Expand Up @@ -113,22 +115,22 @@ async def test_create_app_variant(get_first_user_object):
)
app = result.scalars().first()

project_result = await session.execute(
select(ProjectDB).filter_by(is_default=True)
)
project = project_result.scalars().first()

db_image = ImageDB(
docker_id="sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
tags="agentaai/templates_v2:local_test_prompt",
user_id=user.id,
project_id=project.id,
)
session.add(db_image)
await session.commit()

db_config = ConfigDB(
config_name="default",
parameters={},
)

db_deployment = DeploymentDB(
app_id=app.id,
user_id=user.id,
project_id=project.id,
container_name="container_a_test",
container_id="w243e34red",
uri="http://localhost/app/w243e34red",
Expand All @@ -140,7 +142,7 @@ async def test_create_app_variant(get_first_user_object):
db_base = VariantBaseDB(
base_name="app",
app_id=app.id,
user_id=user.id,
project_id=project.id,
image_id=db_image.id,
deployment_id=db_deployment.id,
)
Expand All @@ -151,7 +153,7 @@ async def test_create_app_variant(get_first_user_object):
app_id=app.id,
variant_name="app",
image_id=db_image.id,
user_id=user.id,
project_id=project.id,
config_parameters={},
base_name="app",
config_name="default",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ async def create_evaluation_with_evaluator(evaluator_config_name):
app_variant = app_variant_result.scalars().first()

testset_result = await session.execute(
select(TestSetDB).filter_by(app_id=app.id)
select(TestSetDB).filter_by(project_id=app.project_id)
)
testset = testset_result.scalars().first()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async def test_update_testset():
app = result.scalars().first()

testset_result = await session.execute(
select(TestSetDB).filter_by(app_id=app.id)
select(TestSetDB).filter_by(project_id=app.project_id)
)
testset = testset_result.scalars().first()

Expand Down Expand Up @@ -112,7 +112,7 @@ async def test_get_testsets():
)

assert response.status_code == 200
assert len(response.json()) == 1
assert len(response.json()) == 2


@pytest.mark.asyncio()
Expand All @@ -124,7 +124,7 @@ async def test_get_testset():
app = result.scalars().first()

testset_result = await session.execute(
select(TestSetDB).filter_by(app_id=app.id)
select(TestSetDB).filter_by(project_id=app.project_id)
)
testset = testset_result.scalars().first()

Expand All @@ -146,7 +146,7 @@ async def test_delete_testsets():
app = result.scalars().first()

testset_result = await session.execute(
select(TestSetDB).filter_by(app_id=app.id)
select(TestSetDB).filter_by(project_id=app.project_id)
)
testsets = testset_result.scalars().all()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from agenta_backend.models.db.postgres_engine import db_engine
from agenta_backend.models.db_models import (
AppDB,
TestSetDB,
AppVariantDB,
)

Expand All @@ -18,7 +17,6 @@
timeout = httpx.Timeout(timeout=5, read=None, write=5)

# Set global variables
APP_NAME = "evaluation_in_backend"
ENVIRONMENT = os.environ.get("ENVIRONMENT")
VARIANT_DEPLOY_ENVIRONMENTS = ["development", "staging", "production"]
OPEN_AI_KEY = os.environ.get("OPENAI_API_KEY")
Expand All @@ -31,13 +29,10 @@
@pytest.mark.asyncio
async def test_update_app_variant_parameters(app_variant_parameters_updated):
async with db_engine.get_session() as session:
result = await session.execute(select(AppDB).filter_by(app_name=APP_NAME))
app = result.scalars().first()

testset_result = await session.execute(
select(TestSetDB).filter_by(app_id=app.id)
result = await session.execute(
select(AppDB).filter_by(app_name="evaluation_in_backend")
)
testset = testset_result.scalars().first()
app = result.scalars().first()

app_variant_result = await session.execute(
select(AppVariantDB).filter_by(app_id=app.id, variant_name="app.default")
Expand All @@ -49,7 +44,6 @@ async def test_update_app_variant_parameters(app_variant_parameters_updated):
parameters["temperature"] = random.uniform(0.9, 1.5)
parameters["frequence_penalty"] = random.uniform(0.9, 1.5)
parameters["frequence_penalty"] = random.uniform(0.9, 1.5)
parameters["inputs"] = [{"name": list(testset.csvdata[0].keys())[0]}]
payload = {"parameters": parameters}

response = await test_client.put(
Expand All @@ -62,7 +56,9 @@ async def test_update_app_variant_parameters(app_variant_parameters_updated):
@pytest.mark.asyncio
async def test_deploy_to_environment(deploy_to_environment_payload):
async with db_engine.get_session() as session:
result = await session.execute(select(AppDB).filter_by(app_name=APP_NAME))
result = await session.execute(
select(AppDB).filter_by(app_name="evaluation_in_backend")
)
app = result.scalars().first()

app_variant_result = await session.execute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ async def test_fetch_user_profile_without_user_id():
assert response.json()["username"] == user_db_dict["username"]


@pytest.mark.asyncio
async def test_fetch_user_profile_with_valid_user_id():
async with db_engine.get_session() as session:
result = await session.execute(select(UserDB).filter_by(uid="0"))
Expand Down Expand Up @@ -75,6 +76,7 @@ async def test_fetch_user_profile_with_valid_user_id():
assert response.json()["username"] == user_db_dict["username"]


@pytest.mark.asyncio
async def test_fetch_user_profile_with_non_existent_user_id_error():
user_non_existent_id = str(uuid4())
response = await test_client.get(
Expand Down

0 comments on commit 83ae0cc

Please sign in to comment.