Browse Source

Make project code value unique in database

master
Joshua Rubingh 6 months ago
parent
commit
9387d99cbd
  1. 18
      VRE/apps/study/migrations/0012_alter_study_code.py
  2. 2
      VRE/apps/study/models.py
  3. 34
      VRE/apps/study/tests.py

18
VRE/apps/study/migrations/0012_alter_study_code.py

@ -0,0 +1,18 @@ @@ -0,0 +1,18 @@
# Generated by Django 3.2.9 on 2021-11-30 09:54
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('study', '0011_auto_20211027_1320'),
]
operations = [
migrations.AlterField(
model_name='study',
name='code',
field=models.CharField(help_text='The research study code.', max_length=50, unique=True, verbose_name='Code'),
),
]

2
VRE/apps/study/models.py

@ -89,7 +89,7 @@ class Study(MetaDataModel): @@ -89,7 +89,7 @@ class Study(MetaDataModel):
avatar = models.ImageField(_('Avatar'), upload_to=file_upload_to, blank=True, null=True)
code = models.CharField(_('Code'), max_length=50, help_text=_('The research study code.'))
code = models.CharField(_('Code'),unique=True, max_length=50, help_text=_('The research study code.'))
human_subject = models.BooleanField(_('Human subject'), help_text=_('Is this research study using real humans.'))
field = models.ForeignKey(StudyField, verbose_name=StudyField._meta.verbose_name, on_delete=models.CASCADE, help_text=_('The study field for this reaserch study.'))

34
VRE/apps/study/tests.py

@ -107,6 +107,40 @@ class StudyCreateTest(TestCase): @@ -107,6 +107,40 @@ class StudyCreateTest(TestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.json()['results']), 1)
def test_create_study_unique_code(self):
# First time it should be ok
self.test_create_study()
# Now we create a new study with the same code value
# Need a study field based on faculty
study_field = StudyField.objects.filter(faculty=self.faculty).first()
endpoint = 'http://testserver' + reverse('api:v1:study-list')
data = {
"name": "Test Onderzoek",
"description": "Doe maar een lange onderzoek",
"code": "12345",
"human_subject": False,
"field": study_field.id
}
response = self.client.post(endpoint, json=data)
# Make sure we get a 201 result back
self.assertEqual(response.status_code, 400)
error_message = response.json()
self.assertEqual(error_message[0]['keyword'], 'unique')
# And now we should have still 1 study
endpoint = 'http://testserver' + reverse('api:v1:study-list')
response = self.client.get(endpoint)
# Check if we have 1 study
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.json()['results']), 1)
def test_create_study_with_png_image(self):
endpoint = 'http://testserver' + reverse('api:v1:study-list')
response = self.client.get(endpoint)

Loading…
Cancel
Save