def get_context_data(self, **kwargs): context = super(EnrollmentGeographyView, self).get_context_data(**kwargs) presenter = CourseEnrollmentPresenter(self.course_id) data = None last_updated = None try: summary, data = presenter.get_geography_data() last_updated = summary['last_updated'] # Add summary data (e.g. num countries, top 3 countries) directly to the context context.update(summary) except NotFoundError: logger.error( "Failed to retrieve enrollment geography data for %s.", self.course_id) context['js_data']['course']['enrollmentByCountry'] = data context.update( {'update_message': self.get_last_updated_message(last_updated)}) context['page_data'] = self.get_page_data(context) return context
def get_context_data(self, **kwargs): context = super(EnrollmentActivityView, self).get_context_data(**kwargs) presenter = CourseEnrollmentPresenter(self.course_id) summary = None trend = None last_updated = None try: summary, trend = presenter.get_summary_and_trend_data() last_updated = summary['last_updated'] except NotFoundError: logger.error("Failed to retrieve enrollment activity data for %s.", self.course_id) # add the enrollment data for the page context['js_data']['course']['enrollmentTrends'] = trend context.update({ 'summary': summary, 'update_message': self.get_last_updated_message(last_updated) }) context['page_data'] = self.get_page_data(context) return context
class CourseEnrollmentPresenterTests(TestCase): def setUp(self): self.course_id = 'edX/DemoX/Demo_Course' self.presenter = CourseEnrollmentPresenter(self.course_id) @mock.patch('analyticsclient.course.Course.enrollment', mock.Mock(return_value=[])) def test_get_trend_summary_no_data(self): actual_summary, actual_trend = self.presenter.get_summary_and_trend_data() expected_summary = { 'last_updated': None, 'current_enrollment': None, 'enrollment_change_last_7_days': None, } self.assertDictEqual(actual_summary, expected_summary) self.assertEqual(actual_trend, []) @mock.patch('analyticsclient.course.Course.enrollment') def test_get_summary_and_trend_data(self, mock_enrollment): expected_trend = get_mock_enrollment_data(self.course_id) mock_enrollment.return_value = expected_trend actual_summary, actual_trend = self.presenter.get_summary_and_trend_data() self.assertDictEqual(actual_summary, get_mock_enrollment_summary()) self.assertListEqual(actual_trend, expected_trend) @mock.patch('analyticsclient.course.Course.enrollment') def test_get_summary_and_trend_data_small(self, mock_enrollment): api_trend = [get_mock_enrollment_data(self.course_id)[-1]] mock_enrollment.return_value = api_trend actual_summary, actual_trend = self.presenter.get_summary_and_trend_data() self.assertDictEqual(actual_summary, get_mock_presenter_enrollment_summary_small()) self.assertListEqual(actual_trend, get_mock_presenter_enrollment_data_small(self.course_id)) @mock.patch('analyticsclient.course.Course.enrollment') def test_get_geography_data(self, mock_enrollment): # test with a full set of countries mock_data = get_mock_api_enrollment_geography_data(self.course_id) mock_enrollment.return_value = mock_data expected_summary, expected_data = get_mock_presenter_enrollment_geography_data() summary, actual_data = self.presenter.get_geography_data() self.assertDictEqual(summary, expected_summary) self.assertListEqual(actual_data, expected_data) # test with a small set of countries mock_data = get_mock_api_enrollment_geography_data_limited(self.course_id) mock_enrollment.return_value = mock_data expected_summary, expected_data = get_mock_presenter_enrollment_geography_data_limited() summary, actual_data = self.presenter.get_geography_data() self.assertDictEqual(summary, expected_summary) self.assertListEqual(actual_data, expected_data)
def get_context_data(self, **kwargs): context = super(EnrollmentActivityView, self).get_context_data(**kwargs) presenter = CourseEnrollmentPresenter(self.course_id) summary, trend = presenter.get_summary_and_trend_data() last_updated = summary['last_updated'] # add the enrollment data for the page context['js_data']['course']['enrollmentTrends'] = trend # Translators: Do not translate UTC. update_message = _('Enrollment activity data was last updated %(update_date)s at %(update_time)s UTC.') context.update({ 'page_data': json.dumps(context['js_data']), 'summary': summary, 'update_message': update_message % self.format_last_updated_date_and_time(last_updated) }) return context
def get_context_data(self, **kwargs): context = super(EnrollmentGeographyView, self).get_context_data(**kwargs) presenter = CourseEnrollmentPresenter(self.course_id) summary, data = presenter.get_geography_data() last_updated = summary['last_updated'] # Add summary data (e.g. num countries, top 3 countries) directly to the context context.update(summary) context['js_data']['course']['enrollmentByCountry'] = data # Translators: Do not translate UTC. update_message = _('Geographic student data was last updated %(update_date)s at %(update_time)s UTC.') context.update({ 'page_data': json.dumps(context['js_data']), 'update_message': update_message % self.format_last_updated_date_and_time(last_updated) }) return context
def get_context_data(self, **kwargs): context = super(EnrollmentGeographyView, self).get_context_data(**kwargs) presenter = CourseEnrollmentPresenter(self.course_id) data = None last_updated = None try: summary, data = presenter.get_geography_data() last_updated = summary['last_updated'] # Add summary data (e.g. num countries, top 3 countries) directly to the context context.update(summary) except NotFoundError: logger.error("Failed to retrieve enrollment geography data for %s.", self.course_id) context['js_data']['course']['enrollmentByCountry'] = data context.update({ 'page_data': json.dumps(context['js_data']), 'update_message': self.get_last_updated_message(last_updated) }) return context
def get_context_data(self, **kwargs): context = super(EnrollmentActivityView, self).get_context_data(**kwargs) presenter = CourseEnrollmentPresenter(self.course_id) summary = None trend = None last_updated = None try: summary, trend = presenter.get_summary_and_trend_data() last_updated = summary['last_updated'] except NotFoundError: logger.error("Failed to retrieve enrollment activity data for %s.", self.course_id) # add the enrollment data for the page context['js_data']['course']['enrollmentTrends'] = trend context.update({ 'page_data': json.dumps(context['js_data']), 'summary': summary, 'update_message': self.get_last_updated_message(last_updated) }) return context
def setUp(self): self.course_id = 'edX/DemoX/Demo_Course' self.presenter = CourseEnrollmentPresenter(self.course_id)
class CourseEnrollmentPresenterTests(SwitchMixin, TestCase): @classmethod def setUpClass(cls): cls.toggle_switch('display_verified_enrollment', True) def setUp(self): self.course_id = 'edX/DemoX/Demo_Course' self.presenter = CourseEnrollmentPresenter(self.course_id) @mock.patch('analyticsclient.course.Course.enrollment', mock.Mock(return_value=[])) def test_get_trend_summary_no_data(self): actual_summary, actual_trend = self.presenter.get_summary_and_trend_data() expected_summary = { 'last_updated': None, 'current_enrollment': None, 'enrollment_change_last_7_days': None, } self.assertDictEqual(actual_summary, expected_summary) self.assertEqual(actual_trend, []) @mock.patch('analyticsclient.course.Course.enrollment') def test_get_summary_and_trend_data(self, mock_enrollment): mock_enrollment.return_value = utils.get_mock_api_enrollment_data(self.course_id) actual_summary, actual_trend = self.presenter.get_summary_and_trend_data() self.assertDictEqual(actual_summary, utils.get_mock_enrollment_summary()) expected_trend = utils.get_mock_presenter_enrollment_trend(self.course_id) self.assertListEqual(actual_trend, expected_trend) @mock.patch('analyticsclient.course.Course.enrollment') def test_get_summary_and_trend_data_with_gaps(self, mock_enrollment): """ If the API returns data with gaps, get_summary_and_trend_data should fill in those gaps with data from the previous day. """ gaps = utils.get_mock_api_enrollment_data_with_gaps(self.course_id) mock_enrollment.return_value = gaps actual_summary, actual_trend = self.presenter.get_summary_and_trend_data() self.assertDictEqual(actual_summary, utils.get_mock_enrollment_summary()) expected_trend = utils.get_mock_presenter_enrollment_trend_with_gaps_filled(self.course_id) self.assertListEqual(actual_trend, expected_trend) @mock.patch('analyticsclient.course.Course.enrollment') def test_get_summary_and_trend_data_small(self, mock_enrollment): """ Verify the presenter responds appropriately when the course has a limited amount of data (e.g. one data point). """ mock_enrollment.return_value = utils.get_mock_api_enrollment_data(self.course_id)[-1:] actual_summary, actual_trend = self.presenter.get_summary_and_trend_data() self.assertDictEqual(actual_summary, utils.get_mock_presenter_enrollment_summary_small()) self.assertListEqual(actual_trend, utils.get_mock_presenter_enrollment_data_small(self.course_id)) @mock.patch('analyticsclient.course.Course.enrollment') def test_get_geography_data(self, mock_enrollment): mock_enrollment.return_value = utils.get_mock_api_enrollment_geography_data(self.course_id) expected_summary, expected_data = utils.get_mock_presenter_enrollment_geography_data() summary, actual_data = self.presenter.get_geography_data() self.assertDictEqual(summary, expected_summary) self.assertListEqual(actual_data, expected_data) # test with a small set of countries mock_data = utils.get_mock_api_enrollment_geography_data_limited(self.course_id) mock_enrollment.return_value = mock_data expected_summary, expected_data = utils.get_mock_presenter_enrollment_geography_data_limited() summary, actual_data = self.presenter.get_geography_data() self.assertDictEqual(summary, expected_summary) self.assertListEqual(actual_data, expected_data) @mock.patch('analyticsclient.course.Course.enrollment') def test_hide_empty_enrollment_modes(self, mock_enrollment): """ Enrollment modes with no enrolled students should not be returned. """ mock_enrollment.return_value = utils.get_mock_api_enrollment_data(self.course_id, include_verified=False) actual_summary, actual_trend = self.presenter.get_summary_and_trend_data() self.assertDictEqual(actual_summary, utils.get_mock_enrollment_summary(include_verified=False)) expected_trend = utils.get_mock_presenter_enrollment_trend(self.course_id, include_verified=False) self.assertListEqual(actual_trend, expected_trend)
class CourseEnrollmentPresenterTests(TestCase): def setUp(self): self.course_id = 'edX/DemoX/Demo_Course' self.presenter = CourseEnrollmentPresenter(self.course_id) @mock.patch('analyticsclient.course.Course.enrollment', mock.Mock(return_value=[])) def test_get_trend_summary_no_data(self): actual_summary, actual_trend = self.presenter.get_summary_and_trend_data( ) expected_summary = { 'last_updated': None, 'current_enrollment': None, 'enrollment_change_last_7_days': None, } self.assertDictEqual(actual_summary, expected_summary) self.assertEqual(actual_trend, []) @mock.patch('analyticsclient.course.Course.enrollment') def test_get_summary_and_trend_data(self, mock_enrollment): expected_trend = get_mock_enrollment_data(self.course_id) mock_enrollment.return_value = expected_trend actual_summary, actual_trend = self.presenter.get_summary_and_trend_data( ) self.assertDictEqual(actual_summary, get_mock_enrollment_summary()) self.assertListEqual(actual_trend, expected_trend) @mock.patch('analyticsclient.course.Course.enrollment') def test_get_summary_and_trend_data_small(self, mock_enrollment): api_trend = [get_mock_enrollment_data(self.course_id)[-1]] mock_enrollment.return_value = api_trend actual_summary, actual_trend = self.presenter.get_summary_and_trend_data( ) self.assertDictEqual(actual_summary, get_mock_presenter_enrollment_summary_small()) self.assertListEqual( actual_trend, get_mock_presenter_enrollment_data_small(self.course_id)) @mock.patch('analyticsclient.course.Course.enrollment') def test_get_geography_data(self, mock_enrollment): # test with a full set of countries mock_data = get_mock_api_enrollment_geography_data(self.course_id) mock_enrollment.return_value = mock_data expected_summary, expected_data = get_mock_presenter_enrollment_geography_data( ) summary, actual_data = self.presenter.get_geography_data() self.assertDictEqual(summary, expected_summary) self.assertListEqual(actual_data, expected_data) # test with a small set of countries mock_data = get_mock_api_enrollment_geography_data_limited( self.course_id) mock_enrollment.return_value = mock_data expected_summary, expected_data = get_mock_presenter_enrollment_geography_data_limited( ) summary, actual_data = self.presenter.get_geography_data() self.assertDictEqual(summary, expected_summary) self.assertListEqual(actual_data, expected_data)
class CourseEnrollmentPresenterTests(SwitchMixin, TestCase): @classmethod def setUpClass(cls): cls.toggle_switch('display_verified_enrollment', True) def setUp(self): self.course_id = 'edX/DemoX/Demo_Course' self.presenter = CourseEnrollmentPresenter(self.course_id) @mock.patch('analyticsclient.course.Course.enrollment', mock.Mock(return_value=[])) def test_get_trend_summary_no_data(self): actual_summary, actual_trend = self.presenter.get_summary_and_trend_data() expected_summary = { 'last_updated': None, 'current_enrollment': None, 'enrollment_change_last_7_days': None, 'verified_enrollment': None, 'verified_change_last_7_days': None, } self.assertDictEqual(actual_summary, expected_summary) self.assertEqual(actual_trend, []) @mock.patch('analyticsclient.course.Course.enrollment') def test_get_summary_and_trend_data(self, mock_enrollment): mock_enrollment.return_value = utils.get_mock_api_enrollment_data(self.course_id) actual_summary, actual_trend = self.presenter.get_summary_and_trend_data() self.assertDictEqual(actual_summary, utils.get_mock_enrollment_summary()) expected_trend = utils.get_mock_presenter_enrollment_trend(self.course_id) self.assertListEqual(actual_trend, expected_trend) @mock.patch('analyticsclient.course.Course.enrollment') def test_get_summary_and_trend_data_with_gaps(self, mock_enrollment): """ If the API returns data with gaps, get_summary_and_trend_data should fill in those gaps with data from the previous day. """ gaps = utils.get_mock_api_enrollment_data_with_gaps(self.course_id) mock_enrollment.return_value = gaps actual_summary, actual_trend = self.presenter.get_summary_and_trend_data() self.assertDictEqual(actual_summary, utils.get_mock_enrollment_summary()) expected_trend = utils.get_mock_presenter_enrollment_trend_with_gaps_filled(self.course_id) self.assertListEqual(actual_trend, expected_trend) @mock.patch('analyticsclient.course.Course.enrollment') def test_get_summary_and_trend_data_small(self, mock_enrollment): """ Verify the presenter responds appropriately when the course has a limited amount of data (e.g. one data point). """ mock_enrollment.return_value = utils.get_mock_api_enrollment_data(self.course_id)[-1:] actual_summary, actual_trend = self.presenter.get_summary_and_trend_data() self.assertDictEqual(actual_summary, utils.get_mock_presenter_enrollment_summary_small()) self.assertListEqual(actual_trend, utils.get_mock_presenter_enrollment_data_small(self.course_id)) @mock.patch('analyticsclient.course.Course.enrollment') def test_get_geography_data(self, mock_enrollment): mock_enrollment.return_value = utils.get_mock_api_enrollment_geography_data(self.course_id) expected_summary, expected_data = utils.get_mock_presenter_enrollment_geography_data() summary, actual_data = self.presenter.get_geography_data() self.assertDictEqual(summary, expected_summary) self.assertListEqual(actual_data, expected_data) # test with a small set of countries mock_data = utils.get_mock_api_enrollment_geography_data_limited(self.course_id) mock_enrollment.return_value = mock_data expected_summary, expected_data = utils.get_mock_presenter_enrollment_geography_data_limited() summary, actual_data = self.presenter.get_geography_data() self.assertDictEqual(summary, expected_summary) self.assertListEqual(actual_data, expected_data)