def setup_environment(self): self.reset_environment() self.test_helper = MyTestHelper() self.region = self.test_helper.create_region(identifier='xxxx') self.district = self.test_helper.create_district(self.region, identifier='xxxx') self.district2 = self.test_helper.create_district(self.region, identifier='xxxx2') self.district3 = self.test_helper.create_district(self.region, identifier='xxxx3') self.sub_county = self.test_helper.create_sub_county(self.district, identifier='xxxx') self.facility1 = self.test_helper.create_facility(self.sub_county, identifier='xxxx') self.facility2 = self.test_helper.create_facility(self.sub_county, identifier='xxxx2') self.facility3 = self.test_helper.create_facility(self.sub_county, identifier='xxxx3') self.org_unit_data = {'region': self.region, 'district': self.district, 'facility': self.facility1} self.data_set = self.test_helper.create_sample_data_set() self.rdt_data_element = self.test_helper.create_sample_data_element(identifier="fOZ2QUjRSB7", name="105-7.3 Lab Malaria RDTs") self.act_data_element = self.test_helper.create_sample_data_element(identifier="IBuhlkMrwqY", name="105-6 Artemether/ Lumefantrine 100/20mg tablet") self.positive_under5 = self.test_helper.create_sample_category_option(self.rdt_data_element, identifier="PT9vhPUpxc4", name="Number Positive, Under 5 years") self.positive_over4 = self.test_helper.create_sample_category_option(self.rdt_data_element, identifier="Li89EZS6Jss", name="Number Positive, 5 years and above" ) self.days_out_of_stock = self.test_helper.create_sample_category_option(self.act_data_element, identifier="Avvv8JaSwGR", name="Days out of stock" )
def test_that_downloads_only_after_previous_data_has_been_parsed(self): three_days_ago = timezone.now() - timedelta(days=2) first = self.create_old_tracker(201505, three_days_ago) first.status = DataSyncTrackerStatus.DOWNLOADED first.save() second = self.create_old_tracker(201506, three_days_ago) second.status = DataSyncTrackerStatus.PARSED second.save() third = self.create_old_tracker(201507, three_days_ago) third.status = DataSyncTrackerStatus.INIT_PARSE third.save() ds1 = MyTestHelper().create_sample_data_set() with patch('dhisdash.models.DataSyncTracker.update_periods') as update_periods_mock: update_periods_mock.return_value = None with patch.object(DataSetDownloader, 'download', return_value=None) as mock_method: call_command('dhis2_data_download', self.start_period) ds1.delete() self.assertEqual(2, mock_method.call_count)
class JsonDataViewTestCase(TestCase, MyTestCaseHelper): def __init__(self, methodName): super(JsonDataViewTestCase, self).__init__(methodName) self.test_helper = MyTestHelper() def setUp(self): self.setup_environment() def test_data_values_facility_period_data_element_unique_together(self): self.test_helper.create_data_value(self.org_unit_data, self.rdt_data_element, self.positive_under5, self.data_set, 201505, 10, AgeGroups.under_5_years) error_occurred = False try: self.test_helper.create_data_value(self.org_unit_data, self.rdt_data_element, self.positive_under5, self.data_set, 201505, 10, AgeGroups.under_5_years) except Exception, e: # print e.message error_occurred = True self.assertTrue(error_occurred)
def test_download_is_called_only_on_last_downloaded_older_than_a_day(self): three_days_ago = timezone.now() - timedelta(days=2) first = self.create_old_tracker(201505, three_days_ago) second = self.create_old_tracker(201506, three_days_ago) ds1 = MyTestHelper().create_sample_data_set() ds2 = MyTestHelper().create_sample_data_set() with patch('dhisdash.models.DataSyncTracker.update_periods') as update_periods_mock: update_periods_mock.return_value=None with patch.object(DataSetDownloader, 'download', return_value=None) as mock_method: call_command('dhis2_data_download', self.start_period) first.delete() second.delete() total_data_sets = DataSet.objects.count() ds1.delete() ds2.delete() self.assertEqual(2*total_data_sets, mock_method.call_count)
def __init__(self, methodName): super(JsonDataViewTestCase, self).__init__(methodName) self.test_helper = MyTestHelper()
class MyTestCaseHelper(object): def reset_environment(self): Region.objects.all().delete() District.objects.all().delete() SubCounty.objects.all().delete() Facility.objects.all().delete() CategoryOptionCombo.objects.all().delete() DataElement.objects.all().delete() DataSet.objects.all().delete() DataValue.objects.all().delete() def setup_environment(self): self.reset_environment() self.test_helper = MyTestHelper() self.region = self.test_helper.create_region(identifier='xxxx') self.district = self.test_helper.create_district(self.region, identifier='xxxx') self.district2 = self.test_helper.create_district(self.region, identifier='xxxx2') self.district3 = self.test_helper.create_district(self.region, identifier='xxxx3') self.sub_county = self.test_helper.create_sub_county(self.district, identifier='xxxx') self.facility1 = self.test_helper.create_facility(self.sub_county, identifier='xxxx') self.facility2 = self.test_helper.create_facility(self.sub_county, identifier='xxxx2') self.facility3 = self.test_helper.create_facility(self.sub_county, identifier='xxxx3') self.org_unit_data = {'region': self.region, 'district': self.district, 'facility': self.facility1} self.data_set = self.test_helper.create_sample_data_set() self.rdt_data_element = self.test_helper.create_sample_data_element(identifier="fOZ2QUjRSB7", name="105-7.3 Lab Malaria RDTs") self.act_data_element = self.test_helper.create_sample_data_element(identifier="IBuhlkMrwqY", name="105-6 Artemether/ Lumefantrine 100/20mg tablet") self.positive_under5 = self.test_helper.create_sample_category_option(self.rdt_data_element, identifier="PT9vhPUpxc4", name="Number Positive, Under 5 years") self.positive_over4 = self.test_helper.create_sample_category_option(self.rdt_data_element, identifier="Li89EZS6Jss", name="Number Positive, 5 years and above" ) self.days_out_of_stock = self.test_helper.create_sample_category_option(self.act_data_element, identifier="Avvv8JaSwGR", name="Days out of stock" ) def get_value_from_url(self, url, group_key, value_key): request = RequestFactory().get(url) view = JsonDataView() view.im.category_option_combos = { "Number Positive, Under 5 years": "PT9vhPUpxc4", "Number Positive, 5 years and above": "Li89EZS6Jss", "Days out of stock": "Avvv8JaSwGR" } view.im.data_elements = { "105-7.3 Lab Malaria RDTs": "fOZ2QUjRSB7", "105-6 Artemether/ Lumefantrine 100/20mg tablet": "IBuhlkMrwqY", } result = json.loads(view.get(request).getvalue()) print result return result[group_key][value_key]