Exemplo n.º 1
0
def update(record_id: int, record: RecordUpdate,
           db: Session = Depends(get_db)):
    try:
        return RecordsRepository(db).update_by_id(
            record_id, record.dict(exclude_unset=True))
    except Exception:
        raise HTTPException(status_code=404, detail="Record not found")
Exemplo n.º 2
0
def test_update_record_should_return_status_404_if_record_not_found(client):
    RecordsRepository.update_by_id = MagicMock(side_effect=Exception)
    response = client.put(f"/records/{record_1.id}", json=record_dict_1)
    assert response.status_code == HTTP_404_NOT_FOUND
    RecordsRepository.update_by_id.assert_called_once_with(
        record_1.id, RecordUpdate(**record_dict_1).dict()
    )
Exemplo n.º 3
0
def test_update_record_should_ignore_unknown_fields(client):
    update_data = {"key": "value", **record_dict_1}
    RecordsRepository.update_by_id = MagicMock(return_value=record_1)
    client.put(f"/records/{record_1.id}", json=update_data)
    RecordsRepository.update_by_id.assert_called_once_with(
        record_1.id, RecordUpdate(**record_dict_1).dict()
    )
Exemplo n.º 4
0
def test_record_happened_at_must_have_datetime_format():
    happened_at = "abc"
    with raises(ValidationError):
        RecordUpdate(happened_at=happened_at)
Exemplo n.º 5
0
def update_response(client):
    RecordsRepository.update_by_id = MagicMock(return_value=record_1)
    yield client.put(f"/records/{record_1.id}", json=record_dict_1)
    RecordsRepository.update_by_id.assert_called_once_with(
        record_1.id, RecordUpdate(**record_dict_1).dict()
    )
Exemplo n.º 6
0
def test_record_amount_must_have_2_decimal_places():
    amount = 12.345
    with raises(ValidationError):
        RecordUpdate(amount=amount)
Exemplo n.º 7
0
def test_record_description_must_be_shorter_than_141():
    long_description = 141 * "a"
    with raises(ValidationError):
        RecordUpdate(description=long_description)
Exemplo n.º 8
0
def test_record_title_must_be_longer_than_2():
    short_title = 2 * "a"
    with raises(ValidationError):
        RecordUpdate(title=short_title)
Exemplo n.º 9
0
def test_record_title_must_be_shorter_than_51():
    long_title = 51 * "a"
    with raises(ValidationError):
        RecordUpdate(title=long_title)
Exemplo n.º 10
0
def test_record_update_has_no_required_args():
    record_update = RecordUpdate()
    assert record_update.dict(exclude_unset=True) == {}