Skip to content

Commit

Permalink
Merge pull request #19 from Jonatan-Chaverri/add_unittest
Browse files Browse the repository at this point in the history
feat: mock tests and github workflow for them
  • Loading branch information
SAMAD101 authored Jan 24, 2024
2 parents ff98f16 + 7327e40 commit eb99d86
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: LinkLiberate

on:
pull_request:
branches:
- main

workflow_dispatch:
inputs:
message:
description: ''
required: false
default: ''

jobs:

build-pr:
name: Build
runs-on: [ubuntu-latest]
timeout-minutes: 30
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.11.6
uses: actions/setup-python@v3
with:
python-version: 3.11.6

- name: Set build start time
run: echo "BUILD_START_TIME=$(date +'%s')" >> $GITHUB_ENV
if: ${{ always() }}

- name: Install dependencies
run: |
set -o errexit
sudo apt-get update -y
sudo apt-get install -y bc
python -m pip install pdm
pdm install
- name: Run unit tests
run: |
pdm run pytest tests
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ package-type = "application"
[tool.pdm.dev-dependencies]
tests = [
"pytest>=7.4.3",
"pytest-mock==3.6.1",
"requests>=2.31.0",
]

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pydantic-settings==2.1.0
pydantic_core==2.14.6
Pygments==2.17.2
pytest==7.4.4
pytest-mock==3.6.1
python-dotenv==1.0.0
python-multipart==0.0.6
PyYAML==6.0.1
Expand Down
36 changes: 36 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from unittest.mock import Mock

from fastapi.testclient import TestClient

from src.link_liberate.main import app


def test_index_page():
client = TestClient(app)
response = client.get("/")
assert response.status_code == 200
assert "request" in response.context


def test_web():
client = TestClient(app)
response = client.get("/liberate")
assert response.status_code == 200
assert "request" in response.context


def test_web_post(mocker):
# Patch the get_db function to return the db mock
mocker.patch('src.link_liberate.database.Session_Local', return_value=Mock())
client_mock = TestClient(app)
response = client_mock.post("/liberate", data={"content": "valid_content"})
assert response.status_code == 200



def test_get_link(mocker):
# Patch the get_db function to return the db mock
mocker.patch('src.link_liberate.database.Session_Local', return_value=Mock())
client_mock = TestClient(app)
response = client_mock.get("/valid_uuid", follow_redirects=False)
assert response.status_code == 301

0 comments on commit eb99d86

Please sign in to comment.