def setUp(self):
     year = FinancialYear(slug="2030-31")
     sphere = Sphere(financial_year=year, name="A sphere")
     government = Government(sphere=sphere, name="A government")
     self.department = Department(
         government=government,
         name="Fake",
         vote_number=1,
         is_vote_primary=True,
         intro="",
     )
     mock_dataset = Mock()
     mock_openspending_api = Mock()
     self.mock_openspending_api = mock_openspending_api
     mock_openspending_api.get_adjustment_kind_ref = Mock
     mock_openspending_api.get_phase_ref = Mock
     mock_openspending_api.get_programme_name_ref = Mock
     mock_openspending_api.get_department_name_ref = Mock(return_value='department_name_ref')
     mock_openspending_api.get_financial_year_ref = Mock(return_value="financial_year_ref")
     mock_openspending_api.aggregate = Mock
     mock_openspending_api.filter_dept = Mock(return_value={'cells': []})
     mock_openspending_api.filter_by_ref_exclusion = Mock
     mock_openspending_api.aggregate_by_refs = Mock
     mock_openspending_api.aggregate_url = Mock
     mock_dataset.get_openspending_api = Mock(return_value=mock_openspending_api)
     self.department.get_adjusted_estimates_expenditure_dataset = Mock(return_value=mock_dataset)
     self.department.get_financial_year = Mock(return_value=year)
     self.department._get_adjustments_by_econ_class = Mock(return_value=Mock())
     self.department._get_adjustments_by_programme = Mock(return_value=Mock())
     self.department._get_adjustments_by_type = Mock(return_value=Mock())
     self.department._get_budget_virements = Mock(return_value=Mock())
     self.department._get_budget_special_appropriations = Mock(return_value=Mock())
     self.department._get_budget_direct_charges = Mock(return_value=Mock())
     models.csv_url = Mock(return_value=Mock())
    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
    def setUp(self):
        self.mock_data = DEPARTMENT_MOCK_DATA
        year = FinancialYear(slug="2018-19")
        sphere = Sphere(financial_year=year, name="A sphere")
        government = Government(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_geo_ref = Mock(return_value='government.government')
        self.mock_openspending_api.get_programme_name_ref = Mock
        self.mock_openspending_api.get_department_name_ref = Mock(return_value='department_name_ref')
        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_dept = Mock(return_value={'cells': []})
        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)

        dataset_patch = patch('budgetportal.datasets.Dataset.get_latest_cpi_resource', return_value=('2018-19', '5b315ff0-55e9-4ba8-b88c-2d70093bfe9d'))
        dataset_patch.start()
        self.addCleanup(dataset_patch.stop)
