Skip to content

Commit

Permalink
gebruiker serializer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnoutAllaert committed Mar 9, 2024
1 parent 287917d commit 88fc622
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
15 changes: 15 additions & 0 deletions api/tests/models/test_gebruiker.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,18 @@ def test_gebruiker_is_lesgever(self):
user2 = Gebruiker.objects.get(user__username='user2')
self.assertEqual(user1.is_lesgever, False)
self.assertEqual(user2.is_lesgever, True)

def test_user_label(self):
user = Gebruiker.objects.get(user__username='user1')
field_label = user._meta.get_field('user').verbose_name
self.assertEqual(field_label, 'user')

def test_subjects_label(self):
user = Gebruiker.objects.get(user__username='user1')
field_label = user._meta.get_field('subjects').verbose_name
self.assertEqual(field_label, 'subjects')

def test_str_method(self):
gebruiker = Gebruiker.objects.get(user__username='user1')
expected_object_name = gebruiker.user.first_name
self.assertEqual(str(gebruiker), expected_object_name)
Empty file.
44 changes: 44 additions & 0 deletions api/tests/serializers/test_gebruiker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from django.test import TestCase
from rest_framework.test import APITestCase
from rest_framework.exceptions import ValidationError
from api.models.gebruiker import Gebruiker
from api.serializers.gebruiker import GebruikerSerializer
from django.contrib.auth.models import User
from api.models.vak import Vak

class GebruikerSerializerTest(APITestCase):

def setUp(self):
# Create a User instance
self.user = User.objects.create_user(username='testuser')

self.gebruiker_attributes = {
'user': self.user,
}

self.serializer_data = GebruikerSerializer().data
self.gebruiker = Gebruiker.objects.create(**self.gebruiker_attributes)

subjects = ['Math', 'Science']
for subject in subjects:
vak = Vak.objects.create(name=subject)
self.gebruiker.subjects.add(vak)

self.serializer = GebruikerSerializer(instance=self.gebruiker)

def test_contains_expected_fields(self):
data = self.serializer.data
self.assertCountEqual(data.keys(), ['user', 'is_lesgever', 'subjects'])

def test_user_field_content(self):
data = self.serializer.data
self.assertEqual(data['user'], self.user.id)

def test_subjects_field_content(self):
data = self.serializer.data
subjects = [subject.pk for subject in self.gebruiker.subjects.all()]
self.assertEqual(data['subjects'], subjects)

def test_validation_for_blank_items(self):
serializer = GebruikerSerializer(data={'name': '', 'subjects': []})
self.assertRaises(ValidationError, serializer.is_valid, raise_exception=True)
1 change: 0 additions & 1 deletion api/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
class TestViews(TestCase):
def test_should_show_register_page(self):
print('nigger')
#self.client.get(reverse('register'))
#self.assertEqual(response.status_code, 200)
#self.assertTemplateUsed(response, "authentication/register")
Expand Down

0 comments on commit 88fc622

Please sign in to comment.