Skip to content

Commit

Permalink
feat: display last 3 achievements (anitab-org#1134)
Browse files Browse the repository at this point in the history
  • Loading branch information
diananova authored Jul 22, 2021
1 parent 8a7e6f1 commit 4a15a3f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
6 changes: 3 additions & 3 deletions app/api/dao/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,9 @@ def get_user_statistics(user_id: int):

achievements = UserDAO.get_achievements(user_id)
if achievements:
# We only need the first three of these achievements
achievements = achievements[0:3]
sorted(achievements, key=itemgetter("created_at"))
# We only need the last three of these achievements
achievements = achievements[-3:]
achievements.sort(key=itemgetter("completed_at"), reverse=True)

response = {
"name": user.name,
Expand Down
73 changes: 72 additions & 1 deletion tests/users/test_api_home_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def test_cancelled_relations(self):
self.assertEqual(HTTPStatus.OK, actual_response.status_code)
self.assertEqual(expected_response, json.loads(actual_response.data))

def test_achievements(self):
def test_achievements_incomplete(self):
start_date = datetime.utcnow()
end_date = start_date + timedelta(weeks=4)
start_date = start_date.timestamp()
Expand Down Expand Up @@ -285,3 +285,74 @@ def test_achievements(self):
)
self.assertEqual(HTTPStatus.OK, actual_response.status_code)
self.assertEqual(expected_response, json.loads(actual_response.data))

def test_achievements_last_three(self):
start_date = datetime.utcnow()
end_date = start_date + timedelta(weeks=4)
start_date = start_date.timestamp()
end_date = end_date.timestamp()

tasks_list = TasksListModel()
task_created_time = datetime.utcnow().timestamp()
task_completed_time = task_created_time + 100
for i in range(1, 5):
tasks_list.add_task(
description="Test task " + str(i),
created_at=task_created_time,
is_done=True,
completed_at=task_completed_time + (i * 60),
)
db.session.add(tasks_list)
db.session.commit()

mentorship_relation = MentorshipRelationModel(
action_user_id=self.user2.id,
mentor_user=self.user1,
mentee_user=self.user2,
creation_date=start_date,
end_date=end_date,
state=MentorshipRelationState.ACCEPTED,
notes="",
tasks_list=tasks_list,
)

db.session.add(mentorship_relation)
db.session.commit()

expected_response = {
"name": "User1",
"pending_requests": 0,
"accepted_requests": 1,
"rejected_requests": 0,
"completed_relations": 0,
"cancelled_relations": 0,
"achievements": [
{
"completed_at": task_completed_time + 4 * 60,
"created_at": task_created_time,
"description": "Test task 4",
"id": 4,
"is_done": True,
},
{
"completed_at": task_completed_time + 3 * 60,
"created_at": task_created_time,
"description": "Test task 3",
"id": 3,
"is_done": True,
},
{
"completed_at": task_completed_time + 2 * 60,
"created_at": task_created_time,
"description": "Test task 2",
"id": 2,
"is_done": True,
},
],
}
auth_header = get_test_request_header(self.user1.id)
actual_response = self.client.get(
"/home", follow_redirects=True, headers=auth_header
)
self.assertEqual(HTTPStatus.OK, actual_response.status_code)
self.assertEqual(expected_response, json.loads(actual_response.data))

0 comments on commit 4a15a3f

Please sign in to comment.