Skip to content

Commit

Permalink
refactor: change relationship expected data to tests
Browse files Browse the repository at this point in the history
This allow to match the expected new fields in the attributes relationship
to the tests.
  • Loading branch information
johanseto committed Jul 6, 2023
1 parent bad7102 commit 45d9551
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 70 deletions.
32 changes: 32 additions & 0 deletions eox_nelp/course_experience/api/v1/tests/mixins_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from rest_framework import status
from rest_framework.test import APIClient

from eox_nelp.course_experience.api.v1.serializers import COURSE_OVERVIEW_EXTRA_ATTRIBUTES, USER_EXTRA_ATTRIBUTES
from eox_nelp.course_experience.api.v1.views import INVALID_KEY_ERROR
from eox_nelp.edxapp_wrapper.course_overviews import CourseOverview

Expand All @@ -35,6 +36,37 @@ def setUp(self): # pylint: disable=invalid-name
self.my_course, _ = CourseOverview.objects.get_or_create(id=BASE_COURSE_ID)
self.client.force_authenticate(self.user)

def make_relationships_data(self):
"""
Make the relationships dict with custom extra attributes based in the attributes of the serializers
withe variables COURSE_OVERVIEW_EXTRA_ATTRIBUTES and USER_EXTRA_ATTRIBUTES
Returns:
dict: relationships dict with the corresponding shape, and key-values.
"""
return {
"author": {
"data": {
"type": "User",
"id": f"{self.user.id}",
"attributes": {
field: getattr(self.user, field) for field in USER_EXTRA_ATTRIBUTES if hasattr(self.user, field)
}
}
},
"course_id": {
"data": {
"type": "CourseOverview",
"id": f"{self.my_course.id}",
"attributes": {
field: getattr(self.user, field)
for field in COURSE_OVERVIEW_EXTRA_ATTRIBUTES
if hasattr(self.user, field)
},
}
},
}

def test_get_object_list_by_user(self):
""" Test a get request to the list endpoint for the desired view.
Expected behavior:
Expand Down
75 changes: 5 additions & 70 deletions eox_nelp/course_experience/api/v1/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,7 @@ def setUp(self):
"status": self.my_unit_like.status,
"item_id": f"{self.my_unit_like.item_id}",
},
"relationships": {
"author": {
"data": {
"type": "User",
"id": f"{self.user.id}",
}
},
"course_id": {
"data": {
"type": "CourseOverview",
"id": f"{self.my_course.id}",
}
}
}
"relationships": self.make_relationships_data()
}
}
self.object_url_kwarg = {self.object_key: BASE_ITEM_ID}
Expand Down Expand Up @@ -117,20 +104,7 @@ def setUp(self):
"reason": f"{self.my_unit_report.reason}",
"item_id": f"{self.my_unit_report.item_id}",
},
"relationships": {
"author": {
"data": {
"type": "User",
"id": f"{self.user.id}",
}
},
"course_id": {
"data": {
"type": "CourseOverview",
"id": f"{self.my_course.id}",
}
}
}
"relationships": self.make_relationships_data()
}
}
self.object_url_kwarg = {self.object_key: BASE_ITEM_ID}
Expand Down Expand Up @@ -190,20 +164,7 @@ def setUp(self):
"username": f"{self.user.username}",
"status": self.my_course_like.status,
},
"relationships": {
"author": {
"data": {
"type": "User",
"id": f"{self.user.id}",
}
},
"course_id": {
"data": {
"type": "CourseOverview",
"id": f"{self.my_course.id}",
}
}
}
"relationships": self.make_relationships_data()
}
}

Expand Down Expand Up @@ -244,20 +205,7 @@ def setUp(self):
"username": f"{self.user.username}",
"reason": f"{self.my_course_report.reason}"
},
"relationships": {
"author": {
"data": {
"type": "User",
"id": f"{self.user.id}"
}
},
"course_id": {
"data": {
"type": "CourseOverview",
"id": f"{self.my_course.id}"
}
}
}
"relationships": self.make_relationships_data()
}
}

Expand Down Expand Up @@ -315,20 +263,7 @@ def setUp(self):
"public": self.my_course_feedback.public,
"recommended": self.my_course_feedback.recommended,
},
"relationships": {
"author": {
"data": {
"type": "User",
"id": f"{self.user.id}"
}
},
"course_id": {
"data": {
"type": "CourseOverview",
"id": f"{self.my_course.id}"
}
}
}
"relationships": self.make_relationships_data()
}
}

Expand Down

0 comments on commit 45d9551

Please sign in to comment.