Skip to content

Commit

Permalink
Merge pull request #38 from TheBeetles/create_tests
Browse files Browse the repository at this point in the history
Creating unit tests
  • Loading branch information
jason490 authored Aug 16, 2024
2 parents db9dc6c + 9fbfcf7 commit 9a9d0f1
Show file tree
Hide file tree
Showing 13 changed files with 389 additions and 58 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: E2E Tests
name: e2e Tests
on:
push:
branches: [ main, master ]
Expand Down Expand Up @@ -26,8 +26,8 @@ jobs:
- name: Ensure browsers are installed
run: playwright install --with-deps
- name: Start docker containers
run: cd .. & docker compose up -d
- name: Run your tests
run: docker compose -f ../docker-compose.e2e.yml up -d
- name: Run e2e tests
run: pytest --tracing=retain-on-failure
- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
Expand Down
27 changes: 27 additions & 0 deletions .github/workflows/unit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Unit Tests
on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]
jobs:
unit:
timeout-minutes: 60
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./api
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
cache: 'pipenv'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pipenv
pipenv install --system
- name: Start docker containers tests and run unit tests
run: docker compose -f ../docker-compose.test.yml up -d
11 changes: 11 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]

[requires]
python_version = "3.11"
2 changes: 1 addition & 1 deletion api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ COPY . .

RUN pipenv install --ignore-pipfile --system --dev

CMD python setup.py && python wsgi.py
CMD python setup.py && pytest && python wsgi.py
13 changes: 13 additions & 0 deletions api/Dockerfile.e2e
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM python:3.11.9-bookworm

WORKDIR /app

COPY Pipfile Pipfile.lock .

RUN pip install pipenv

COPY . .

RUN pipenv install --ignore-pipfile --system --dev

CMD python setup.py && python wsgi.py
13 changes: 13 additions & 0 deletions api/Dockerfile.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM python:3.11.9-bookworm

WORKDIR /app

COPY Pipfile Pipfile.lock .

RUN pip install pipenv

COPY . .

RUN pipenv install --ignore-pipfile --system --dev

CMD python setup.py && pytest
141 changes: 87 additions & 54 deletions api/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/pollination/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = './storage'
app.config['TESTING'] = False
CORS(app)
app.config.from_object(Config)
app.app_context().push()
Expand Down
9 changes: 9 additions & 0 deletions api/test_unit/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pytest
from pollination import app


@pytest.fixture
def client():
app.config["TESTING"] = True
client = app.test_client()
yield client
88 changes: 88 additions & 0 deletions api/test_unit/test_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from test_unit import client
from pollination import db, File, User


def test_delete(client):
response = client.post("/api/login", json={
"username": "moo",
"password": "moo"
})

assert response.status_code == 200

user: User = db.session.scalars(db.select(User).filter_by(username="moo")).first()

assert user is not None

file: File = db.session.scalars(db.select(File).filter_by(user_id=user.id)).first()

assert file is not None

response = client.post("/api/image/delete", json={
"id": file.alt_id
})

assert response.status_code == 200

response = client.get("/api/logout")

assert response.status_code == 200


def test_get_image(client):
response = client.post("/api/login", json={
"username": "frog",
"password": "frog"
})

assert response.status_code == 200

user: User = db.session.scalars(db.select(User).filter_by(username="frog")).first()

assert user is not None

file: File = db.session.scalars(db.select(File).filter_by(user_id=user.id)).first()

assert file is not None

response = client.get(f"/api/image/get/{file.alt_id}")

assert response.status_code == 200

response = client.get("/api/logout")

assert response.status_code == 200


def test_get_all(client):
response = client.post("/api/login", json={
"username": "frog",
"password": "frog"
})

assert response.status_code == 200

user: User = db.session.scalars(db.select(User).filter_by(username="frog")).first()

assert user is not None

file: File = db.session.scalars(db.select(File).filter_by(user_id=user.id)).all()

assert file is not None

response = client.get("/api/image/all")

assert response.status_code == 200

assert len(response.json["insect"]) == len(file)

for i in range(len(file)):
assert file[i].alt_id in response.json["insect"][i]

response = client.get("/api/logout")

assert response.status_code == 200




Loading

0 comments on commit 9a9d0f1

Please sign in to comment.