Skip to content

Commit

Permalink
Add a comprehensive set of tests for the PACS API endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorge Luis Bernal Rusiel committed Nov 19, 2024
1 parent e4d886b commit 121454c
Show file tree
Hide file tree
Showing 5 changed files with 464 additions and 17 deletions.
1 change: 1 addition & 0 deletions chris_backend/feeds/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ def list(self, request, *args, **kwargs):
request=request),
'userfiles': reverse('userfile-list', request=request),
'pacs': reverse('pacs-list', request=request),
'pacsqueries': reverse('allpacsquery-list', request=request),
'pacsfiles': reverse('pacsfile-list', request=request),
'pacsseries': reverse('pacsseries-list', request=request),
'filebrowser': reverse('chrisfolder-list', request=request)}
Expand Down
18 changes: 14 additions & 4 deletions chris_backend/pacsfiles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ class PACSSerializer(serializers.HyperlinkedModelSerializer):
folder_path = serializers.ReadOnlyField(source='folder.path')
folder = serializers.HyperlinkedRelatedField(view_name='chrisfolder-detail',
read_only=True)
pacs_series_list = serializers.HyperlinkedIdentityField(
view_name='pacs-specific-series-list')
query_list = serializers.HyperlinkedIdentityField(view_name='pacsquery-list')
series_list = serializers.HyperlinkedIdentityField(view_name='pacs-specific-series-list')

class Meta:
model = PACS
fields = ('url', 'id', 'identifier', 'active', 'folder_path', 'folder',
'pacs_series_list')
'query_list', 'series_list')


class PACSQuerySerializer(serializers.HyperlinkedModelSerializer):
query = serializers.JSONField(binary=True)
query = serializers.JSONField(binary=True, required=False)
result = serializers.ReadOnlyField()
pacs_identifier = serializers.ReadOnlyField(source='pacs.identifier')
owner_username = serializers.ReadOnlyField(source='owner.username')
Expand Down Expand Up @@ -85,6 +85,16 @@ def update(self, instance, validated_data):
f'for pacs {pacs.identifier}')
raise serializers.ValidationError([error_msg])

def validate(self, data):
"""
Overriden to validate that the query field is in data when creating a new query.
"""
if not self.instance: # on create
if 'query' not in data:
raise serializers.ValidationError(
{'query': ["This field is required."]})
return data


class PACSSeriesSerializer(serializers.HyperlinkedModelSerializer):
path = serializers.CharField(max_length=1024, write_only=True)
Expand Down
97 changes: 92 additions & 5 deletions chris_backend/pacsfiles/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,114 @@
from unittest import mock
from rest_framework import serializers

from pacsfiles.serializers import PACSSeriesSerializer
from core.models import ChrisFolder
from core.utils import json_zip2str
from pacsfiles.models import PACS, PACSQuery
from pacsfiles.serializers import PACSQuerySerializer, PACSSeriesSerializer


CHRIS_SUPERUSER_PASSWORD = settings.CHRIS_SUPERUSER_PASSWORD


class PACSSeriesSerializerTests(TestCase):

class SerializerTests(TestCase):
def setUp(self):
# avoid cluttered console output (for instance logging all the http requests)
logging.disable(logging.WARNING)

# create superuser chris (owner of root folders)
# superuser chris (owner of root folders)
self.chris_username = 'chris'
self.chris_password = CHRIS_SUPERUSER_PASSWORD
chris_user = User.objects.get(username=self.chris_username)

# create normal user
self.username = 'foo'
self.password = 'bar'
User.objects.create_user(username=self.username, password=self.password)

# create a PACS
self.pacs_name = 'myPACS'
folder_path = f'SERVICES/PACS/{self.pacs_name}'
(pacs_folder, tf) = ChrisFolder.objects.get_or_create(path=folder_path,
owner=chris_user)
PACS.objects.get_or_create(folder=pacs_folder, identifier=self.pacs_name)

def tearDown(self):
# re-enable logging
logging.disable(logging.NOTSET)


class PACSQuerySerializerTests(SerializerTests):

def test_create_success(self):
"""
Test whether overriden 'create' method successfully creates a new PACS query.
"""
user = User.objects.get(username=self.username)
pacs = PACS.objects.get(identifier=self.pacs_name)
query = {'SeriesInstanceUID': '2.3.15.2.1057'}
data = {'title': 'query1', 'query': query, 'owner': user, 'pacs': pacs}

with mock.patch('pacsfiles.serializers.PfdcmClient.query') as pfdcm_query_mock:
result = {'mock': 'mock'}
pfdcm_query_mock.return_value = result
pacs_query_serializer = PACSQuerySerializer(data=data)
pacs_query = pacs_query_serializer.create(data)
pfdcm_query_mock.assert_called_with(self.pacs_name, query)
self.assertEqual(pacs_query.result, json_zip2str(result))


def test_create_failure_pacs_user_title_combination_already_exists(self):
"""
Test whether overriden 'create' method raises a ValidationError when a user has
already registered a PACS query with the same title and pacs.
"""
user = User.objects.get(username=self.username)
pacs = PACS.objects.get(identifier=self.pacs_name)
query = {'SeriesInstanceUID': '1.3.12.2.1107'}

PACSQuery.objects.get_or_create(title='query2', query=query, owner=user, pacs=pacs)

data = {'title': 'query2', 'query': query, 'owner': user, 'pacs': pacs}
pacs_query_serializer = PACSQuerySerializer(data=data)
with self.assertRaises(serializers.ValidationError):
pacs_query_serializer.create(data)

def test_update_success(self):
"""
Test whether overriden 'update' method successfully updates an existing PACS query.
"""
user = User.objects.get(username=self.username)
pacs = PACS.objects.get(identifier=self.pacs_name)
query = {'SeriesInstanceUID': '2.3.15.2.1057'}

pacs_query, _ = PACSQuery.objects.get_or_create(title='query2', query=query,
owner=user, pacs=pacs)

data = {'title': 'query4'}
pacs_query_serializer = PACSQuerySerializer(pacs_query, data)
pacs_query = pacs_query_serializer.update(pacs_query, data)
self.assertEqual(pacs_query.title, 'query4')

def test_update_failure_pacs_user_title_combination_already_exists(self):
"""
Test whether overriden 'update' method raises a ValidationError when a user has
already registered a PACS query with the same title and pacs.
"""
user = User.objects.get(username=self.username)
pacs = PACS.objects.get(identifier=self.pacs_name)
query = {'SeriesInstanceUID': '1.3.12.2.1107'}

pacs_query, _ = PACSQuery.objects.get_or_create(title='query2', query=query,
owner=user, pacs=pacs)
PACSQuery.objects.get_or_create(title='query3', query=query, owner=user, pacs=pacs)

data = {'title': 'query3'}
pacs_query_serializer = PACSQuerySerializer(pacs_query, data)
with self.assertRaises(serializers.ValidationError):
pacs_query_serializer.update(pacs_query, data)


class PACSSeriesSerializerTests(SerializerTests):

def test_validate_ndicom_failure_not_positive(self):
"""
Test whether overriden validate_ndicom method validates submitted ndicom must
Expand Down
Loading

0 comments on commit 121454c

Please sign in to comment.