Esempio n. 1
0
 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
Esempio n. 2
0
 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']
Esempio n. 3
0
 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 == {}
Esempio n. 4
0
 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
Esempio n. 5
0
 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]')
Esempio n. 6
0
 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}'))
Esempio n. 7
0
 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
Esempio n. 8
0
 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']
Esempio n. 9
0
 def test_type_not_found(self):
     Parser('{type: str}')
     with pytest.raises(SchemaError):
         Parser('{type: Foo}')
Esempio n. 10
0
 def test_elements_and_mapping(self):
     with pytest.raises(SchemaError):
         Parser('{elements: {}, mapping: {}}')
Esempio n. 11
0
 def test_mapping_non_dict(self):
     with pytest.raises(SchemaError):
         Parser('{mapping: None}')
     with pytest.raises(SchemaError):
         Parser('{mapping: [13]}')
Esempio n. 12
0
 def test_nested_non_dicts(self):
     with pytest.raises(SchemaError):
         Parser('None')
Esempio n. 13
0
 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
Esempio n. 14
0
 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)]
Esempio n. 15
0
 def test_arbitrary_elements(self):
     parser = Parser('{type: list, elements:}')
     assert parser('[1, 2, Foo]') == [1, 2, 'Foo']
Esempio n. 16
0
 def test_arguments(self):
     parser = Parser(filename('schema/reference_arguments.yaml'))
     assert parser('reference: {type: ClassNoArgument}').foo.value == 42
Esempio n. 17
0
 def test_arguments_default(self):
     parser = Parser(filename('schema/reference_arguments_default.yaml'))
     assert parser('{}').foo.value == 42
Esempio n. 18
0
 def test_parse_elements(self):
     assert Parser('')('[1, 2, Foo]') == [1, 2, 'Foo']
Esempio n. 19
0
 def test_attrdict_attr_access(self):
     parser = Parser('{type: dict, mapping: {foo: {}}}')
     definition = parser('{foo: bar}', attrdicts=True)
     assert definition.foo == 'bar'
Esempio n. 20
0
 def test_dict(self):
     schema = filename('schema/reference_dict.yaml')
     definition = Parser(schema)('reference: 42')
     assert definition.foo == 42
Esempio n. 21
0
 def test_dict_dict_access(self):
     parser = Parser('{type: dict, mapping: {foo: {}}}')
     definition = parser('{foo: bar}', attrdicts=False)
     assert definition['foo'] == 'bar'
Esempio n. 22
0
 def test_dict_nested(self):
     schema = filename('schema/reference_nested.yaml')
     definition = Parser(schema)('reference: {nested: 42}')
     print(definition)
     assert definition.foo.nested == 42
Esempio n. 23
0
 def test_elements_non_dict(self):
     with pytest.raises(SchemaError):
         Parser('{elements: None}')
     with pytest.raises(SchemaError):
         Parser('{elements: [13]}')
Esempio n. 24
0
 def test_elements(self):
     schema = filename('schema/reference_elements.yaml')
     definition = Parser(schema)('reference: 42')
     assert definition.foo == [42, 42]
Esempio n. 25
0
 def test_arguments_and_elements(self):
     with pytest.raises(SchemaError):
         Parser('{arguments: {}, elements: {}}')
Esempio n. 26
0
 def test_index(self):
     schema = filename('schema/reference_index.yaml')
     definition = Parser(schema)('reference: [13, 42]')
     assert definition.first == 13
     assert definition.second == 42
Esempio n. 27
0
 def test_type_found(self):
     Parser('{type: str}')
Esempio n. 28
0
 def test_index_out_of_bounds(self):
     schema = filename('schema/reference_index.yaml')
     with pytest.raises(DefinitionError):
         Parser(schema)('reference: [13]')
Esempio n. 29
0
 def test_empty(self):
     Parser('')
Esempio n. 30
0
 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]