def test_get_type(): custom_type = "custom" register_custom_type(custom_type, ma.fields.String()) assert isinstance(get_type({"type": custom_type}), ma.fields.String) with pytest.raises(UnknownTypeException): get_type({"type": "unknown"})
def test_make_schema(): custom_type = "custom" register_custom_type(custom_type, ma.fields.String()) schema = { "labels": { "lab": { "type": custom_type, "validators": {} } }, "additional_members": { "custom": { "type": custom_type } }, } assert get_param_schema(schema) bad_schema = copy.deepcopy(schema) bad_schema["labels"]["lab"]["type"] = "unknown" with pytest.raises(UnknownTypeException): get_param_schema(bad_schema) bad_schema = copy.deepcopy(schema) bad_schema["additional_members"]["custom"]["type"] = "unknown" with pytest.raises(UnknownTypeException): get_param_schema(bad_schema) schema = { "labels": { "lab": { "type": custom_type, "validators": { "choice": { "choices": ["hello"] } }, } }, "additional_members": { "custom": { "type": custom_type } }, } with pytest.raises(ParamToolsError): get_param_schema(schema)
def test_register_custom_type(): """ Test allowed to register marshmallow field and PartialField instances and test that uninitialized fields and random classes throw type errors. """ custom_type = "custom" assert custom_type not in ALLOWED_TYPES register_custom_type(custom_type, ma.fields.String()) assert custom_type in ALLOWED_TYPES register_custom_type("partial-test", PartialField(ma.fields.Str(), {})) assert "partial-test" in ALLOWED_TYPES with pytest.raises(TypeError): register_custom_type("custom", ma.fields.Str) class Whatever: pass with pytest.raises(TypeError): register_custom_type("whatever", Whatever())
from tclocal.utils import json_to_dict class CompatibleDataSchema(ma.Schema): """ Schema for Compatible data object { "compatible_data": {"data1": bool, "data2": bool, ...} } """ puf = ma.fields.Boolean() cps = ma.fields.Boolean() pt.register_custom_type("compatible_data", ma.fields.Nested(CompatibleDataSchema())) class Parameters(pt.Parameters): """ Base Parameters class that wraps ParamTools, providing parameter indexing for tax policy in the adjust method and backwards-compatible preserving layer that supports Tax-Calculator's conventional reform formatting style as well as convenience methods like set_Year for classes operating on this one. The defaults file path may be set through the defaults class attribute variable or through the old DEFAULTS_FILE_NAME/DEFAULTS_FILE_PATH work flow. A custom getter method is implemented so that the value of a parameter
return paramtools.Parameters.read_params(obj, 'revision') # end of Specification class class DepreciationRules(ma.Schema): # set some field validation ranges that can't set in JSON life = ma.fields.Float(validate=ma.validate.Range(min=0, max=100)) method = ma.fields.String( validate=ma.validate.OneOf(choices=[ "SL", "Expensing", "DB 150%", "DB 200%", "Economic"]) ) # Register custom type defined above paramtools.register_custom_type("depreciation_rules", ma.fields.Nested(DepreciationRules())) class DepreciationParams(paramtools.Parameters): ''' Depreciation parameters class, contains model depreciation parameters. Inherits ParamTools Parameters abstract base class. ''' defaults = os.path.join( CURRENT_PATH, "..", "data", "depreciation_rates", "tax_depreciation_rules.json") def revision_warnings_errors(spec_revision): '''
def register_compatible_data(): register_custom_type("compatible_data", fields.Nested(CompatibleDataSchema()))