Commit f03ac53a authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[databases][tests] Add module tear down

This commit also cleans up the code.
parent 39255663
......@@ -25,8 +25,7 @@
# #
###############################################################################
import simplejson as json
from datetime import datetime
import json
from django.contrib.auth.models import User
from django.conf import settings
......@@ -37,39 +36,42 @@ from .models import Database
from ..dataformats.models import DataFormat
from ..common.testutils import BaseTestCase
from ..common.testutils import tearDownModule # noqa test runner will call it
TEST_PWD = "1234"
class DatabaseAPIBase(BaseTestCase):
DATABASE = {
"root_folder": "/path/to/root/folder",
"protocols": [
{
"name": "protocol1",
"template": "test",
"sets": [
{
"name": "set1",
"template": "set",
"view": "dummy",
"outputs": {
"out": settings.SYSTEM_ACCOUNT + "/float/1"
}
}
]
}
]
"protocols": [
{
"name": "protocol1",
"template": "test",
"sets": [
{
"name": "set1",
"template": "set",
"view": "dummy",
"outputs": {"out": settings.SYSTEM_ACCOUNT + "/float/1"},
}
],
}
],
}
def setUp(self):
# Users
self.system_user = User.objects.create_user(settings.SYSTEM_ACCOUNT,
settings.SYSTEM_ACCOUNT + '@test.org', '1234')
self.system_user = User.objects.create_user(
settings.SYSTEM_ACCOUNT, settings.SYSTEM_ACCOUNT + "@test.org", TEST_PWD
)
self.system_user.is_staff = True
self.system_user.save()
user = User.objects.create_user('jackdoe', 'jackdoe@test.org', '1234')
User.objects.create_user('johndoe', 'johndoe@test.org', '1234')
User.objects.create_user("jackdoe", "jackdoe@test.org", TEST_PWD)
User.objects.create_user("johndoe", "johndoe@test.org", TEST_PWD)
self.db_name = 'test_db'
self.db_name = "test_db"
def tearDown(self):
pass
......@@ -79,47 +81,46 @@ class DatabaseCreationAPI(DatabaseAPIBase):
def setUp(self):
super(DatabaseCreationAPI, self).setUp()
self.url = reverse('api_databases:all')
self.url = reverse("api_databases:all")
def test_no_access_for_anonymous_user(self):
response = self.client.post(self.url)
self.checkResponse(response, 403)
def test_no_access_for_non_admin_user(self):
self.client.login(username='jackdoe', password='1234')
self.client.login(username="jackdoe", password=TEST_PWD)
response = self.client.post(self.url)
self.checkResponse(response, 403)
def test_create_database_failure(self):
self.client.login(username=settings.SYSTEM_ACCOUNT, password='1234')
response = self.client.post(self.url,
json.dumps({
'name': self.db_name,
'declaration': self.DATABASE
}), content_type='application/json')
data = self.checkResponse(response, 400)
self.client.login(username=settings.SYSTEM_ACCOUNT, password=TEST_PWD)
response = self.client.post(
self.url,
json.dumps({"name": self.db_name, "declaration": self.DATABASE}),
content_type="application/json",
)
self.checkResponse(response, 400)
def test_create_database(self):
# Create a dataformat
(dataformat, errors) = DataFormat.objects.create_dataformat(self.system_user, 'float', '')
assert dataformat, errors
(dataformat, errors) = DataFormat.objects.create_dataformat(
self.system_user, "float", ""
)
self.assertIsNotNone(dataformat, errors)
dataformat.share()
self.client.login(username=settings.SYSTEM_ACCOUNT, password='1234')
self.client.login(username=settings.SYSTEM_ACCOUNT, password=TEST_PWD)
response = self.client.post(self.url,
json.dumps({
'name': self.db_name,
'declaration': self.DATABASE
}), content_type='application/json')
response = self.client.post(
self.url,
json.dumps({"name": self.db_name, "declaration": self.DATABASE}),
content_type="application/json",
)
data = self.checkResponse(response, 201, content_type='application/json')
data = self.checkResponse(response, 201, content_type="application/json")
self.assertTrue(data['name'] == self.db_name)
self.assertTrue(data["name"] == self.db_name)
databases = Database.objects.all()
self.assertEqual(databases.count(), 1)
......@@ -127,24 +128,29 @@ class DatabaseCreationAPI(DatabaseAPIBase):
class DatabaseRetrievalAPI(DatabaseAPIBase):
def test_retrieve_database(self):
(dataformat, errors) = DataFormat.objects.create_dataformat(self.system_user, 'float', '')
assert dataformat, errors
(dataformat, errors) = DataFormat.objects.create_dataformat(
self.system_user, "float", ""
)
self.assertIsNotNone(dataformat, errors)
dataformat.share()
(database, errors) = Database.objects.create_database(self.db_name, declaration=self.DATABASE)
assert database, errors
(database, errors) = Database.objects.create_database(
self.db_name, declaration=self.DATABASE
)
self.assertIsNotNone(database, errors)
database.share()
self.client.login(username=settings.SYSTEM_ACCOUNT, password='1234')
self.client.login(username=settings.SYSTEM_ACCOUNT, password=TEST_PWD)
url = reverse('api_databases:object', kwargs={'database_name': self.db_name, 'version': 1})
url = reverse(
"api_databases:object", kwargs={"database_name": self.db_name, "version": 1}
)
response = self.client.get(url, format='json')
data = self.checkResponse(response, 200, content_type='application/json')
response = self.client.get(url, format="json")
data = self.checkResponse(response, 200, content_type="application/json")
declaration = json.loads(data['declaration'])
self.assertTrue(declaration['root_folder'].startswith('/path_to_db_folder'))
declaration = json.loads(data["declaration"])
self.assertTrue(declaration["root_folder"].startswith("/path_to_db_folder"))
database.delete()
\ No newline at end of file
database.delete()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment