def test_extend_result(self): """ Result fields can also be extended with L{Schema.extend}. """ schema = Schema(result={'name': Unicode()}) schema2 = schema.extend( result={'id': Integer()}) result_structure = Structure(fields=schema2.result) self.assertEqual( {'name': u'foo', 'id': 5}, result_structure.coerce({'name': u'foo', 'id': '5'}))
def test_extend_result(self): """ Result fields can also be extended with L{Schema.extend}. """ schema = Schema(result={'name': Unicode()}) schema2 = schema.extend(result={'id': Integer()}) result_structure = Structure(fields=schema2.result) self.assertEqual({ 'name': u'foo', 'id': 5 }, result_structure.coerce({ 'name': u'foo', 'id': '5' }))
def test_structure(self): """ L{Schema}s with L{Structure} parameters can have arguments extracted. """ schema = Schema(Structure("foo", {"a": Integer(), "b": Integer()})) arguments, _ = schema.extract({"foo.a": "1", "foo.b": "2"}) self.assertEqual(1, arguments.foo.a) self.assertEqual(2, arguments.foo.b)
def test_extract_structure_with_optional(self): """L{Schema.extract} can handle optional parameters.""" schema = Schema( Structure("struct", fields={"name": Unicode(optional=True, default="radix")})) arguments, _ = schema.extract({"struct": {}}) self.assertEqual(u"radix", arguments.struct.name)
def test_new_parameters(self): """ L{Schema} accepts a C{parameters} parameter to specify parameters in a {name: field} format. """ schema = Schema( parameters=[Structure("foo", fields={"l": List(item=Integer())})]) arguments, _ = schema.extract({"foo.l.1": "1", "foo.l.2": "2"}) self.assertEqual([1, 2], arguments.foo.l)
def test_structure_of_structures(self): """L{Structure}s can be nested.""" sub_struct = Structure(fields={"a": Unicode(), "b": Unicode()}) schema = Schema( Structure("foo", fields={ "a": sub_struct, "b": sub_struct })) arguments, _ = schema.extract({ "foo.a.a": "a-a", "foo.a.b": "a-b", "foo.b.a": "b-a", "foo.b.b": "b-b" }) self.assertEqual("a-a", arguments.foo.a.a) self.assertEqual("a-b", arguments.foo.a.b) self.assertEqual("b-a", arguments.foo.b.a) self.assertEqual("b-b", arguments.foo.b.b)
def test_bundle_with_structure(self): """L{Schema.bundle} can bundle L{Structure}s.""" schema = Schema(parameters=[ Structure("struct", fields={ "field1": Unicode(), "field2": Integer() }) ]) params = schema.bundle(struct={"field1": "hi", "field2": 59}) self.assertEqual({ "struct.field1": "hi", "struct.field2": "59" }, params)
def test_parameter_doc(self): """ All L{Parameter} subclasses accept a 'doc' keyword argument. """ parameters = [ Unicode(doc="foo"), RawStr(doc="foo"), Integer(doc="foo"), Bool(doc="foo"), Enum(mapping={"hey": 1}, doc="foo"), Date(doc="foo"), List(item=Integer(), doc="foo"), Structure(fields={}, doc="foo") ] for parameter in parameters: self.assertEqual("foo", parameter.doc)
def test_list_of_structures(self): """L{List}s of L{Structure}s are extracted properly.""" schema = Schema( List("foo", Structure(fields={ "a": Integer(), "b": Integer() }))) arguments, _ = schema.extract({ "foo.1.a": "1", "foo.1.b": "2", "foo.2.a": "3", "foo.2.b": "4" }) self.assertEqual(1, arguments.foo[0]['a']) self.assertEqual(2, arguments.foo[0]['b']) self.assertEqual(3, arguments.foo[1]['a']) self.assertEqual(4, arguments.foo[1]['b'])
def test_structure_of_list(self): """L{Structure}s of L{List}s are extracted properly.""" schema = Schema(Structure("foo", fields={"l": List(item=Integer())})) arguments, _ = schema.extract({"foo.l.1": "1", "foo.l.2": "2"}) self.assertEqual([1, 2], arguments.foo.l)
def test_schema_field_names(self): structure = Structure(fields={"foo": Integer()}) self.assertEqual("foo", structure.fields["foo"].name)