Skip to content

Commit

Permalink
Merge pull request #2946 from merwanehamadi/feature/add-decorator-to-…
Browse files Browse the repository at this point in the history
…tests

add decorator to tests so it's skipped if api key required but not present in the environment
  • Loading branch information
ntindle authored and Pwuts committed Apr 22, 2023
2 parents d3e4ec1 + b7cd56f commit 986bdaa
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 41 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ jobs:
- name: Run unittest tests with coverage
run: |
pytest --cov=autogpt --without-integration --without-slow-integration --cov-report term-missing --cov-branch --cov-report xml --cov-report term
pytest --cov=autogpt --cov-report term-missing --cov-branch --cov-report xml --cov-report term
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
- name: Generate coverage report
run: |
coverage report
Expand Down
37 changes: 0 additions & 37 deletions .github/workflows/goal_oriented_tasks.yml

This file was deleted.

3 changes: 3 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from dotenv import load_dotenv

load_dotenv()
4 changes: 2 additions & 2 deletions tests/integration/goal_oriented/test_write_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
import unittest

import pytest
import vcr

from autogpt.agent import Agent
Expand All @@ -14,6 +13,7 @@
# from autogpt.prompt import Prompt
from autogpt.workspace import WORKSPACE_PATH
from tests.integration.goal_oriented.vcr_helper import before_record_request
from tests.utils import requires_api_key

current_file_dir = os.path.dirname(os.path.abspath(__file__))
# tests_directory = os.path.join(current_file_dir, 'tests')
Expand All @@ -27,7 +27,7 @@
CFG = Config()


@pytest.mark.integration_test
@requires_api_key("OPENAI_API_KEY")
def test_write_file() -> None:
# if file exist
file_name = "hello_world.txt"
Expand Down
6 changes: 6 additions & 0 deletions tests/local_cache_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest

from autogpt.memory.local import LocalCache
from tests.utils import requires_api_key


def mock_config() -> dict:
Expand All @@ -32,24 +33,28 @@ def setUp(self) -> None:
self.cfg = mock_config()
self.cache = LocalCache(self.cfg)

@requires_api_key("OPENAI_API_KEY")
def test_add(self) -> None:
"""Test adding a text to the cache"""
text = "Sample text"
self.cache.add(text)
self.assertIn(text, self.cache.data.texts)

@requires_api_key("OPENAI_API_KEY")
def test_clear(self) -> None:
"""Test clearing the cache"""
self.cache.clear()
self.assertEqual(self.cache.data.texts, [])

@requires_api_key("OPENAI_API_KEY")
def test_get(self) -> None:
"""Test getting a text from the cache"""
text = "Sample text"
self.cache.add(text)
result = self.cache.get(text)
self.assertEqual(result, [text])

@requires_api_key("OPENAI_API_KEY")
def test_get_relevant(self) -> None:
"""Test getting relevant texts from the cache"""
text1 = "Sample text 1"
Expand All @@ -59,6 +64,7 @@ def test_get_relevant(self) -> None:
result = self.cache.get_relevant(text1, 1)
self.assertEqual(result, [text1])

@requires_api_key("OPENAI_API_KEY")
def test_get_stats(self) -> None:
"""Test getting the cache stats"""
text = "Sample text"
Expand Down
3 changes: 3 additions & 0 deletions tests/test_image_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from autogpt.commands.image_gen import generate_image, generate_image_with_sd_webui
from autogpt.config import Config
from autogpt.workspace import path_in_workspace
from tests.utils import requires_api_key


def lst(txt):
Expand All @@ -18,6 +19,7 @@ class TestImageGen(unittest.TestCase):
def setUp(self):
self.config = Config()

@requires_api_key("OPENAI_API_KEY")
def test_dalle(self):
self.config.image_provider = "dalle"

Expand All @@ -36,6 +38,7 @@ def test_dalle(self):
self.assertEqual(img.size, (512, 512))
image_path.unlink()

@requires_api_key("HUGGINGFACE_API_TOKEN")
def test_huggingface(self):
self.config.image_provider = "huggingface"

Expand Down
18 changes: 18 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os

import pytest


def requires_api_key(env_var):
def decorator(func):
def wrapper(*args, **kwargs):
if not os.environ.get(env_var):
pytest.skip(
f"Environment variable '{env_var}' is not set, skipping the test."
)
else:
return func(*args, **kwargs)

return wrapper

return decorator

0 comments on commit 986bdaa

Please sign in to comment.