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_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_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_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_get_parameters_order_on_parameter_only_construction(self): """ L{Schema.get_parameters} returns the original list of L{Parameter}s even when they are passed as positional arguments to L{Schema}. """ schema = Schema(Unicode("name"), List("scores", Integer()), Integer("index", Integer())) self.assertEqual(["name", "scores", "index"], [p.name for p in schema.get_parameters()])
def test_get_parameters(self): """ L{Schema.get_parameters} returns the original list of parameters. """ schema = Schema( parameters=[Unicode("name"), List("scores", Integer())]) parameters = schema.get_parameters() self.assertEqual("name", parameters[0].name) self.assertEqual("scores", parameters[1].name)
def test_coerce_list(self): """ When a L{List} coerces the value of one of its item, it uses the the proper name in the C{MissingParameter} error raised. """ parameter = List("foo", Unicode()) error = self.assertRaises(APIError, parameter.item.coerce, "") self.assertEqual(400, error.status) self.assertEqual("MissingParameter", error.code) self.assertEqual("The request must contain the parameter foo " "(str)", error.message)
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_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)
def test_bundle_with_list_with_arguments(self): """L{Schema.bundle} can bundle L{List}s (specified as L{Arguments}).""" schema = Schema(parameters=[List("things", item=Unicode())]) params = schema.bundle(things=Arguments({1: "foo", 2: "bar"})) self.assertEqual({"things.1": "foo", "things.2": "bar"}, params)
def test_bundle_with_list(self): """L{Schema.bundle} can bundle L{List}s.""" schema = Schema(parameters=[List("things", item=Unicode())]) params = schema.bundle(things=["foo", "bar"]) self.assertEqual({"things.1": "foo", "things.2": "bar"}, params)