From 2b3d11a7a5a7b8e04ca333d96d51f1c52fef2007 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Tue, 20 Dec 2011 02:01:34 -0500 Subject: XEP-0009: Added value conversion unit tests Added tests for bidirectional conversion of all XML-RPC data types --- tests/test_stanza_xep_0009.py | 247 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 240 insertions(+), 7 deletions(-) (limited to 'tests') diff --git a/tests/test_stanza_xep_0009.py b/tests/test_stanza_xep_0009.py index 6186dd90..90cc879f 100644 --- a/tests/test_stanza_xep_0009.py +++ b/tests/test_stanza_xep_0009.py @@ -6,23 +6,27 @@ See the file LICENSE for copying permission. """ +import base64 + from sleekxmpp.plugins.xep_0009.stanza.RPC import RPCQuery, MethodCall, \ MethodResponse -from sleekxmpp.plugins.xep_0009.binding import py2xml +from sleekxmpp.plugins.xep_0009.binding import py2xml, xml2py, rpcbase64, \ + rpctime from sleekxmpp.stanza.iq import Iq from sleekxmpp.test.sleektest import SleekTest from sleekxmpp.xmlstream.stanzabase import register_stanza_plugin +from sleekxmpp.xmlstream.tostring import tostring import unittest class TestJabberRPC(SleekTest): - + def setUp(self): register_stanza_plugin(Iq, RPCQuery) - register_stanza_plugin(RPCQuery, MethodCall) + register_stanza_plugin(RPCQuery, MethodCall) register_stanza_plugin(RPCQuery, MethodResponse) - + def testMethodCall(self): iq = self.Iq() iq['rpc_query']['method_call']['method_name'] = 'system.exit' @@ -50,6 +54,235 @@ class TestJabberRPC(SleekTest): """, use_values=False) - -suite = unittest.TestLoader().loadTestsFromTestCase(TestJabberRPC) - + + def testConvertNil(self): + params = [None] + params_xml = py2xml(*params) + expected_xml = self.parse_xml(""" + + + + + + + + """) + self.failUnless(self.compare(expected_xml, params_xml), + "Nil to XML conversion\nExpected: %s\nGot: %s" % ( + tostring(expected_xml), tostring(params_xml))) + self.assertEqual(params, xml2py(expected_xml), + "XML to nil conversion") + + def testConvertBoolean(self): + params = [True, False] + params_xml = py2xml(*params) + expected_xml = self.parse_xml(""" + + + + 1 + + + + + 0 + + + + """) + self.failUnless(self.compare(expected_xml, params_xml), + "Boolean to XML conversion\nExpected: %s\nGot: %s" % ( + tostring(expected_xml), tostring(params_xml))) + self.assertEqual(params, xml2py(expected_xml), + "XML to boolean conversion") + + def testConvertString(self): + params = ["'This' & \"That\""] + params_xml = py2xml(*params) + expected_xml = self.parse_xml(""" + + + + 'This' & "That" + + + + """) + self.failUnless(self.compare(expected_xml, params_xml), + "String to XML conversion\nExpected: %s\nGot: %s" % ( + tostring(expected_xml), tostring(params_xml))) + self.assertEqual(params, xml2py(expected_xml), + "XML to string conversion") + + def testConvertInteger(self): + params = [32767, -32768] + params_xml = py2xml(*params) + expected_xml = self.parse_xml(""" + + + + 32767 + + + + + -32768 + + + + """) + alternate_xml = self.parse_xml(""" + + + + 32767 + + + + + -32768 + + + + """) + self.failUnless(self.compare(expected_xml, params_xml), + "Integer to XML conversion\nExpected: %s\nGot: %s" % ( + tostring(expected_xml), tostring(params_xml))) + self.assertEqual(params, xml2py(expected_xml), + "XML to boolean conversion") + self.assertEqual(params, xml2py(alternate_xml), + "Alternate XML to boolean conversion") + + + def testConvertDouble(self): + params = [3.14159265] + params_xml = py2xml(*params) + expected_xml = self.parse_xml(""" + + + + 3.14159265 + + + + """) + self.failUnless(self.compare(expected_xml, params_xml), + "Double to XML conversion\nExpected: %s\nGot: %s" % ( + tostring(expected_xml), tostring(params_xml))) + self.assertEqual(params, xml2py(expected_xml), + "XML to double conversion") + + def testConvertBase64(self): + params = [rpcbase64(base64.encodestring("Hello, world!"))] + params_xml = py2xml(*params) + expected_xml = self.parse_xml(""" + + + + SGVsbG8sIHdvcmxkIQ== + + + + """) + alternate_xml = self.parse_xml(""" + + + + SGVsbG8sIHdvcmxkIQ== + + + + """) + self.failUnless(self.compare(expected_xml, params_xml), + "Base64 to XML conversion\nExpected: %s\nGot: %s" % ( + tostring(expected_xml), tostring(params_xml))) + self.assertEqual(map(lambda x: x.decode(), params), + map(lambda x: x.decode(), xml2py(expected_xml)), + "XML to base64 conversion") + self.assertEqual(map(lambda x: x.decode(), params), + map(lambda x: x.decode(), xml2py(alternate_xml)), + "Alternate XML to base64 conversion") + + def testConvertDateTime(self): + params = [rpctime("20111220T01:50:00")] + params_xml = py2xml(*params) + expected_xml = self.parse_xml(""" + + + + 20111220T01:50:00 + + + + """) + self.failUnless(self.compare(expected_xml, params_xml), + "DateTime to XML conversion\nExpected: %s\nGot: %s" % ( + tostring(expected_xml), tostring(params_xml))) + self.assertEqual(map(lambda x: x.iso8601(), params), + map(lambda x: x.iso8601(), xml2py(expected_xml)), + None) + + def testConvertArray(self): + params = [[1,2,3], ('a', 'b', 'c')] + params_xml = py2xml(*params) + expected_xml = self.parse_xml(""" + + + + + + 1 + 2 + 3 + + + + + + + + + a + b + c + + + + + + """) + self.failUnless(self.compare(expected_xml, params_xml), + "Array to XML conversion\nExpected: %s\nGot: %s" % ( + tostring(expected_xml), tostring(params_xml))) + self.assertEqual(map(list, params), xml2py(expected_xml), + "XML to array conversion") + + def testConvertStruct(self): + params = [{"foo": "bar", "baz": False}] + params_xml = py2xml(*params) + expected_xml = self.parse_xml(""" + + + + + + foo + bar + + + baz + 0 + + + + + + """) + self.failUnless(self.compare(expected_xml, params_xml), + "Struct to XML conversion\nExpected: %s\nGot: %s" % ( + tostring(expected_xml), tostring(params_xml))) + self.assertEqual(params, xml2py(expected_xml), + "XML to struct conversion") + +suite = unittest.TestLoader().loadTestsFromTestCase(TestJabberRPC) + -- cgit v1.2.3