From 732df1de46ad6b0ae7f12931b95cb1954d358167 Mon Sep 17 00:00:00 2001 From: Johan Castiblanco Date: Tue, 12 Sep 2023 12:17:40 -0500 Subject: [PATCH] test: add test for view including total certs --- eox_nelp/stats/tests/api/v1/tests_views.py | 46 +++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/eox_nelp/stats/tests/api/v1/tests_views.py b/eox_nelp/stats/tests/api/v1/tests_views.py index 4958e69b..b2eb23ae 100644 --- a/eox_nelp/stats/tests/api/v1/tests_views.py +++ b/eox_nelp/stats/tests/api/v1/tests_views.py @@ -88,6 +88,49 @@ def test_total_components(self, mock_metrics): self.assertEqual(expected_components, response.data["components"]) mock_metrics.get_courses_metrics.assert_called_once_with("testserver") + @override_settings( + MIDDLEWARE=["eox_tenant.middleware.CurrentSiteMiddleware"], + ) + @patch("eox_nelp.stats.api.v1.views.metrics") + def test_total_certificates(self, mock_metrics): + """ + Test that the view will calculate the total of certificates based on the metrics values + + Expected behavior: + - Status code 200. + - Components total values are the expected. + - get_courses_metrics is called once. + """ + total_courses = 4 + fake_metric = { + "certificates": { + "verified": {"downloadable": 0, "not_passing": 0}, + "honor": {"downloadable": 0, "not_passing": 0}, + "audit": {"downloadable": 0, "not_passing": 0}, + "professional": {"downloadable": 0, "not_passing": 0}, + "no-id-professional": {"downloadable": 5, "not_passing": 4}, + "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": 9, + } + } + mock_metrics.get_learners_metric.return_value = 5 + mock_metrics.get_instructors_metric.return_value = 4875 + mock_metrics.get_courses_metrics.return_value = { + "total_courses": total_courses, + "metrics": [fake_metric for c in range(total_courses)], + } + expected_certificates = fake_metric["certificates"]["total"] * total_courses + url_endpoint = reverse("stats-api:v1:general-stats") + + response = self.client.get(url_endpoint) + + self.assertEqual(status.HTTP_200_OK, response.status_code) + self.assertEqual(expected_certificates, response.data["certificates"]) + mock_metrics.get_courses_metrics.assert_called_once_with("testserver") + @override_settings(MIDDLEWARE=["eox_tenant.middleware.CurrentSiteMiddleware"]) @data("post", "put", "patch", "delete") def test_invalid_method(self, method): @@ -176,7 +219,8 @@ def test_get_detail(self, mock_metrics): "openassessment": 0, "problem": 49, "video": 0 - } + }, + "certificates": 12 } url_endpoint = reverse("stats-api:v1:course-stats", args=[course_id])