def test_has_default_has_value(self): schema = filename('schema/date_default.yaml') parser = Parser(schema) definition = filename('definition/date.yaml') definition = parser(definition) assert isinstance(definition, date) assert definition.year == 13
def test_has_default_has_value(self): schema = filename('schema/list_default.yaml') parser = Parser(schema) definition = filename('definition/list.yaml') definition = parser(definition) assert isinstance(definition, list) assert definition == ['Foo']
def test_nested(self): schema, definition = '{}', '{}' for _ in range(5): schema = schema.replace('{}', '{type: dict, mapping: {key: {}}}') definition = definition.replace('{}', '{key: {}}') definition = Parser(schema)(definition, attrdicts=True) assert definition.key.key.key.key.key == {}
def test_additional(self): parser = Parser('{type: BaseClassArgs, module: test.test_type}') definition = '{type: SubClassArgs, argument1: 42, argument2: 13}' definition = parser(definition) assert isinstance(definition, SubClassArgs) assert definition.argument1 == 42 assert definition.argument2 == 13
def test_type(self): parser = Parser('{type: list, elements: {type: int}}') assert parser('[1, 2, 3]') == [1, 2, 3] assert parser('- 1\n- 2\n- 3') == [1, 2, 3] with pytest.raises(DefinitionError): parser('Foo') with pytest.raises(DefinitionError): parser('[Foo, Bar]')
def test_reject_non_list(self): parser = Parser('{type: list, elements: {}}') with pytest.raises(DefinitionError): print(parser('None')) with pytest.raises(DefinitionError): print(parser('42')) with pytest.raises(DefinitionError): print(parser('{foo: bar}'))
def test_example(self): parser = Parser(filename('schema/readme_example.yaml')) definition = parser(filename('definition/readme_example.yaml')) assert isinstance(definition.cost, SquaredError) assert len(definition.constraints) == 2 assert all(isinstance(x, Constraint) for x in definition.constraints) assert definition.constraints[0].angle == 70 assert definition.constraints[1].angle == 120 assert isinstance(definition.distribution, Gaussian) assert definition.distribution.mean == 0 assert definition.distribution.variance == 2.5
def test_intro(self): parser = Parser(filename('schema/readme_intro.yaml')) definition = parser(filename('definition/readme_intro.yaml')) assert definition.key1 == 'foo' assert isinstance(definition.key2, SubClassName) assert isinstance(definition.key3, ClassName) assert definition.key3.argument1 == 'foo' assert definition.key3.argument2 == 42 assert isinstance(definition.key4, SubClassName) assert definition.key4.argument1 == 'foo' assert definition.key4.argument2 == 42 assert definition.key5 == ['value1', 'value2', 'value3']
def test_type_not_found(self): Parser('{type: str}') with pytest.raises(SchemaError): Parser('{type: Foo}')
def test_elements_and_mapping(self): with pytest.raises(SchemaError): Parser('{elements: {}, mapping: {}}')
def test_mapping_non_dict(self): with pytest.raises(SchemaError): Parser('{mapping: None}') with pytest.raises(SchemaError): Parser('{mapping: [13]}')
def test_nested_non_dicts(self): with pytest.raises(SchemaError): Parser('None')
def test_dict_attr_access(self): parser = Parser('{type: dict, mapping: {foo: {}}}') definition = parser('{foo: bar}', attrdicts=False) with pytest.raises(AttributeError): # pylint: disable=pointless-statement definition.foo
def test_arguments(self): parser = Parser( '{type: list, elements: {type: date, module: datetime, arguments: ' '{year: {type: int}, month: {type: int}, day: {type: int}}}}') assert parser('[{year: 42, month: 3, day: 8}]') == [date(42, 3, 8)]
def test_arbitrary_elements(self): parser = Parser('{type: list, elements:}') assert parser('[1, 2, Foo]') == [1, 2, 'Foo']
def test_arguments(self): parser = Parser(filename('schema/reference_arguments.yaml')) assert parser('reference: {type: ClassNoArgument}').foo.value == 42
def test_arguments_default(self): parser = Parser(filename('schema/reference_arguments_default.yaml')) assert parser('{}').foo.value == 42
def test_parse_elements(self): assert Parser('')('[1, 2, Foo]') == [1, 2, 'Foo']
def test_attrdict_attr_access(self): parser = Parser('{type: dict, mapping: {foo: {}}}') definition = parser('{foo: bar}', attrdicts=True) assert definition.foo == 'bar'
def test_dict(self): schema = filename('schema/reference_dict.yaml') definition = Parser(schema)('reference: 42') assert definition.foo == 42
def test_dict_dict_access(self): parser = Parser('{type: dict, mapping: {foo: {}}}') definition = parser('{foo: bar}', attrdicts=False) assert definition['foo'] == 'bar'
def test_dict_nested(self): schema = filename('schema/reference_nested.yaml') definition = Parser(schema)('reference: {nested: 42}') print(definition) assert definition.foo.nested == 42
def test_elements_non_dict(self): with pytest.raises(SchemaError): Parser('{elements: None}') with pytest.raises(SchemaError): Parser('{elements: [13]}')
def test_elements(self): schema = filename('schema/reference_elements.yaml') definition = Parser(schema)('reference: 42') assert definition.foo == [42, 42]
def test_arguments_and_elements(self): with pytest.raises(SchemaError): Parser('{arguments: {}, elements: {}}')
def test_index(self): schema = filename('schema/reference_index.yaml') definition = Parser(schema)('reference: [13, 42]') assert definition.first == 13 assert definition.second == 42
def test_type_found(self): Parser('{type: str}')
def test_index_out_of_bounds(self): schema = filename('schema/reference_index.yaml') with pytest.raises(DefinitionError): Parser(schema)('reference: [13]')
def test_empty(self): Parser('')
def test_index_both(self): parser = Parser(filename('schema/two_lists.yaml')) definition = parser("{foo: [13, 42], bar: ['$foo[1]', '$foo[0]']}") assert definition.bar == [42, 13]