class AdjustedBudgetTestCase(TestCase):
    """Unit tests of adjusted budget data summary for a department"""
    def setUp(self):
        year = FinancialYear(slug="2030-31")
        sphere = Sphere(financial_year=year, name="A sphere")
        government = Government(sphere=sphere, name="A government")
        self.department = Department(
            government=government,
            name="Fake",
            vote_number=1,
            is_vote_primary=True,
            intro="",
        )
        mock_dataset = Mock()
        mock_openspending_api = Mock()
        self.mock_openspending_api = mock_openspending_api
        mock_openspending_api.get_adjustment_kind_ref = Mock
        mock_openspending_api.get_phase_ref = Mock
        mock_openspending_api.get_programme_name_ref = Mock
        mock_openspending_api.get_department_name_ref = Mock(
            return_value="department_name_ref")
        mock_openspending_api.get_financial_year_ref = Mock(
            return_value="financial_year_ref")
        mock_openspending_api.aggregate = Mock
        mock_openspending_api.filter_dept = Mock(return_value={"cells": []})
        mock_openspending_api.filter_by_ref_exclusion = Mock
        mock_openspending_api.aggregate_by_refs = Mock
        mock_openspending_api.aggregate_url = Mock
        mock_dataset.get_openspending_api = Mock(
            return_value=mock_openspending_api)
        self.department.get_adjusted_estimates_expenditure_dataset = Mock(
            return_value=mock_dataset)
        self.department.get_financial_year = Mock(return_value=year)
        self.department._get_adjustments_by_econ_class = Mock(
            return_value=Mock())
        self.department._get_adjustments_by_programme = Mock(
            return_value=Mock())
        self.department._get_adjustments_by_type = Mock(return_value=Mock())
        self.department._get_budget_virements = Mock(return_value=Mock())
        self.department._get_budget_special_appropriations = Mock(
            return_value=Mock())
        self.department._get_budget_direct_charges = Mock(return_value=Mock())
        models.csv_url = Mock(return_value=Mock())

    def test_no_adjustment(self):
        self.department._get_total_budget_adjustment = Mock(return_value=(123,
                                                                          0))
        result = self.department.get_adjusted_budget_summary()
        self.assertEqual(result["total_change"]["amount"], 0)
        self.assertEqual(result["total_change"]["percentage"], 0)

    def test_adjustment(self):
        self.department._get_total_budget_adjustment = Mock(return_value=(100,
                                                                          11))
        result = self.department.get_adjusted_budget_summary()
        self.assertEqual(result["total_change"]["amount"], 11)
        self.assertEqual(result["total_change"]["percentage"], 11)
 def setUp(self):
     year = FinancialYear(slug="2030-31")
     sphere = Sphere(financial_year=year, name="A sphere")
     government = Government(sphere=sphere, name="A government")
     self.department = Department(
         government=government,
         name="Fake",
         vote_number=1,
         is_vote_primary=True,
         intro="",
     )
     self.department.get_adjusted_estimates_expenditure_dataset = Mock(return_value=None)
Beispiel #6
0
    def setUp(self):
        self.year = FinancialYear.objects.create(slug="2019-20")
        self.year.save()
        national = Sphere(financial_year=self.year, name="national")
        national.save()
        provincial = Sphere(financial_year=self.year, name="provincial")
        provincial.save()
        southafrica = Government(sphere=national, name="South Africa")
        southafrica.save()
        province = Government(sphere=provincial, name="Test Province 1")
        province.save()
        Department(
            government=southafrica,
            name="TP1 National Test Dept 2",
            vote_number=1,
            is_vote_primary=True,
            intro="",
        ).save()
        Department(
            government=province,
            name="TP1 Provincial Test Dept 2",
            vote_number=1,
            is_vote_primary=True,
            intro="",
        ).save()

        self.mock_dataset = Mock()
        self.mock_openspending_api = Mock()
        self.mock_openspending_api.get_geo_ref = Mock(
            return_value='geo_source.government')
        self.mock_openspending_api.get_function_ref = Mock(
            return_value='function_group.function_group')
        self.mock_openspending_api.get_year_ref = Mock(
            return_value='function_group.function_group')
        self.mock_openspending_api.get_financial_year_ref = Mock(
            return_value="financial_year.financial_year")
        self.mock_openspending_api.get_department_name_ref = Mock(
            return_value="vote_number.department")
        self.mock_openspending_api.get_subprogramme_name_ref = Mock(
            return_value="subprogramme.subprogramme")
        self.mock_dataset.get_openspending_api = Mock(
            return_value=self.mock_openspending_api)

        def mock_get_focus_area_data(__, sphere):
            if sphere == "provincial":
                cells = FOCUS_AREA_PROVINCIAL_MOCK_DATA
            elif sphere == "national":
                cells = FOCUS_AREA_NATIONAL_MOCK_DATA
            return cells, self.mock_openspending_api

        self.mock_get_focus_area_data = mock_get_focus_area_data
