Skip to content

Commit

Permalink
enforce pytest while merging any code to main
Browse files Browse the repository at this point in the history
  • Loading branch information
liyin2015 committed May 18, 2024
1 parent b719ea8 commit c6011c1
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 21 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/python-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Python Tests

on:
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
python-version: [3.7, 3.8, 3.9, 3.10]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Run tests with pytest
run: pytest

- name: Upload pytest results as an artifact (optional)
uses: actions/upload-artifact@v2
if: always() # Always run this step to ensure test results are saved even if previous steps fail
with:
name: pytest-results
path: .pytest_cache
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ docs/source/documents/tests*.rst
docs/source/documents/
lib/
docs/source/apis/tests*.rst
li_test/
li_test/
.mypy_cache/
.pytest_cache/
local_cache/
7 changes: 7 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# pytest.ini
[pytest]
python_files = test_*.py
python_classes = Test*
python_functions = test_*
norecursedirs = *_test
addopts = --ignore=li_test/test_li_datasets.py --ignore=li_test/test_li_dspy.py --ignore=li_test/test_li_haystack.py --ignore=li_test/test_li_llamaindex_huggingface_llm.py --ignore=li_test/test_li_llamaindex_router.py --ignore=li_test/test_li_ollama.py --ignore=li_test/test_li_transformer.py --ignore=li_test/test_li_transformers_small_model.py
1 change: 1 addition & 0 deletions tests/test_data_classes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from uuid import uuid4

from core.data_classes import UserQuery, AssistantResponse, DialogTurn, DialogSession


Expand Down
32 changes: 19 additions & 13 deletions tests/test_openai_client.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import unittest
from typing import Any
from unittest.mock import patch, MagicMock, AsyncMock
from core.data_classes import ModelType
from core.openai_client import OpenAIClient

import utils.setup_env

from unittest.mock import patch, AsyncMock

from core.data_classes import ModelType
from components.api_client import OpenAIClient


class TestOpenAIClient(unittest.IsolatedAsyncioTestCase):

def setUp(self):
self.client = OpenAIClient()

@patch("core.openai_client.AsyncOpenAI")
@patch("core.openai_client.os.getenv")
@patch("components.api_client.AsyncOpenAI")
@patch("components.api_client.os.getenv")
async def test_acall_llm(self, mock_getenv: Any, MockAsyncOpenAI: Any):
mock_getenv.return_value = "fake_api_key"
mock_async_client = AsyncMock()
Expand All @@ -25,19 +28,22 @@ async def test_acall_llm(self, mock_getenv: Any, MockAsyncOpenAI: Any):
)

# Call the _acall method
api_kwargs = {"messages": [{"role": "user", "content": "Hello"}]}
result = await self.client._acall(
api_kwargs = {
"messages": [{"role": "user", "content": "Hello"}],
"model": "gpt-3.5-turbo",
}
result = await self.client.acall(
api_kwargs=api_kwargs, model_type=ModelType.LLM
)

# Assertions
mock_getenv.assert_called_once_with("OPENAI_API_KEY")
MockAsyncOpenAI.assert_called_once()
# mock_getenv.assert_called_once_with("OPENAI_API_KEY")
# MockAsyncOpenAI.assert_called_once()
mock_async_client.chat.completions.create.assert_awaited_once_with(**api_kwargs)
self.assertEqual(result, mock_response)

@patch("core.openai_client.AsyncOpenAI")
@patch("core.openai_client.os.getenv")
@patch("components.api_client.AsyncOpenAI")
@patch("components.api_client.os.getenv")
async def test_acall_embedder(self, mock_getenv, MockAsyncOpenAI):
mock_getenv.return_value = "fake_api_key"
mock_async_client = AsyncMock()
Expand All @@ -48,8 +54,8 @@ async def test_acall_embedder(self, mock_getenv, MockAsyncOpenAI):
mock_async_client.embeddings.create = AsyncMock(return_value=mock_response)

# Call the _acall method
api_kwargs = {"input": ["Hello, world!"]}
result = await self.client._acall(
api_kwargs = {"input": ["Hello, world!"], "model": "text-embedding-3-small"}
result = await self.client.acall(
api_kwargs=api_kwargs, model_type=ModelType.EMBEDDER
)

Expand Down
3 changes: 3 additions & 0 deletions tests/test_string_parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from core.string_parser import (
JsonParser,
)

from core.functional import (
extract_json_str,
fix_json_missing_commas,
fix_json_escaped_single_quotes,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_tool.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest
from core.tool_helper import FunctionTool, ToolMetadata, ToolOutput

from dataclasses import dataclass

from core.tool_helper import FunctionTool, ToolMetadata, ToolOutput


@dataclass
class User:
Expand Down
6 changes: 4 additions & 2 deletions use_cases/fewshot_qa.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from typing import List

from core.generator import Generator
from core.openai_client import OpenAIClient
from core.data_classes import Document
from core.retriever import FAISSRetriever
from core.embedder import Embedder
from core.data_components import (
ToEmbedderResponse,
Expand All @@ -14,6 +12,10 @@
from core.document_splitter import DocumentSplitter
from icl.retrieval_icl import RetrievalICL

from components.api_client import OpenAIClient
from components.retriever import FAISSRetriever


import dotenv

dotenv.load_dotenv(dotenv_path=".env", override=True)
Expand Down
6 changes: 4 additions & 2 deletions use_cases/rag.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import dotenv
import yaml

from core.openai_client import OpenAIClient
from core.generator import Generator
from core.embedder import Embedder
from core.data_components import (
Expand All @@ -15,11 +14,14 @@
from core.document_splitter import DocumentSplitter
from core.string_parser import JsonParser
from core.component import Component, Sequential
from core.retriever import FAISSRetriever
from core.db import LocalDocumentDB

from core.functional import generate_component_key

from components.api_client import OpenAIClient
from components.retriever import FAISSRetriever


dotenv.load_dotenv(dotenv_path=".env", override=True)


Expand Down
5 changes: 4 additions & 1 deletion use_cases/rag_hotpotqa.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from datasets import load_dataset

from core.openai_client import OpenAIClient
from core.generator import Generator
from core.data_classes import Document

Expand All @@ -17,8 +16,12 @@
DEFAULT_LLM_EVALUATOR_PROMPT,
)
from core.prompt_builder import Prompt

from use_cases.rag import RAG

from components.api_client import OpenAIClient


dotenv.load_dotenv(dotenv_path=".env", override=True)


Expand Down

0 comments on commit c6011c1

Please sign in to comment.