def test_param_schema_explicit(): spec = load_spec( dict( id_name="x", name="x", category="Clean", parameters=[{"id_name": "whee", "type": "custom"}], param_schema={ "id_name": { "type": "dict", "properties": { "x": {"type": "integer"}, "y": {"type": "string", "default": "X"}, }, } }, ) ) assert spec.param_schema == ParamSchema.Dict( { "id_name": ParamSchema.Dict( {"x": ParamSchema.Integer(), "y": ParamSchema.String(default="X")} ) } )
def test_list_recurse(): assert parse({ "type": "list", "inner_dtype": { "type": "string" } }) == ParamSchema.List(ParamSchema.String())
def test_param_schema_implicit(): spec = load_spec( dict( id_name="googlesheets", name="x", category="Clean", parameters=[ {"id_name": "foo", "type": "string", "default": "X"}, { "id_name": "bar", "type": "secret", "secret_logic": {"provider": "oauth2", "service": "google"}, }, { "id_name": "baz", "type": "menu", "options": [ {"value": "a", "label": "A"}, "separator", {"value": "c", "label": "C"}, ], "default": "c", }, ], ) ) assert spec.param_schema == ParamSchema.Dict( { "foo": ParamSchema.String(default="X"), # secret is not in param_schema "baz": ParamSchema.Enum(choices=frozenset({"a", "c"}), default="c"), } )
def test_map_recurse(): assert parse({ "type": "map", "value_dtype": { "type": "string" } }) == ParamSchema.Map(value_schema=ParamSchema.String())
def test_dict_recurse(): assert parse({ "type": "dict", "properties": { "x": { "type": "string" } } }) == ParamSchema.Dict(properties={"x": ParamSchema.String()})
def test_clean_normal_dict(self): schema = ParamSchema.Dict({ "str": ParamSchema.String(), "int": ParamSchema.Integer() }) value = {"str": "foo", "int": 3} expected = dict(value) # no-op result = self._call_clean_value(schema, value) self.assertEqual(result, expected)
def test_clean_normal_dict(self): input_shape = TableMetadata(3, [Column("A", ColumnType.Number())]) schema = ParamSchema.Dict({ "str": ParamSchema.String(), "int": ParamSchema.Integer() }) value = {"str": "foo", "int": 3} expected = dict(value) # no-op result = clean_value(schema, value, input_shape) self.assertEqual(result, expected)
def _(self, schema: ParamSchema.Multichartseries, value: List[Dict[str, str]]) -> List[Dict[str, str]]: # Recurse to clean_value(ParamSchema.Column) to clear missing columns inner_schema = ParamSchema.Dict({ "color": ParamSchema.String(default="#000000"), "column": ParamSchema.Column(column_types=frozenset(["number"])), }) ret = [] error_agg = PromptingErrorAggregator() for v in value: try: clean_v = self.clean_value(inner_schema, v) if clean_v["column"]: # it's a valid column ret.append(clean_v) except PromptingError as err: error_agg.extend(err.errors) error_agg.raise_if_nonempty() return ret
def test_default(self): # [2019-06-05] We don't support non-None default on Option params assert S.Option(S.String(default="x")).default is None
def test_default(self): assert S.Map(S.String()).default == {}
def test_validate_ok(self): schema = S.Map(value_schema=S.String()) value = {"a": "b", "c": "d"} schema.validate(value)
def test_validate_not_dict(self): with pytest.raises(ValueError, match="not a dict"): S.Map(value_schema=S.String()).validate([])
def test_validate_invalid_child(self): with pytest.raises(ValueError, match="not a string"): S.Dict({"foo": S.String()}).validate({"foo": 3})
def test_validate_ok(self): S.Dict({"foo": S.String(default="FOO"), "bar": S.Integer(default=3)}).validate( {"foo": "FOO", "bar": 3} )
def test_validate_not_dict(self): with pytest.raises(ValueError, match="not a dict"): S.Dict({"foo": S.String()}).validate([])
def test_validate_zero_byte(self): with pytest.raises(ValueError): S.String().validate("A\x00B")
def test_validate_inner_ok(self): S.Option(S.String()).validate("foo")
def test_validate_lone_surrogate(self): with pytest.raises(ValueError): S.String().validate("A\ud802B")
def test_validate_non_str(self): with pytest.raises(ValueError): S.String().validate(23)
def test_validate_emoji(self): S.String().validate("💩") # do not raise
def test_validate_inner_error(self): with pytest.raises(ValueError): S.Option(S.String()).validate(3)
def test_validate_extra_key(self): with pytest.raises(ValueError, match="wrong keys"): S.Dict({"foo": S.String()}).validate({"foo": "x", "bar": "y"})
def test_default(self): assert S.Dict( {"foo": S.String(default="FOO"), "bar": S.Integer(default=3)} ).default == {"foo": "FOO", "bar": 3}
def test_validate_bad_inner_schema(self): with pytest.raises(ValueError, match="not a string"): S.Map(value_schema=S.String()).validate({"a": "1", "c": 2})