class NationalTreemapExpenditureByDepartmentTestCase(TestCase): """ Unit tests for the treemap expenditure by department function. """ def setUp(self): self.mock_data = TREEMAP_MOCK_DATA year = FinancialYear.objects.create(slug="2019-20") sphere = Sphere.objects.create(financial_year=year, name="national") government = Government.objects.create(sphere=sphere, name="A government") self.department = Department( government=government, name="Fake", vote_number=1, is_vote_primary=True, intro="", ) self.mock_openspending_api = Mock() self.mock_openspending_api.get_adjustment_kind_ref = Mock( return_value="adjustment_kind_ref") self.mock_openspending_api.get_phase_ref = Mock( return_value="budget_phase.budget_phase") self.mock_openspending_api.get_programme_name_ref = Mock( return_value="programme_number.programme") self.mock_openspending_api.get_department_name_ref = Mock( return_value="vote_number.department") self.mock_openspending_api.get_financial_year_ref = Mock( return_value="financial_year.financial_year") self.mock_openspending_api.aggregate = Mock( return_value={"cells": [{ "value.sum": 1, "_count": 0 }]}) self.mock_openspending_api.filter_by_ref_exclusion = Mock self.mock_openspending_api.aggregate_url = Mock self.mock_dataset = Mock() self.mock_dataset.get_openspending_api = Mock( return_value=self.mock_openspending_api) vote_number = 1 for mock_object in self.mock_data["complete"]: Department.objects.create( government=government, is_vote_primary=True, name=mock_object["vote_number.department"], vote_number=vote_number, ) vote_number += 1 @mock.patch( "budgetportal.models.Department.get_all_budget_totals_by_year_and_phase", return_value=mock.MagicMock(), ) @mock.patch("budgetportal.models.get_expenditure_time_series_dataset") def test_no_cells_null_response(self, mock_get_dataset, total_budgets_mock): self.mock_openspending_api.aggregate_by_refs = Mock(return_value=[]) mock_get_dataset.return_value = self.mock_dataset result = self.department.get_national_expenditure_treemap( financial_year_id="2018-19", budget_phase="original") self.assertEqual(result, None) @mock.patch( "budgetportal.models.Department.get_all_budget_totals_by_year_and_phase", return_value=mock.MagicMock(), ) @mock.patch("budgetportal.models.get_expenditure_time_series_dataset") def test_complete_data(self, mock_get_dataset, total_budgets_mock): self.mock_openspending_api.aggregate_by_refs = Mock( return_value=self.mock_data["complete"]) mock_get_dataset.return_value = self.mock_dataset result = self.department.get_national_expenditure_treemap( financial_year_id="2019-20", budget_phase="original") data = result["data"] self.assertEqual(len(data), 2) data_keys = data.keys() self.assertIn("items", data_keys) self.assertIn("total", data_keys) self.assertEqual(len(data["items"]), 3) expenditure_keys = data["items"][0].keys() self.assertIn("name", expenditure_keys) self.assertIn("amount", expenditure_keys) self.assertIn("percentage_of_total", expenditure_keys) self.assertIn("url", expenditure_keys) self.assertIn("province", expenditure_keys)
class NationalTreemapExpenditureByDepartmentTestCase(TestCase): """ Unit tests for the treemap expenditure by department function. """ def setUp(self): self.mock_data = TREEMAP_MOCK_DATA year = FinancialYear.objects.create(slug="2019-20") sphere = Sphere.objects.create(financial_year=year, name="national") government = Government.objects.create(sphere=sphere, name="A government") self.department = Department( government=government, name="Fake", vote_number=1, is_vote_primary=True, intro="", ) self.mock_openspending_api = Mock() self.mock_openspending_api.get_adjustment_kind_ref = Mock(return_value='adjustment_kind_ref') self.mock_openspending_api.get_phase_ref = Mock(return_value='budget_phase.budget_phase') self.mock_openspending_api.get_programme_name_ref = Mock(return_value='programme_number.programme') self.mock_openspending_api.get_department_name_ref = Mock(return_value='vote_number.department') self.mock_openspending_api.get_financial_year_ref = Mock(return_value="financial_year.financial_year") self.mock_openspending_api.aggregate = Mock(return_value={'cells': [{'value.sum': 1, '_count': 0}]}) self.mock_openspending_api.filter_by_ref_exclusion = Mock self.mock_openspending_api.aggregate_url = Mock self.mock_dataset = Mock() self.mock_dataset.get_openspending_api = Mock(return_value=self.mock_openspending_api) vote_number = 1 for mock_object in self.mock_data['complete']: Department.objects.create( government=government, is_vote_primary=True, name=mock_object['vote_number.department'], vote_number=vote_number ) vote_number += 1 @mock.patch( 'budgetportal.models.Department.get_all_budget_totals_by_year_and_phase', return_value=mock.MagicMock() ) @mock.patch('budgetportal.models.get_expenditure_time_series_dataset') def test_no_cells_null_response(self, mock_get_dataset, total_budgets_mock): self.mock_openspending_api.aggregate_by_refs = Mock(return_value=[]) mock_get_dataset.return_value = self.mock_dataset result = self.department.get_national_expenditure_treemap( financial_year_id='2018-19', budget_phase='original' ) self.assertEqual(result, None) @mock.patch( 'budgetportal.models.Department.get_all_budget_totals_by_year_and_phase', return_value=mock.MagicMock() ) @mock.patch('budgetportal.models.get_expenditure_time_series_dataset') def test_complete_data(self, mock_get_dataset, total_budgets_mock): self.mock_openspending_api.aggregate_by_refs = Mock(return_value=self.mock_data['complete']) mock_get_dataset.return_value = self.mock_dataset result = self.department.get_national_expenditure_treemap( financial_year_id='2019-20', budget_phase='original' ) data = result['data'] self.assertEqual(len(data), 2) data_keys = data.keys() self.assertIn('items', data_keys) self.assertIn('total', data_keys) self.assertEqual(len(data['items']), 3) expenditure_keys = data['items'][0].keys() self.assertIn('name', expenditure_keys) self.assertIn('amount', expenditure_keys) self.assertIn('percentage_of_total', expenditure_keys) self.assertIn('url', expenditure_keys) self.assertIn('province', expenditure_keys)