Beispiel #7
0
    def setUp(self):
        self.mock_data = NATIONAL_DEPARTMENT_PREVIEW_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="South Africa")
        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_geo_ref = Mock(return_value='geo_source.government')
        self.mock_openspending_api.get_function_ref = Mock(return_value='function_group_1.function_group_1')
        self.mock_openspending_api.get_financial_year_ref = Mock(return_value="financial_year.financial_year")
        self.mock_openspending_api.aggregate = Mock(return_value={'cells': self.mock_data['programmes']})
        self.mock_openspending_api.filter_by_ref_exclusion = Mock(return_value=self.mock_data['programmes'])
        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['departments']:
            Department.objects.create(
                government=government,
                is_vote_primary=True,
                name=mock_object['vote_number.department'],
                vote_number=vote_number
            )
            vote_number += 1
 def setUp(self):
     self.mock_data = DEPARTMENT_MOCK_DATA
     year = FinancialYear(slug="2018-19")
     sphere = Sphere(financial_year=year, name="A sphere")
     government = Government(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_geo_ref = Mock(
         return_value="government.government")
     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="department_name_ref")
     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_dept = Mock(
         return_value={
             "cells": self.mock_data["program_test_cells_complete"]
         })
     self.mock_openspending_api.aggregate_url = Mock
     self.mock_dataset = Mock()
     self.mock_dataset.get_openspending_api = Mock(
         return_value=self.mock_openspending_api)
class AdjustedBudgetMissingTestCase(TestCase):
    """Unit tests of adjusted budget data summary for a department"""
    def setUp(self):
        year = FinancialYear(slug="2030-31")
        sphere = Sphere(financial_year=year, name="A sphere")
        government = Government(sphere=sphere, name="A government")
        self.department = Department(
            government=government,
            name="Fake",
            vote_number=1,
            is_vote_primary=True,
            intro="",
        )
        self.department.get_adjusted_estimates_expenditure_dataset = Mock(
            return_value=None)

    def test_missing_dataset(self):
        self.assertEqual(self.department.get_adjusted_budget_summary(), None)
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 BudgetedAndActualExpenditureSummaryTestCase(TestCase):
    """tests of budgeted and actual expenditure summary for a department"""
    def setUp(self):
        self.mock_data = DEPARTMENT_MOCK_DATA
        year = FinancialYear(slug="2018-19")
        sphere = Sphere(financial_year=year, name="A sphere")
        government = Government(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_geo_ref = Mock(
            return_value="government.government")
        self.mock_openspending_api.get_programme_name_ref = Mock
        self.mock_openspending_api.get_department_name_ref = Mock(
            return_value="department_name_ref")
        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_dept = Mock(
            return_value={"cells": []})
        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)

        dataset_patch = patch(
            "budgetportal.datasets.Dataset.get_latest_cpi_resource",
            return_value=("2018-19", "5b315ff0-55e9-4ba8-b88c-2d70093bfe9d"),
        )
        dataset_patch.start()
        self.addCleanup(dataset_patch.stop)

    @mock.patch("budgetportal.models.get_expenditure_time_series_dataset")
    def test_no_cells_null_response(self, mock_get_dataset):
        self.mock_openspending_api.aggregate_by_refs = Mock(return_value=[])
        mock_get_dataset.return_value = self.mock_dataset

        result = self.department.get_expenditure_time_series_summary()
        self.assertEqual(result, None)

    @mock.patch("budgetportal.models.get_expenditure_time_series_dataset")
    @mock.patch("budgetportal.models.get_cpi",
                return_value=mock_data.CPI_2019_20)
    def test_complete_data_no_notices(self, mock_get_cpi, mock_get_dataset):
        mock_get_dataset.return_value = self.mock_dataset
        self.mock_openspending_api.aggregate_by_refs = Mock(
            return_value=self.mock_data["test_cells_data_complete"])

        result = self.department.get_expenditure_time_series_summary()
        self.assertEqual(result["notices"], [])

    @mock.patch("budgetportal.models.get_expenditure_time_series_dataset")
    @mock.patch("budgetportal.models.get_cpi",
                return_value=mock_data.CPI_2019_20)
    def test_missing_data_not_published(self, mock_get_cpi, mock_get_dataset):
        """
        Here we feed an incomplete set of cells and expect it to tell us that
        2018 data has not been published
        """

        mock_get_dataset.return_value = self.mock_dataset
        self.mock_openspending_api.aggregate = Mock(
            return_value={"cells": [{
                "value.sum": 0,
                "_count": 0
            }]})
        self.mock_openspending_api.aggregate_by_refs = Mock(
            return_value=self.mock_data["test_cells_data_missing_2018"])

        result = self.department.get_expenditure_time_series_summary()
        self.assertEqual(
            result["notices"],
            [
                "Please note that the data for 2018 has not been published on vulekamali."
            ],
        )

    @mock.patch("budgetportal.models.get_expenditure_time_series_dataset")
    @mock.patch("budgetportal.models.get_cpi",
                return_value=mock_data.CPI_2019_20)
    def test_missing_data_dept_did_not_exist(self, mock_get_cpi,
                                             mock_get_dataset):
        """
        Here we feed an incomplete set of cells and expect it to tell us
        that the department did not exist (removed 2018 data)
        """

        mock_get_dataset.return_value = self.mock_dataset
        self.mock_openspending_api.aggregate = Mock(
            return_value={"cells": [{
                "value.sum": 1,
                "_count": 0
            }]})
        self.mock_openspending_api.aggregate_by_refs = Mock(
            return_value=self.mock_data["test_cells_data_missing_2018"])

        result = self.department.get_expenditure_time_series_summary()
        self.assertEqual(
            result["notices"],
            ["This department did not exist for some years displayed."],
        )
