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 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)