def test_get_with_non_existing_index(self): """An error is raised when trying to access a non existing item.""" schema = SequenceSchema("foo", NodeSchema("item", [LeafSchema("bar")])) root = etree.fromstring("<foo><item><bar>egg</bar></item></foo>") foo = schema.create(root) error = self.assertRaises(WSDLParseError, foo.__getitem__, 1) self.assertEqual("Non existing item in tag 'foo'", error.message)
def test_set_with_leaf(self): """ L{SequenceSchema.set} raises an error if the given child is a leaf node """ schema = SequenceSchema("foo") error = self.assertRaises(RuntimeError, schema.set, LeafSchema("bar")) self.assertEqual("Sequence can't have leaf children", str(error))
def test_get(self): """ The child elements of a L{SequenceItem} can be accessed as attributes. """ schema = SequenceSchema("foo", NodeSchema("item", [LeafSchema("bar")])) root = etree.fromstring("<foo><item><bar>egg</bar></item></foo>") foo = schema.create(root) self.assertEqual("egg", foo[0].bar)
def test_set_with_previous_child(self): """ L{SequenceSchema.set} raises an error if the sequence has already a child. """ schema = SequenceSchema("foo", NodeSchema("item", [LeafSchema("bar")])) error = self.assertRaises(RuntimeError, schema.set, NodeSchema("egg")) self.assertEqual("Sequence has already a child", str(error))
def test_dump(self): """ L{SequenceSchema.dump} creates a L{etree.Element} out of a L{SequenceItem}. """ schema = SequenceSchema("foo", NodeSchema("item", [LeafSchema("bar")])) foo = SequenceItem(schema) foo.append().bar = "egg" self.assertEqual("<foo><item><bar>egg</bar></item></foo>", etree.tostring(schema.dump(foo)))
def test_get_items(self): """L{SequenceItem} supports elements with many child items.""" schema = SequenceSchema("foo", NodeSchema("item", [LeafSchema("bar")])) root = etree.fromstring("<foo>" "<item><bar>egg0</bar></item>" "<item><bar>egg1</bar></item>" "</foo>") foo = schema.create(root) self.assertEqual("egg0", foo[0].bar) self.assertEqual("egg1", foo[1].bar)
def test_create_with_bad_tag(self): """ L{SequenceSchema.create} raises an error if the tag of the given element doesn't match the expected one. """ schema = SequenceSchema("foo", NodeSchema("item", [LeafSchema("bar")])) root = etree.fromstring("<spam><item><bar>egg</bar></item></spam>") error = self.assertRaises(WSDLParseError, schema.create, root) self.assertEqual("Expected response with tag 'foo', but got " "'spam' instead", error.args[0])
def test_iter(self): """L{SequenceItem} objects are iterable.""" schema = SequenceSchema("foo", NodeSchema("item", [LeafSchema("bar")])) root = etree.fromstring("<foo>" "<item><bar>egg0</bar></item>" "<item><bar>egg1</bar></item>" "</foo>") foo = schema.create(root) [item0, item1] = list(foo) self.assertEqual("egg0", item0.bar) self.assertEqual("egg1", item1.bar)
def test_remove_with_non_existing_item(self): """ L{SequenceItem.remove} raises an exception when trying to remove a non existing item """ schema = SequenceSchema("foo", NodeSchema("item", [LeafSchema("bar")])) root = etree.fromstring("<foo><item><bar>egg</bar></item></foo>") foo = schema.create(root) item = foo.remove(foo[0]) error = self.assertRaises(WSDLParseError, foo.remove, item) self.assertEqual("Non existing item in tag 'foo'", error.message)
def test_append_with_too_many_items(self): """ An error is raised when trying to append items above the max. """ schema = SequenceSchema("foo") schema.set(NodeSchema("item", [LeafSchema("bar")]), min_occurs=0, max_occurs=1) root = etree.fromstring("<foo><item><bar>egg</bar></item></foo>") foo = schema.create(root) error = self.assertRaises(WSDLParseError, foo.append) self.assertEqual("Too many items in tag 'foo'", error.args[0]) self.assertEqual(1, len(list(foo)))
def test_dump_with_many_items(self): """ L{SequenceSchema.dump} supports many child items in the sequence. """ schema = SequenceSchema("foo", NodeSchema("item", [LeafSchema("bar")])) foo = SequenceItem(schema) foo.append().bar = "spam0" foo.append().bar = "spam1" self.assertEqual( "<foo>" "<item><bar>spam0</bar></item>" "<item><bar>spam1</bar></item>" "</foo>", etree.tostring(schema.dump(foo)))
def test_delitem_with_not_enough_items(self): """ L{SequenceItem.__delitem__} raises an error if trying to remove an item would make the sequence shorter than the required minimum. """ schema = SequenceSchema("foo") schema.set(NodeSchema("item", [LeafSchema("bar")]), min_occurs=1, max_occurs=10) root = etree.fromstring("<foo><item><bar>egg</bar></item></foo>") foo = schema.create(root) error = self.assertRaises(WSDLParseError, foo.__delitem__, 0) self.assertEqual("Not enough items in tag 'foo'", error.args[0]) self.assertEqual(1, len(list(foo)))
def test_set_with_no_min_or_max(self): """ L{SequenceSchema.set} raises an error if no values are provided for the min and max parameters. """ schema = SequenceSchema("foo") child = NodeSchema("item", [LeafSchema("bar")]) error = self.assertRaises(RuntimeError, schema.set, child, min_occurs=0, max_occurs=None) self.assertEqual("Sequence node without min or max", str(error)) error = self.assertRaises(RuntimeError, schema.set, child, min_occurs=None, max_occurs=1) self.assertEqual("Sequence node without min or max", str(error))
def test_remove(self): """ L{SequenceItem.remove} removes the given item from the sequence. """ schema = SequenceSchema("foo", NodeSchema("item", [LeafSchema("bar")])) root = etree.fromstring("<foo>" "<item><bar>egg0</bar></item>" "<item><bar>egg1</bar></item>" "</foo>") foo = schema.create(root) foo.remove(foo[0]) self.assertEqual("egg1", foo[0].bar) self.assertEqual("<foo><item><bar>egg1</bar></item></foo>", etree.tostring(schema.dump(foo)))
def test_set_with_sequence_tag(self): """ It is possible to set a sequence tag to C{None}, in that case all its children will be removed """ schema = NodeSchema("foo") schema.add( SequenceSchema("bar", NodeSchema("item", [LeafSchema("egg")]))) root = etree.fromstring("<foo>" "<bar><item><egg>spam</egg></item></bar><" "/foo>") foo = schema.create(root) foo.bar = None self.assertEqual("<foo><bar/></foo>", etree.tostring(schema.dump(foo)))
def test_delitem(self): """ L{SequenceItem.__delitem__} removes from the sequence the item with the given index. """ schema = SequenceSchema("foo", NodeSchema("item", [LeafSchema("bar")])) root = etree.fromstring("<foo>" "<item><bar>egg0</bar></item>" "<item><bar>egg1</bar></item>" "</foo>") foo = schema.create(root) del foo[0] self.assertEqual("egg1", foo[0].bar) self.assertEqual("<foo><item><bar>egg1</bar></item></foo>", etree.tostring(schema.dump(foo)))
def test_append(self): """ L{SequenceItem.append} adds a new item to the sequence, appending it at the end. """ schema = SequenceSchema("foo", NodeSchema("item", [LeafSchema("bar")])) root = etree.fromstring("<foo><item><bar>egg0</bar></item></foo>") foo = schema.create(root) foo.append().bar = "egg1" self.assertEqual("egg1", foo[1].bar) self.assertEqual( "<foo>" "<item><bar>egg0</bar></item>" "<item><bar>egg1</bar></item>" "</foo>", etree.tostring(schema.dump(foo)))
def test_get_with_index_higher_than_max(self): """ An error is raised when trying to access an item above the allowed max value. """ schema = SequenceSchema("foo") schema.set(NodeSchema("item", [LeafSchema("bar")]), min_occurs=0, max_occurs=1) root = etree.fromstring("<foo>" "<item><bar>egg0</bar></item>" "<item><bar>egg1</bar></item>" "</foo>") foo = schema.create(root) error = self.assertRaises(WSDLParseError, foo.__getitem__, 1) self.assertEqual("Out of range item in tag 'foo'", error.args[0])