Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aaq urgency check endpoint V2 #616

Merged
merged 9 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions aaq/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ class ContentFeedbackSerializer(serializers.Serializer):
feedback_text = serializers.CharField(required=False)
query_id = serializers.IntegerField(required=True)
content_id = serializers.IntegerField(required=True)


class UrgencyCheckV2Serializer(serializers.Serializer):
message_text = serializers.CharField(required=True)
29 changes: 29 additions & 0 deletions aaq/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,32 @@ def post_search_return_empty(self, request):
}

return (200, {}, json.dumps(resp_body))


class FakeAaqUdV2Api:
def post_urgency_detect_return_true(self, request):
resp_body = {
"details": {
"0": {"distance": 0.1, "urgency_rule": "Blurry vision and dizziness"},
"1": {"distance": 0.2, "urgency_rule": "Nausea that lasts for 3 days"},
},
"is_urgent": True,
"matched_rules": [
"Blurry vision and dizziness",
"Nausea that lasts for 3 days",
],
}

return (200, {}, json.dumps(resp_body))

def post_urgency_detect_return_false(self, request):
resp_body = {
"details": {
"0": {"distance": 0.1, "urgency_rule": "Baby okay"},
"1": {"distance": 0.2, "urgency_rule": "Baby healthy"},
},
"is_urgent": False,
"matched_rules": ["Baby okay", "Baby healthy"],
}

return (200, {}, json.dumps(resp_body))
91 changes: 90 additions & 1 deletion aaq/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from rest_framework import status
from rest_framework.test import APITestCase

from .helpers import FakeAaqApi, FakeAaqCoreApi, FakeAaqUdApi, FakeTask
from .helpers import FakeAaqApi, FakeAaqCoreApi, FakeAaqUdApi, FakeAaqUdV2Api, FakeTask


class GetFirstPageViewTests(APITestCase):
Expand Down Expand Up @@ -376,12 +376,14 @@ def test_search(self):
response = self.client.post(
self.url, data=payload, content_type="application/json"
)
[request] = responses.calls

self.assertEqual(response.status_code, 200)
self.assertIn("message", response.data)
self.assertIn("body", response.data)
self.assertIn("query_id", response.data)
self.assertIn("feedback_secret_key", response.data)
self.assertEqual(json.loads(request.request.body), json.loads(payload))

assert response.json() == {
"message": "*0* - Example content title\n*1* -"
Expand Down Expand Up @@ -527,3 +529,90 @@ def test_response_feedback_invalid_feedback_text_view(self):
assert response.json() == {
"feedback_sentiment": ['"test" is not a valid choice.']
}


class CheckUrgencyV2ViewTests(APITestCase):
url = reverse("aaq-check-urgency-v2")

@responses.activate
def test_urgency_check_urgent(self):
"""
Test that we can get is_urgent True
"""
user = get_user_model().objects.create_user("test")
self.client.force_authenticate(user)
fakeAaqUdV2Api = FakeAaqUdV2Api()
responses.add_callback(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you validate the request payload that is sent to the AAQ endpoint please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have this test test_urgency_check_invalid to check if the payload is valid

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is the payload to our endpoint, I'm talking about the payload e send to the aaq endpoint

responses.POST,
"http://aaq_v2/urgency-check-v2",
callback=fakeAaqUdV2Api.post_urgency_detect_return_true,
content_type="application/json",
)

payload = json.dumps({"message_text": "I am pregnant and out of breath"})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you remove the .dumps here, you can remove the 2 .loads() calls on line 571


response = self.client.post(
self.url, data=payload, content_type="application/json"
)
[request] = responses.calls

assert response.status_code == 200
assert response.json() == {
"details": {
"0": {"distance": 0.1, "urgency_rule": "Blurry vision and dizziness"},
"1": {"distance": 0.2, "urgency_rule": "Nausea that lasts for 3 days"},
},
"is_urgent": True,
"matched_rules": [
"Blurry vision and dizziness",
"Nausea that lasts for 3 days",
],
}
self.assertEqual(json.loads(request.request.body), json.loads(payload))

@responses.activate
def test_urgency_check_not_urgent(self):
"""
Test that we can get is_urgent False
"""
user = get_user_model().objects.create_user("test")
self.client.force_authenticate(user)
fakeAaqUdV2Api = FakeAaqUdV2Api()
responses.add_callback(
responses.POST,
"http://aaq_v2/urgency-check-v2",
callback=fakeAaqUdV2Api.post_urgency_detect_return_false,
content_type="application/json",
)

payload = json.dumps({"message_text": "I am fine"})

response = self.client.post(
self.url, data=payload, content_type="application/json"
)

assert response.status_code == 200
assert response.json() == {
"details": {
"0": {"distance": 0.1, "urgency_rule": "Baby okay"},
"1": {"distance": 0.2, "urgency_rule": "Baby healthy"},
},
"is_urgent": False,
"matched_rules": ["Baby okay", "Baby healthy"],
}

@responses.activate
def test_urgency_check_invalid(self):
"""
Test that we can get a field required message
"""
user = get_user_model().objects.create_user("test")
self.client.force_authenticate(user)
payload = json.dumps({})

response = self.client.post(
self.url, data=payload, content_type="application/json"
)

assert response.status_code == 400
assert response.json() == {"message_text": ["This field is required."]}
5 changes: 5 additions & 0 deletions aaq/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@
views.search,
name="aaq-search",
),
re_path(
r"^api/v2/check-urgency",
views.check_urgency_v2,
name="aaq-check-urgency-v2",
),
]
19 changes: 19 additions & 0 deletions aaq/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ResponseFeedbackSerializer,
SearchSerializer,
UrgencyCheckSerializer,
UrgencyCheckV2Serializer,
)

from .tasks import send_feedback_task, send_feedback_task_v2
Expand Down Expand Up @@ -222,3 +223,21 @@ def search(request, *args, **kwargs):
}

return Response(json_msg, status=status.HTTP_200_OK)


@api_view(("POST",))
@renderer_classes((JSONRenderer,))
def check_urgency_v2(request, *args, **kwargs):
serializer = UrgencyCheckV2Serializer(data=request.data)
serializer.is_valid(raise_exception=True)
message_text = {}
message_text["message_text"] = serializer.validated_data["message_text"]
url = urllib.parse.urljoin(settings.AAQ_V2_API_URL, "/urgency-check-v2")
headers = {
"Authorization": settings.AAQ_V2_API_URL,
"Content-Type": "application/json",
}

response = requests.request("POST", url, json=message_text, headers=headers)

return Response(response.json(), status=status.HTTP_200_OK)
Loading