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,
        )
Exemple #2
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)
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_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)
Exemple #6
0
def test_create_models_can_add_data_with_orm(valid_data_dictionary,
                                             empty_database):
    table_descriptors = valid_data_dictionary["data"]
    base = create_models(table_descriptors)

    people_orm = getattr(base.classes, "people")
    team_orm = getattr(base.classes, "team")

    person1 = people_orm(name="testperson")
    team1 = team_orm(name="testteam")

    person1.team_collection.append(team1)

    db_session.add(team1)
    db_session.add(person1)

    db_session.commit()
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())
Exemple #8
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_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_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)
Exemple #11
0
    def insert_one(self, resource_orm: object = None, request: Request = None):
        """Insert a new object.

        Args:
            resource_orm (object): SQLAlchemy ORM model.
            request_obj (flask.Request): HTTP request object.

        Return:
            dict, int: The response object and associated HTTP status code.
        """
        try:
            request_obj = request.json
        except Exception:
            raise ApiError("No request body found.", 400)

        try:
            new_object = resource_orm()
            for key, value in request_obj.items():
                setattr(new_object, key, value)

            db_session.add(new_object)
            db_session.commit()
            # Can we get primary key(s) from sqlalchemy model?
            # https://stackoverflow.com/questions/6745189/how-do-i-get-the-name-of-an-sqlalchemy-objects-primary-key
            # id_value = getattr(new_object, table_schema["primaryKey"])
            id_value = new_object.id

            return {
                "message": "Successfully added new resource.",
                "id": id_value
            }, 201
        except Exception:
            # wrong type -- psycopg2.errors.InvalidTextRepresentation
            # IntegrityError('(psycopg2.errors.NotNullViolation) null value in column "required" violates not-null constraint\nDETAIL:  Failing row contains (1, null, 1).\n')
            db_session.rollback()
            raise ApiError("Failed to create new resource.", 400)