コード例 #1
0
def test_get_or_create_object():
    model = model_factory({
        "type": "object",
        "properties": {
            "l": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "a": {
                            "type": "string"
                        },
                        "b": {
                            "type": "string"
                        },
                    },
                },
            },
        },
    })
    obj = model()
    obj.l.get_or_create(a="foo")
    assert obj == {"l": [{"a": "foo"}]}
    obj.l.get_or_create(a="foo")
    assert obj == {"l": [{"a": "foo"}]}
    obj.l.get_or_create(a="foo", b="bar")
    assert obj == {"l": [{"a": "foo"}, {"a": "foo", "b": "bar"}]}
    obj.l.get_or_create(a="foo", b="bar")
    assert obj == {"l": [{"a": "foo"}, {"a": "foo", "b": "bar"}]}
コード例 #2
0
def test_get_or_create_simple():
    model = model_factory({
        "type": "object",
        "properties": {
            "l": {
                "type": "array",
                "items": {
                    "type": "string"
                }
            },
        },
    })
    obj = model()
    obj.l.get_or_create("foo")
    obj.l.get_or_create("foo")
    assert obj == {"l": ["foo"]}
    obj.l.get_or_create("bar")
    assert obj == {"l": ["foo", "bar"]}
コード例 #3
0
def test_name():
    model = model_factory({
        "type": "object",
        "name": "FooObject",
        "properties": {
            "foo": {
                "type": "object",
                "properties": {
                    "foo": {
                        "type": "string"
                    },
                },
            },
        },
    })
    obj = model(foo={"foo": "bar"})
    assert obj.__class__.__name__ == "FooObject"
    assert obj.foo.__class__.__name__ == "Object"
コード例 #4
0
def test_get_or_create_invalid():
    model = model_factory({
        "type": "object",
        "properties": {
            "l": {
                "type": "array",
                "items": {
                    "type": "string"
                }
            },
        },
    })
    obj = model()
    with pytest.raises(RuntimeError):
        obj.l.get_or_create("foo", "bar")
    with pytest.raises(RuntimeError):
        obj.l.get_or_create("foo", a="bar")
    with pytest.raises(MultipleItemFound):
        obj.l.add("foo")
        obj.l.add("foo")
        obj.l.get_or_create("foo")
コード例 #5
0
def Model():
    # evil nested schema
    schema = {
        "name": "Model",
        "type": "object",
        "properties": {
            "foo": {
                "type": "string"
            },
            "bar": {
                "type": "array",
                "items": {
                    "type": "string"
                }
            },
        }
    }
    orig = copy.deepcopy(schema)
    schema["properties"]["zaz"] = orig
    schema["properties"]["qux"] = {"type": "array", "items": orig}
    return model_factory(schema)