Пример #1
0
def test_put(empty_database, valid_people_orm):
    # When nothing in DB - PUT will add the resource
    resource_handler = ResourceHandler()
    resource_orm = valid_people_orm

    class FakeFlaskRequest:
        json = {"name": "tester"}

    result = resource_handler.update_one(id=1,
                                         resource_orm=resource_orm,
                                         request=FakeFlaskRequest(),
                                         mode="PUT")

    assert result == ({
        "id": 1,
        "message": "Successfully added new resource."
    }, 201)

    #  When something in DB - PUT changes the item
    class FakeFlaskRequestPut:
        json = {"name": "newtester"}

    result = resource_handler.update_one(id=1,
                                         resource_orm=resource_orm,
                                         request=FakeFlaskRequestPut(),
                                         mode="PUT")

    assert result == ({
        "id": 1,
        "message": "Successfully updated resource."
    }, 200)
def test_put_mn_one_works_when_items_exist(empty_database, valid_people_orm,
                                           valid_team_orm):
    """Ideally we would be able to also assert on the error message that
    returns.

    # assert result == ({"error": "Resource with id '1' not found."},
    404)
    """
    resource_handler = ResourceHandler()

    person = valid_people_orm(name="tester")
    team1 = valid_team_orm(name="team")
    team2 = valid_team_orm(name="team2")
    person.team_collection.append(team1)

    db_session.add(person)
    db_session.add(team1)
    db_session.add(team2)
    db_session.commit()

    result = resource_handler.put_mn_one(
        id=1,
        body=[2],
        parent_orm=valid_people_orm,
        child_orm=valid_team_orm,
        patch=True,
    )

    assert result == ([1, 2], 200)
def test_put_errors_when_a_child_does_not_exist(empty_database,
                                                valid_people_orm,
                                                valid_team_orm):
    """Ideally we would be able to also assert on the error message that
    returns.

    # assert result == ({"error": "Resource with id '1' not found."},
    404)
    """
    resource_handler = ResourceHandler()

    person = valid_people_orm(id=1, name="tester")
    team = valid_team_orm(id=1, name="team")
    person.team_collection.append(team)

    db_session.add(person)
    db_session.add(team)
    db_session.commit()

    with pytest.raises(ApiError):
        _ = resource_handler.put_mn_one(
            id=1,
            body=[1, 2],
            parent_orm=valid_people_orm,
            child_orm=valid_team_orm,
            patch=True,
        )
Пример #4
0
def test_post_missing_required_fields(empty_database, valid_orm_with_required_field):
    # Creating without required fields errors
    resource_handler = ResourceHandler()
    resource_orm = valid_orm_with_required_field

    class FakeFlaskRequest:
        json = {"optional": 1}

    with pytest.raises(ApiError):
        _ = resource_handler.insert_one(
            resource_orm=resource_orm, request=FakeFlaskRequest()
        )
Пример #5
0
def test_get_one_works_when_item_exists(empty_database, valid_people_orm):
    # When one item in DB - GET returns that item
    resource_handler = ResourceHandler()

    new_object = valid_people_orm(name="tester")
    db_session.add(new_object)
    db_session.commit()

    result = resource_handler.get_one(
        resource_name="test", resource_orm=valid_people_orm, id=1
    )

    assert result == ({"id": 1, "name": "tester"}, 200)
Пример #6
0
def test_get_one_errors_when_it_does_not_exist(empty_database, valid_people_orm):
    """Ideally we would be able to also assert on the error message that
    returns.

    # assert result == ({"error": "Resource with id '1' not found."},
    404)
    """
    # When nothing in DB - GET returns error
    resource_handler = ResourceHandler()

    with pytest.raises(ApiError):
        _ = resource_handler.get_one(
            resource_name="test", resource_orm=valid_people_orm, id=1
        )
Пример #7
0
def test_post(empty_database, valid_people_orm):
    # When nothing in DB, adds to db.
    resource_handler = ResourceHandler()
    resource_orm = valid_people_orm

    class FakeFlaskRequest:
        json = {"name": "tester"}

    result = resource_handler.insert_one(
        resource_orm=resource_orm, request=FakeFlaskRequest()
    )

    assert result[0]["id"] == 1
    assert result[1] == 201
def test_query_works_with_correct_data(valid_people_orm):
    resource_handler = ResourceHandler()

    class FakeFlaskRequest:
        json = {"name": "tester"}

    new_object = valid_people_orm(name="tester")
    db_session.add(new_object)
    db_session.commit()

    result = resource_handler.query_one(resource_orm=valid_people_orm,
                                        request=FakeFlaskRequest())

    assert result == ({"results": [{"id": 1, "name": "tester"}]}, 200)
