Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pytest unit testing #25

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .github/workflows/pytest-unit-testing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Unit testing

on:
push:
pull_request:

jobs:
run-tests:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version:
- "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
Empty file added app/tests/__init__.py
Empty file.
54 changes: 54 additions & 0 deletions app/tests/test_database.py
Original file line number Diff line number Diff line change
@@ -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
Loading