class BudgetedAndActualExpenditureProgrammeTestCase(TestCase):
    """tests of budgeted and actual expenditure summary for a department"""
    def setUp(self):
        self.mock_data = DEPARTMENT_MOCK_DATA
        year = FinancialYear(slug="2018-19")
        sphere = Sphere(financial_year=year, name="A sphere")
        government = Government(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_geo_ref = Mock(
            return_value="government.government")
        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="department_name_ref")
        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_dept = Mock(
            return_value={
                "cells": self.mock_data["program_test_cells_complete"]
            })
        self.mock_openspending_api.aggregate_url = Mock
        self.mock_dataset = Mock()
        self.mock_dataset.get_openspending_api = Mock(
            return_value=self.mock_openspending_api)

    @mock.patch("budgetportal.models.get_expenditure_time_series_dataset")
    def test_no_cells_null_response(self, mock_get_dataset):
        self.mock_openspending_api.filter_dept = Mock(
            return_value={"cells": []})
        mock_get_dataset.return_value = self.mock_dataset

        result = self.department.get_expenditure_time_series_by_programme()
        self.assertEqual(result, None)

    @mock.patch("budgetportal.models.get_expenditure_time_series_dataset")
    @mock.patch("budgetportal.models.get_cpi",
                return_value=mock_data.CPI_2019_20)
    def test_complete_data_no_notices(self, mock_get_cpi, mock_get_dataset):
        mock_get_dataset.return_value = self.mock_dataset

        result = self.department.get_expenditure_time_series_by_programme()
        self.assertEqual(result["notices"], [])

    @mock.patch("budgetportal.models.get_expenditure_time_series_dataset")
    @mock.patch("budgetportal.models.get_cpi",
                return_value=mock_data.CPI_2019_20)
    def test_missing_data_prog_did_not_exist(self, mock_get_cpi,
                                             mock_get_dataset):
        """
        Here we feed an incomplete set of cells and expect it to tell us that
        the department did not exist (removed 2018 data)
        """
        mock_get_dataset.return_value = self.mock_dataset
        self.mock_openspending_api.aggregate = Mock(
            return_value={"cells": [{
                "value.sum": 1,
                "_count": 0
            }]})
        self.mock_openspending_api.filter_dept = Mock(
            return_value={
                "cells":
                self.mock_data["program_test_cells_missing_2018_revenue_admin"]
            })

        result = self.department.get_expenditure_time_series_by_programme()
        self.assertEqual(
            result["notices"],
            ["One or more programmes did not exist for some years displayed."],
        )
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)