Esempio n. 1
0
def test_greater_minimum_date_than_maximum_date(app):
    answer = {
        "id": "date_answer",
        "type": "Date",
        "minimum": {
            "value": "2018-02-15",
            "offset_by": {
                "days": -10
            }
        },
        "maximum": {
            "value": "2018-01-15",
            "offset_by": {
                "days": 10
            }
        },
    }

    handler = DateHandler(answer)

    with pytest.raises(Exception) as ite:
        handler.get_date_value("minimum")

        assert (
            str(ite.exception) ==
            "The minimum offset date is greater than the maximum offset date for date-answer."
        )
def test_minimum_and_maximum_offset_dates(app, value_source_resolver, rule_evaluator):
    value_source_resolver.metadata = {"date": "2018-02-20"}
    answer_store = AnswerStore()

    test_answer_id = Answer(answer_id="date", value="2018-03-20")
    answer_store.add_or_update(test_answer_id)
    value_source_resolver.answer_store = answer_store
    answer = {
        "id": "date_answer",
        "type": "Date",
        "minimum": {
            "value": {"identifier": "date", "source": "metadata"},
            "offset_by": {"days": -10},
        },
        "maximum": {
            "value": {"identifier": "date", "source": "answers"},
            "offset_by": {"years": 1},
        },
    }

    handler = DateHandler(answer, value_source_resolver, rule_evaluator, error_messages)
    minimum_date = handler.get_date_value("minimum")
    maximum_date = handler.get_date_value("maximum")

    assert minimum_date == parse_datetime("2018-02-10")
    assert maximum_date == parse_datetime("2019-03-20")
Esempio n. 3
0
def test_get_referenced_offset_value_with_list_item_id(app, schema_mock):
    list_item_id = "abcde"
    answer_store = AnswerStore()

    test_answer_id = Answer(answer_id="date",
                            value="2018-03-20",
                            list_item_id=list_item_id)

    location = Location(section_id="test", list_item_id=list_item_id)

    answer_store.add_or_update(test_answer_id)

    answer = {
        "maximum": {
            "value": {
                "identifier": "date",
                "source": "answers"
            },
            "offset_by": {
                "months": 1
            },
        }
    }

    handler = DateHandler(answer, answer_store=answer_store, location=location)
    maximum_date = handler.get_date_value("maximum")

    assert maximum_date == convert_to_datetime("2018-04-20")
def test_get_referenced_offset_value_with_list_item_id(
    app, value_source_resolver, rule_evaluator
):
    list_item_id = "abcde"

    test_answer_id = Answer(
        answer_id="date", value="2018-03-20", list_item_id=list_item_id
    )

    location = Location(section_id="test", list_item_id=list_item_id)

    answer_store = AnswerStore()

    answer_store.add_or_update(test_answer_id)
    value_source_resolver.answer_store = answer_store
    value_source_resolver.location = location
    value_source_resolver.list_item_id = list_item_id
    answer = {
        "maximum": {
            "value": {"identifier": "date", "source": "answers"},
            "offset_by": {"months": 1},
        }
    }

    handler = DateHandler(answer, value_source_resolver, rule_evaluator, error_messages)
    maximum_date = handler.get_date_value("maximum")

    assert maximum_date == parse_datetime("2018-04-20")
Esempio n. 5
0
def test_get_referenced_offset_value_for_value(app):
    answer = {"minimum": {"value": "2017-06-11"}}

    handler = DateHandler(answer)
    minimum_date = handler.get_date_value("minimum")
    minimum_date = handler.transform_date_by_offset(minimum_date, {"days": 10})

    assert minimum_date == convert_to_datetime("2017-06-21")
Esempio n. 6
0
def test_get_referenced_offset_value_for_now_value(app):
    answer = {"minimum": {"value": "now"}}

    handler = DateHandler(answer)
    minimum_date = handler.get_date_value("minimum")
    minimum_date = handler.transform_date_by_offset(minimum_date, {"days": 10})

    assert datetime.date(minimum_date) == (datetime.utcnow().date() +
                                           relativedelta(days=10))
    def _get_period_range_for_single_date(self, date_from, date_to):
        handler = DateHandler(date_from, {},
                              self.answer_store,
                              self.metadata,
                              location=self.location)
        from_min_period_date = handler.get_date_value("minimum")
        from_max_period_date = handler.get_date_value("maximum")

        handler.answer_schema = date_to
        to_min_period_date = handler.get_date_value("minimum")
        to_max_period_date = handler.get_date_value("maximum")

        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 _get_period_range_for_single_date(
        self,
        date_from: Mapping[str, dict],
        date_to: Mapping[str, dict],
    ) -> timedelta:

        list_item_id = self.location.list_item_id if self.location else None
        value_source_resolver = ValueSourceResolver(
            answer_store=self.answer_store,
            list_store=self.list_store,
            metadata=self.metadata,
            response_metadata=self.response_metadata,
            schema=self.schema,
            location=self.location,
            list_item_id=list_item_id,
            escape_answer_values=False,
        )

        rule_evaluator = RuleEvaluator(
            schema=self.schema,
            answer_store=self.answer_store,
            list_store=self.list_store,
            metadata=self.metadata,
            response_metadata=self.response_metadata,
            location=self.location,
        )

        handler = DateHandler(date_from, value_source_resolver, rule_evaluator,
                              error_messages)

        min_period_date = handler.get_date_value(
            "minimum") or handler.get_date_value("maximum")
        handler.answer_schema = date_to
        max_period_date = handler.get_date_value(
            "maximum") or handler.get_date_value("minimum")

        if not min_period_date or not max_period_date:
            raise ValueError("Period range must have a start and end date")

        # Work out the largest possible range, for date range question
        period_range = max_period_date - min_period_date

        return period_range
