Skip to content
Snippets Groups Projects
Commit 70382483 authored by Jörn Künsemöller's avatar Jörn Künsemöller
Browse files

Task LSMR-13: Basic test coverage for all templates

parent 1b87b842
No related branches found
No related tags found
1 merge request!87Lsmr epic
...@@ -4245,6 +4245,8 @@ SAS/LSMR/src/lsmr/lsmrapp/__init__.py -text ...@@ -4245,6 +4245,8 @@ SAS/LSMR/src/lsmr/lsmrapp/__init__.py -text
SAS/LSMR/src/lsmr/lsmrapp/admin.py -text SAS/LSMR/src/lsmr/lsmrapp/admin.py -text
SAS/LSMR/src/lsmr/lsmrapp/apps.py -text SAS/LSMR/src/lsmr/lsmrapp/apps.py -text
SAS/LSMR/src/lsmr/lsmrapp/migrations/0001_initial.py -text SAS/LSMR/src/lsmr/lsmrapp/migrations/0001_initial.py -text
SAS/LSMR/src/lsmr/lsmrapp/migrations/0002_generatortemplate_runtemplate_tags_workioroles_workrelationtemplate_workrequesttemplate.py -text
SAS/LSMR/src/lsmr/lsmrapp/migrations/CMakeLists.txt -text
SAS/LSMR/src/lsmr/lsmrapp/migrations/__init__.py -text SAS/LSMR/src/lsmr/lsmrapp/migrations/__init__.py -text
SAS/LSMR/src/lsmr/lsmrapp/models.py -text SAS/LSMR/src/lsmr/lsmrapp/models.py -text
SAS/LSMR/src/lsmr/lsmrapp/serializers.py -text SAS/LSMR/src/lsmr/lsmrapp/serializers.py -text
......
...@@ -11,4 +11,7 @@ set(_py_files ...@@ -11,4 +11,7 @@ set(_py_files
) )
python_install(${_py_files} python_install(${_py_files}
DESTINATION lofar/sas/lsmr/lsmr/lsmrapp) DESTINATION lofar/sas/lsmr/lsmr/lsmrapp)
\ No newline at end of file
add_subdirectory(migrations)
\ No newline at end of file
# Generated by Django 2.0.6 on 2018-07-03 17:01
import django.contrib.postgres.fields
import django.contrib.postgres.fields.jsonb
from django.db import migrations, models
import lsmr.lsmrapp.models
class Migration(migrations.Migration):
initial = True
dependencies = [
('lsmrapp', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='GeneratorTemplate',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('tags', django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=30), blank=True, null=True, size=8)),
('creation', models.DateTimeField(auto_now_add=True)),
('update', models.DateTimeField(auto_now=True)),
('name', models.CharField(max_length=30)),
('description', models.CharField(max_length=255)),
('default_version', models.BooleanField()),
('version', models.CharField(max_length=30)),
('schema', django.contrib.postgres.fields.jsonb.JSONField()),
('create_func', models.CharField(max_length=30)),
],
options={
'abstract': False,
},
),
migrations.CreateModel(
name='RunTemplate',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('tags', django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=30), blank=True, null=True, size=8)),
('creation', models.DateTimeField(auto_now_add=True)),
('update', models.DateTimeField(auto_now=True)),
('name', models.CharField(max_length=30)),
('description', models.CharField(max_length=255)),
('default_version', models.BooleanField()),
('version', models.CharField(max_length=30)),
('schema', django.contrib.postgres.fields.jsonb.JSONField()),
],
options={
'abstract': False,
},
),
migrations.CreateModel(
name='Tags',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=30)),
('description', models.CharField(max_length=255)),
],
),
migrations.CreateModel(
name='WorkIORoles',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('role', models.CharField(choices=[(lsmr.lsmrapp.models.RoleChoice('correlator'), 'correlator'), (lsmr.lsmrapp.models.RoleChoice('beamformer'), 'beamformer'), (lsmr.lsmrapp.models.RoleChoice('inspection plots'), 'inspection plots'), (lsmr.lsmrapp.models.RoleChoice('calibrator'), 'calibrator'), (lsmr.lsmrapp.models.RoleChoice('target'), 'target'), (lsmr.lsmrapp.models.RoleChoice('input, output'), 'input, output')], max_length=30)),
('datatype', models.CharField(choices=[(lsmr.lsmrapp.models.DatatypeChoice('visibilities'), 'visibilities'), (lsmr.lsmrapp.models.DatatypeChoice('time series'), 'time series'), (lsmr.lsmrapp.models.DatatypeChoice('instrument model'), 'instrument model'), (lsmr.lsmrapp.models.DatatypeChoice('image'), 'image'), (lsmr.lsmrapp.models.DatatypeChoice('quality'), 'quality')], max_length=30)),
('dataformat', django.contrib.postgres.fields.ArrayField(base_field=models.CharField(choices=[(lsmr.lsmrapp.models.DataformatChoice('MeasurementSet'), 'MeasurementSet'), (lsmr.lsmrapp.models.DataformatChoice('HDF5'), 'HDF5')], max_length=30), blank=True, null=True, size=8)),
],
),
migrations.CreateModel(
name='WorkRelationTemplate',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('tags', django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=30), blank=True, null=True, size=8)),
('creation', models.DateTimeField(auto_now_add=True)),
('update', models.DateTimeField(auto_now=True)),
('name', models.CharField(max_length=30)),
('description', models.CharField(max_length=255)),
('default_version', models.BooleanField()),
('version', models.CharField(max_length=30)),
('schema', django.contrib.postgres.fields.jsonb.JSONField()),
],
options={
'abstract': False,
},
),
migrations.CreateModel(
name='WorkRequestTemplate',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('tags', django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=30), blank=True, null=True, size=8)),
('creation', models.DateTimeField(auto_now_add=True)),
('update', models.DateTimeField(auto_now=True)),
('name', models.CharField(max_length=30)),
('description', models.CharField(max_length=255)),
('default_version', models.BooleanField()),
('version', models.CharField(max_length=30)),
('schema', django.contrib.postgres.fields.jsonb.JSONField()),
('validation_code_js', models.CharField(max_length=30)),
],
options={
'abstract': False,
},
),
]
include(PythonInstall)
set(_py_files
__init__.py
0001_initial.py
0002_generatortemplate_runtemplate_tags_workioroles_workrelationtemplate_workrequesttemplate.py
)
python_install(${_py_files}
DESTINATION lofar/sas/lsmr/lsmr/lsmrapp/migrations)
\ No newline at end of file
...@@ -19,7 +19,7 @@ class BasicCommon(Model): ...@@ -19,7 +19,7 @@ class BasicCommon(Model):
# todo: we could switch to a manytomany field instead? # todo: we could switch to a manytomany field instead?
tags = ArrayField(CharField(max_length=30), size=8, blank=True, null=True) tags = ArrayField(CharField(max_length=30), size=8, blank=True, null=True)
creation = DateTimeField(auto_now_add=True) creation = DateTimeField(auto_now_add=True)
# update = DateTimeField(auto_now=True) # todo: maybe useful too? update = DateTimeField(auto_now=True)
class Meta: class Meta:
abstract = True abstract = True
...@@ -70,7 +70,6 @@ class DatatypeChoice(Enum): ...@@ -70,7 +70,6 @@ class DatatypeChoice(Enum):
QUALITY = "quality" QUALITY = "quality"
# todo: where is this supposed to be used?!
class DataformatChoice(Enum): class DataformatChoice(Enum):
MEASUREMENTSET = "MeasurementSet" MEASUREMENTSET = "MeasurementSet"
HDF5 = "HDF5" HDF5 = "HDF5"
...@@ -79,7 +78,7 @@ class DataformatChoice(Enum): ...@@ -79,7 +78,7 @@ class DataformatChoice(Enum):
# concrete models # concrete models
class WorkIORoles(Model): class WorkIORoles(Model):
# todo: Choices does not seem to translate to a a Postgres ENUM type # todo: Choices does not seem to translate to a a Postgres ENUM type currently
# todo: If we are not happy with this as is, I see two options: # todo: If we are not happy with this as is, I see two options:
# todo: 1. Wait to see if they implement that EnumField in the meantime (https://code.djangoproject.com/ticket/24342) # todo: 1. Wait to see if they implement that EnumField in the meantime (https://code.djangoproject.com/ticket/24342)
# todo: 2. Do that in raw SQL outside Django # todo: 2. Do that in raw SQL outside Django
...@@ -91,6 +90,10 @@ class WorkIORoles(Model): ...@@ -91,6 +90,10 @@ class WorkIORoles(Model):
max_length=30, max_length=30,
choices=[(item, item.value) for item in DatatypeChoice] choices=[(item, item.value) for item in DatatypeChoice]
) )
dataformat = ArrayField(CharField(
max_length=30,
choices=[(item, item.value) for item in DataformatChoice]
), size=8, blank=True, null=True)
# #
...@@ -100,12 +103,15 @@ class WorkIORoles(Model): ...@@ -100,12 +103,15 @@ class WorkIORoles(Model):
# abstract models # abstract models
class Template(SystemDefinedCommon): class Template(SystemDefinedCommon):
name = CharField(max_length=30) # todo: check if this should go in SystemDefinedCommon or BasicCommon
description = CharField(max_length=255) # todo: check if this should go in SystemDefinedCommon or BasicCommon
default_version = BooleanField() default_version = BooleanField()
version = CharField(max_length=30) version = CharField(max_length=30)
schema = JSONField() schema = JSONField()
class Meta: class Meta:
abstract = True abstract = True
#unique_together = ("name", "default_version") # todo: We only want this for default_version=True, find a way to do so
# concrete models # concrete models
...@@ -120,8 +126,8 @@ class RunTemplate(Template): ...@@ -120,8 +126,8 @@ class RunTemplate(Template):
class WorkRequestTemplate(Template): class WorkRequestTemplate(Template):
validation_code_js = CharField(max_length=30) validation_code_js = CharField(max_length=30)
outputs = ForeignKey("WorkIORoles", related_name='role_output', on_delete=CASCADE) #outputs = ForeignKey("WorkIORoles", related_name='role_output', on_delete=CASCADE) # todo: removed in latest draft, delete?
inputs = ForeignKey("WorkIORoles", related_name='role_input', on_delete=CASCADE) #inputs = ForeignKey("WorkIORoles", related_name='role_input', on_delete=CASCADE) # todo: removed in latest draft, delete?
class WorkRelationTemplate(Template): class WorkRelationTemplate(Template):
......
...@@ -8,7 +8,7 @@ from lsmr.lsmrapp import models, serializers ...@@ -8,7 +8,7 @@ from lsmr.lsmrapp import models, serializers
class TagsViewSet(viewsets.ModelViewSet): class TagsViewSet(viewsets.ModelViewSet):
queryset = models.Tags.objects.all() queryset = models.Tags.objects.all()
serializer_class = serializers.GeneratorTemplateSerializer serializer_class = serializers.TagsSerializer
class GeneratorTemplateViewSet(viewsets.ModelViewSet): class GeneratorTemplateViewSet(viewsets.ModelViewSet):
...@@ -18,14 +18,14 @@ class GeneratorTemplateViewSet(viewsets.ModelViewSet): ...@@ -18,14 +18,14 @@ class GeneratorTemplateViewSet(viewsets.ModelViewSet):
class RunTemplateViewSet(viewsets.ModelViewSet): class RunTemplateViewSet(viewsets.ModelViewSet):
queryset = models.RunTemplate.objects.all() queryset = models.RunTemplate.objects.all()
serializer_class = serializers.GeneratorTemplateSerializer serializer_class = serializers.RunTemplateSerializer
class WorkRequestTemplateViewSet(viewsets.ModelViewSet): class WorkRequestTemplateViewSet(viewsets.ModelViewSet):
queryset = models.WorkRequestTemplate.objects.all() queryset = models.WorkRequestTemplate.objects.all()
serializer_class = serializers.GeneratorTemplateSerializer serializer_class = serializers.WorkRequestTemplateSerializer
class WorkRelationTemplateViewSet(viewsets.ModelViewSet): class WorkRelationTemplateViewSet(viewsets.ModelViewSet):
queryset = models.WorkRelationTemplate.objects.all() queryset = models.WorkRelationTemplate.objects.all()
serializer_class = serializers.GeneratorTemplateSerializer serializer_class = serializers.WorkRelationTemplateSerializer
...@@ -12,6 +12,7 @@ from django.test.runner import DiscoverRunner ...@@ -12,6 +12,7 @@ from django.test.runner import DiscoverRunner
import psycopg2 import psycopg2
from lofar.sas.lsmr.lsmr.settings import DATABASES from lofar.sas.lsmr.lsmr.settings import DATABASES
# todo: consider switching from testing.postgresql to pyembedpg to allow easy testing on specific versions. That may be slower, though.
try: try:
import testing.postgresql import testing.postgresql
......
...@@ -21,19 +21,179 @@ ...@@ -21,19 +21,179 @@
import unittest import unittest
import requests import requests
import json
BASE_URL = 'http://localhost:8777' BASE_URL = 'http://localhost:8777'
def PUT_and_assert_correct(self, url, data):
"""
POST data on url and assert the correctness based on the server response.
:return: url for new item
"""
response = requests.put(url, json=data)
if response.status_code is not 200:
print(response.content)
self.assertEqual(response.status_code, 200)
response_json = json.loads(response.content.decode('utf-8'))
for item in data.items():
self.assertIn(item, response_json.items())
return response_json['url']
def POST_and_assert_correct(self, url, data):
"""
POST data on url and assert the correctness based on the server response.
:return: url for new item
"""
response = requests.post(url, json=data)
if response.status_code is not 201:
print(response.content)
self.assertEqual(response.status_code, 201)
response_json = json.loads(response.content.decode('utf-8'))
for item in data.items():
self.assertIn(item, response_json.items())
return response_json['url']
def GET_and_assert_correct(self, url, data):
"""
GET from url and assert correctness given provided expected data.
"""
response = requests.get(url, json=data)
if response.status_code is not 200:
print(response.content)
self.assertEqual(response.status_code, 200)
response_json = json.loads(response.content.decode('utf-8'))
for item in data.items():
self.assertIn(item, response_json.items())
def PATCH_and_assert_correct(self, url, data):
"""
POST data on url and assert the provided values have changed based on the server response.
:return: url for new item
"""
response = requests.patch(url, json=data)
if response.status_code is not 200:
print(response.content)
self.assertEqual(response.status_code, 200)
response_json = json.loads(response.content.decode('utf-8'))
for item in data.items():
self.assertIn(item, response_json.items())
def DELETE_and_assert_gone(self, url):
"""
DELETE item at provided url and assert that the request was accepted by the server
:return: url for new item
"""
response = requests.delete(url)
if response.status_code is not 204:
print(response.content)
self.assertEqual(response.status_code, 204)
response = requests.get(url)
if response.status_code is not 404:
print(response.content)
self.assertEqual(response.status_code, 404)
class BasicFunctionTestCase(unittest.TestCase): class BasicFunctionTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
pass pass
# todo: test_welcome_page (once we have one :))
class GeneratorTemplateTestCase(unittest.TestCase):
test_data_1 = {"name": "observation",
"description": 'My one observation',
"default_version": False,
"version": 'v0.314159265359',
"schema": {"mykey": "my value"},
"create_func": 'Funky',
"tags": ["LSMR", "TESTING"]}
test_data_2 = {"name": "observation",
"description": 'My other observation',
"default_version": False,
"version": 'v3.14159265359',
"schema": {"mykey": "my other value"},
"create_func": 'Trousers',
"tags": []}
test_patch = {"version": 'v6.28318530718',
"schema": {"mykey": "my better value"},
}
def test_generator_template_list_apiformat(self): def test_generator_template_list_apiformat(self):
r = requests.get(BASE_URL + '/generator_template/?format=api') r = requests.get(BASE_URL + '/generator_template/?format=api')
self.assertEqual(r.status_code, 200) self.assertEqual(r.status_code, 200)
self.assertTrue("Generator Template List" in r.content.decode('utf8')) self.assertTrue("Generator Template List" in r.content.decode('utf8'))
def test_generator_template_GET_nonexistant_item_raises_error(self):
r = requests.get(BASE_URL + '/generator_template/1234321/')
self.assertEqual(r.status_code, 404)
def test_generator_template_POST_and_GET(self):
# POST and GET a new item and assert correctness
url = POST_and_assert_correct(self, BASE_URL + '/generator_template/', self.test_data_1)
GET_and_assert_correct(self, url+'?format=json', self.test_data_1)
def test_generator_template_PUT_invalid_raises_error(self):
r = requests.put(BASE_URL + '/generator_template/9876789876/', self.test_data_1)
self.assertEqual(r.status_code, 404)
def test_generator_template_PUT(self):
# POST new item, verify
url = POST_and_assert_correct(self, BASE_URL + '/generator_template/', self.test_data_1)
GET_and_assert_correct(self, url, self.test_data_1)
# PUT new values, verify
PUT_and_assert_correct(self, url, self.test_data_2)
GET_and_assert_correct(self, url, self.test_data_2)
def test_generator_template_PATCH(self):
# POST new item, verify
url = POST_and_assert_correct(self, BASE_URL + '/generator_template/', self.test_data_1)
GET_and_assert_correct(self, url, self.test_data_1)
# PATCH item and verify
PATCH_and_assert_correct(self, url, self.test_patch)
expected_data = dict(self.test_data_1)
expected_data.update(self.test_patch)
GET_and_assert_correct(self, url, expected_data)
def test_generator_template_DELETE(self):
# POST new item, verify
url = POST_and_assert_correct(self, BASE_URL + '/generator_template/', self.test_data_1)
GET_and_assert_correct(self, url, self.test_data_1)
# DELETE and check it's gone
DELETE_and_assert_gone(self, url)
class RunTemplateTestCase(unittest.TestCase):
# todo
pass
class WorkRequestTemplateTestCase(unittest.TestCase):
# todo
pass
class WorkRelationTemplateTestCase(unittest.TestCase):
# todo
pass
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()
......
...@@ -20,38 +20,43 @@ ...@@ -20,38 +20,43 @@
# $Id: $ # $Id: $
import rest_framework.test import rest_framework.test
from lsmr.lsmrapp.models import GeneratorTemplate, RunTemplate, WorkRequestTemplate, WorkRelationTemplate from lsmr.lsmrapp import models
from django.utils import timezone # can't use datetime due to offset-awareness from django.utils import timezone # can't use datetime due to offset-awareness
client = rest_framework.test.APIClient() client = rest_framework.test.APIClient()
class GeneratorTemplateTest(rest_framework.test.APITransactionTestCase):
reset_sequences = True # todo: Decide whether we want this. It slows down tests, but we can hardcode primary keys.
class GeneratorTemplateTest(rest_framework.test.APITestCase):
# test data # test data
test_data_1 = {"default_version": True, test_data_1 = {"name": "observation",
"description": 'My one observation',
"default_version": False,
"version": 'v0.314159265359', "version": 'v0.314159265359',
"schema": '{"mykey": "my value"}', "schema": {"mykey": "my value"},
"create_func": 'Funky', "create_func": 'Funky',
"tags": ["LSMR", "TESTING"]} "tags": ["LSMR", "TESTING"]}
test_data_2 = {"default_version": False, test_data_2 = {"name": "observation",
"description": 'My other observation',
"default_version": False,
"version": 'v3.14159265359', "version": 'v3.14159265359',
"schema": '{"mykey": "my other value"}', "schema": {"mykey": "my other value"},
"create_func": 'Trousers', "create_func": 'Trousers',
"tags": []} "tags": []}
def setUp(self): def setUp(self):
pass pass
def test_GeneratorTemplate_gets_created_with_correct_creation_timestamp(self): def test_GeneratorTemplate_gets_created_with_correct_creation_timestamp(self):
# setup # setup
before = timezone.now() before = timezone.now()
entry = GeneratorTemplate.objects.create(**self.test_data_1) entry = models.GeneratorTemplate.objects.create(**self.test_data_1)
entry.save()
after = timezone.now() after = timezone.now()
...@@ -59,29 +64,292 @@ class GeneratorTemplateTest(rest_framework.test.APITestCase): ...@@ -59,29 +64,292 @@ class GeneratorTemplateTest(rest_framework.test.APITestCase):
self.assertLess(before, entry.creation) self.assertLess(before, entry.creation)
self.assertGreater(after, entry.creation) self.assertGreater(after, entry.creation)
def test_GeneratorTemplate_update_timestamp_gets_changed_correctly(self):
# setup
entry = models.GeneratorTemplate.objects.create(**self.test_data_1)
before = timezone.now()
entry.version = "0.42"
entry.save()
after = timezone.now()
# assert
self.assertLess(before, entry.update)
self.assertGreater(after, entry.update)
def test_GET_GeneratorTemplate_list_view_shows_entry(self): def test_GET_GeneratorTemplate_list_view_shows_entry(self):
# setup # setup
entry = GeneratorTemplate.objects.create(**self.test_data_1) entry = models.GeneratorTemplate.objects.create(**self.test_data_1)
# assert # assert
response = client.get('/generator_template/', format='json', follow=True) response = client.get('/generator_template/', format='json', follow=True)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
for item in self.test_data_1.items(): # assertDictContainsSubset() is deprecated... for item in self.test_data_1.items():
self.assertIn(item, response.data[0].items()) self.assertIn(item, response.data[0].items())
def test_GET_GeneratorTemplate_view_returns_correct_entry(self): def test_GET_GeneratorTemplate_view_returns_correct_entry(self):
# setup # setup
id1 = GeneratorTemplate.objects.create(**self.test_data_1).id id1 = models.GeneratorTemplate.objects.create(**self.test_data_1).id
id2 = GeneratorTemplate.objects.create(**self.test_data_2).id id2 = models.GeneratorTemplate.objects.create(**self.test_data_2).id
# assert # assert
response1 = client.get('/generator_template/%s/' % id1, format='json', follow=True) response1 = client.get('/generator_template/%s/' % id1, format='json', follow=True)
response2 = client.get('/generator_template/%s/' % id2, format='json', follow=True) response2 = client.get('/generator_template/%s/' % id2, format='json', follow=True)
self.assertEqual(response1.status_code, 200) self.assertEqual(response1.status_code, 200)
self.assertEqual(response2.status_code, 200) self.assertEqual(response2.status_code, 200)
for item in self.test_data_1.items(): # assertDictContainsSubset() is deprecated... for item in self.test_data_1.items():
self.assertIn(item, response1.data.items())
for item in self.test_data_2.items():
self.assertIn(item, response2.data.items())
# todo: find a technical solution how to actually do this first, also implement for the other templates then.
# def test_GeneratorTemplate_prevents_double_default_version_for_same_name(self):
#
# # setup
# test_data_1 = dict(self.test_data_1)
# test_data_2 = dict(self.test_data_2)
# test_data_1['default_version'] = True
# test_data_2['default_version'] = True
# models.GeneratorTemplate.objects.create(**test_data_1)
#
# # assert
# with self.assertRaises(Exception):
# models.GeneratorTemplate.objects.create(**test_data_2)
class RunTemplateTest(rest_framework.test.APITransactionTestCase):
reset_sequences = True
# test data
test_data_1 = {"name": "observation",
"description": 'My one observation',
"default_version": False,
"version": 'v0.314159265359',
"schema": {"mykey": "my value"},
"tags": ["LSMR", "TESTING"]}
test_data_2 = {"name": "observation",
"description": 'My other observation',
"default_version": False,
"version": 'v3.14159265359',
"schema": {"mykey": "my other value"},
"tags": []}
def setUp(self):
pass
def test_RunTemplate_gets_created_with_correct_creation_timestamp(self):
# setup
before = timezone.now()
entry = models.RunTemplate.objects.create(**self.test_data_1)
after = timezone.now()
# assert
self.assertLess(before, entry.creation)
self.assertGreater(after, entry.creation)
def test_RunTemplate_update_timestamp_gets_changed_correctly(self):
# setup
entry = models.RunTemplate.objects.create(**self.test_data_1)
before = timezone.now()
entry.version = "0.42"
entry.save()
after = timezone.now()
# assert
self.assertLess(before, entry.update)
self.assertGreater(after, entry.update)
def test_GET_RunTemplate_list_view_shows_entry(self):
# setup
entry = models.RunTemplate.objects.create(**self.test_data_1)
# assert
response = client.get('/run_template/', format='json', follow=True)
self.assertEqual(response.status_code, 200)
for item in self.test_data_1.items():
self.assertIn(item, response.data[0].items())
def test_GET_RunTemplate_view_returns_correct_entry(self):
# setup
id1 = models.RunTemplate.objects.create(**self.test_data_1).id
id2 = models.RunTemplate.objects.create(**self.test_data_2).id
# assert
response1 = client.get('/run_template/%s/' % id1, format='json', follow=True)
response2 = client.get('/run_template/%s/' % id2, format='json', follow=True)
self.assertEqual(response1.status_code, 200)
self.assertEqual(response2.status_code, 200)
for item in self.test_data_1.items():
self.assertIn(item, response1.data.items())
for item in self.test_data_2.items():
self.assertIn(item, response2.data.items())
class WorkRequestTemplateTest(rest_framework.test.APITransactionTestCase):
reset_sequences = True
# test data
test_data_1 = {"validation_code_js":"",
#"inputs": "",
#"outputs": "",
"name": "observation",
"description": 'My one observation',
"default_version": False,
"version": 'v0.314159265359',
"schema": {"mykey": "my value"},
"tags": ["LSMR", "TESTING"]}
test_data_2 = {"validation_code_js":"",
#"inputs": "",
#"outputs": "",
"name": "observation",
"description": 'My other observation',
"default_version": False,
"version": 'v3.14159265359',
"schema": {"mykey": "my other value"},
"tags": []}
def setUp(self):
pass
def test_WorkRequestTemplate_gets_created_with_correct_creation_timestamp(self):
# setup
before = timezone.now()
entry = models.WorkRequestTemplate.objects.create(**self.test_data_1)
after = timezone.now()
# assert
self.assertLess(before, entry.creation)
self.assertGreater(after, entry.creation)
def test_WorkRequestTemplate_update_timestamp_gets_changed_correctly(self):
# setup
entry = models.WorkRequestTemplate.objects.create(**self.test_data_1)
before = timezone.now()
entry.version = "0.42"
entry.save()
after = timezone.now()
# assert
self.assertLess(before, entry.update)
self.assertGreater(after, entry.update)
def test_GET_WorkRequestTemplate_list_view_shows_entry(self):
# setup
entry = models.WorkRequestTemplate.objects.create(**self.test_data_1)
# assert
response = client.get('/work_request_template/', format='json', follow=True)
self.assertEqual(response.status_code, 200)
for item in self.test_data_1.items():
self.assertIn(item, response.data[0].items())
def test_GET_WorkRequestTemplate_view_returns_correct_entry(self):
# setup
id1 = models.WorkRequestTemplate.objects.create(**self.test_data_1).id
id2 = models.WorkRequestTemplate.objects.create(**self.test_data_2).id
# assert
response1 = client.get('/work_request_template/%s/' % id1, format='json', follow=True)
response2 = client.get('/work_request_template/%s/' % id2, format='json', follow=True)
self.assertEqual(response1.status_code, 200)
self.assertEqual(response2.status_code, 200)
for item in self.test_data_1.items():
self.assertIn(item, response1.data.items())
for item in self.test_data_2.items():
self.assertIn(item, response2.data.items())
class WorkRelationTemplateTest(rest_framework.test.APITransactionTestCase):
reset_sequences = True
# test data
test_data_1 = {"name": "observation",
"description": 'My one observation',
"default_version": False,
"version": 'v0.314159265359',
"schema": {"mykey": "my value"},
"tags": ["LSMR", "TESTING"]}
test_data_2 = {"name": "observation",
"description": 'My other observation',
"default_version": False,
"version": 'v3.14159265359',
"schema": {"mykey": "my other value"},
"tags": []}
def setUp(self):
pass
def test_WorkRelationTemplate_gets_created_with_correct_creation_timestamp(self):
# setup
before = timezone.now()
entry = models.WorkRelationTemplate.objects.create(**self.test_data_1)
after = timezone.now()
# assert
self.assertLess(before, entry.creation)
self.assertGreater(after, entry.creation)
def test_WorkRelationTemplate_update_timestamp_gets_changed_correctly(self):
# setup
entry = models.WorkRelationTemplate.objects.create(**self.test_data_1)
before = timezone.now()
entry.version = "0.42"
entry.save()
after = timezone.now()
# assert
self.assertLess(before, entry.update)
self.assertGreater(after, entry.update)
def test_GET_WorkRelationTemplate_list_view_shows_entry(self):
# setup
entry = models.WorkRelationTemplate.objects.create(**self.test_data_1)
# assert
response = client.get('/work_relation_template/', format='json', follow=True)
self.assertEqual(response.status_code, 200)
for item in self.test_data_1.items():
self.assertIn(item, response.data[0].items())
def test_GET_WorkRelationTemplate_view_returns_correct_entry(self):
# setup
id1 = models.WorkRelationTemplate.objects.create(**self.test_data_1).id
id2 = models.WorkRelationTemplate.objects.create(**self.test_data_2).id
# assert
response1 = client.get('/work_relation_template/%s/' % id1, format='json', follow=True)
response2 = client.get('/work_relation_template/%s/' % id2, format='json', follow=True)
self.assertEqual(response1.status_code, 200)
self.assertEqual(response2.status_code, 200)
for item in self.test_data_1.items():
self.assertIn(item, response1.data.items()) self.assertIn(item, response1.data.items())
for item in self.test_data_2.items(): for item in self.test_data_2.items():
self.assertIn(item, response2.data.items()) self.assertIn(item, response2.data.items())
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment