예제 #1
0
def test_construct_schema_union():
    def fun(x: Union[int, str]):
        pass

    model_type = schema.construct_schema("FunSchema", fun)
    assert model_type({"x": 1}).to_native() == {"x": 1}
    assert model_type({"x": "hi"}).to_native() == {"x": "hi"}
예제 #2
0
def test_construct_schema_numpy():
    def fun(x: np.ndarray):
        pass

    model_type = schema.construct_schema("FunSchema", fun)
    model_native = model_type({"x": np.array([1, 2, 3])}).to_native()
    assert list(model_native.keys()) == ["x"]
    np.testing.assert_array_equal(model_native["x"], [1, 2, 3])
예제 #3
0
def test_construct_schema_2positional():
    def fun(x: int, y: float):
        pass

    model_type = schema.construct_schema("FunSchema",
                                         fun,
                                         skip_first_arg=False)
    assert model_type({"x": 5, "y": 2}).to_native() == {"x": 5, "y": 2}
예제 #4
0
def test_construct_schema_default():
    def fun(x: int = 3):
        pass

    model_type = schema.construct_schema("FunSchema",
                                         fun,
                                         skip_first_arg=False)
    assert model_type().to_native() == {"x": 3}
    assert model_type({"x": 5}).to_native() == {"x": 5}
예제 #5
0
def test_construct_schema_nested():
    def fun(x: Union[int, List[Union[int, str]]]):
        pass

    model_type = schema.construct_schema("FunSchema", fun)
    assert model_type({"x": 1}).to_native() == {"x": 1}
    assert model_type({"x": ["hi"]}).to_native() == {"x": ["hi"]}
    assert model_type({"x": [2, "hi"]}).to_native() == {"x": [2, "hi"]}
    with pytest.raises(models.DataError):
        model_type({"x": "hi"}).to_native() == {"x": "hi"}
예제 #6
0
def test_construct_schema_1positional():
    def fun(x: int):
        pass

    model_type = schema.construct_schema("FunSchema",
                                         fun,
                                         skip_first_arg=False)
    assert model_type({"x": 5}).to_native() == {"x": 5}
    with pytest.raises(models.DataError):
        model_type({"x": "hi"}).to_native() == {"x": "hi"}
예제 #7
0
def test_construct_schema_optplan_base_model():
    def fun(x: int):
        pass

    model_type = schema.construct_schema("FunSchema",
                                         fun,
                                         base_classes=(optplan.Model, ))
    data = model_type(x=3)
    data.validate()
    assert data.to_native() == {"x": 3}
예제 #8
0
def test_construct_schema_nested():
    def fun(x: Union[int, List[Union[int, str]]]):
        pass

    model_type = schema.construct_schema("FunSchema", fun)
    assert model_type({"x": 1}).to_native() == {"x": 1}
    assert model_type({"x": ["hi"]}).to_native() == {"x": ["hi"]}
    #TODO(@jskarda,@jesse): Determine why this commented assert fails
    #   when gitlab runs all tests:
    #assert model_type({"x": [2, "hi"]}).to_native() == {"x": [2, "hi"]}
    with pytest.raises(models.DataError):
        model_type({"x": "hi"}).to_native() == {"x": "hi"}
예제 #9
0
def test_construct_schema_schematics_model():
    @optplan.polymorphic_model()
    class Model(optplan.Model):
        type = optplan.ModelNameType("model")
        y = optplan.types.IntType()

    def fun(x: Model):
        pass

    model_type = schema.construct_schema("FunSchema", fun)
    assert model_type({
        "x": {
            "type": "model",
            "y": 3
        }
    }).to_native() == {
        "x": {
            "type": "model",
            "y": 3
        }
    }
예제 #10
0
def test_construct_schema_complex():
    def fun(x: complex):
        pass

    model_type = schema.construct_schema("FunSchema", fun)
    assert model_type({"x": 1 + 2j}).to_native() == {"x": 1 + 2j}
예제 #11
0
def test_construct_schema_list():
    def fun(x: List[int]):
        pass

    model_type = schema.construct_schema("FunSchema", fun)
    assert model_type({"x": [1, 2, 3]}).to_native() == {"x": [1, 2, 3]}