Skip to content

Commit

Permalink
fix: Updated Learning Assistant History payload to return in asc order
Browse files Browse the repository at this point in the history
  • Loading branch information
rijuma committed Nov 8, 2024
1 parent 9153aa1 commit a430ccb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Change Log
Unreleased
**********
* Updated Learning Assistant History payload to return in ascending order

4.4.4 - 2024-11-06
******************
Expand Down
2 changes: 1 addition & 1 deletion learning_assistant/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,6 @@ def get(self, request, course_run_id):
user = request.user

message_count = int(request.GET.get('message_count', 50))
message_history = get_message_history(courserun_key, user, message_count)
message_history = reversed(list(get_message_history(courserun_key, user, message_count))) # Reversing order
data = MessageSerializer(message_history, many=True).data
return Response(status=http_status.HTTP_200_OK, data=data)
30 changes: 21 additions & 9 deletions tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Tests for the learning assistant views.
"""
import datetime
import json
import sys
from importlib import import_module
Expand Down Expand Up @@ -347,22 +348,33 @@ def test_learning_message_history_view_get(
course_id=self.course_id,
user=self.user,
role='staff',
content='Expected content of message',
content='Older message',
created=datetime.date(2024, 10, 1)
)
message_count = 1

LearningAssistantMessage.objects.create(
course_id=self.course_id,
user=self.user,
role='staff',
content='Newer message',
created=datetime.date(2024, 10, 3)
)

db_messages = LearningAssistantMessage.objects.all().order_by('created')
db_messages_count = len(db_messages)

mock_get_course_id.return_value = self.course_id
response = self.client.get(
reverse('message-history', kwargs={'course_run_id': self.course_id})+f'?message_count={message_count}',
reverse('message-history', kwargs={'course_run_id': self.course_id})+f'?message_count={db_messages_count}',
content_type='application/json'
)
data = response.data

# Ensure same number of entries
actual_message_count = LearningAssistantMessage.objects.count()
self.assertEqual(len(data), actual_message_count)
self.assertEqual(len(data), db_messages_count)

# Ensure values are as expected
actual_message = LearningAssistantMessage.objects.get(course_id=self.course_id)
self.assertEqual(data[0]['role'], actual_message.role)
self.assertEqual(data[0]['content'], actual_message.content)
self.assertEqual(data[0]['timestamp'], actual_message.created.isoformat())
for i, expected in enumerate(db_messages):
self.assertEqual(expected.role, data[i]['role'])
self.assertEqual(expected.content, data[i]['content'])
self.assertEqual(expected.created.isoformat(), data[i]['timestamp'])

0 comments on commit a430ccb

Please sign in to comment.