def get(request): """ Pass graph data to frontend. """ timeline = InstallationStatistics.timeline() students, courses, instances = InstallationStatistics.data_per_period() instances_count, courses_count, students_count = InstallationStatistics.overall_counts( ) first_datetime_of_update_data, last_datetime_of_update_data = get_data_created_datetime_scope( ) context = { 'timeline': json.dumps(timeline), 'students': json.dumps(students), 'courses': json.dumps(courses), 'instances': json.dumps(instances), 'instances_count': instances_count, 'courses_count': courses_count, 'students_count': students_count, 'first_datetime_of_update_data': first_datetime_of_update_data, 'last_datetime_of_update_data': last_datetime_of_update_data } return render(request, 'charts/graphs.html', context)
def get(self, request): """ Pass graph data to frontend. """ first_datetime_of_update_data, last_datetime_of_update_data = get_data_created_datetime_scope( ) datamap_format_countries_list, tabular_format_countries_list = \ InstallationStatistics().get_students_per_country() countries_amount = InstallationStatistics( ).get_students_countries_amount() context = { 'datamap_countries_list': json.dumps(datamap_format_countries_list), 'tabular_countries_list': tabular_format_countries_list, 'top_country': self.get_statistics_top_country(tabular_format_countries_list), 'countries_amount': countries_amount, 'first_datetime_of_update_data': first_datetime_of_update_data, 'last_datetime_of_update_data': last_datetime_of_update_data } return render(request, 'charts/worldmap.html', context)
def test_data_per_period(self): """ Verify that data_per_period method annotates by day with trunc and then sums statistics amounts. """ result = InstallationStatistics.data_per_period() self.assertEqual( ([10, 10, 10, 5, 10], [2, 2, 2, 1, 2], [2, 2, 2, 1, 2]), result )
def test_timeline(self): """ Verify that timeline method returns unique existing datetime sorted in descending order. """ result = InstallationStatistics.timeline() self.assertEqual( ['2017-06-01', '2017-06-02', '2017-06-03', '2017-06-04', '2017-06-05'], result )
def test_student_percentage(self, country_count_in_statistics, all_active_students_in_statistics, expected_result): """ Verify that get_student_amount_percentage method returns correct value. """ result = InstallationStatistics.get_student_amount_percentage( country_count_in_statistics, all_active_students_in_statistics ) self.assertEqual(expected_result, result)
def test_overall_counts(self, mock_get_last_calendar_day): """ Verify that overall_counts method returns overall statistics instance counts for previous calendar day. """ mock_get_last_calendar_day.return_value = date(2017, 6, 1), date(2017, 6, 2) result = InstallationStatistics.overall_counts() self.assertEqual( (2, 2, 10), result )
def test_students_per_country_render(self, mock_get_last_calendar_day): """ Verify that get_students_per_country method returns data to render correct values. """ mock_get_last_calendar_day.return_value = date(2017, 6, 1), date(2017, 6, 2) datamap_format_countries_list, tabular_format_countries_list = self.create_expected_default_data() result = InstallationStatistics.get_students_per_country() self.assertEqual( (datamap_format_countries_list, tabular_format_countries_list), result )
def test_students_per_country_as_dict(self, mock_get_last_calendar_day): """ Verify that get_students_per_country_stats method returns correct accordance as dict. """ mock_get_last_calendar_day.return_value = date(2017, 6, 1), date(2017, 6, 2) result = InstallationStatistics.get_students_per_country_stats() country_count_accordance_for_previous_calendar_day = { 'RU': 5264, 'CA': 37086, 'UA': 4022, 'null': 2 } self.assertDictEqual(country_count_accordance_for_previous_calendar_day, result)
def test_datamap_and_tabular_lists(self): """ Verify that view gets datamap and tabular lists with corresponding model method. Model method is create_students_per_country. """ worlds_students_per_country = { 'RU': 5264, 'CA': 37086, 'UA': 4022, 'null': 2 } datamap_format_countries_list, tabular_format_countries_list = self.create_expected_default_data() result = InstallationStatistics.create_students_per_country(worlds_students_per_country) self.assertEqual( (datamap_format_countries_list, tabular_format_countries_list), result )