def test_schema_conversion_optional_list(self): """ Backwards-compatibility conversions maintains optional-ness of lists. """ schema = Schema(Unicode("foos.N", optional=True)) arguments, _ = schema.extract({}) self.assertEqual([], arguments.foos)
def test_optional_list(self): """ The default value of an optional L{List} is C{[]}. """ schema = Schema(List("names", Unicode(), optional=True)) arguments, _ = schema.extract({}) self.assertEqual([], arguments.names)
def test_extract_complex(self): """L{Schema} can cope with complex schemas.""" schema = Schema( Unicode("GroupName"), RawStr("IpPermissions.n.IpProtocol"), Integer("IpPermissions.n.FromPort"), Integer("IpPermissions.n.ToPort"), Unicode("IpPermissions.n.Groups.m.UserId", optional=True), Unicode("IpPermissions.n.Groups.m.GroupName", optional=True)) arguments, _ = schema.extract( {"GroupName": "Foo", "IpPermissions.1.IpProtocol": "tcp", "IpPermissions.1.FromPort": "1234", "IpPermissions.1.ToPort": "5678", "IpPermissions.1.Groups.1.GroupName": "Bar", "IpPermissions.1.Groups.2.GroupName": "Egg"}) self.assertEqual(u"Foo", arguments.GroupName) self.assertEqual(1, len(arguments.IpPermissions)) self.assertEqual(1234, arguments.IpPermissions[0].FromPort) self.assertEqual(5678, arguments.IpPermissions[0].ToPort) self.assertEqual(2, len(arguments.IpPermissions[0].Groups)) self.assertEqual("Bar", arguments.IpPermissions[0].Groups[0].GroupName) self.assertEqual("Egg", arguments.IpPermissions[0].Groups[1].GroupName)
def test_extract_with_single_numbered(self): """ L{Schema.extract} can handle a single parameter with a numbered value. """ schema = Schema(Unicode("name.n")) arguments, _ = schema.extract({"name.0": "Joe"}) self.assertEqual("Joe", arguments.name[0])
def test_extract_complex(self): """L{Schema} can cope with complex schemas.""" schema = Schema( Unicode("GroupName"), RawStr("IpPermissions.n.IpProtocol"), Integer("IpPermissions.n.FromPort"), Integer("IpPermissions.n.ToPort"), Unicode("IpPermissions.n.Groups.m.UserId", optional=True), Unicode("IpPermissions.n.Groups.m.GroupName", optional=True)) arguments, _ = schema.extract({ "GroupName": "Foo", "IpPermissions.1.IpProtocol": "tcp", "IpPermissions.1.FromPort": "1234", "IpPermissions.1.ToPort": "5678", "IpPermissions.1.Groups.1.GroupName": "Bar", "IpPermissions.1.Groups.2.GroupName": "Egg" }) self.assertEqual(u"Foo", arguments.GroupName) self.assertEqual(1, len(arguments.IpPermissions)) self.assertEqual(1234, arguments.IpPermissions[0].FromPort) self.assertEqual(5678, arguments.IpPermissions[0].ToPort) self.assertEqual(2, len(arguments.IpPermissions[0].Groups)) self.assertEqual("Bar", arguments.IpPermissions[0].Groups[0].GroupName) self.assertEqual("Egg", arguments.IpPermissions[0].Groups[1].GroupName)
def test_add_single_extra_schema_item(self): """New Parameters can be added to the Schema.""" schema = Schema(Unicode("name")) schema = schema.extend(Unicode("computer")) arguments, _ = schema.extract({"name": "value", "computer": "testing"}) self.assertEqual(u"value", arguments.name) self.assertEqual("testing", arguments.computer)
def test_extract_with_non_numbered_template(self): """ L{Schema.extract} accepts a single numbered argument even if the associated template is not numbered. """ schema = Schema(Unicode("name")) arguments, _ = schema.extract({"name.1": "foo"}) self.assertEqual("foo", arguments.name)
def test_extract_with_single_numbered(self): """ L{Schema.extract} can handle an un-numbered argument passed in to a numbered parameter. """ schema = Schema(Unicode("name.n")) arguments, _ = schema.extract({"name": "Joe"}) self.assertEqual("Joe", arguments.name[0])
def test_extract_with_numbered(self): """ L{Schema.extract} can handle parameters with numbered values. """ schema = Schema(Unicode("name.n")) arguments, _ = schema.extract({"name.0": "Joe", "name.1": "Tom"}) self.assertEqual("Joe", arguments.name[0]) self.assertEqual("Tom", arguments.name[1])
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_with_rest(self): """ L{Schema.extract} stores unknown parameters in the 'rest' return dictionary. """ schema = Schema() _, rest = schema.extract({"name": "value"}) self.assertEqual(rest, {"name": "value"})
def test_extract(self): """ L{Schema.extract} returns an L{Argument} object whose attributes are the arguments extracted from the given C{request}, as specified. """ schema = Schema(Unicode("name")) arguments, _ = schema.extract({"name": "value"}) self.assertEqual("value", arguments.name)
def test_extract_with_mixed(self): """ L{Schema.extract} stores in the rest result all numbered parameters given without an index. """ schema = Schema(Unicode("name.n")) _, rest = schema.extract({"name": "foo", "name.1": "bar"}) self.assertEqual(rest, {"name": "foo"})
def test_default_list(self): """ The default of a L{List} can be specified as a list. """ schema = Schema(List("names", Unicode(), optional=True, default=[u"foo", u"bar"])) arguments, _ = schema.extract({}) self.assertEqual([u"foo", u"bar"], arguments.names)
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_default_list(self): """ The default of a L{List} can be specified as a list. """ schema = Schema( List("names", Unicode(), optional=True, default=[u"foo", u"bar"])) arguments, _ = schema.extract({}) self.assertEqual([u"foo", u"bar"], arguments.names)
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_add_extra_schema_items(self): """A list of new Parameters can be added to the Schema.""" schema = Schema(Unicode("name")) schema = schema.extend(Unicode("computer"), Integer("count")) arguments, _ = schema.extract({"name": "value", "computer": "testing", "count": "5"}) self.assertEqual(u"value", arguments.name) self.assertEqual("testing", arguments.computer) self.assertEqual(5, arguments.count)
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_extract_with_goofy_numbered(self): """ L{Schema.extract} only uses the relative values of indices to determine the index in the resultant list. """ schema = Schema(Unicode("name.n")) arguments, _ = schema.extract({"name.5": "Joe", "name.10": "Tom"}) self.assertEqual("Joe", arguments.name[0]) self.assertEqual("Tom", arguments.name[1])
def test_list_of_list(self): """L{List}s can be nested.""" schema = Schema(List("foo", List(item=Unicode()))) arguments, _ = schema.extract( {"foo.1.1": "first-first", "foo.1.2": "first-second", "foo.2.1": "second-first", "foo.2.2": "second-second"}) self.assertEqual([["first-first", "first-second"], ["second-first", "second-second"]], arguments.foo)
def test_extract_with_optional_default(self): """ The value of C{default} on a parameter is used as the value when it is not provided as an argument and the parameter is C{optional}. """ schema = Schema(Unicode("name"), Integer("count", optional=True, default=5)) arguments, _ = schema.extract({"name": "value"}) self.assertEqual(u"value", arguments.name) self.assertEqual(5, arguments.count)
def test_schema_conversion_optional_structure_field(self): """ Backwards-compatibility conversion maintains optional-ness of structure fields. """ schema = Schema(Unicode("foos.N.field"), Unicode("foos.N.field2", optional=True, default=u"hi")) arguments, _ = schema.extract({"foos.0.field": u"existent"}) self.assertEqual(u"existent", arguments.foos[0].field) self.assertEqual(u"hi", arguments.foos[0].field2)
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_list_of_list(self): """L{List}s can be nested.""" schema = Schema(List("foo", List(item=Unicode()))) arguments, _ = schema.extract({ "foo.1.1": "first-first", "foo.1.2": "first-second", "foo.2.1": "second-first", "foo.2.2": "second-second" }) self.assertEqual([["first-first", "first-second"], ["second-first", "second-second"]], arguments.foo)
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_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_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_extract_with_nested_rest(self): schema = Schema() _, rest = schema.extract({"foo.1.bar": "hey", "foo.2.baz": "there"}) self.assertEqual({"foo.1.bar": "hey", "foo.2.baz": "there"}, rest)
def test_extract_with_many_arguments(self): """L{Schema.extract} can handle multiple parameters.""" schema = Schema(Unicode("name"), Integer("count")) arguments, _ = schema.extract({"name": "value", "count": "123"}) self.assertEqual(u"value", arguments.name) self.assertEqual(123, arguments.count)
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_extract_with_optional(self): """L{Schema.extract} can handle optional parameters.""" schema = Schema(Unicode("name"), Integer("count", optional=True)) arguments, _ = schema.extract({"name": "value"}) self.assertEqual(u"value", arguments.name) self.assertEqual(None, arguments.count)
def test_list(self): """L{List}s can be extracted.""" schema = Schema(List("foo", Integer())) arguments, _ = schema.extract({"foo.1": "1", "foo.2": "2"}) self.assertEqual([1, 2], arguments.foo)