Commit d51b8956 authored by Fabio Roberto Vitello's avatar Fabio Roberto Vitello

Tests

Implemented Django and Functional test
parent 5f0d397b
......@@ -126,13 +126,8 @@ class CopyReason(AbstractChoice):
REPEATED = "repeated"
class ResourceUnit(AbstractChoice):
"""Defines the model and predefined list of possible Unit's for Resources.
The items in the Choises class below are automagically populated into the database via a data migration."""
# class Choices(Enum):
# GIGABYTE = "Gigabyte"
# TERABYTE = "Terabyte"
# MINUTES = "Minutes"
# HOURS = "Hours"
value = CharField(max_length=128)
# concrete models
......@@ -219,7 +214,7 @@ class Project(NamedCommonPK):
class ProjectQuotum(NamedCommonPK):
project = ForeignKey('Project', related_name="project_quotums", on_delete=PROTECT, help_text='Project to wich this quota belongs.') # protected to avoid accidents
resource = FloatField(help_text='Resource Quota value')
resource_quota = FloatField(help_text='Resource Quota value')
resource_type = ForeignKey('ResourceType', related_name="resource_type", on_delete=PROTECT, help_text='Resource type.') # protected to avoid accidents
#lta_storage = FloatField(help_text='LTA storage offered for the project')
#observation_hours = IntegerField(help_text='Number of offered hours for observations.')
......@@ -285,4 +280,4 @@ class TaskRelationBlueprint(BasicCommon):
input = ForeignKey('TaskConnectors', related_name='inputs_task_relation_blueprint', on_delete=CASCADE, help_text='Input connector of consumer.')
output = ForeignKey('TaskConnectors', related_name='outputs_task_relation_blueprint', on_delete=CASCADE, help_text='Output connector of producer.')
draft = ForeignKey('TaskRelationDraft', on_delete=CASCADE, related_name='related_task_relation_blueprint', help_text='Task Relation Draft which this work request instantiates.')
selection_template = ForeignKey('WorkRelationSelectionTemplate', on_delete=CASCADE, help_text='Schema used for selection_doc.') # todo: 'schema'?
selection_template = ForeignKey('WorkRelationSelectionTemplate', on_delete=CASCADE, help_text='Schema used for selection_doc.') # todo: 'schema'?
\ No newline at end of file
......@@ -98,7 +98,7 @@ class ResourceUnitSerializer(serializers.ModelSerializer):
class Meta:
model = models.ResourceUnit
fields = '__all__'
class TaskConnectorsSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
......
......@@ -501,6 +501,40 @@ class ProjectTest(unittest.TestCase):
# self.assertEqual(response.status_code, 404)
class ProjectQuotumTest(unittest.TestCase):
def test_ProjectQuotum_gets_created_with_correct_creation_timestamp(self):
# setup
before = datetime.utcnow()
entry = models.ProjectQuotum.objects.create(**ProjectQuotum_test_data())
after = datetime.utcnow()
# assert
self.assertLess(before, entry.created_at)
self.assertGreater(after, entry.created_at)
def test_ProjectQuotum_update_timestamp_gets_changed_correctly(self):
# setup
entry = models.ProjectQuotum.objects.create(**ProjectQuotum_test_data())
before = datetime.utcnow()
entry.save()
after = datetime.utcnow()
# assert
self.assertLess(before, entry.updated_at)
self.assertGreater(after, entry.updated_at)
def test_ProjectQuotum_prevents_missing_project(self):
# setup
test_data = dict(ProjectQuotum_test_data())
test_data['project'] = None
# assert
with self.assertRaises(IntegrityError):
models.ProjectQuotum.objects.create(**test_data)
class SchedulingSetTest(unittest.TestCase):
# TODO: rest API testing should be moved out of this test module.
# def test_GET_SchedulingSet_list_view_shows_entry(self):
......
......@@ -686,6 +686,116 @@ class ProjectTestCase(unittest.TestCase):
GET_and_assert_expected_response(self, cycle_url, 200, cycle_test_data)
class ResourceTypeTestCase(unittest.TestCase):
def test_resource_type_list_apiformat(self):
r = requests.get(BASE_URL + '/resource_type/?format=api', auth=AUTH)
self.assertEqual(r.status_code, 200)
self.assertTrue("Resource Type List" in r.content.decode('utf8'))
def test_resource_type_GET_nonexistant_raises_error(self):
GET_and_assert_expected_response(self, BASE_URL + '/resource_type/1234321/', 404, {})
def test_resource_type_POST_and_GET(self):
resource_type_test_data = test_data_creator.ResourceType()
# POST and GET a new item and assert correctness
r_dict = POST_and_assert_expected_response(self, BASE_URL + '/resource_type/', resource_type_test_data, 201, resource_type_test_data)
url = r_dict['url']
GET_and_assert_expected_response(self, url, 200, resource_type_test_data)
class ProjectQuotumTestCase(unittest.TestCase):
def test_project_quotum_list_apiformat(self):
r = requests.get(BASE_URL + '/project_quotum/?format=api', auth=AUTH)
self.assertEqual(r.status_code, 200)
self.assertTrue("Project Quotum List" in r.content.decode('utf8'))
def test_project_quotum_GET_nonexistant_raises_error(self):
GET_and_assert_expected_response(self, BASE_URL + '/project_quotum/1234321/', 404, {})
def test_project_quotum_POST_and_GET(self):
project_quotum_test_data = test_data_creator.ProjectQuotum()
# POST and GET a new item and assert correctness
r_dict = POST_and_assert_expected_response(self, BASE_URL + '/project_quotum/', project_quotum_test_data, 201, project_quotum_test_data)
url = r_dict['url']
GET_and_assert_expected_response(self, url, 200, project_quotum_test_data)
def test_project_quotum_POST_and_GET(self):
project_quotum_test_data = test_data_creator.ProjectQuotum()
# POST and GET a new item and assert correctness
r_dict = POST_and_assert_expected_response(self, BASE_URL + '/project_quotum/', project_quotum_test_data, 201, project_quotum_test_data)
url = r_dict['url']
GET_and_assert_expected_response(self, url, 200, project_quotum_test_data)
def test_project_quotum_PUT_invalid_raises_error(self):
PUT_and_assert_expected_response(self, BASE_URL + '/project_quotum/9876789876/', test_data_creator.Project(), 404, {})
def test_project_quotum_PUT(self):
project_quotum_test_data = test_data_creator.ProjectQuotum()
# POST new item, verify
r_dict = POST_and_assert_expected_response(self, BASE_URL + '/project_quotum/', project_quotum_test_data, 201, project_quotum_test_data)
url = r_dict['url']
GET_and_assert_expected_response(self, url, 200, project_quotum_test_data)
# PUT new values, verify
test_data = dict(test_data_creator.ProjectQuotum("other description"))
test_data['name'] = project_quotum_test_data['name'] # since name is PK, need to keep that unchanged
PUT_and_assert_expected_response(self, url, test_data, 200, test_data)
GET_and_assert_expected_response(self, url, 200, test_data)
def test_project_quotum_PATCH(self):
project_quotum_test_data = test_data_creator.ProjectQuotum()
# POST new item, verify
r_dict = POST_and_assert_expected_response(self, BASE_URL + '/project_quotum/', project_quotum_test_data, 201, project_quotum_test_data)
url = r_dict['url']
GET_and_assert_expected_response(self, url, 200, project_quotum_test_data)
test_patch = {"resource_quota": 500}
# PATCH item and verify
PATCH_and_assert_expected_response(self, url, test_patch, 200, test_patch)
expected_data = dict(project_quotum_test_data)
expected_data.update(test_patch)
GET_and_assert_expected_response(self, url, 200, expected_data)
def test_project_quotum_DELETE(self):
project_quotum_test_data = test_data_creator.ProjectQuotum()
# POST new item, verify
r_dict = POST_and_assert_expected_response(self, BASE_URL + '/project_quotum/', project_quotum_test_data, 201, project_quotum_test_data)
url = r_dict['url']
GET_and_assert_expected_response(self, url, 200, project_quotum_test_data)
# DELETE and check it's gone
DELETE_and_assert_gone(self, url)
def test_project_quotum_PROTECT_behavior_on_project_deleted(self):
# POST new item with dependencies
project_test_data = test_data_creator.Project()
project_url = POST_and_assert_expected_response(self, BASE_URL + '/project/', project_test_data, 201, project_test_data)['url']
test_data = dict(test_data_creator.ProjectQuotum(project_url=project_url))
url = POST_and_assert_expected_response(self, BASE_URL + '/project_quotum/', test_data, 201, test_data)['url']
# verify
GET_and_assert_expected_response(self, url, 200, test_data)
project_test_data['project'] = [url] # add the
# Try to DELETE dependency, verify that was not successful
# Unfortunately we don't get a nice error in json, but a Django debug page on error 500...
response = requests.delete(project_url, auth=AUTH)
self.assertEqual(500, response.status_code)
self.assertTrue("ProtectedError" in str(response.content))
GET_and_assert_expected_response(self, project_url, 200, project_test_data)
class SchedulingSetTestCase(unittest.TestCase):
def test_scheduling_set_list_apiformat(self):
r = requests.get(BASE_URL + '/scheduling_set/?format=api', auth=AUTH)
......
......@@ -95,8 +95,34 @@ def Project_test_data() -> dict:
"can_trigger": False,
"private_data": True,
"expert": True,
"filler": False}
"filler": False,
}
def ResourceUnit_test_data() -> dict:
return {
"value": "Terabyte"
}
def ResourceType_test_data() -> dict:
return {
"tags": [],
"description": 'my description ' + str(uuid.uuid4()),
"resource_unit": models.ResourceUnit.objects.create(**ResourceUnit_test_data()),
"name": 'my_resource_type_' + str(uuid.uuid4()),
}
def ProjectQuotum_test_data() -> dict:
return {
"tags": [],
"description": 'my description ' + str(uuid.uuid4()),
"resource_quota": '1000',
"project": models.Project.objects.create(**Project_test_data()),
"resource_type": models.ResourceType.objects.create(**ResourceType_test_data()),
"name": 'my_project_quotum_' + str(uuid.uuid4()),
}
def SchedulingSet_test_data() -> dict:
return {"name": 'my_scheduling_set',
"description": "",
......
......@@ -38,6 +38,9 @@ class TMSSRESTTestDataCreator():
def post_data_and_get_url(self, data, url_postfix):
"""POST the given data the self.django_api_url+url_postfix, and return the response's url"""
return json.loads(self.post_data_and_get_response(data, url_postfix).content.decode('utf-8'))['url']
def post_data_and_get_id(self, data, url_postfix):
"""POST the given data the self.django_api_url+url_postfix, and return the response's url"""
return json.loads(self.post_data_and_get_response(data, url_postfix).content.decode('utf-8'))['id']
#######################################################
# the methods below can be used to create test data
......@@ -110,10 +113,45 @@ class TMSSRESTTestDataCreator():
return {"name": 'my_project_' + str(uuid.uuid4()),
"description": description,
"tags": [],
"project_quotums": [],
"priority": 1,
"can_trigger": False,
"private_data": True}
def ResourceUnit(self ):
return {
"value": 'my_resource_unit_' + str(uuid.uuid4()),
}
def ResourceType(self, description="my project quotum description", resource_url=None):
if resource_url is None:
resource_url = self.post_data_and_get_id(self.ResourceUnit(), '/resource_unit/')
return {
"tags": [],
"description": description,
"resource_unit": self.django_api_url +"/resource_unit/"+str(resource_url)+"/?format=api",
"name": 'my_resource_type_' + str(uuid.uuid4())
}
def ProjectQuotum(self, description="my project quotum description", project_url=None, resource_url=None):
if project_url is None:
project_url = self.post_data_and_get_url(self.Project(), '/project/')
if resource_url is None:
resource_url = self.post_data_and_get_url(self.ResourceType(), '/resource_type/')
return {
"name": 'my_project_quotum_' + str(uuid.uuid4()),
"description": description,
"tags": [],
"resource_quota": 1000,
"project": project_url,
"resource_type": resource_url
}
def SchedulingSet(self, name="my_scheduling_set", project_url=None, generator_template_url=None):
if project_url is None:
project_url = self.post_data_and_get_url(self.Project(), '/project/')
......
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