1
Fork 0
mirror of https://github.com/Steffo99/sophon.git synced 2024-12-22 06:44:21 +00:00

🧪 Add initial test framework

This commit is contained in:
Steffo 2021-08-11 23:54:39 +02:00
parent b9d3fe94fe
commit 0d2718a0fb
4 changed files with 193 additions and 5 deletions

View file

@ -10,11 +10,11 @@
<env name="DJANGO_DEBUG" value="1" />
<env name="DJANGO_TIME_ZONE" value="CET" />
</envs>
<option name="SDK_HOME" value="" />
<option name="SDK_HOME" value="$USER_HOME$/.cache/pypoetry/virtualenvs/sophon-oyAYqTyT-py3.9/bin/python" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="ADD_CONTENT_ROOTS" value="false" />
<option name="ADD_SOURCE_ROOTS" value="false" />
<option name="launchJavascriptDebuger" value="false" />
<option name="port" value="30033" />
<option name="host" value="127.0.0.1" />

View file

@ -0,0 +1,26 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Test sophon backend" type="DjangoTestsConfigurationType">
<module name="Backend (Django)" />
<option name="INTERPRETER_OPTIONS" value="-Wa" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
<env name="DJANGO_SETTINGS_MODULE" value="sophon.settings" />
<env name="DJANGO_SECRET_KEY" value="change-me-in-production" />
<env name="DJANGO_DEBUG" value="1" />
<env name="DJANGO_TIME_ZONE" value="CET" />
</envs>
<option name="SDK_HOME" value="$USER_HOME$/.cache/pypoetry/virtualenvs/sophon-oyAYqTyT-py3.9/bin/python" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="false" />
<option name="ADD_SOURCE_ROOTS" value="false" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="TARGET" value="sophon" />
<option name="SETTINGS_FILE" value="" />
<option name="CUSTOM_SETTINGS" value="false" />
<option name="USE_OPTIONS" value="true" />
<option name="OPTIONS" value="--no-input" />
<method v="2" />
</configuration>
</component>

View file

@ -1,3 +1,163 @@
from django.test import TestCase
import abc
import collections
# Create your tests here.
from django.contrib.auth.models import User
from django.urls import reverse
from rest_framework import status
from rest_framework.response import Response
from rest_framework.test import APITestCase
from . import models
class SophonModelTestCase(APITestCase, metaclass=abc.ABCMeta):
@classmethod
@abc.abstractmethod
def get_basename(cls) -> str:
raise NotImplementedError()
@classmethod
def get_url(cls, kind: str, *args, **kwargs) -> str:
basename = cls.get_basename()
return reverse(f"{basename}-{kind}", args=args, kwargs=kwargs)
def list(self) -> Response:
url = self.get_url("list")
return self.client.get(url, {}, format="json")
def list_unwrap(self) -> collections.OrderedDict:
response = self.list()
self.assertEqual(response.status_code, 200)
self.assertTrue(isinstance(response.data, dict))
return response.data
def retrieve(self, pk) -> Response:
url = self.get_url("detail", pk=pk)
return self.client.get(url, {}, format="json")
def retrieve_unwrap(self, pk) -> collections.OrderedDict:
response = self.retrieve(pk=pk)
self.assertEqual(response.status_code, 200)
self.assertTrue(isinstance(response.data, dict))
return response.data
def create(self, data) -> Response:
url = self.get_url("list")
return self.client.post(url, data, format="json")
def create_unwrap(self, data) -> collections.OrderedDict:
response = self.create(data=data)
self.assertEqual(response.status_code, 201)
self.assertTrue(isinstance(response.data, dict))
return response.data
def update(self, pk, data) -> Response:
url = self.get_url("detail", pk=pk)
return self.client.put(url, data, format="json")
def update_unwrap(self, pk, data) -> collections.OrderedDict:
response = self.update(pk=pk, data=data)
self.assertEqual(response.status_code, 200)
self.assertTrue(isinstance(response.data, dict))
return response.data
def destroy(self, pk) -> Response:
url = self.get_url("detail", pk=pk)
return self.client.delete(url, format="json")
def destroy_unwrap(self, pk) -> None:
response = self.destroy(pk=pk)
self.assertEqual(response.status_code, 204)
class ResearchGroupTests(SophonModelTestCase):
@classmethod
def get_basename(cls) -> str:
return "research-group"
@classmethod
def setUpTestData(cls):
test_user = User.objects.create_user(username="TEST", password="TheGreatDjangoTest")
models.ResearchGroup.objects.create(
slug="alpha",
name="Alpha",
description="First test group.",
owner=test_user,
access="MANUAL",
)
models.ResearchGroup.objects.create(
slug="beta",
name="Beta",
description="Second test group.",
owner=test_user,
access="OPEN",
)
def test_list(self):
r = self.list_unwrap()
count = r["count"]
self.assertEqual(count, 2)
results = r["results"]
self.assertIn("slug", results[0])
self.assertIn("name", results[0])
self.assertIn("description", results[0])
self.assertIn("owner", results[0])
self.assertIn("members", results[0])
self.assertIn("access", results[0])
def test_retrieve(self):
result = self.retrieve_unwrap("alpha")
self.assertIn("slug", result)
self.assertIn("name", result)
self.assertIn("description", result)
self.assertIn("owner", result)
self.assertIn("members", result)
self.assertIn("access", result)
self.assertEqual(result["slug"], "alpha")
self.assertEqual(result["name"], "Alpha")
self.assertEqual(result["description"], "First test group.")
# TODO: How to verify the owner id?
self.assertEqual(result["members"], [])
self.assertEqual(result["access"], "MANUAL")
def test_create(self):
self.client.login(username="TEST", password="TheGreatDjangoTest")
result = self.create_unwrap({
"slug": "omega",
"name": "Omega",
"description": "Last test group.",
"members": [],
"access": "OPEN",
})
self.assertIn("slug", result)
self.assertIn("name", result)
self.assertIn("description", result)
self.assertIn("members", result)
self.assertIn("access", result)
check = self.retrieve_unwrap("omega")
self.assertIn("slug", check)
self.assertIn("name", check)
self.assertIn("description", check)
self.assertIn("owner", check)
self.assertIn("members", check)
self.assertIn("access", check)
self.assertEqual(check["slug"], "omega")
self.assertEqual(check["name"], "Omega")
self.assertEqual(check["description"], "Last test group.")
# TODO: How to verify the owner id?
self.assertEqual(result["members"], [])
self.assertEqual(result["access"], "OPEN")
# TODO: Create update test
# TODO: Create destroy test

View file

@ -96,6 +96,8 @@ DATABASES = {
# sudo -iu postgres
# createuser sophon
# createdb --owner=sophon sophon
#
# If you need to run tests, also ensure `sophon` is a superuser, or it won't be able to create and drop the testing database!
# Password validation