Beispiel #1
0
def test_loan_extend(loan_created, db, params,
                     mock_is_item_available):
    """Test loan extend action."""

    def get_max_count_1(loan):
        return 1

    loan = current_circulation.circulation.trigger(
        loan_created, **dict(params, trigger='checkout')
    )
    db.session.commit()
    end_date = parse_date(loan['end_date'])

    loan = current_circulation.circulation.trigger(
        loan, **dict(params, trigger='extend')
    )
    db.session.commit()
    new_end_date = parse_date(loan['end_date'])
    assert new_end_date == end_date + timedelta(days=30)
    assert loan['extension_count'] == 1
    loan = current_circulation.circulation.trigger(
        loan, **dict(params, trigger='extend')
    )
    db.session.commit()

    # test to manny extensions
    current_app.config['CIRCULATION_POLICIES']['extension'][
        'max_count'] = get_max_count_1
    with pytest.raises(TransitionConstraintsViolation):
        loan = current_circulation.circulation.trigger(
            loan, **dict(params, trigger='extend')
        )
def test_loan_extend(loan_created, db, params,
                     mock_ensure_item_is_available_for_checkout):
    """Test loan extend action."""

    mock_ensure_item_is_available_for_checkout.side_effect = None

    loan = current_circulation.circulation.trigger(
        loan_created, **dict(params, trigger="checkout")
    )
    db.session.commit()
    end_date = parse_date(loan["end_date"])

    def get_max_count_0(loan):
        return 0

    current_app.config["CIRCULATION_POLICIES"]["extension"][
        "max_count"
    ] = get_max_count_0

    with pytest.raises(LoanMaxExtensionError):
        loan = current_circulation.circulation.trigger(
            loan, **dict(params, trigger="extend")
        )

    def get_max_count_2(loan):
        return 2

    current_app.config["CIRCULATION_POLICIES"]["extension"][
        "max_count"
    ] = get_max_count_2

    loan = current_circulation.circulation.trigger(
        loan, **dict(params, trigger="extend")
    )
    db.session.commit()
    new_end_date = parse_date(loan["end_date"])
    assert new_end_date == end_date + timedelta(days=30)
    assert loan["extension_count"] == 1

    loan = current_circulation.circulation.trigger(
        loan, **dict(params, trigger="extend")
    )
    db.session.commit()

    # test too many extensions
    with pytest.raises(LoanMaxExtensionError):
        loan = current_circulation.circulation.trigger(
            loan, **dict(params, trigger="extend")
        )
def test_checkout_extend_with_timedelta(
    loan_created, db, params, mock_ensure_item_is_available_for_checkout
):
    """Test checkout with a timedelta duration."""
    mock_ensure_item_is_available_for_checkout.side_effect = None

    duration = timedelta(hours=4)

    with SwappedNestedConfig(
        ["CIRCULATION_POLICIES", "checkout", "duration_default"],
        lambda x: duration,
    ):
        loan = current_circulation.circulation.trigger(
            loan_created, **dict(params, trigger="checkout")
        )
        db.session.commit()

        assert loan["state"] == "ITEM_ON_LOAN"
        assert loan["start_date"] == loan["transaction_date"]
        start_date = parse_date(loan["start_date"])
        end_date = start_date + duration
        assert loan["end_date"] == end_date.isoformat()

        current_app.config["CIRCULATION_POLICIES"]["extension"][
            "from_end_date"
        ] = True

        with SwappedNestedConfig(
            ["CIRCULATION_POLICIES", "extension", "duration_default"],
            lambda x: duration,
        ):

            duration = timedelta(days=1)
            loan = current_circulation.circulation.trigger(
                loan, **dict(params, trigger="extend")
            )
            db.session.commit()
            first_extend_end_date = parse_date(loan["end_date"])
            assert first_extend_end_date == end_date + timedelta(days=1)

            duration = timedelta(days=3, minutes=33)
            loan = current_circulation.circulation.trigger(
                loan, **dict(params, trigger="extend")
            )
            db.session.commit()
            second_extend_end_date = parse_date(loan["end_date"])
            assert second_extend_end_date == \
                first_extend_end_date + timedelta(days=3, minutes=33)
def test_loan_extend_from_enddate(loan_created, db, params,
                                  mock_is_item_available,
                                  mock_can_be_requested):
    """Test loan extend action from transaction date."""
    loan = current_circulation.circulation.trigger(
        loan_created, **dict(params, trigger="checkout"))
    db.session.commit()
    extension_date = parse_date(loan.get("transaction_date"))
    current_app.config["CIRCULATION_POLICIES"]["extension"][
        "from_end_date"] = False

    loan = current_circulation.circulation.trigger(
        loan, **dict(params, trigger="extend"))
    db.session.commit()
    new_end_date = parse_date(loan["end_date"])
    assert new_end_date == extension_date + timedelta(days=30)
    assert loan["extension_count"] == 1
def test_loan_extend(loan_created, db, params,
                     mock_is_item_available_for_checkout):
    """Test loan extend action."""
    loan = current_circulation.circulation.trigger(
        loan_created, **dict(params, trigger="checkout")
    )
    db.session.commit()
    end_date = parse_date(loan["end_date"])

    def get_max_count_0(loan):
        return 0

    current_app.config["CIRCULATION_POLICIES"]["extension"][
        "max_count"
    ] = get_max_count_0

    with pytest.raises(LoanMaxExtensionError):
        loan = current_circulation.circulation.trigger(
            loan, **dict(params, trigger="extend")
        )

    def get_max_count_2(loan):
        return 2

    current_app.config["CIRCULATION_POLICIES"]["extension"][
        "max_count"
    ] = get_max_count_2

    loan = current_circulation.circulation.trigger(
        loan, **dict(params, trigger="extend")
    )
    db.session.commit()
    new_end_date = parse_date(loan["end_date"])
    assert new_end_date == end_date + timedelta(days=30)
    assert loan["extension_count"] == 1

    loan = current_circulation.circulation.trigger(
        loan, **dict(params, trigger="extend")
    )
    db.session.commit()

    # test too many extensions
    with pytest.raises(LoanMaxExtensionError):
        loan = current_circulation.circulation.trigger(
            loan, **dict(params, trigger="extend")
        )
