def test_clean_normal_dict(self): input_shape = TableShape(3, [Column('A', ColumnType.NUMBER())]) schema = ParamDType.Dict({ 'str': ParamDType.String(), 'int': ParamDType.Integer(), }) value = {'str': 'foo', 'int': 3} expected = dict(value) # no-op result = clean_value(schema, value, input_shape) self.assertEqual(result, expected)
def test_clean_normal_dict(self): context = RenderContext(None, None, None, None, None) schema = ParamDType.Dict({ 'str': ParamDType.String(), 'int': ParamDType.Integer(), }) value = {'str': 'foo', 'int': 3} expected = dict(value) # no-op result = clean_value(schema, value, context) self.assertEqual(result, expected)
def test_clean_normal_dict(self): input_shape = TableShape(3, [Column("A", ColumnType.NUMBER())]) schema = ParamDType.Dict({ "str": ParamDType.String(), "int": ParamDType.Integer() }) value = {"str": "foo", "int": 3} expected = dict(value) # no-op result = clean_value(schema, value, input_shape) self.assertEqual(result, expected)
def test_param_schema_explicit(self): mv = ModuleVersion.create_or_replace_from_spec( { "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" }, }, } }, }, source_version_hash="1.0", ) self.assertEqual( repr(mv.param_schema), repr( ParamDType.Dict({ "id_name": ParamDType.Dict({ "x": ParamDType.Integer(), "y": ParamDType.String(default="X"), }) })), )
def test_param_schema_explicit(self): mv = ModuleVersion.create_or_replace_from_spec({ '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'}, }, }, }, }, source_version_hash='1.0') self.assertEqual(repr(mv.param_schema), repr(ParamDType.Dict({ 'id_name': ParamDType.Dict({ 'x': ParamDType.Integer(), 'y': ParamDType.String(default='X'), }), })))
def test_map_parse(self): dtype = ParamDType.parse({ 'type': 'map', 'value_dtype': { 'type': 'dict', # test nesting 'properties': { 'foo': {'type': 'string'}, }, }, }) self.assertEqual(repr(dtype), repr(ParamDType.Map( value_dtype=ParamDType.Dict(properties={ 'foo': ParamDType.String(), }) )))
def test_dict_prompting_error_concatenate_same_type(self): context = RenderContext(None, None, TableShape(3, [ Column('A', ColumnType.TEXT()), Column('B', ColumnType.TEXT()), ]), None, None) schema = ParamDType.Dict({ 'x': ParamDType.Column(column_types=frozenset({'number'})), 'y': ParamDType.Column(column_types=frozenset({'number'})), }) with self.assertRaises(PromptingError) as cm: clean_value(schema, {'x': 'A', 'y': 'B'}, context) self.assertEqual(cm.exception.errors, [ PromptingError.WrongColumnType(['A', 'B'], 'text', frozenset({'number'})), ])
def test_param_schema_implicit(self): mv = ModuleVersion.create_or_replace_from_spec({ 'id_name': 'x', 'name': 'x', 'category': 'Clean', 'parameters': [ {'id_name': 'foo', 'type': 'string', 'default': 'X'}, {'id_name': 'bar', 'type': 'secret', 'name': 'Secret'}, {'id_name': 'baz', 'type': 'menu', 'options': [ {'value': 'a', 'label': 'A'}, 'separator', {'value': 'c', 'label': 'C'}, ], 'default': 'c'}, ] }, source_version_hash='1.0') self.assertEqual(repr(mv.param_schema), repr(ParamDType.Dict({ 'foo': ParamDType.String(default='X'), 'baz': ParamDType.Enum(choices=frozenset({'a', 'c'}), default='c'), })))
def test_map_parse(self): dtype = ParamDType.parse( { "type": "map", "value_dtype": { "type": "dict", # test nesting "properties": {"foo": {"type": "string"}}, }, } ) self.assertEqual( repr(dtype), repr( ParamDType.Map( value_dtype=ParamDType.Dict(properties={"foo": ParamDType.String()}) ) ), )
def test_clean_multicolumn_from_other_tab_that_does_not_exist(self): # The other tab would not exist if the user selected and then deleted # it. workflow = Workflow.create_and_init() workflow.tabs.first() schema = ParamDType.Dict({ 'tab': ParamDType.Tab(), 'columns': ParamDType.Multicolumn(tab_parameter='tab'), }) param_values = {'tab': 'tab-missing', 'columns': ['A-from-tab']} params = Params(schema, param_values, {}) context = RenderContext(workflow.id, None, TableShape(3, [ Column('A-from-tab-1', ColumnType.NUMBER()), ]), {}, params) result = clean_value(schema, param_values, context) # result['tab'] is not what we're testing here self.assertEqual(result['columns'], [])
def test_dict_prompting_error(self): input_shape = TableShape(3, [ Column('A', ColumnType.TEXT()), Column('B', ColumnType.TEXT()), ]) schema = ParamDType.Dict({ 'col1': ParamDType.Column(column_types=frozenset({'number'})), 'col2': ParamDType.Column(column_types=frozenset({'datetime'})), }) with self.assertRaises(PromptingError) as cm: clean_value(schema, {'col1': 'A', 'col2': 'B'}, input_shape) self.assertEqual(cm.exception.errors, [ PromptingError.WrongColumnType(['A'], 'text', frozenset({'number' })), PromptingError.WrongColumnType(['B'], 'text', frozenset({'datetime'})), ])
def test_dict_prompting_error(self): input_shape = TableShape( 3, [Column("A", ColumnType.TEXT()), Column("B", ColumnType.TEXT())] ) schema = ParamDType.Dict( { "col1": ParamDType.Column(column_types=frozenset({"number"})), "col2": ParamDType.Column(column_types=frozenset({"datetime"})), } ) with self.assertRaises(PromptingError) as cm: clean_value(schema, {"col1": "A", "col2": "B"}, input_shape) self.assertEqual( cm.exception.errors, [ PromptingError.WrongColumnType(["A"], "text", frozenset({"number"})), PromptingError.WrongColumnType(["B"], "text", frozenset({"datetime"})), ], )
def test_clean_multicolumn_from_other_tab_that_does_not_exist(self): # The other tab would not exist if the user selected and then deleted # it. workflow = Workflow.create_and_init() workflow.tabs.first() schema = ParamDType.Dict({ "tab": ParamDType.Tab(), "columns": ParamDType.Multicolumn(tab_parameter="tab"), }) params = {"tab": "tab-missing", "columns": ["A-from-tab"]} context = RenderContext( workflow.id, None, TableShape(3, [Column("A-from-tab-1", ColumnType.NUMBER())]), {}, params, ) result = clean_value(schema, params, context) # result['tab'] is not what we're testing here self.assertEqual(result["columns"], [])