From 881366b9bcb7bf168e46c68d5f58ffb403713e77 Mon Sep 17 00:00:00 2001 From: kaanHapptiq <168740395+kaanHapptiq@users.noreply.github.com> Date: Mon, 12 Aug 2024 14:51:46 +0200 Subject: [PATCH 01/10] Add CI for pytest --- .github/workflows/python-package.yml | 44 ++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 .github/workflows/python-package.yml diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml new file mode 100644 index 0000000..a2f621d --- /dev/null +++ b/.github/workflows/python-package.yml @@ -0,0 +1,44 @@ +# This workflow will install Python dependencies, run tests and lint with a variety of Python versions +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python + +name: Python package + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + build: + + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + python-version: ["3.9", "3.10", "3.11"] + package: ["happtiq_commons_gen_ai", "happtiq_commons_google_cloud"] + + steps: + - uses: actions/checkout@v4 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + cd packages/${{ matrix.package }} + python -m pip install --upgrade pip + python -m pip install flake8 pytest + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + - name: Lint with flake8 + run: | + cd packages/${{ matrix.package }} + # stop the build if there are Python syntax errors or undefined names + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide + flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + - name: Test with pytest + run: | + cd packages/${{ matrix.package }} + pytest From e882f33f5e65d6244d83026dbe47ccb036d58fcf Mon Sep 17 00:00:00 2001 From: mab Date: Thu, 29 Aug 2024 12:11:53 +0200 Subject: [PATCH 02/10] try another pytest cpmmand --- .github/workflows/python-package.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index a2f621d..d20933f 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -41,4 +41,4 @@ jobs: - name: Test with pytest run: | cd packages/${{ matrix.package }} - pytest + python -m pytest tests From 06e6238fd39deacfc3c8ad18221ab7de4e9ee390 Mon Sep 17 00:00:00 2001 From: mab Date: Thu, 29 Aug 2024 12:38:08 +0200 Subject: [PATCH 03/10] change install deps --- .github/workflows/python-package.yml | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index d20933f..9bea4f6 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -29,16 +29,9 @@ jobs: run: | cd packages/${{ matrix.package }} python -m pip install --upgrade pip - python -m pip install flake8 pytest - if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - - name: Lint with flake8 - run: | - cd packages/${{ matrix.package }} - # stop the build if there are Python syntax errors or undefined names - flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics - # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide - flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + pip install -e '.' + pip install -e '.[dev]' - name: Test with pytest run: | cd packages/${{ matrix.package }} - python -m pytest tests + pytest From da735839406de9b967603f3f7c4e2cb0806fabb9 Mon Sep 17 00:00:00 2001 From: mab Date: Thu, 29 Aug 2024 12:52:37 +0200 Subject: [PATCH 04/10] mock google auth --- .../tests/happtiq_commons_gen_ai/gemini_service_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/happtiq_commons_gen_ai/tests/happtiq_commons_gen_ai/gemini_service_test.py b/packages/happtiq_commons_gen_ai/tests/happtiq_commons_gen_ai/gemini_service_test.py index 5cc1eb1..bd70d93 100644 --- a/packages/happtiq_commons_gen_ai/tests/happtiq_commons_gen_ai/gemini_service_test.py +++ b/packages/happtiq_commons_gen_ai/tests/happtiq_commons_gen_ai/gemini_service_test.py @@ -4,6 +4,7 @@ @pytest.fixture def vertex_ai_service(monkeypatch): + monkeypatch.setattr("google.auth.default", MagicMock()) monkeypatch.setattr("vertexai.init", MagicMock()) return GeminiService(project_id="some-project", location="some-location", model_id="some-model") From 32ccb4e4a85bf1d67fdb6c14d65c48c7ac056c83 Mon Sep 17 00:00:00 2001 From: mab Date: Thu, 29 Aug 2024 12:58:41 +0200 Subject: [PATCH 05/10] return tuple --- .../tests/happtiq_commons_gen_ai/gemini_service_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/happtiq_commons_gen_ai/tests/happtiq_commons_gen_ai/gemini_service_test.py b/packages/happtiq_commons_gen_ai/tests/happtiq_commons_gen_ai/gemini_service_test.py index bd70d93..2b78078 100644 --- a/packages/happtiq_commons_gen_ai/tests/happtiq_commons_gen_ai/gemini_service_test.py +++ b/packages/happtiq_commons_gen_ai/tests/happtiq_commons_gen_ai/gemini_service_test.py @@ -5,7 +5,7 @@ @pytest.fixture def vertex_ai_service(monkeypatch): monkeypatch.setattr("google.auth.default", MagicMock()) - monkeypatch.setattr("vertexai.init", MagicMock()) + monkeypatch.setattr("vertexai.init", (MagicMock(), MagicMock())) return GeminiService(project_id="some-project", location="some-location", model_id="some-model") def mockGenerativeModel(): From 40449dc195969a667720656c5ea4deb5a9b67ae0 Mon Sep 17 00:00:00 2001 From: mab Date: Mon, 2 Sep 2024 08:33:20 +0200 Subject: [PATCH 06/10] mock correctly --- .../tests/happtiq_commons_gen_ai/gemini_service_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/happtiq_commons_gen_ai/tests/happtiq_commons_gen_ai/gemini_service_test.py b/packages/happtiq_commons_gen_ai/tests/happtiq_commons_gen_ai/gemini_service_test.py index 2b78078..204fb5f 100644 --- a/packages/happtiq_commons_gen_ai/tests/happtiq_commons_gen_ai/gemini_service_test.py +++ b/packages/happtiq_commons_gen_ai/tests/happtiq_commons_gen_ai/gemini_service_test.py @@ -4,8 +4,8 @@ @pytest.fixture def vertex_ai_service(monkeypatch): - monkeypatch.setattr("google.auth.default", MagicMock()) - monkeypatch.setattr("vertexai.init", (MagicMock(), MagicMock())) + monkeypatch.setattr("google.auth.default", (MagicMock(), MagicMock())) + monkeypatch.setattr("vertexai.init", MagicMock()) return GeminiService(project_id="some-project", location="some-location", model_id="some-model") def mockGenerativeModel(): From b2876b35938c066f480e6bacc2a41742114bd62a Mon Sep 17 00:00:00 2001 From: mab Date: Mon, 2 Sep 2024 08:46:31 +0200 Subject: [PATCH 07/10] fix mocking --- .../tests/happtiq_commons_gen_ai/gemini_service_test.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/happtiq_commons_gen_ai/tests/happtiq_commons_gen_ai/gemini_service_test.py b/packages/happtiq_commons_gen_ai/tests/happtiq_commons_gen_ai/gemini_service_test.py index 204fb5f..c7be1c8 100644 --- a/packages/happtiq_commons_gen_ai/tests/happtiq_commons_gen_ai/gemini_service_test.py +++ b/packages/happtiq_commons_gen_ai/tests/happtiq_commons_gen_ai/gemini_service_test.py @@ -2,9 +2,12 @@ from unittest.mock import MagicMock, patch from happtiq_commons_gen_ai.gemini_service import GeminiService +@pytest.fixture +def google_auth(monkeypatch): + monkeypatch.setattr("google.auth.default", MagicMock(return_value=(MagicMock(),MagicMock()))) + @pytest.fixture def vertex_ai_service(monkeypatch): - monkeypatch.setattr("google.auth.default", (MagicMock(), MagicMock())) monkeypatch.setattr("vertexai.init", MagicMock()) return GeminiService(project_id="some-project", location="some-location", model_id="some-model") From dd2b17a5e97093b0d121e7f65624ff020e3e3190 Mon Sep 17 00:00:00 2001 From: mab Date: Mon, 2 Sep 2024 08:50:16 +0200 Subject: [PATCH 08/10] try something else --- .../tests/happtiq_commons_gen_ai/gemini_service_test.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/happtiq_commons_gen_ai/tests/happtiq_commons_gen_ai/gemini_service_test.py b/packages/happtiq_commons_gen_ai/tests/happtiq_commons_gen_ai/gemini_service_test.py index c7be1c8..aff33a2 100644 --- a/packages/happtiq_commons_gen_ai/tests/happtiq_commons_gen_ai/gemini_service_test.py +++ b/packages/happtiq_commons_gen_ai/tests/happtiq_commons_gen_ai/gemini_service_test.py @@ -2,12 +2,9 @@ from unittest.mock import MagicMock, patch from happtiq_commons_gen_ai.gemini_service import GeminiService -@pytest.fixture -def google_auth(monkeypatch): - monkeypatch.setattr("google.auth.default", MagicMock(return_value=(MagicMock(),MagicMock()))) - @pytest.fixture def vertex_ai_service(monkeypatch): + monkeypatch.setattr("google.auth.default", MagicMock(return_value=(MagicMock(),MagicMock()))) monkeypatch.setattr("vertexai.init", MagicMock()) return GeminiService(project_id="some-project", location="some-location", model_id="some-model") From c52afa6f0370b8187f69db715cb737b5a3952c6c Mon Sep 17 00:00:00 2001 From: mab Date: Mon, 2 Sep 2024 08:57:15 +0200 Subject: [PATCH 09/10] mock auth --- .../tests/happtiq_commons_google_cloud/gcs_service_test.py | 3 ++- .../happtiq_commons_google_cloud/pubsub_api_service_test.py | 3 ++- .../secret_manager_api_service_test.py | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/happtiq_commons_google_cloud/tests/happtiq_commons_google_cloud/gcs_service_test.py b/packages/happtiq_commons_google_cloud/tests/happtiq_commons_google_cloud/gcs_service_test.py index 20340bd..a5b5e9e 100644 --- a/packages/happtiq_commons_google_cloud/tests/happtiq_commons_google_cloud/gcs_service_test.py +++ b/packages/happtiq_commons_google_cloud/tests/happtiq_commons_google_cloud/gcs_service_test.py @@ -3,7 +3,8 @@ from happtiq_commons_google_cloud.gcs_api_service import GcsApiService @pytest.fixture -def gcs_api_service(): +def gcs_api_service(monkeypatch): + monkeypatch.setattr("google.auth.default", MagicMock(return_value=(MagicMock(universe_domain="googleapis.com"),"some-proje"))) return GcsApiService(timeout=30) def mock_storage_client(): diff --git a/packages/happtiq_commons_google_cloud/tests/happtiq_commons_google_cloud/pubsub_api_service_test.py b/packages/happtiq_commons_google_cloud/tests/happtiq_commons_google_cloud/pubsub_api_service_test.py index cbff902..09e6b53 100644 --- a/packages/happtiq_commons_google_cloud/tests/happtiq_commons_google_cloud/pubsub_api_service_test.py +++ b/packages/happtiq_commons_google_cloud/tests/happtiq_commons_google_cloud/pubsub_api_service_test.py @@ -5,7 +5,8 @@ @pytest.fixture -def pubsub_api_service(): +def pubsub_api_service(monkeypatch): + monkeypatch.setattr("google.auth.default", MagicMock(return_value=(MagicMock(),"some-proje"))) return PubsubApiService() def test_publish_message(pubsub_api_service, monkeypatch): diff --git a/packages/happtiq_commons_google_cloud/tests/happtiq_commons_google_cloud/secret_manager_api_service_test.py b/packages/happtiq_commons_google_cloud/tests/happtiq_commons_google_cloud/secret_manager_api_service_test.py index a56b969..ca8d31d 100644 --- a/packages/happtiq_commons_google_cloud/tests/happtiq_commons_google_cloud/secret_manager_api_service_test.py +++ b/packages/happtiq_commons_google_cloud/tests/happtiq_commons_google_cloud/secret_manager_api_service_test.py @@ -3,7 +3,8 @@ from unittest.mock import MagicMock, patch @pytest.fixture -def secret_manager_api_service(): +def secret_manager_api_service(monkeypatch): + monkeypatch.setattr("google.auth.default", MagicMock(return_value=(MagicMock(),"some-proje"))) return SecretManagerApiService() def test_read_secret(secret_manager_api_service, monkeypatch): From 309822b1a00ce87d675b41254ff037f89cf13cbf Mon Sep 17 00:00:00 2001 From: mab Date: Mon, 2 Sep 2024 09:17:10 +0200 Subject: [PATCH 10/10] add test to workflow --- .github/workflows/publish_pypi.yml | 24 +++++++++++++++++++ .../{python-package.yml => test.yml} | 11 +++------ 2 files changed, 27 insertions(+), 8 deletions(-) rename .github/workflows/{python-package.yml => test.yml} (89%) diff --git a/.github/workflows/publish_pypi.yml b/.github/workflows/publish_pypi.yml index 9080e1c..2e6a659 100644 --- a/.github/workflows/publish_pypi.yml +++ b/.github/workflows/publish_pypi.yml @@ -3,6 +3,30 @@ name: Publish Python 🐍 distribution 📦 to PyPI on: [push, pull_request] jobs: + test: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + python-version: ["3.9", "3.10", "3.11"] + package: ["happtiq_commons_gen_ai", "happtiq_commons_google_cloud"] + + steps: + - uses: actions/checkout@v4 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + cd packages/${{ matrix.package }} + python -m pip install --upgrade pip + pip install -e '.' + pip install -e '.[dev]' + - name: Test with pytest + run: | + cd packages/${{ matrix.package }} + pytest build: name: Build distribution 📦 runs-on: ubuntu-latest diff --git a/.github/workflows/python-package.yml b/.github/workflows/test.yml similarity index 89% rename from .github/workflows/python-package.yml rename to .github/workflows/test.yml index 9bea4f6..c790e6a 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/test.yml @@ -1,17 +1,12 @@ # This workflow will install Python dependencies, run tests and lint with a variety of Python versions # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python -name: Python package +name: Test with pytest -on: - push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] +on: [push, pull_request] jobs: - build: - + test: runs-on: ubuntu-latest strategy: fail-fast: false