Esempio n. 1
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)
Esempio n. 2
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)
Esempio n. 3
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()])
Esempio n. 4
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)
Esempio n. 5
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)
Esempio n. 6
0
 def test_schema_conversion_structure_name(self):
     """
     Backwards-compatibility conversion maintains the names of fields in
     structures.
     """
     schema = Schema(Unicode("foos.N.field"), Unicode("foos.N.field2"))
     parameters = schema.get_parameters()
     self.assertEqual(1, len(parameters))
     self.assertTrue(isinstance(parameters[0], List))
     self.assertEqual("foos", parameters[0].name)
     self.assertEqual("N", parameters[0].item.name)
     self.assertEqual("field", parameters[0].item.fields["field"].name)
     self.assertEqual("field2", parameters[0].item.fields["field2"].name)
Esempio n. 7
0
 def test_bundle_with_two_numbered(self):
     """
     L{Schema.bundle} can bundle multiple numbered lists.
     """
     schema = Schema(Unicode("names.n"), Unicode("things.n"))
     params = schema.bundle(names=["foo", "bar"], things=["baz", "quux"])
     self.assertEqual(
         {
             "names.1": "foo",
             "names.2": "bar",
             "things.1": "baz",
             "things.2": "quux"
         }, params)
Esempio n. 8
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)
Esempio n. 9
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)
Esempio n. 10
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])
Esempio n. 11
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)
Esempio n. 12
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. 13
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)
Esempio n. 14
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)
Esempio n. 15
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)
Esempio n. 16
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)
Esempio n. 17
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"})
Esempio n. 18
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])
Esempio n. 19
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)
Esempio n. 20
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])
Esempio n. 21
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)
Esempio n. 22
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)
Esempio n. 23
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. 24
0
 def test_invalid_unicode(self):
     """
     The L{Unicode} parameter returns an error with invalid unicode data.
     """
     parameter = Unicode("Test")
     error = self.assertRaises(APIError, parameter.coerce, "Test\x95Error")
     self.assertIn(u"Invalid unicode value", error.message)
     self.assertEqual(400, error.status)
     self.assertEqual("InvalidParameterValue", error.code)
Esempio n. 25
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. 26
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])
Esempio n. 27
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)
Esempio n. 28
0
 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)
Esempio n. 29
0
    def test_additional_schema_attributes(self):
        """
        Additional data can be specified on the Schema class for specifying a
        more rich schema.
        """
        result = {'id': Integer(), 'name': Unicode(), 'data': RawStr()}
        errors = [APIError]

        schema = Schema(name="GetStuff",
                        doc="""Get the stuff.""",
                        parameters=[Integer("id"),
                                    Unicode("scope")],
                        result=result,
                        errors=errors)

        self.assertEqual("GetStuff", schema.name)
        self.assertEqual("Get the stuff.", schema.doc)
        self.assertEqual(result, schema.result)
        self.assertEqual(set(errors), schema.errors)
Esempio 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"
     })