22
33import pytest
44from guardian .shortcuts import assign_perm
5+ from rest_framework .exceptions import ValidationError
56
67from grandchallenge .cases .models import RawImageUploadSession
78from grandchallenge .components .models import (
254255
255256
256257def test_civ_post_validate_provided_fields ():
257- values = [
258- {}, # no values is also valid
259- {"file" : "https://some-api-url/" },
260- {"image" : "https://some-api-url/" },
261- {"upload_session" : "https://some-api-url/" },
262- {"user_upload" : "https://some-api-url/" },
263- {"image_name" : "foobar" , "user_uploads" : ["https://some-api-url/" ]},
264- {"value" : 42 },
265- ]
258+ with pytest .raises (
259+ ValidationError , match = "An interface must be specified"
260+ ):
261+ ComponentInterfaceValuePostSerializer ._validate_provided_fields (
262+ attrs = {}
263+ )
264+
265+ with pytest .raises (
266+ ValidationError ,
267+ match = "You must provide at least one of" ,
268+ ):
269+ ComponentInterfaceValuePostSerializer ._validate_provided_fields (
270+ attrs = {"interface" : "my-socket" }
271+ )
272+
266273 payload_empty = {
267274 "interface" : "my-socket" ,
268275 "file" : None ,
@@ -274,13 +281,41 @@ def test_civ_post_validate_provided_fields():
274281 "value" : None ,
275282 }
276283
284+ # All None values is valid
285+ with nullcontext ():
286+ ComponentInterfaceValuePostSerializer ._validate_provided_fields (
287+ attrs = payload_empty
288+ )
289+
290+ values = [
291+ {"file" : "https://some-api-url/" },
292+ {"image" : "https://some-api-url/" },
293+ {"upload_session" : "https://some-api-url/" },
294+ {"user_upload" : "https://some-api-url/" },
295+ {"image_name" : "foobar" , "user_uploads" : ["https://some-api-url/" ]},
296+ {"value" : 42 },
297+ ]
298+
277299 for value in values :
278300 payload = {** payload_empty , ** value }
301+
302+ # providing one value is valid
279303 with nullcontext ():
280304 ComponentInterfaceValuePostSerializer ._validate_provided_fields (
281305 attrs = payload
282306 )
283307
308+ # providing multiple values is invalid
309+ other_values = values .copy ()
310+ other_values .remove (value )
311+ for other_value in other_values :
312+ with pytest .raises (
313+ ValidationError , match = "You can only provide one of"
314+ ):
315+ ComponentInterfaceValuePostSerializer ._validate_provided_fields (
316+ attrs = {** payload , ** other_value }
317+ )
318+
284319
285320@pytest .mark .django_db
286321def test_civ_post_objects_do_not_exist ():
0 commit comments