From b7cd56f72b5cefcf3a96a141f6c1d491c1580ab4 Mon Sep 17 00:00:00 2001 From: Merwane Hamadi Date: Sat, 22 Apr 2023 12:48:47 -0700 Subject: [PATCH] move decorator higher up Signed-off-by: Merwane Hamadi --- .github/workflows/ci.yml | 3 +-- tests/conftest.py | 2 -- tests/integration/goal_oriented/decorators.py | 14 -------------- .../goal_oriented/test_write_file.py | 8 ++------ tests/local_cache_test.py | 6 ++++++ tests/test_image_gen.py | 3 +++ tests/utils.py | 18 ++++++++++++++++++ 7 files changed, 30 insertions(+), 24 deletions(-) delete mode 100644 tests/integration/goal_oriented/decorators.py create mode 100644 tests/utils.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 069e5633..780db14f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -71,8 +71,7 @@ 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 tests/integration/goal_oriented --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 diff --git a/tests/conftest.py b/tests/conftest.py index 36233c18..bf6bd6c5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,3 @@ from dotenv import load_dotenv load_dotenv() - -# Your other pytest configurations and fixtures diff --git a/tests/integration/goal_oriented/decorators.py b/tests/integration/goal_oriented/decorators.py deleted file mode 100644 index 9f35fc33..00000000 --- a/tests/integration/goal_oriented/decorators.py +++ /dev/null @@ -1,14 +0,0 @@ -import os -import pytest - - -def requires_openai_api_key(func): - def wrapper(*args, **kwargs): - if not os.environ.get('OPENAI_API_KEY'): - pytest.skip( - "Environment variable 'OPENAI_API_KEY' is not set, skipping the test." - ) - else: - return func(*args, **kwargs) - - return wrapper diff --git a/tests/integration/goal_oriented/test_write_file.py b/tests/integration/goal_oriented/test_write_file.py index 55c51f20..d995c7a3 100644 --- a/tests/integration/goal_oriented/test_write_file.py +++ b/tests/integration/goal_oriented/test_write_file.py @@ -2,7 +2,6 @@ import concurrent import os import unittest -import pytest import vcr from autogpt.agent import Agent @@ -13,8 +12,8 @@ from autogpt.memory import get_memory # from autogpt.prompt import Prompt from autogpt.workspace import WORKSPACE_PATH -from tests.integration.goal_oriented.decorators import requires_openai_api_key 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') @@ -28,11 +27,8 @@ my_vcr = vcr.VCR( CFG = Config() -@requires_openai_api_key -@pytest.mark.integration_test +@requires_api_key("OPENAI_API_KEY") def test_write_file() -> None: - # Your test code here - # if file exist file_name = "hello_world.txt" diff --git a/tests/local_cache_test.py b/tests/local_cache_test.py index bb108626..bbaf8e57 100644 --- a/tests/local_cache_test.py +++ b/tests/local_cache_test.py @@ -7,6 +7,7 @@ import unittest import pytest from autogpt.memory.local import LocalCache +from tests.utils import requires_api_key def mock_config() -> dict: @@ -32,17 +33,20 @@ class TestLocalCache(unittest.TestCase): 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" @@ -50,6 +54,7 @@ class TestLocalCache(unittest.TestCase): 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" @@ -59,6 +64,7 @@ class TestLocalCache(unittest.TestCase): 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" diff --git a/tests/test_image_gen.py b/tests/test_image_gen.py index 19c57e42..58b8337f 100644 --- a/tests/test_image_gen.py +++ b/tests/test_image_gen.py @@ -7,6 +7,7 @@ from PIL import Image 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): @@ -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" @@ -36,6 +38,7 @@ class TestImageGen(unittest.TestCase): self.assertEqual(img.size, (512, 512)) image_path.unlink() + @requires_api_key("HUGGINGFACE_API_TOKEN") def test_huggingface(self): self.config.image_provider = "huggingface" diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 00000000..a09daa8d --- /dev/null +++ b/tests/utils.py @@ -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