def test_it_complex__fullset_decision():
    from alchemyjsonschema import (
        SchemaFactory,
        StructuralWalker,
        UseForeignKeyIfPossibleDecision,
    )
    from alchemyjsonschema.dictify import ModelLookup
    import alchemyjsonschema.tests.models as models
    from datetime import datetime

    factory = SchemaFactory(
        StructuralWalker, relation_decision=UseForeignKeyIfPossibleDecision())
    user_schema = factory(models.User)

    created_at = datetime(2000, 1, 1)
    user_dict = dict(name="foo", created_at=created_at,
                     group_id=1)  # pk is not found
    modellookup = ModelLookup(models)

    result = _callFUT(user_dict, user_schema, modellookup, strict=False)

    assert isinstance(result, models.User)
    assert result.pk is None
    assert result.name == "foo"
    assert result.created_at == datetime(2000, 1, 1)
    assert result.group_id is 1
    assert modellookup.name_stack == []
Example #2
0
def detect_decision(x):
    if x == "default":
        return RelationDesicion()
    elif x == "useforeignkey":
        return UseForeignKeyIfPossibleDecision()
    else:
        raise ValueError(x)
Example #3
0
def test_properties__include_OnetoMany_relation2():
    from alchemyjsonschema import StructuralWalker, UseForeignKeyIfPossibleDecision
    target = _makeOne(StructuralWalker,
                      relation_decision=UseForeignKeyIfPossibleDecision())
    result = target(User)

    assert "required" in result
    assert list(sorted(result["properties"])) == ["group_id", "name", "pk"]
    assert result["properties"]["group_id"] == {
        'type': 'integer',
        "relation": "group"
    }
Example #4
0
def test_properties__infinite_loop2():
    from alchemyjsonschema import StructuralWalker, UseForeignKeyIfPossibleDecision
    target = _makeOne(StructuralWalker,
                      relation_decision=UseForeignKeyIfPossibleDecision())
    result = target(X)
    assert "required" in result
    assert list(sorted(result["properties"])) == ["id", "y_id"]

    assert result["properties"]["y_id"] == {
        "type": "integer",
        "relation": "ys"
    }