From 9f310ed12b0997670d78cd638d170182908800d5 Mon Sep 17 00:00:00 2001 From: zlatanjakic Date: Fri, 31 May 2024 14:57:56 +0200 Subject: [PATCH 1/4] Added the database test file --- app/tests/__init__.py | 0 app/tests/test_database.py | 54 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 app/tests/__init__.py create mode 100644 app/tests/test_database.py diff --git a/app/tests/__init__.py b/app/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/tests/test_database.py b/app/tests/test_database.py new file mode 100644 index 00000000..7bc93bd5 --- /dev/null +++ b/app/tests/test_database.py @@ -0,0 +1,54 @@ +from unittest.mock import patch, MagicMock +from services.database import create_document, get_documents, get_document, delete_document, update_document + + +@patch('services.database.firestore.client') +def test_create_document(mock_firestore_client): + mock_db = MagicMock() + mock_firestore_client.return_value = mock_db + collection, doc_id, data = 'test_collection', 'test_doc_id', { + 'field': 'value'} + result = create_document(collection, doc_id, data) + mock_db.collection().document().set.assert_called_with(data) + assert result == mock_db.collection().document().set.return_value + + +@patch('services.database.firestore.client') +def test_get_documents(mock_firestore_client): + mock_db = MagicMock() + mock_firestore_client.return_value = mock_db + collection, field, op, value = 'test_collection', 'field', '==', 'value' + result = get_documents(collection, field, op, value) + mock_db.collection().where().stream.assert_called_once() + assert result == mock_db.collection().where().stream.return_value + + +@patch('services.database.firestore.client') +def test_get_document(mock_firestore_client): + mock_db = MagicMock() + mock_firestore_client.return_value = mock_db + collection, doc_id = 'test_collection', 'test_doc_id' + result = get_document(collection, doc_id) + mock_db.collection().document().get.assert_called_once() + assert result == mock_db.collection().document().get.return_value + + +@patch('services.database.firestore.client') +def test_delete_document(mock_firestore_client): + mock_db = MagicMock() + mock_firestore_client.return_value = mock_db + collection, doc_id = 'test_collection', 'test_doc_id' + result = delete_document(collection, doc_id) + mock_db.collection().document().delete.assert_called_once() + assert result == mock_db.collection().document().delete.return_value + + +@patch('services.database.firestore.client') +def test_update_document(mock_firestore_client): + mock_db = MagicMock() + mock_firestore_client.return_value = mock_db + collection, doc_id, data = 'test_collection', 'test_doc_id', { + 'field': 'new_value'} + result = update_document(collection, doc_id, data) + mock_db.collection().document().update.assert_called_with(data) + assert result == mock_db.collection().document().update.return_value From 1b342cccb1550689fdab3357ec7eaeec0f26b822 Mon Sep 17 00:00:00 2001 From: zlatanjakic Date: Fri, 31 May 2024 18:04:15 +0200 Subject: [PATCH 2/4] Added the github workflow for running unit tests --- .github/workflows/pytest-unit-testing.yml | 35 +++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 .github/workflows/pytest-unit-testing.yml diff --git a/.github/workflows/pytest-unit-testing.yml b/.github/workflows/pytest-unit-testing.yml new file mode 100644 index 00000000..0559be34 --- /dev/null +++ b/.github/workflows/pytest-unit-testing.yml @@ -0,0 +1,35 @@ +name: Unit testing + +on: + push: + pull_requsts: + +jobs: + run-tests: + startegy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + python-version: + - "3.9" + - "3.10" + - "3.11" + - "3.12" + + name: + runs-on: ${{ matrix.os }} + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + + - name: Install dependecies + run: python -m pip install pytest firebase-admin scikit-learn pandas numpy + + - name: Run tests + run: pytest From 11232b86b0a87e550dfb4654c3e605875b969504 Mon Sep 17 00:00:00 2001 From: zlatanjakic Date: Fri, 31 May 2024 19:00:57 +0200 Subject: [PATCH 3/4] Fixed spelling error in workflow file --- .github/workflows/pytest-unit-testing.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/pytest-unit-testing.yml b/.github/workflows/pytest-unit-testing.yml index 0559be34..5f3f40d4 100644 --- a/.github/workflows/pytest-unit-testing.yml +++ b/.github/workflows/pytest-unit-testing.yml @@ -2,7 +2,7 @@ name: Unit testing on: push: - pull_requsts: + pull_request: jobs: run-tests: @@ -11,7 +11,6 @@ jobs: matrix: os: [ubuntu-latest, macos-latest, windows-latest] python-version: - - "3.9" - "3.10" - "3.11" - "3.12" From c2d8d3a6f1e21a19669850c16c5618046d285486 Mon Sep 17 00:00:00 2001 From: zlatanjakic Date: Fri, 31 May 2024 19:30:14 +0200 Subject: [PATCH 4/4] Fixed another spelling error in workflow file --- .github/workflows/pytest-unit-testing.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pytest-unit-testing.yml b/.github/workflows/pytest-unit-testing.yml index 5f3f40d4..0ae4b66e 100644 --- a/.github/workflows/pytest-unit-testing.yml +++ b/.github/workflows/pytest-unit-testing.yml @@ -6,7 +6,7 @@ on: jobs: run-tests: - startegy: + strategy: fail-fast: false matrix: os: [ubuntu-latest, macos-latest, windows-latest]