def test_get_one_errors_when_it_does_not_exist(empty_database,
                                               valid_people_orm,
                                               valid_team_orm):
    """Ideally we would be able to also assert on the error message that
    returns.

    # assert result == ({"error": "Resource with id '1' not found."},
    404)
    """
    resource_handler = ResourceHandler()

    with pytest.raises(ApiError):
        _ = resource_handler.get_mn_one(id=1,
                                        parent_orm=valid_people_orm,
                                        child_orm=valid_team_orm)
def test_query_empty_body_errors(valid_people_orm):
    """Ideally we would be able to also assert on the error message that
    returns.

    # assert result == ({"error": "Resource with id '1' not found."},
    404)
    """
    resource_handler = ResourceHandler()

    class FakeFlaskRequest:
        json = {}

    with pytest.raises(ApiError):
        resource_handler.query_one(resource_orm=valid_people_orm,
                                   request=FakeFlaskRequest())
def test_query_returns_none_when_given_incorrect_field_data(valid_people_orm):
    # When one item in DB - GET returns that item
    resource_handler = ResourceHandler()

    class FakeFlaskRequest:
        json = {"name": "wrongname"}

    new_object = valid_people_orm(name="tester")
    db_session.add(new_object)
    db_session.commit()

    result = resource_handler.query_one(resource_orm=valid_people_orm,
                                        request=FakeFlaskRequest())

    assert result == ({"message": "No matches found"}, 404)
def test_get_mn_one_works_when_item_exists(empty_database, valid_people_orm,
                                           valid_team_orm):
    resource_handler = ResourceHandler()

    person = valid_people_orm(name="tester")
    team = valid_team_orm(name="team")
    person.team_collection.append(team)

    db_session.add(person)
    db_session.add(team)
    db_session.commit()

    result = resource_handler.get_mn_one(id=1,
                                         parent_orm=valid_people_orm,
                                         child_orm=valid_team_orm)

    assert result == ([1], 200)
def test_query_errors_when_given_incorrect_field_data(valid_people_orm):
    """Ideally we would be able to also assert on the error message that
    returns.

    # assert result == ({"error": "Resource with id '1' not found."},
    404)
    """
    # When one item in DB - GET returns that item
    resource_handler = ResourceHandler()

    class FakeFlaskRequest:
        json = {"doesnotexist": "error"}

    new_object = valid_people_orm(name="tester")
    db_session.add(new_object)
    db_session.commit()

    with pytest.raises(ApiError):
        resource_handler.query_one(resource_orm=valid_people_orm,
                                   request=FakeFlaskRequest())
def test_patch_error_when_empty(empty_database, valid_people_orm):
    """Ideally we would be able to also assert on the error message that
    returns.

    # assert result == ({"error": "Resource with id '1' not found."},
    404)
    """
    # When nothing in DB - PATCH error
    resource_handler = ResourceHandler()

    class FakeFlaskRequest:
        json = {"name": "tester"}

    with pytest.raises(ApiError):
        _ = resource_handler.update_one(
            id=1,
            resource_name="test",
            resource_orm=valid_people_orm,
            request=FakeFlaskRequest(),
            mode="PATCH",
        )
Пример #15
0
def test_get_all(empty_database, valid_people_orm):
    # When nothing in DB - GET returns empty list
    resource_handler = ResourceHandler()

    result = resource_handler.get_all(
        resource_name="test", resource_orm=valid_people_orm, offset=0, limit=10
    )

    assert result == ({"test": [], "links": []}, 200)

    # When one item in DB - GET returns an item
    new_object = valid_people_orm(name="tester")
    db_session.add(new_object)
    db_session.commit()

    result = resource_handler.get_all(
        resource_name="test", resource_orm=valid_people_orm, offset=0, limit=10
    )

    assert result[0]["test"] == [{"id": 1, "name": "tester"}]
    assert result[1] == 200
def test_patch_works_when_item_exists(empty_database, valid_people_orm):
    # When something in DB - PATCH changes the item
    resource_handler = ResourceHandler()

    class FakeFlaskRequest:
        json = {"name": "tester"}

    new_object = valid_people_orm(name="tester")
    db_session.add(new_object)
    db_session.commit()

    result = resource_handler.update_one(
        id=1,
        resource_name="test",
        resource_orm=valid_people_orm,
        request=FakeFlaskRequest(),
        mode="PATCH",
    )

    assert result == ({
        "id": 1,
        "message": "Successfully updated resource."
    }, 200)