Esempio n. 1
0
 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)
Esempio n. 2
0
 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)
Esempio n. 3
0
 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)
Esempio n. 4
0
 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)
Esempio n. 5
0
 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()])
Esempio n. 6
0
 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)
Esempio n. 7
0
 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)
Esempio n. 8
0
 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)
Esempio n. 9
0
 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'])
Esempio n. 10
0
 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)
Esempio n. 11
0
 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)
Esempio n. 12
0
 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)
Esempio n. 13
0
 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)