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)
def test_contains(self): """ The presence of a certain argument can be inspected using the 'in' keyword. .""" arguments = Arguments({"foo": 1}) self.assertIn("foo", arguments) self.assertNotIn("bar", arguments)
def test_bundle_with_arguments_and_extra(self): """ L{Schema.bundle} can bundle L{Arguments} with keyword arguments too. Keyword arguments take precedence. """ schema = Schema(Unicode("name.n"), Integer("count")) arguments = Arguments({"name": {1: "Foo", 7: "Bar"}, "count": 321}) params = schema.bundle(arguments, count=123) self.assertEqual({"name.1": "Foo", "name.2": "Bar", "count": "123"}, params)
def test_bundle_with_structure_with_arguments(self): """ L{Schema.bundle} can bundle L{Structure}s (specified as L{Arguments}). """ schema = Schema(parameters=[ Structure("struct", fields={ "field1": Unicode(), "field2": Integer() }) ]) params = schema.bundle(struct=Arguments({ "field1": "hi", "field2": 59 })) self.assertEqual({ "struct.field1": "hi", "struct.field2": "59" }, params)
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_nested_data_with_numbers(self): """L{Arguments} can cope fine with list items.""" arguments = Arguments({"foo": {1: "egg"}}) self.assertEqual("egg", arguments.foo[0])
def test_nested_data(self): """L{Arguments} can cope fine with nested data structures.""" arguments = Arguments({"foo": Arguments({"bar": "egg"})}) self.assertEqual("egg", arguments.foo.bar)
def test_len(self): """C{len()} can be used with an L{Arguments} instance.""" self.assertEqual(0, len(Arguments({}))) self.assertEqual(1, len(Arguments({1: 2})))
def test_getitem_error(self): """L{KeyError} is raised when the argument is not found.""" arguments = Arguments({}) self.assertRaises(KeyError, arguments.__getitem__, 1)
def test_getitem(self): """Values can be looked up using C{[index]} notation.""" arguments = Arguments({1: "a", 2: "b", "foo": "bar"}) self.assertEqual("b", arguments[2]) self.assertEqual("bar", arguments["foo"])
def test_iterate(self): """L{Arguments} returns an iterator with both keys and values.""" arguments = Arguments({"foo": 123, "bar": 456}) self.assertEqual([("foo", 123), ("bar", 456)], list(arguments))
def test_instantiate_non_empty(self): """Creating an L{Arguments} object with some arguments.""" arguments = Arguments({"foo": 123, "bar": 456}) self.assertEqual(123, arguments.foo) self.assertEqual(456, arguments.bar)
def test_instantiate_empty(self): """Creating an L{Arguments} object.""" arguments = Arguments({}) self.assertEqual({}, arguments.__dict__)