Skip to content

Commit

Permalink
ci: add app unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kweinmeister committed Jun 2, 2024
1 parent 6d83a08 commit 295e919
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 7 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: App unit tests

on:
push:
branches:
- main
paths:
- 'api/**'
- '.github/workflows/app.yaml'
pull_request:
branches:
- main
paths:
- 'api/**'
- '.github/workflows/app.yaml'

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8","3.11"]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'
- run: pip install -r src/frontend_service/requirements.txt
- run: pip install -r src/frontend_service/requirements-test.txt
- run: pytest src/frontend_service
- run: pip install -r src/retrieval_service/requirements.txt
- run: pip install -r src/retrieval_service/requirements-test.txt
- run: pytest src/retrieval_service/app
40 changes: 38 additions & 2 deletions src/frontend_service/app_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,42 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from unittest.mock import AsyncMock, MagicMock, patch
from fastapi import FastAPI
from fastapi.testclient import TestClient
import pytest
from main import init_app
from agent import init_agent
from langchain_core.messages import HumanMessage

def test_empty():
pass
CLIENT_ID = "test client id"
SECRET_KEY = "test_secret"
HISTORY = [HumanMessage(content="test")]


@pytest.fixture(scope="module")
def app():
app = init_app(client_id=CLIENT_ID, secret_key=SECRET_KEY)
if app is None:
raise TypeError("app did not initialize")
return app


@patch("agent.init_agent")
def test_index_handler(mock_init_agent, app: FastAPI):
mock_init_agent.return_value = AsyncMock(agent=AsyncMock(ainvoke=AsyncMock()))
with TestClient(app) as client:
response = client.get("/")
assert response.status_code == 200
assert CLIENT_ID in response.text


@pytest.mark.asyncio
@patch("agent.init_agent")
async def test_init_agent(mock_init_agent):
mock_init_agent.return_value = AsyncMock(agent=AsyncMock(ainvoke=AsyncMock()))
history = HISTORY
user_agent = await init_agent(history)
assert user_agent is not None
assert user_agent.agent is not None
assert user_agent.client is not None
5 changes: 2 additions & 3 deletions src/retrieval_service/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@

from contextlib import asynccontextmanager
from ipaddress import IPv4Address, IPv6Address
from typing import Optional
from typing import Optional, Union

import yaml
import os
from fastapi import FastAPI
from langchain.embeddings import VertexAIEmbeddings
Expand All @@ -30,7 +29,7 @@


class AppConfig(BaseModel):
host: IPv4Address | IPv6Address = IPv4Address("127.0.0.1")
host: Union[IPv4Address, IPv6Address] = IPv4Address("127.0.0.1")
port: int = 8080
datastore: datastore.Config
clientId: Optional[str] = None
Expand Down
4 changes: 2 additions & 2 deletions src/retrieval_service/datastore/providers/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import asyncio
from datetime import datetime
from ipaddress import IPv4Address, IPv6Address
from typing import Literal, Optional
from typing import Literal, Optional, Union

import asyncpg
from pgvector.asyncpg import register_vector
Expand All @@ -30,7 +30,7 @@

class Config(BaseModel, datastore.AbstractConfig):
kind: Literal["postgres"]
host: IPv4Address | IPv6Address = IPv4Address("127.0.0.1")
host: Union[IPv4Address, IPv6Address] = IPv4Address("127.0.0.1")
port: int = 5432
user: str
password: str
Expand Down

0 comments on commit 295e919

Please sign in to comment.