Beispiel #6
0
def test_loan_extend_from_enddate(loan_created, db, params,
                                  mock_is_item_available):
    """Test loan extend action from transaction date."""

    loan = current_circulation.circulation.trigger(
        loan_created, **dict(params, trigger='checkout')
    )
    db.session.commit()
    extension_date = parse_date(loan.get('transaction_date'))
    current_app.config['CIRCULATION_POLICIES']['extension'][
        'from_end_date'] = False

    loan = current_circulation.circulation.trigger(
        loan, **dict(params, trigger='extend')
    )
    db.session.commit()
    new_end_date = parse_date(loan['end_date'])
    assert new_end_date == extension_date + timedelta(days=30)
    assert loan['extension_count'] == 1
def test_loan_extend_from_enddate(
    loan_created, db, params, mock_is_item_available_for_checkout
):
    """Test loan extend action from transaction date."""
    loan = current_circulation.circulation.trigger(
        loan_created, **dict(params, trigger="checkout")
    )
    db.session.commit()
    extension_date = parse_date(loan.get("transaction_date"))
    current_app.config["CIRCULATION_POLICIES"]["extension"][
        "from_end_date"
    ] = False

    loan = current_circulation.circulation.trigger(
        loan, **dict(params, trigger="extend")
    )
    db.session.commit()
    new_end_date = parse_date(loan["end_date"])
    assert new_end_date == extension_date + timedelta(days=30)
    assert loan["extension_count"] == 1
def test_loan_extend(loan_created, db, params, mock_is_item_available,
                     mock_can_be_requested):
    """Test loan extend action."""
    loan = current_circulation.circulation.trigger(
        loan_created, **dict(params, trigger="checkout"))
    db.session.commit()
    end_date = parse_date(loan["end_date"])

    def get_max_count_0(loan):
        return 0

    current_app.config["CIRCULATION_POLICIES"]["extension"][
        "max_count"] = get_max_count_0

    with pytest.raises(TransitionConstraintsViolation):
        loan = current_circulation.circulation.trigger(
            loan, **dict(params, trigger="extend"))

    def get_max_count_2(loan):
        return 2

    current_app.config["CIRCULATION_POLICIES"]["extension"][
        "max_count"] = get_max_count_2

    loan = current_circulation.circulation.trigger(
        loan, **dict(params, trigger="extend"))
    db.session.commit()
    new_end_date = parse_date(loan["end_date"])
    assert new_end_date == end_date + timedelta(days=30)
    assert loan["extension_count"] == 1

    loan = current_circulation.circulation.trigger(
        loan, **dict(params, trigger="extend"))
    db.session.commit()

    # test too many extensions
    with pytest.raises(TransitionConstraintsViolation):
        loan = current_circulation.circulation.trigger(
            loan, **dict(params, trigger="extend"))
def test_checkout_start_is_transaction_date(loan_created, db, params):
    """Test checkout start date to transaction date when not set."""
    number_of_days = 10

    with SwappedNestedConfig(
        ['CIRCULATION_POLICIES', 'checkout', 'duration_default'],
            lambda x: number_of_days):
        loan = current_circulation.circulation.trigger(
            loan_created, **dict(params, trigger='checkout'))
        db.session.commit()

        assert loan['state'] == 'ITEM_ON_LOAN'
        assert loan['start_date'] == loan['transaction_date']
        start_date = parse_date(loan['start_date'])
        end_date = start_date + timedelta(number_of_days)
        assert loan['end_date'] == end_date.isoformat()
def test_checkout_start_is_transaction_date(loan_created, db, params,
                                            mock_is_item_available,
                                            mock_can_be_requested):
    """Test checkout start date to transaction date when not set."""
    number_of_days = 10

    with SwappedNestedConfig(
        ["CIRCULATION_POLICIES", "checkout", "duration_default"],
            lambda x: number_of_days,
    ):
        loan = current_circulation.circulation.trigger(
            loan_created, **dict(params, trigger="checkout"))
        db.session.commit()

        assert loan["state"] == "ITEM_ON_LOAN"
        assert loan["start_date"] == loan["transaction_date"]
        start_date = parse_date(loan["start_date"])
        end_date = start_date + timedelta(number_of_days)
        assert loan["end_date"] == end_date.isoformat()
def test_checkout_start_is_transaction_date(
    loan_created, db, params, mock_is_item_available_for_checkout
):
    """Test checkout start date to transaction date when not set."""
    number_of_days = 10

    with SwappedNestedConfig(
        ["CIRCULATION_POLICIES", "checkout", "duration_default"],
        lambda x: number_of_days,
    ):
        loan = current_circulation.circulation.trigger(
            loan_created, **dict(params, trigger="checkout")
        )
        db.session.commit()

        assert loan["state"] == "ITEM_ON_LOAN"
        assert loan["start_date"] == loan["transaction_date"]
        start_date = parse_date(loan["start_date"])
        end_date = start_date + timedelta(number_of_days)
        assert loan["end_date"] == end_date.isoformat()