Commit 47243894 authored by Samuel GAIST's avatar Samuel GAIST

[plotters][tests] Add module tear down

This commit also cleans up the code.
parent 4f7419b7
......@@ -30,426 +30,411 @@ import os
import shutil
import simplejson as json
from django.test import TestCase
from django.contrib.auth.models import User
from django.conf import settings
from django.core.urlresolvers import reverse
from ..dataformats.models import DataFormat
from .models import Plotter, PlotterParameter
from ..common.models import Shareable
from ..common.testutils import BaseTestCase
from ..common.testutils import tearDownModule # noqa test runner will call it
from .models import Plotter
from rest_framework import status
from rest_framework.test import APITestCase
#----------------------------------------------------------
# ----------------------------------------------------------
class PlotterParameterTestCase(APITestCase):
def setUp(self):
self.tearDown()
# Create the users
self.password = '1234'
self.password = "1234"
self.johndoe = User.objects.create_user('johndoe', 'johndoe@test.org', self.password)
self.jackdoe = User.objects.create_user('jackdoe', 'jackdoe@test.org', self.password)
self.plot = User.objects.create_user('plot', 'plotdoe@test.org', self.password)
self.johndoe = User.objects.create_user(
"johndoe", "johndoe@test.org", self.password
)
self.jackdoe = User.objects.create_user(
"jackdoe", "jackdoe@test.org", self.password
)
self.plot = User.objects.create_user("plot", "plotdoe@test.org", self.password)
# Create a dataformat
(dataformat, errors) = DataFormat.objects.create_dataformat(
author=self.plot,
name='text',
short_description='description',
declaration={
"text": "string"
},
name="text",
short_description="description",
declaration={"text": "string"},
)
assert dataformat, errors
self.assertIsNotNone(dataformat, errors)
# Create a plotter
self.client.login(username=self.plot.username, password=self.password)
self.url_plotter = reverse('api_plotters:list_create', kwargs={'author_name': self.plot.username})
self.data_plotter = {\
'author':self.plot.username,\
'name':'plotter_test',\
'short_description':'some description plotter',\
'description':'some longer description plotter',\
'declaration':{"language": "python","parameters":{},"dataformat":"plot/text/1"},\
'code':'#test'\
self.url_plotter = reverse(
"api_plotters:list_create", kwargs={"author_name": self.plot.username}
)
self.data_plotter = {
"author": self.plot.username,
"name": "plotter_test",
"short_description": "some description plotter",
"description": "some longer description plotter",
"declaration": {
"language": "python",
"parameters": {},
"dataformat": "plot/text/1",
},
"code": "#test",
}
response = self.client.post(self.url_plotter, self.data_plotter, format='json')
response = self.client.post(self.url_plotter, self.data_plotter, format="json")
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.plotter = Plotter.objects.get(name='plotter_test')
self.plotter = Plotter.objects.get(name="plotter_test")
self.url = reverse(
"api_plotters:view", kwargs={"author_name": self.johndoe.username}
)
self.data = {
"name": "plotterparameter1",
"short_description": "some description",
"description": "some longer description",
"plotter": self.plotter.id,
}
self.data2 = {
"name": "plotterparameter2",
"short_description": "some description2",
"description": "some longer description2",
"plotter": self.plotter.id,
}
def tearDown(self):
for path in [settings.TOOLCHAINS_ROOT, settings.EXPERIMENTS_ROOT,
settings.DATAFORMATS_ROOT, settings.ALGORITHMS_ROOT,
settings.CACHE_ROOT]:
for path in [
settings.TOOLCHAINS_ROOT,
settings.EXPERIMENTS_ROOT,
settings.DATAFORMATS_ROOT,
settings.ALGORITHMS_ROOT,
settings.CACHE_ROOT,
]:
if os.path.exists(path):
shutil.rmtree(path)
#----------------------------------------------------------
# ----------------------------------------------------------
class PlotterParameterCreationTestCase(PlotterParameterTestCase):
def setUp(self):
super(PlotterParameterCreationTestCase, self).setUp()
self.url = reverse('api_plotters:view', kwargs={'author_name': self.johndoe.username})
self.data = {\
#'author':self.johndoe,\
'name':'plotterparameter1',\
'short_description':'some description',\
'description':'some longer description',\
'plotter':self.plotter.id\
}
def test_anonymous_user(self):
response = self.client.post(self.url, self.data, format='json')
response = self.client.post(self.url, self.data, format="json")
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assertEqual(response.content, b'{"detail":"You do not have permission to perform this action."}')
self.assertEqual(
response.content,
b'{"detail":"You do not have permission to perform this action."}',
)
def test_logged_in_user(self):
self.client.login(username=self.johndoe.username, password=self.password)
response = self.client.post(self.url, self.data, format='json')
response = self.client.post(self.url, self.data, format="json")
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(json.loads(response.content)['name'], self.data['name'])
self.assertEqual(json.loads(response.content)["name"], self.data["name"])
def test_logged_in_user_existing_plotterparameter_name(self):
self.client.login(username=self.johndoe.username, password=self.password)
response = self.client.post(self.url, self.data, format='json')
response = self.client.post(self.url, self.data, format="json")
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(json.loads(response.content)['name'], self.data['name'])
self.assertEqual(json.loads(response.content)["name"], self.data["name"])
response = self.client.post(self.url, self.data, format='json')
response = self.client.post(self.url, self.data, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(json.loads(response.content)['non_field_errors'][0], 'This plotterparameter name already exists on this account')
self.assertEqual(
json.loads(response.content)["non_field_errors"][0],
"This plotterparameter name already exists on this account",
)
#----------------------------------------------------------
# ----------------------------------------------------------
class PlotterParameterListTestCase(PlotterParameterTestCase):
def setUp(self):
super(PlotterParameterListTestCase, self).setUp()
self.url = reverse('api_plotters:view', kwargs={'author_name': self.johndoe.username})
self.data = {\
#'author':self.johndoe,\
'name':'plotterparameter1',\
'short_description':'some description',\
'description':'some longer description',\
'plotter':self.plotter.id\
}
self.data2 = {\
#'author':self.johndoe,\
'name':'plotterparameter2',\
'short_description':'some description2',\
'description':'some longer description2',\
'plotter':self.plotter.id\
}
def test_anonymous_user(self):
response = self.client.get(self.url, format='json')
response = self.client.get(self.url, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_logged_in_user_no_plotterparameter(self):
self.client.login(username=self.johndoe.username, password=self.password)
response = self.client.get(self.url, format='json')
response = self.client.get(self.url, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(json.loads(response.content)), 0)
def test_logged_in_user_single_plotterparameter(self):
self.client.login(username=self.johndoe.username, password=self.password)
response = self.client.post(self.url, self.data, format='json')
response = self.client.post(self.url, self.data, format="json")
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(json.loads(response.content)['name'], self.data['name'])
self.assertEqual(json.loads(response.content)["name"], self.data["name"])
response = self.client.get(self.url, format='json')
response = self.client.get(self.url, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(json.loads(response.content)), 1)
def test_logged_in_user_multiple_plotterparameter(self):
self.client.login(username=self.johndoe.username, password=self.password)
response = self.client.post(self.url, self.data, format='json')
response = self.client.post(self.url, self.data, format="json")
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(json.loads(response.content)['name'], self.data['name'])
self.assertEqual(json.loads(response.content)["name"], self.data["name"])
response = self.client.post(self.url, self.data2, format='json')
response = self.client.post(self.url, self.data2, format="json")
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(json.loads(response.content)['name'], self.data2['name'])
self.assertEqual(json.loads(response.content)["name"], self.data2["name"])
response = self.client.get(self.url, format='json')
response = self.client.get(self.url, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(json.loads(response.content)), 2)
#----------------------------------------------------------
# ----------------------------------------------------------
class PlotterParameterRetrievalTestCase(PlotterParameterTestCase):
def setUp(self):
super(PlotterParameterRetrievalTestCase, self).setUp()
self.url = reverse('api_plotters:view', kwargs={'author_name': self.johndoe.username})
self.url_single_plotterparameter = reverse('api_plotters:view', kwargs={
'author_name': self.johndoe.username,
'object_name': 'plotterparameter1',
'version': 1,
})
self.url_single_plotterparameter2 = reverse('api_plotters:view', kwargs={
'author_name': self.johndoe.username,
'object_name': 'plotterparameter2',
'version': 1,
})
self.data = {\
#'author':self.johndoe,\
'name':'plotterparameter1',\
'short_description':'some description',\
'description':'some longer description',\
'plotter':self.plotter.id\
}
self.url_single_plotterparameter = reverse(
"api_plotters:view",
kwargs={
"author_name": self.johndoe.username,
"object_name": "plotterparameter1",
"version": 1,
},
)
self.data2 = {\
#'author':self.johndoe,\
'name':'plotterparameter2',\
'short_description':'some description2',\
'description':'some longer description2',\
'plotter':self.plotter.id\
}
self.url_single_plotterparameter2 = reverse(
"api_plotters:view",
kwargs={
"author_name": self.johndoe.username,
"object_name": "plotterparameter2",
"version": 1,
},
)
def test_anonymous_user(self):
response = self.client.get(self.url_single_plotterparameter, format='json')
response = self.client.get(self.url_single_plotterparameter, format="json")
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_logged_in_user_no_plotterparameter(self):
self.client.login(username=self.johndoe.username, password=self.password)
response = self.client.get(self.url_single_plotterparameter, format='json')
response = self.client.get(self.url_single_plotterparameter, format="json")
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_logged_in_user_single_plotterparameter(self):
self.client.login(username=self.johndoe.username, password=self.password)
response = self.client.post(self.url, self.data, format='json')
response = self.client.post(self.url, self.data, format="json")
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(json.loads(response.content)['name'], self.data['name'])
self.assertEqual(json.loads(response.content)["name"], self.data["name"])
response = self.client.get(self.url_single_plotterparameter, format='json')
response = self.client.get(self.url_single_plotterparameter, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(json.loads(response.content)['name'], self.johndoe.username+'/'+self.data['name']+'/1')
self.assertEqual(
json.loads(response.content)["name"],
self.johndoe.username + "/" + self.data["name"] + "/1",
)
def test_logged_in_user_multiple_plotterparameter(self):
self.client.login(username=self.johndoe.username, password=self.password)
response = self.client.post(self.url, self.data, format='json')
response = self.client.post(self.url, self.data, format="json")
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(json.loads(response.content)['name'], self.data['name'])
self.assertEqual(json.loads(response.content)["name"], self.data["name"])
response = self.client.post(self.url, self.data2, format='json')
response = self.client.post(self.url, self.data2, format="json")
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(json.loads(response.content)['name'], self.data2['name'])
self.assertEqual(json.loads(response.content)["name"], self.data2["name"])
response = self.client.get(self.url_single_plotterparameter, format='json')
response = self.client.get(self.url_single_plotterparameter, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(json.loads(response.content)['name'], self.johndoe.username+'/'+self.data['name']+'/1')
self.assertEqual(
json.loads(response.content)["name"],
self.johndoe.username + "/" + self.data["name"] + "/1",
)
response = self.client.get(self.url_single_plotterparameter2, format='json')
response = self.client.get(self.url_single_plotterparameter2, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(json.loads(response.content)['name'], self.johndoe.username+'/'+self.data2['name']+'/1')
self.assertEqual(
json.loads(response.content)["name"],
self.johndoe.username + "/" + self.data2["name"] + "/1",
)
#----------------------------------------------------------
# ----------------------------------------------------------
class PlotterParameterUpdateTestCase(PlotterParameterTestCase):
def setUp(self):
super(PlotterParameterUpdateTestCase, self).setUp()
self.url = reverse('api_plotters:view', kwargs={'author_name': self.johndoe.username})
self.url_single_plotterparameter = reverse('api_plotters:view', kwargs={
'author_name': self.johndoe.username,
'object_name': 'plotterparameter1',
'version': 1,
})
self.url_single_plotterparameter2 = reverse('api_plotters:view', kwargs={
'author_name': self.johndoe.username,
'object_name': 'plotterparameter2',
'version': 1,
})
self.data = {\
#'author':self.johndoe,\
'name':'plotterparameter1',\
'short_description':'some description',\
'description':'some longer description',\
'plotter':self.plotter.id\
}
self.url_single_plotterparameter = reverse(
"api_plotters:view",
kwargs={
"author_name": self.johndoe.username,
"object_name": "plotterparameter1",
"version": 1,
},
)
self.data2 = {\
#'author':self.johndoe,\
'name':'plotterparameter2',\
'short_description':'some description2',\
'description':'some longer description2',\
'plotter':self.plotter.id\
}
self.url_single_plotterparameter2 = reverse(
"api_plotters:view",
kwargs={
"author_name": self.johndoe.username,
"object_name": "plotterparameter2",
"version": 1,
},
)
def test_anonymous_user(self):
response = self.client.get(self.url_single_plotterparameter, format='json')
response = self.client.get(self.url_single_plotterparameter, format="json")
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_logged_in_user_no_plotterparameter(self):
self.client.login(username=self.johndoe.username, password=self.password)
response = self.client.get(self.url_single_plotterparameter, format='json')
response = self.client.get(self.url_single_plotterparameter, format="json")
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_logged_in_user_single_plotterparameter(self):
self.client.login(username=self.johndoe.username, password=self.password)
response = self.client.post(self.url, self.data, format='json')
response = self.client.post(self.url, self.data, format="json")
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(json.loads(response.content)['name'], self.data['name'])
self.assertEqual(json.loads(response.content)["name"], self.data["name"])
response = self.client.get(self.url_single_plotterparameter, format='json')
response = self.client.get(self.url_single_plotterparameter, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(json.loads(response.content)['name'], self.johndoe.username+'/'+self.data['name']+'/1')
self.assertEqual(
json.loads(response.content)["name"],
self.johndoe.username + "/" + self.data["name"] + "/1",
)
def test_logged_in_user_multiple_plotterparameter(self):
self.client.login(username=self.johndoe.username, password=self.password)
response = self.client.post(self.url, self.data, format='json')
response = self.client.post(self.url, self.data, format="json")
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(json.loads(response.content)['name'], self.data['name'])
self.assertEqual(json.loads(response.content)["name"], self.data["name"])
response = self.client.post(self.url, self.data2, format='json')
response = self.client.post(self.url, self.data2, format="json")
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(json.loads(response.content)['name'], self.data2['name'])
self.assertEqual(json.loads(response.content)["name"], self.data2["name"])
response = self.client.get(self.url_single_plotterparameter, format='json')
response = self.client.get(self.url_single_plotterparameter, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(json.loads(response.content)['name'], self.johndoe.username+'/'+self.data['name']+'/1')
self.assertEqual(
json.loads(response.content)["name"],
self.johndoe.username + "/" + self.data["name"] + "/1",
)
response = self.client.get(self.url_single_plotterparameter2, format='json')
response = self.client.get(self.url_single_plotterparameter2, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(json.loads(response.content)['name'], self.johndoe.username+'/'+self.data2['name']+'/1')
self.assertEqual(
json.loads(response.content)["name"],
self.johndoe.username + "/" + self.data2["name"] + "/1",
)
#----------------------------------------------------------
# ----------------------------------------------------------
class PlotterParameterDeletionTestCase(PlotterParameterTestCase):
def setUp(self):
super(PlotterParameterDeletionTestCase, self).setUp()
self.url = reverse('api_plotters:view', kwargs={'author_name': self.johndoe.username})
self.url_single_plotterparameter = reverse('api_plotters:view', kwargs={
'author_name': self.johndoe.username,
'object_name': 'plotterparameter1',
'version': 1,
})
self.url_single_plotterparameter2 = reverse('api_plotters:view', kwargs={
'author_name': self.johndoe.username,
'object_name': 'plotterparameter2',
'version': 1,
})
self.data = {\
#'author':self.johndoe,\
'name':'plotterparameter1',\
'short_description':'some description',\
'description':'some longer description',\
'plotter':self.plotter.id\
}
self.url_single_plotterparameter = reverse(
"api_plotters:view",
kwargs={
"author_name": self.johndoe.username,
"object_name": "plotterparameter1",
"version": 1,
},
)
self.data2 = {\
#'author':self.johndoe,\
'name':'plotterparameter2',\
'short_description':'some description2',\
'description':'some longer description2',\
'plotter':self.plotter.id\
}
self.url_single_plotterparameter2 = reverse(
"api_plotters:view",
kwargs={
"author_name": self.johndoe.username,
"object_name": "plotterparameter2",
"version": 1,
},
)
def test_anonymous_user(self):
response = self.client.get(self.url_single_plotterparameter, format='json')
response = self.client.get(self.url_single_plotterparameter, format="json")
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
response = self.client.delete(self.url_single_plotterparameter, format='json')
response = self.client.delete(self.url_single_plotterparameter, format="json")
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_logged_in_user_no_plotterparameter(self):
self.client.login(username=self.johndoe.username, password=self.password)
response = self.client.get(self.url_single_plotterparameter, format='json')
response = self.client.get(self.url_single_plotterparameter, format="json")
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
response = self.client.delete(self.url_single_plotterparameter, format='json')
response = self.client.delete(self.url_single_plotterparameter, format="json")
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_logged_in_user_single_plotterparameter(self):
self.client.login(username=self.johndoe.username, password=self.password)
response = self.client.post(self.url, self.data, format='json')
response = self.client.post(self.url, self.data, format="json")
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(json.loads(response.content)['name'], self.data['name'])
self.assertEqual(json.loads(response.content)["name"], self.data["name"])
response = self.client.get(self.url_single_plotterparameter, format='json')
response = self.client.get(self.url_single_plotterparameter, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(json.loads(response.content)['name'], self.johndoe.username+'/'+self.data['name']+'/1')
self.assertEqual(
json.loads(response.content)["name"],
self.johndoe.username + "/" + self.data["name"] + "/1",
)
response = self.client.delete(self.url_single_plotterparameter, format='json')
response = self.client.delete(self.url_single_plotterparameter, format="json")
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
def test_logged_in_user_multiple_plotterparameter(self):
self.client.login(username=self.johndoe.username, password=self.password)
response = self.client.post(self.url, self.data, format='json')
response = self.client.post(self.url, self.data, format="json")
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(json.loads(response.content)['name'], self.data['name'])
self.assertEqual(json.loads(response.content)["name"], self.data["name"])
response = self.client.post(self.url, self.data2, format='json')
response = self.client.post(self.url, self.data2, format="json")
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(json.loads(response.content)['name'], self.data2['name'])
self.assertEqual(json.loads(response.content)["name"], self.data2["name"])
response = self.client.get(self.url_single_plotterparameter, format='json')
response = self.client.get(self.url_single_plotterparameter, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(json.loads(response.content)['name'], self.johndoe.username+'/'+self.data['name']+'/1')
self.assertEqual(
json.loads(response.content)["name"],
self.johndoe.username + "/" + self.data["name"] + "/1",
)
response = self.client.get(self.url_single_plotterparameter2, format='json')
response = self.client.get(self.url_single_plotterparameter2, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(json.loads(response.content)['name'], self.johndoe.username+'/'+self.data2['name']+'/1')
self.assertEqual(
json.loads(response.content)["name"],
self.johndoe.username + "/" + self.data2["name"] + "/1",
)
response = self.client.delete(self.url_single_plotterparameter, format='json')
response = self.client.delete(self.url_single_plotterparameter, format="json")
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
response = self.client.delete(self.url_single_plotterparameter2, format='json')
response = self.client.delete(self.url_single_plotterparameter2, format="json")
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
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