From eb5a8a87d869d7cb591f48890df7fdad75f002de Mon Sep 17 00:00:00 2001 From: Merwane Hamadi Date: Sat, 22 Apr 2023 12:39:56 -0700 Subject: [PATCH 1/2] add decorator to tests Signed-off-by: Merwane Hamadi --- .github/workflows/ci.yml | 4 +- .github/workflows/goal_oriented_tasks.yml | 37 ------------------- tests/conftest.py | 5 +++ tests/integration/goal_oriented/decorators.py | 14 +++++++ .../goal_oriented/test_write_file.py | 4 ++ 5 files changed, 26 insertions(+), 38 deletions(-) delete mode 100644 .github/workflows/goal_oriented_tasks.yml create mode 100644 tests/conftest.py create mode 100644 tests/integration/goal_oriented/decorators.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d3dee93d69..069e5633ea 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -72,7 +72,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 tests/integration/goal_oriented --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 diff --git a/.github/workflows/goal_oriented_tasks.yml b/.github/workflows/goal_oriented_tasks.yml deleted file mode 100644 index 816b64ff2d..0000000000 --- a/.github/workflows/goal_oriented_tasks.yml +++ /dev/null @@ -1,37 +0,0 @@ -name: Goal Oriented Tasks - -on: - push: - branches: - - master - pull_request: - branches: - - master - -jobs: - build: - runs-on: ubuntu-latest - - strategy: - matrix: - python-version: ['3.10'] - - steps: - - name: Check out repository - uses: actions/checkout@v3 - - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v3 - with: - python-version: ${{ matrix.python-version }} - - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install -r requirements.txt - - - name: Generate coverage report - run: | - coverage run --source=autogpt -m pytest -s -k tests/integration/goal_oriented - env: - OPENAI_API_KEY: 'dummy_api_key' diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000000..36233c18ff --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,5 @@ +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 new file mode 100644 index 0000000000..9f35fc330c --- /dev/null +++ b/tests/integration/goal_oriented/decorators.py @@ -0,0 +1,14 @@ +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 033c783b9b..55c51f204b 100644 --- a/tests/integration/goal_oriented/test_write_file.py +++ b/tests/integration/goal_oriented/test_write_file.py @@ -13,6 +13,7 @@ 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 current_file_dir = os.path.dirname(os.path.abspath(__file__)) @@ -27,8 +28,11 @@ my_vcr = vcr.VCR( CFG = Config() +@requires_openai_api_key @pytest.mark.integration_test def test_write_file() -> None: + # Your test code here + # if file exist file_name = "hello_world.txt" From b7cd56f72b5cefcf3a96a141f6c1d491c1580ab4 Mon Sep 17 00:00:00 2001 From: Merwane Hamadi Date: Sat, 22 Apr 2023 12:48:47 -0700 Subject: [PATCH 2/2] 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 069e5633ea..780db14fc6 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 36233c18ff..bf6bd6c59f 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 9f35fc330c..0000000000 --- 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 55c51f204b..d995c7a39a 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 bb10862656..bbaf8e572f 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 19c57e427d..58b8337f42 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 0000000000..a09daa8dfd --- /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