Skip to content

Commit 8349b5b

Browse files
committed
Make IdentificationTask.result null if source is null
1 parent 873273e commit 8349b5b

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

api/serializers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,7 @@ class IdentificationTaskResultSerializer(serializers.ModelSerializer):
11711171
confidence = serializers.FloatField(min_value=0, max_value=1, read_only=True)
11721172
confidence_label = serializers.SerializerMethodField()
11731173
is_high_confidence = serializers.SerializerMethodField()
1174-
source = serializers.ChoiceField(source='result_source',choices=IdentificationTask.ResultSource.choices, allow_null=True)
1174+
source = serializers.ChoiceField(source='result_source',choices=IdentificationTask.ResultSource.choices)
11751175

11761176
def get_confidence_label(self, obj) -> str:
11771177
return obj.confidence_label
@@ -1180,7 +1180,7 @@ def get_is_high_confidence(self, obj) -> bool:
11801180
return obj.is_high_confidence
11811181

11821182
def to_representation(self, instance):
1183-
if self.allow_null and not instance.is_done:
1183+
if self.allow_null and not instance.result_source:
11841184
return None # Return None or an empty dict as needed
11851185
return super().to_representation(instance)
11861186

@@ -1214,7 +1214,7 @@ class Meta(BaseAssignmentSerializer.Meta):
12141214
observation = SimplifiedObservationWithPhotosSerializer(source='report', read_only=True)
12151215
public_photo = SimplePhotoSerializer(source='photo', required=True)
12161216
review = IdentificationTaskReviewSerializer(source='*', allow_null=True, read_only=True)
1217-
result = IdentificationTaskResultSerializer(source='*', read_only=True)
1217+
result = IdentificationTaskResultSerializer(source='*', read_only=True, allow_null=True)
12181218
assignments = UserAssignmentSerializer(many=True, read_only=True)
12191219

12201220
class Meta:

api/tests/integration/identification_tasks/get.tavern.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ stages:
5858
Authorization: "Bearer {jwt_token_user_can_view:s}"
5959
response:
6060
status_code: 200
61-
json: !force_format_include "{response_data_validation}"
61+
strict:
62+
- json:off
63+
json:
64+
observation:
65+
uuid: "{identification_task.report.pk}"
6266

6367
---
6468

api/tests/integration/identification_tasks/list.tavern.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ includes:
4343
marks:
4444
- usefixtures:
4545
- api_live_url
46+
- annotation
4647
- identification_task
4748
- jwt_token_user_can_view
4849

api/tests/test_views.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,44 @@ class TestObservationAPI(BaseReportTest):
174174
def report_object(self, app_user):
175175
return create_observation_object(user=app_user)
176176

177+
@pytest.mark.django_db
178+
class TestIdentificationTaskAPI:
179+
@classmethod
180+
def build_url(cls, identification_task):
181+
return f"/api/v1/identification-tasks/{identification_task.pk}/"
182+
183+
@pytest.fixture
184+
def permitted_user(self, user):
185+
grant_permission_to_user(type='view', model_class=IdentificationTask, user=user)
186+
Token.objects.create(user=user)
187+
return user
188+
189+
@pytest.fixture
190+
def api_client(self, user):
191+
api_client = APIClient()
192+
api_client.force_login(user=user)
193+
194+
return api_client
195+
196+
@pytest.mark.parametrize(
197+
"result_source, expect_null",
198+
[
199+
(IdentificationTask.ResultSource.AI, False),
200+
(IdentificationTask.ResultSource.EXPERT, False),
201+
(None, True)
202+
]
203+
)
204+
def test_result(self, api_client, permitted_user, identification_task, result_source, expect_null):
205+
identification_task.result_source = result_source
206+
identification_task.save()
207+
208+
response = api_client.get(
209+
self.build_url(identification_task=identification_task),
210+
format='json'
211+
)
212+
assert response.status_code == status.HTTP_200_OK
213+
assert (response.data['result'] == None) == expect_null
214+
177215
@pytest.mark.django_db
178216
class TestIdentificationTaskPredictionAPI:
179217
@classmethod

0 commit comments

Comments
 (0)