Skip to content

Commit

Permalink
DBC22-1088: unit test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ray-oxd committed Oct 27, 2023
1 parent bc1c8fd commit 0627ea3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 31 deletions.
6 changes: 2 additions & 4 deletions src/backend/apps/cms/tests/test_advisory_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ def setUp(self):
super().setUp()
advisory = Advisory.objects.create(
title="Advisory title",
description="Advisory description",
active=True,
body="Advisory body",
geometry=LineString([(-119, 35), (-118, 32)]),
path="000100010001",
depth=3,
Expand All @@ -25,8 +24,7 @@ def setUp(self):

advisory_2 = Advisory.objects.create(
title="Advisory title 2",
description="Advisory description 2",
active=True,
body="Advisory body 2",
geometry=LineString([(-119, 35), (-118, 32)]),
path="000100010002",
depth=3,
Expand Down
21 changes: 9 additions & 12 deletions src/backend/apps/cms/tests/test_advisory_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,28 @@ def setUp(self):

self.advisory = Advisory(
title="Advisory title 1",
active=True,
description="Advisory description 1",
geometry=LineString([(-123.569743, 48.561231),
body="Advisory body 1",
geometry=LineString([(-123.569743, 48.561231),
(-123.569743, 48.561231)]),
path="000100010001",
depth=3,
)
self.advisory.save()
self.serializer = AdvisorySerializer(self.advisory)

def test_serializer_valid_data(self):
# Check if the serializer data matches the expected data
assert self.serializer.data["title"] == \
'Advisory title 1'
assert self.serializer.data["active"], "The value is not True"
assert self.serializer.data["description"] == \
'Advisory description 1'
assert self.serializer.data["body"] == \
'Advisory body 1'
assert self.serializer.data["geometry"] is not None

def test_serializer_invalid_data(self):
# Create a serializer with invalid data
invalid_data = {
'title': '', # advisory title is required, invalid data
'description': 'Advisory description 1',
'body': 'Advisory body 1',
}
self.serializer = AdvisorySerializer(data=invalid_data)

Expand All @@ -42,9 +40,8 @@ def test_serializer_save(self):
valid_data = {
'id': 3,
'title': 'Advisory title 1',
'active': True,
'description': 'Advisory description 1',
'geometry': LineString([(-123.569743, 48.561231),
'body': 'Advisory body 1',
'geometry': LineString([(-123.569743, 48.561231),
(-123.569743, 48.561231)]),
'content_type': 55,
'depth': 1,
Expand All @@ -57,4 +54,4 @@ def test_serializer_save(self):
assert serializer.is_valid() is True
# Save the serializer data to create a new Advisory instance
saved_advisory = serializer.save()
assert saved_advisory.title == "Advisory title 1"
assert saved_advisory.title == "Advisory title 1"
6 changes: 2 additions & 4 deletions src/backend/apps/cms/tests/test_bulletin_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ def setUp(self):
super().setUp()
bulletin = Bulletin.objects.create(
title="Bulletin title",
description="Bulletin description",
active=True,
body="Bulletin body",
path="000100010001",
depth=3,
content_type=ContentType.objects.get(app_label='cms',
Expand All @@ -23,8 +22,7 @@ def setUp(self):

bulletin_2 = Bulletin.objects.create(
title="Bulletin title 2",
description="Bulletin description 2",
active=True,
body="Bulletin body 2",
path="000100010002",
depth=3,
content_type=ContentType.objects.get(app_label='cms',
Expand Down
18 changes: 7 additions & 11 deletions src/backend/apps/cms/tests/test_bulletin_serializer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from apps.cms.models import Bulletin
from apps.cms.serializers import BulletinSerializer
from apps.shared.tests import BaseTest
from django.contrib.gis.geos import LineString


class TestBulletinSerializer(BaseTest):
Expand All @@ -10,27 +9,25 @@ def setUp(self):

self.bulletin = Bulletin(
title="Bulletin title 1",
active=True,
description="Bulletin description 1",
body="Bulletin body 1",
path="000100010001",
depth=3,
)
self.bulletin.save()
self.serializer = BulletinSerializer(self.bulletin)

def test_serializer_valid_data(self):
# Check if the serializer data matches the expected data
assert self.serializer.data["title"] == \
'Bulletin title 1'
assert self.serializer.data["active"], "The value is not True"
assert self.serializer.data["description"] == \
'Bulletin description 1'
assert self.serializer.data["body"] == \
'Bulletin body 1'

def test_serializer_invalid_data(self):
# Create a serializer with invalid data
invalid_data = {
'title': '', # bulletin title is required, invalid data
'description': 'Bulletin description 1',
'body': 'Bulletin body 1',
}
self.serializer = BulletinSerializer(data=invalid_data)

Expand All @@ -39,8 +36,7 @@ def test_serializer_save(self):
valid_data = {
'id': 3,
'title': 'Bulletin title 1',
'active': True,
'description': 'Bulletin description 1',
'body': 'Bulletin body 1',
'content_type': 55,
'depth': 1,
'path': '000100010005',
Expand All @@ -52,4 +48,4 @@ def test_serializer_save(self):
assert serializer.is_valid() is True
# Save the serializer data to create a new Bulletin instance
saved_bulletin = serializer.save()
assert saved_bulletin.title == "Bulletin title 1"
assert saved_bulletin.title == "Bulletin title 1"

0 comments on commit 0627ea3

Please sign in to comment.