Skip to content

Commit

Permalink
feat: add test for course certificates metric fx
Browse files Browse the repository at this point in the history
  • Loading branch information
johanseto committed Sep 12, 2023
1 parent 1d4d71b commit 7eb1c93
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 19 deletions.
16 changes: 9 additions & 7 deletions eox_nelp/stats/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class GeneralTenantStatsView(APIView):
"problem": 49,
"video": 0
},
certiticates: 12
"certiticates": 12
}
```
"""
Expand Down Expand Up @@ -143,16 +143,18 @@ class GeneralCourseStatsView(APIView):
"video": 0
},
"certificates" : {
"verified": {},
"honor": {},
"audit": {},
"verified": {...},
"honor": {...},
"audit": {...},
"professional": {},
"no-id-professional": {
"downloadable": 5,
"not_passing": 4,
"not_passing": 4...
},
"masters": {},
"executive-education": {},
"masters": {...},
"executive-education": {...},
"paid-executive-education": {...},
"paid-bootcamp": {...},
"total": 0
}
}
Expand Down
26 changes: 17 additions & 9 deletions eox_nelp/stats/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,29 +152,37 @@ def get_course_certificates_metric(course_key):
Return:
<Dictionary>: Contains certificates of course metric.
{
"verified": {},
"honor": {},
"verified": {...},
"honor": {...},
"audit": {},
"professional": {},
"no-id-professional": {},
"masters": {},
"executive-education": {},
"professional": {...},
"no-id-professional": {
"downloadable": 5,
"not_passing": 4,
},
"masters": {...},
"executive-education": {...}
"paid-executive-education":{...},
"paid-bootcamp": {...},
"total": 0
}
"""
certificates = {}
course_certificates_qs = GeneratedCertificate.objects.filter(
course_id=course_key,
)
cert_statuses = [cert['status'] for cert in course_certificates_qs.values('status').distinct()]
cert_statuses = [cert["status"] for cert in course_certificates_qs.values("status").distinct()]

for mode in GeneratedCertificate.MODES:
db_mode = mode[0]
human_mode = mode[1]
certificates[human_mode] = {
cert_status: course_certificates_qs.filter(mode=db_mode, status=cert_status).values('user').distinct().count()
cert_status: course_certificates_qs.filter(
mode=db_mode,
status=cert_status,
).values("user").distinct().count()
for cert_status in cert_statuses
}
certificates['total'] = course_certificates_qs.values('user').distinct().count()
certificates["total"] = course_certificates_qs.values("user").distinct().count()

return certificates
82 changes: 79 additions & 3 deletions eox_nelp/stats/tests/tests_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ def setUp(self): # pylint: disable=invalid-name
# this block set the GeneratedCertificates mock and its returned values.

user, _ = User.objects.get_or_create(username="vader")
user2, _ = User.objects.get_or_create(username="vader2")
GeneratedCertificate.objects.get_or_create(**{
'user': user,
'course_id': CourseKey.from_string("course-v1:test+Cx105+2022_T4"),
Expand All @@ -279,12 +280,38 @@ def setUp(self): # pylint: disable=invalid-name
'key': '807e31d92ab6aeaab514d7669bb2b014',
'distinction': False,
'status': 'downloadable',
'mode': 'no-id-professional',
'name': 'Peter Park',
'error_reason': '',
})
GeneratedCertificate.objects.get_or_create(**{
'user': user2,
'course_id': CourseKey.from_string("course-v1:test+Cx105+2022_T4"),
'verify_uuid': 'ddad6d87c5084a3facfd7925b0b2b9a3',
'download_uuid': '',
'download_url': '',
'grade': '59.0',
'key': '807e31d92ab6aeaab514d7669bb2b014',
'distinction': False,
'status': 'not_passing',
'mode': 'honor',
'name': 'Ben Park',
'error_reason': '',
})
GeneratedCertificate.objects.get_or_create(**{
'user': user,
'course_id': CourseKey.from_string("course-v1:test2+Cx105+2022_T4"),
'verify_uuid': 'ddad6d87c5084a3facfd7925b0b2b9a3',
'download_uuid': '',
'download_url': '',
'grade': '90.0',
'key': '807e31d92ab6aeaab514d7669bb2b014',
'distinction': False,
'status': 'downloadable',
'mode': 'honor',
'name': 'Peter Park',
'error_reason': '',
}

)
})

def tearDown(self):
"""Clean cache and restarts mocks"""
Expand Down Expand Up @@ -378,6 +405,55 @@ def test_set_empty_allowed_components(self):

self.assertFalse(course["components"])

def test_right_certificates_metric(self):
"""Based on the initial conditions, this check that the course metrics has the expected certificates value.
Expected behavior:
- Certificates dict is the expected.
"""
cert_dict = {
"verified": {
"downloadable": 0,
"not_passing": 0
},
"honor": {
"downloadable": 0,
"not_passing": 1,
},
"audit": {
"downloadable": 0,
"not_passing": 0
},
"professional": {
"downloadable": 0,
"not_passing": 0
},
"no-id-professional": {
"downloadable": 1,
"not_passing": 0
},
"masters": {
"downloadable": 0,
"not_passing": 0
},
"executive-education": {
"downloadable": 0,
"not_passing": 0
},
"paid-executive-education": {
"downloadable": 0,
"not_passing": 0
},
"paid-bootcamp": {
"downloadable": 0,
"not_passing": 0
},
"total": 2
}
course = get_course_metrics(self.course_key)

self.assertDictEqual(course["certificates"], cert_dict)

@override_settings(STATS_SETTINGS={"API_XBLOCK_TYPES": ["html", "problem", "video"]})
def test_set_allowed_components(self):
"""This modifies the STATS_SETTINGS value in order to test that
Expand Down

0 comments on commit 7eb1c93

Please sign in to comment.