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

API: Add unit tests #100

Merged
merged 10 commits into from
Aug 25, 2023
Merged
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
44 changes: 42 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
pull_request:

jobs:
tests:
tests_client:
runs-on: ${{ matrix.os }}
timeout-minutes: 60
strategy:
Expand All @@ -28,7 +28,6 @@ jobs:
python -m pip install --upgrade pip
pip install tox
- name: Test using tox environment
shell: bash
env:
FOLDER: ${{ matrix.folder }}
run: |
Expand All @@ -38,3 +37,44 @@ jobs:
env:
FOLDER: ${{ matrix.folder }}
run: tox -ecoverage

tests_api:
runs-on: ${{ matrix.os }}
timeout-minutes: 60
strategy:
max-parallel: 2
matrix:
os: [ubuntu-latest]
# In future we can add [macos-latest, windows-latest]
python-version: [3.9]
folder: ["api_server"]
services:
postgres:
image: postgres
env:
POSTGRES_USER: purplecaffeine
POSTGRES_PASSWORD: purplecaffeinepassword
POSTGRES_DB: purplecaffeine
POSTGRES_HOST_AUTH_METHOD: trust
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install tox
- name: Test using tox environment
env:
FOLDER: ${{ matrix.folder }}
run: |
tox -edjango
2 changes: 1 addition & 1 deletion api_server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ curl -X POST "http://localhost:8000/api/trials/" \
"metrics": [["nb_qubits", 2]],
"parameters": [["OS", "ubuntu"]],
"circuits": [],
"operators": [["obs", Pauli("XZYI")]],
"operators": [],
"artifacts": [],
"texts": [],
"arrays": [],
Expand Down
1 change: 1 addition & 0 deletions api_server/docker/Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ CMD cd /opt/api_server; \
python3 manage.py migrate; \
python3 manage.py createsuperuser --no-input; \
python3 manage.py collectstatic; \
python3 manage.py test; \
exec python3 manage.py runserver 0.0.0.0:8000
7 changes: 7 additions & 0 deletions api_server/purplecaffeine/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@
"core",
"health_check",
"drf_spectacular",
"django_nose",
]

TEST_RUNNER = "django_nose.NoseTestSuiteRunner"
NOSE_ARGS = [
"--cover-erase",
"--cover-package=purplecaffeine",
]

MIDDLEWARE = [
Expand Down
1 change: 1 addition & 0 deletions api_server/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ gunicorn==21.2.0
django-health-check==3.17.0
whitenoise==6.5.0
drf-spectacular==0.26.4
django-nose==1.4.7
106 changes: 106 additions & 0 deletions api_server/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
"""Tests file."""
import json
from django.contrib.auth.models import User
from django.urls import reverse
from django.test import TestCase


class UnitTests(TestCase):
"""Unit tests."""

def setUp(self) -> None:
admin_username = "admin"
admin_pass = "admin"
User.objects.create_superuser(admin_username, "[email protected]", admin_pass)

def get_token(self):
"""Get token."""

data = {"username": "admin", "password": "admin"}

url = reverse("token_obtain_pair")
login = self.client.post(url, data=data, content_type="application/json")
return json.loads(login.content)["access"]

def test_get_token(self):
"""
Ensure we can get a token.
"""

data = {"username": "admin", "password": "admin"}

url = reverse("token_obtain_pair")
login = self.client.post(url, data=data, content_type="application/json")
self.assertEqual(login.status_code, 200)

token_refresh = json.loads(login.content)["refresh"]

url = reverse("token_refresh")
refresh = self.client.post(
url, data={"refresh": f"{token_refresh}"}, content_type="application/json"
)
self.assertEqual(refresh.status_code, 200)

token_access = json.loads(login.content)["access"]

url = reverse("token_verify")
verify = self.client.post(
url, data={"token": f"{token_access}"}, content_type="application/json"
)
self.assertEqual(verify.status_code, 200)

def test_get_swagger(self):
"""
Ensure we can get a swagger.
"""

url = reverse("swagger-ui")
response = self.client.get(url)
self.assertEqual(response.status_code, 200)

def test_trials(self):
"""Tests getting trials."""

data = {
"name": "My super experiment",
"description": "My super experiments desciption",
"storage": {"__type__": "PurpleCaffeineBackend"},
"metrics": [["nb_qubits", 2]],
"parameters": [["OS", "ubuntu"]],
"circuits": [],
"operators": [],
"artifacts": [],
"texts": [],
"arrays": [],
"tags": [],
}
post = self.client.post(
"/api/trials/",
data=data,
headers={"Authorization": f" Bearer {self.get_token()}"},
content_type="application/json",
)
self.assertEqual(post.status_code, 201)

get_all = self.client.get(
"/api/trials/",
headers={"Authorization": f" Bearer {self.get_token()}"},
content_type="application/json",
)
self.assertEqual(get_all.status_code, 200)
self.assertIsInstance(json.loads(get_all.content)["results"], list)

get_one = self.client.get(
"/api/trials/1/",
headers={"Authorization": f" Bearer {self.get_token()}"},
content_type="application/json",
)
self.assertEqual(get_one.status_code, 200)
self.assertEqual(json.loads(get_one.content)["id"], 1)

delete = self.client.delete(
"/api/trials/1/",
headers={"Authorization": f" Bearer {self.get_token()}"},
content_type="application/json",
)
self.assertEqual(delete.status_code, 204)
4 changes: 2 additions & 2 deletions docs/guides/04_setup_api.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@
" \"metrics\": [[\"nb_qubits\", 2]],\n",
" \"parameters\": [[\"OS\", \"ubuntu\"]],\n",
" \"circuits\": [],\n",
" \"operators\": [[\"obs\", Pauli(\"XZYI\")]],\n",
" \"operators\": [],\n",
" \"artifacts\": [],\n",
" \"texts\": [],\n",
" \"arrays\": [],\n",
Expand All @@ -236,7 +236,7 @@
" \"metrics\": [[\"nb_qubits\", 2]],\n",
" \"parameters\": [[\"OS\", \"ubuntu\"]],\n",
" \"circuits\": [],\n",
" \"operators\": [[\"obs\", Pauli(\"XZYI\")]],\n",
" \"operators\": [],\n",
" \"artifacts\": [],\n",
" \"texts\": [],\n",
" \"arrays\": [],\n",
Expand Down
9 changes: 8 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tox]
minversion = 3.8
envlist = py38, py39, py310, lint, coverage, black, ecosystem, docs
envlist = py38, py39, py310, lint, coverage, django, lint, black, ecosystem, docs, jupyter
skipsdist = True

[testenv]
Expand All @@ -23,6 +23,13 @@ commands =
coverage3 run -m unittest discover -s {env:FOLDER:} -v
coverage3 report --fail-under=80

[testenv:django]
basepython = python3
commands =
pip check {env:FOLDER:}
coverage3 run {env:FOLDER:}/manage.py test {env:FOLDER:}/
coverage3 report --fail-under=80

[testenv:lint]
envdir = .tox/lint
commands =
Expand Down
Loading