Example #1
0
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")}
            )
        }
    )
Example #2
0
def test_list_recurse():
    assert parse({
        "type": "list",
        "inner_dtype": {
            "type": "string"
        }
    }) == ParamSchema.List(ParamSchema.String())
Example #3
0
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"),
        }
    )
Example #4
0
def test_map_recurse():
    assert parse({
        "type": "map",
        "value_dtype": {
            "type": "string"
        }
    }) == ParamSchema.Map(value_schema=ParamSchema.String())
Example #5
0
def test_dict_recurse():
    assert parse({
        "type": "dict",
        "properties": {
            "x": {
                "type": "string"
            }
        }
    }) == ParamSchema.Dict(properties={"x": ParamSchema.String()})
Example #6
0
 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)
Example #7
0
 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)
Example #8
0
    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
Example #10
0
 def test_default(self):
     assert S.Map(S.String()).default == {}
Example #11
0
 def test_validate_ok(self):
     schema = S.Map(value_schema=S.String())
     value = {"a": "b", "c": "d"}
     schema.validate(value)
Example #12
0
 def test_validate_not_dict(self):
     with pytest.raises(ValueError, match="not a dict"):
         S.Map(value_schema=S.String()).validate([])
Example #13
0
 def test_validate_invalid_child(self):
     with pytest.raises(ValueError, match="not a string"):
         S.Dict({"foo": S.String()}).validate({"foo": 3})
Example #14
0
 def test_validate_ok(self):
     S.Dict({"foo": S.String(default="FOO"), "bar": S.Integer(default=3)}).validate(
         {"foo": "FOO", "bar": 3}
     )
Example #15
0
 def test_validate_not_dict(self):
     with pytest.raises(ValueError, match="not a dict"):
         S.Dict({"foo": S.String()}).validate([])
Example #16
0
 def test_validate_zero_byte(self):
     with pytest.raises(ValueError):
         S.String().validate("A\x00B")
Example #17
0
 def test_validate_inner_ok(self):
     S.Option(S.String()).validate("foo")
Example #18
0
 def test_validate_lone_surrogate(self):
     with pytest.raises(ValueError):
         S.String().validate("A\ud802B")
Example #19
0
 def test_validate_non_str(self):
     with pytest.raises(ValueError):
         S.String().validate(23)
Example #20
0
 def test_validate_emoji(self):
     S.String().validate("💩")  # do not raise
Example #21
0
 def test_validate_inner_error(self):
     with pytest.raises(ValueError):
         S.Option(S.String()).validate(3)
Example #22
0
 def test_validate_extra_key(self):
     with pytest.raises(ValueError, match="wrong keys"):
         S.Dict({"foo": S.String()}).validate({"foo": "x", "bar": "y"})
Example #23
0
 def test_default(self):
     assert S.Dict(
         {"foo": S.String(default="FOO"), "bar": S.Integer(default=3)}
     ).default == {"foo": "FOO", "bar": 3}
Example #24
0
 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})