def _get_period_range_for_single_date(self, date_from, date_to): from_min_period_date, from_max_period_date = get_dates_for_single_date_period_validation( date_from, self.answer_store, self.metadata) to_min_period_date, to_max_period_date = get_dates_for_single_date_period_validation( date_to, self.answer_store, self.metadata) min_period_date = from_min_period_date or from_max_period_date max_period_date = to_max_period_date or to_min_period_date # Work out the largest possible range, for date range question period_range = max_period_date - min_period_date return period_range
def test_minimum_and_maximum_offset_dates(self, mock1): test_metadata = {'date': '2018-02-20'} store = AnswerStore() test_answer_id = Answer( answer_id='date', answer_instance=1, group_instance=0, value='2018-03-20', ) store.add_or_update(test_answer_id) answer = { 'id': 'date_answer', 'type': 'Date', 'minimum': { 'meta': 'date', 'offset_by': { 'days': -10 } }, 'maximum': { 'answer_id': 'date', 'offset_by': { 'years': 1 } } } offset_dates = get_dates_for_single_date_period_validation( answer, store, metadata=test_metadata) self.assertEqual(offset_dates, (convert_to_datetime('2018-02-10'), convert_to_datetime('2019-03-20')))
def test_greater_minimum_date_than_maximum_date(self): answer = { 'id': 'date_answer', 'type': 'Date', 'minimum': { 'value': '2018-02-15', 'offset_by': { 'days': -10 } }, 'maximum': { 'value': '2018-01-15', 'offset_by': { 'days': 10 } } } with self.assertRaises(Exception) as ite: get_dates_for_single_date_period_validation( answer, AnswerStore(), {}) self.assertEqual( 'The minimum offset date is greater than the maximum offset date for date-answer.', str(ite.exception))