Ejemplo n.º 1
0
 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])
Ejemplo 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)
Ejemplo n.º 3
0
 def test_bundle_with_numbered(self):
     """
     L{Schema.bundle} correctly handles numbered arguments.
     """
     schema = Schema(Unicode("name.n"))
     params = schema.bundle(name=["foo", "bar"])
     self.assertEqual({"name.1": "foo", "name.2": "bar"}, params)
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
 def test_bundle_with_empty_numbered(self):
     """
     L{Schema.bundle} correctly handles an empty numbered arguments list.
     """
     schema = Schema(Unicode("name.n"))
     params = schema.bundle(name=[])
     self.assertEqual({}, params)
Ejemplo n.º 6
0
 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)
Ejemplo n.º 7
0
 def test_bundle_with_numbered_not_supplied(self):
     """
     L{Schema.bundle} ignores parameters that are not present.
     """
     schema = Schema(Unicode("name.n"))
     params = schema.bundle()
     self.assertEqual({}, params)
Ejemplo n.º 8
0
 def test_bundle_with_missing_parameter(self):
     """
     L{Schema.bundle} raises an exception one of the given parameters
     doesn't exist in the schema.
     """
     schema = Schema(Integer("count"))
     self.assertRaises(RuntimeError, schema.bundle, name="foo")
Ejemplo n.º 9
0
 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)
Ejemplo n.º 10
0
 def test_extend_errors(self):
     """
     Errors can be extended with L{Schema.extend}.
     """
     schema = Schema(parameters=[], errors=[APIError])
     schema2 = schema.extend(errors=[ZeroDivisionError])
     self.assertEqual(set([APIError, ZeroDivisionError]), schema2.errors)
Ejemplo n.º 11
0
 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"})
Ejemplo n.º 12
0
 def test_bundle(self):
     """
     L{Schema.bundle} returns a dictionary of raw parameters that
     can be used for an EC2-style query.
     """
     schema = Schema(Unicode("name"))
     params = schema.bundle(name="foo")
     self.assertEqual({"name": "foo"}, params)
Ejemplo n.º 13
0
 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)
Ejemplo n.º 14
0
 def test_bundle_with_arguments(self):
     """L{Schema.bundle} can bundle L{Arguments} too."""
     schema = Schema(Unicode("name.n"), Integer("count"))
     arguments = Arguments({"name": Arguments({1: "Foo", 7: "Bar"}),
                            "count": 123})
     params = schema.bundle(arguments)
     self.assertEqual({"name.1": "Foo", "name.7": "Bar", "count": "123"},
                      params)
Ejemplo n.º 15
0
 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])
Ejemplo n.º 16
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])
Ejemplo n.º 17
0
 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)
Ejemplo n.º 18
0
 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)
Ejemplo n.º 19
0
 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"})
Ejemplo n.º 20
0
 def test_bundle_with_multiple(self):
     """
     L{Schema.bundle} correctly handles multiple arguments.
     """
     schema = Schema(Unicode("name.n"), Integer("count"))
     params = schema.bundle(name=["Foo", "Bar"], count=123)
     self.assertEqual({"name.1": "Foo", "name.2": "Bar", "count": "123"},
                      params)
Ejemplo n.º 21
0
 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)
Ejemplo n.º 22
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)
Ejemplo n.º 23
0
 def test_schema_conversion_list(self):
     """
     Backwards-compatibility conversion maintains the name of lists.
     """
     schema = Schema(Unicode("foos.N"))
     parameters = schema.get_parameters()
     self.assertEqual(1, len(parameters))
     self.assertTrue(isinstance(parameters[0], List))
     self.assertEqual("foos", parameters[0].name)
Ejemplo n.º 24
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)
Ejemplo n.º 25
0
 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)
Ejemplo n.º 26
0
 def test_extend_maintains_parameter_order(self):
     """
     Extending a schema with additional parameters puts the new parameters
     at the end.
     """
     schema = Schema(parameters=[Unicode("name"), Unicode("value")])
     schema2 = schema.extend(parameters=[Integer("foo"), Unicode("index")])
     self.assertEqual(["name", "value", "foo", "index"],
                      [p.name for p in schema2.get_parameters()])
Ejemplo n.º 27
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()])
Ejemplo n.º 28
0
 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])
Ejemplo n.º 29
0
 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)
Ejemplo n.º 30
0
 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"))
     self.assertRaises(InconsistentParameterError, schema.extract, {
         "nameFOOO": "foo",
         "nameFOOO.1": "bar"
     })