Exemplo n.º 1
0
def test_builds_resource_with_relationship_not_in_included():
    class Relationship(BaseResource):
        id: str = resource_id()
        attribute1: str = attribute()

    class Resource(BaseResource):
        id: str = resource_id()
        relationship1: Relationship = relationship()

    result = build_resource(
        JsonApiCallContext(
            data={
                "id": "100",
                "relationships": {
                    "relationship1": {
                        "data": {
                            "id": "42",
                            "type": "relationships"
                        }
                    }
                },
            }),
        Resource(),
    )

    assert result.relationship1.id == "42"
Exemplo n.º 2
0
def test_build_resource_with_cyclic_dependency():
    class Resource(BaseResource):
        id: str = resource_id()
        relationship1: ForwardRef("Relationship") = relationship()

    class Relationshup(BaseResource):
        id: str = resource_id()

    result = build_resource(
        JsonApiCallContext(
            data={
                "id": "100",
                "relationships": {
                    "relationship1": {
                        "data": {
                            "id": "42",
                            "type": "relationships"
                        }
                    }
                },
            }),
        Resource(),
    )

    assert result.relationship1.id == "42"
Exemplo n.º 3
0
def test_builds_resource_with_id():
    class Resource(BaseResource):
        id: str = resource_id()

    result = build_resource(JsonApiCallContext(data={"id": "42"}), Resource())

    assert result.id == "42"
Exemplo n.º 4
0
def test_build_resource_raises_if_relationship_is_not_optional():
    class Relationship(BaseResource):
        id: str = resource_id()

    class Resource(BaseResource):
        id: str = resource_id()
        relationship1: Relationship = relationship()

    with pytest.raises(ValueError, match=r".* relationship1 .*"):
        build_resource(
            JsonApiCallContext(data={
                "id": "100",
                "relationships": {}
            }), Resource())
Exemplo n.º 5
0
def test_builds_resource_raises_type_error():
    class Resource(BaseResource):
        id: str = resource_id()
        attribute1: str = attribute()

    with pytest.raises(TypeError, match=r".* attribute1 .*"):
        build_resource(
            JsonApiCallContext(data={
                "id": "42",
                "attributes": {
                    "attribute1": 1
                }
            }),
            Resource(),
        )
Exemplo n.º 6
0
def test_builds_resource_with_decoded_attributes():
    class Resource(BaseResource):
        id: str = resource_id()
        attribute1: date = attribute(decoder=date.fromisoformat)

    result = build_resource(
        JsonApiCallContext(data={
            "id": "42",
            "attributes": {
                "attribute1": "2019-07-04"
            }
        }),
        Resource(),
    )

    assert result.attribute1 == date(2019, 7, 4)
Exemplo n.º 7
0
def test_builds_resource_with_attributes():
    class Resource(BaseResource):
        id: str = resource_id()
        attribute1: str = attribute()

    result = build_resource(
        JsonApiCallContext(data={
            "id": "42",
            "attributes": {
                "attribute1": "value"
            }
        }),
        Resource(),
    )

    assert result.attribute1 == "value"
Exemplo n.º 8
0
def test_build_resource_with_non_optional_relationship_raise_error():
    class Relationship(BaseResource):
        id: str = resource_id()

    class Resource(BaseResource):
        id: str = resource_id()
        relationship1: Relationship = relationship()

    with pytest.raises(ValueError, match=r".* relationship1 .*"):
        build_resource(
            JsonApiCallContext(data={
                "id": "100",
                "relationships": {
                    "relationship1": {
                        "data": None
                    }
                }
            }),
            Resource(),
        )
Exemplo n.º 9
0
def test_build_resource_with_optional_relationship():
    class Relationship(BaseResource):
        id: str = resource_id()

    class Resource(BaseResource):
        id: str = resource_id()
        relationship1: Optional[Relationship] = relationship()

    result = build_resource(
        JsonApiCallContext(data={
            "id": "100",
            "relationships": {
                "relationship1": {
                    "data": None
                }
            }
        }),
        Resource(),
    )

    assert result.relationship1 is None
def test_find_in_include_returns_none_not_found():
    include = {"type": "articles", "id": "100", "attributes": {"attribute1": "value1"}}
    json_api_call_context = JsonApiCallContext(data={}, included=[include])
    assert json_api_call_context.find_in_included("bla", "42") is None
def test_get_attribute():
    json_api_call_context = JsonApiCallContext(
        data={"attributes": {"attribute1": "value1"}}
    )
    assert json_api_call_context.get_attribute("attribute1") == "value1"
def test_find_in_included_returns_none_if_included_not_set():
    json_api_call_context = JsonApiCallContext(data={})
    assert json_api_call_context.find_in_included("articles", "100") is None
def test_get_id():
    json_api_call_context = JsonApiCallContext(data={"id": "2"})
    assert json_api_call_context.get_id() == "2"
def test_find_in_included():
    include = {"type": "articles", "id": "100", "attributes": {"attribute1": "value1"}}
    json_api_call_context = JsonApiCallContext(data={}, included=[include])
    assert json_api_call_context.find_in_included("articles", "100") == include
def test_get_relationship_returns_none_if_not_exist():
    json_api_call_context = JsonApiCallContext(
        data={"relationships": {"author": "some_relationship"}}
    )
    assert json_api_call_context.get_relationship("unknown") is None
def test_get_relationship():
    json_api_call_context = JsonApiCallContext(
        data={"relationships": {"author": "some_relationship"}}
    )
    assert json_api_call_context.get_relationship("author") == "some_relationship"