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"}
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])
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}
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}
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"}
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"}
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}
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"}
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 } }
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}
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]}