def test_get_referenced_offset_value_for_value(
    app, value_source_resolver, rule_evaluator
):
    answer = {"minimum": {"value": "2017-06-11"}}

    handler = DateHandler(answer, value_source_resolver, rule_evaluator, error_messages)
    minimum_date = handler.get_date_value("minimum")
    minimum_date = handler.transform_date_by_offset(minimum_date, {"days": 10})

    assert minimum_date == parse_datetime("2017-06-21")
def test_get_referenced_offset_value_for_meta(
    app, value_source_resolver, rule_evaluator
):
    value_source_resolver.metadata = {"date": "2018-02-20"}
    answer = {"minimum": {"value": {"identifier": "date", "source": "metadata"}}}

    handler = DateHandler(answer, value_source_resolver, rule_evaluator, error_messages)
    minimum_date = handler.get_date_value("minimum")
    minimum_date = handler.transform_date_by_offset(minimum_date, {"days": -10})

    assert minimum_date == parse_datetime("2018-02-10")
def test_get_referenced_offset_value_for_now_value(
    app, value_source_resolver, rule_evaluator
):
    answer = {"minimum": {"value": "now"}}

    handler = DateHandler(answer, value_source_resolver, rule_evaluator, error_messages)
    minimum_date = handler.get_date_value("minimum")
    minimum_date = handler.transform_date_by_offset(minimum_date, {"days": 10})

    assert datetime.date(minimum_date) == (
        datetime.now(tz=timezone.utc).date() + relativedelta(days=10)
    )
def test_get_referenced_offset_value_for_answer_id(
    app, value_source_resolver, rule_evaluator
):
    answer_store = AnswerStore()

    test_answer_id = Answer(answer_id="date", value="2018-03-20")
    answer_store.add_or_update(test_answer_id)
    value_source_resolver.answer_store = answer_store
    answer = {"maximum": {"value": {"identifier": "date", "source": "answers"}}}

    handler = DateHandler(answer, value_source_resolver, rule_evaluator, error_messages)
    maximum_date = handler.get_date_value("maximum")
    maximum_date = handler.transform_date_by_offset(maximum_date, {"months": 1})

    assert maximum_date == parse_datetime("2018-04-20")
Esempio n. 13
0
def test_get_referenced_offset_value_for_meta(app):
    test_metadata = {"date": "2018-02-20"}
    answer = {
        "minimum": {
            "value": {
                "identifier": "date",
                "source": "metadata"
            }
        }
    }

    handler = DateHandler(answer, metadata=test_metadata)
    minimum_date = handler.get_date_value("minimum")
    minimum_date = handler.transform_date_by_offset(minimum_date,
                                                    {"days": -10})

    assert minimum_date == convert_to_datetime("2018-02-10")
Esempio n. 14
0
def test_get_referenced_offset_value_for_answer_id(app):
    answer_store = AnswerStore()

    test_answer_id = Answer(answer_id="date", value="2018-03-20")
    answer_store.add_or_update(test_answer_id)

    answer = {
        "maximum": {
            "value": {
                "identifier": "date",
                "source": "answers"
            }
        }
    }

    handler = DateHandler(answer, answer_store=answer_store)
    maximum_date = handler.get_date_value("maximum")
    maximum_date = handler.transform_date_by_offset(maximum_date,
                                                    {"months": 1})

    assert maximum_date == convert_to_datetime("2018-04-20")
Esempio n. 15
0
def test_minimum_and_maximum_offset_dates(app):
    test_metadata = {"date": "2018-02-20"}
    store = AnswerStore()

    test_answer_id = Answer(answer_id="date", value="2018-03-20")
    store.add_or_update(test_answer_id)

    answer = {
        "id": "date_answer",
        "type": "Date",
        "minimum": {
            "value": {
                "identifier": "date",
                "source": "metadata"
            },
            "offset_by": {
                "days": -10
            },
        },
        "maximum": {
            "value": {
                "identifier": "date",
                "source": "answers"
            },
            "offset_by": {
                "years": 1
            },
        },
    }

    handler = DateHandler(answer, answer_store=store, metadata=test_metadata)
    minimum_date = handler.get_date_value("minimum")
    maximum_date = handler.get_date_value("maximum")

    assert minimum_date == convert_to_datetime("2018-02-10")
    assert maximum_date == convert_to_datetime("2019-03-20")