mirror of
https://github.com/correl/SleekXMPP.git
synced 2024-11-27 19:19:54 +00:00
* added tests
This commit is contained in:
parent
137ef61523
commit
218c7318e0
5 changed files with 115 additions and 27 deletions
|
@ -4,7 +4,7 @@ from . error import Error
|
||||||
from . rootstanza import RootStanza
|
from . rootstanza import RootStanza
|
||||||
|
|
||||||
class Message(RootStanza):
|
class Message(RootStanza):
|
||||||
interfaces = set(('type', 'to', 'from', 'id', 'body', 'subject'))
|
interfaces = set(('type', 'to', 'from', 'id', 'body', 'subject', 'groupchat'))
|
||||||
types = set((None, 'normal', 'chat', 'headline', 'error', 'groupchat'))
|
types = set((None, 'normal', 'chat', 'headline', 'error', 'groupchat'))
|
||||||
sub_interfaces = set(('body', 'subject'))
|
sub_interfaces = set(('body', 'subject'))
|
||||||
name = 'message'
|
name = 'message'
|
||||||
|
@ -23,6 +23,8 @@ class Message(RootStanza):
|
||||||
|
|
||||||
def reply(self, body=None):
|
def reply(self, body=None):
|
||||||
StanzaBase.reply(self)
|
StanzaBase.reply(self)
|
||||||
|
if self['type'] == 'groupchat':
|
||||||
|
self['to'] = self['to'].bare
|
||||||
del self['id']
|
del self['id']
|
||||||
if body is not None:
|
if body is not None:
|
||||||
self['body'] = body
|
self['body'] = body
|
||||||
|
|
65
testall.py
Normal file
65
testall.py
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
#!/usr/bin/python2.6
|
||||||
|
import unittest
|
||||||
|
import logging
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
class testoverall(unittest.TestCase):
|
||||||
|
|
||||||
|
def testModules(self):
|
||||||
|
"""Testing all modules by compiling them"""
|
||||||
|
import compileall
|
||||||
|
import re
|
||||||
|
self.failUnless(compileall.compile_dir('.' + os.sep + 'sleekxmpp', rx=re.compile('/[.]svn'), quiet=True))
|
||||||
|
|
||||||
|
def testTabNanny(self):
|
||||||
|
"""Invoking the tabnanny"""
|
||||||
|
import tabnanny
|
||||||
|
self.failIf(tabnanny.check("." + os.sep + 'sleekxmpp'))
|
||||||
|
|
||||||
|
def testMethodLength(self):
|
||||||
|
"""Testing for excessive method lengths"""
|
||||||
|
import re
|
||||||
|
dirs = os.walk(sys.path[0] + os.sep + 'sleekxmpp')
|
||||||
|
offenders = []
|
||||||
|
for d in dirs:
|
||||||
|
if not '.svn' in d[0]:
|
||||||
|
for filename in d[2]:
|
||||||
|
if filename.endswith('.py') and d[0].find("template%stemplates" % os.sep) == -1:
|
||||||
|
with open("%s%s%s" % (d[0],os.sep,filename), "r") as fp:
|
||||||
|
cur = None
|
||||||
|
methodline = lineno = methodlen = methodindent = 0
|
||||||
|
for line in fp:
|
||||||
|
indentlevel = re.compile("^[\t ]*").search(line).end()
|
||||||
|
line = line.expandtabs()
|
||||||
|
lineno += 1
|
||||||
|
if line.strip().startswith("def ") or line.strip().startswith("except") or (line.strip() and methodindent > indentlevel) or (line.strip() and methodindent == indentlevel): #new method found or old one ended
|
||||||
|
if cur: #existing method needs final evaluation
|
||||||
|
if methodlen > 50 and not cur.strip().startswith("def setupUi"):
|
||||||
|
offenders.append("Method '%s' on line %s of %s/%s is longer than 50 lines (%s)" % (cur.strip(),methodline,d[0][len(rootp):],filename,methodlen))
|
||||||
|
methodlen = 0
|
||||||
|
cur = line
|
||||||
|
methodindent = indentlevel
|
||||||
|
methodline = lineno
|
||||||
|
if line and cur and not line.strip().startswith("#") and not (cur.strip().startswith("try:") and methodindent == 0): #if we weren't all whitespace and weren't a comment
|
||||||
|
methodlen += 1
|
||||||
|
self.failIf(offenders,"\n".join(offenders))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
logging.basicConfig(level=100)
|
||||||
|
logging.disable(100)
|
||||||
|
#this doesn't need to be very clean
|
||||||
|
alltests = [unittest.TestLoader().loadTestsFromTestCase(testoverall)]
|
||||||
|
rootp = sys.path[0] + os.sep + 'tests'
|
||||||
|
dirs = os.walk(rootp)
|
||||||
|
for d in dirs:
|
||||||
|
if not '.svn' in d[0]:
|
||||||
|
for filename in d[2]:
|
||||||
|
if filename.startswith('test_') and filename.endswith('.py'):
|
||||||
|
modname = ('tests' + "." + filename)[:-3].replace(os.sep,'.')
|
||||||
|
__import__(modname)
|
||||||
|
#sys.modules[modname].config = moduleconfig
|
||||||
|
alltests.append(sys.modules[modname].suite)
|
||||||
|
alltests_suite = unittest.TestSuite(alltests)
|
||||||
|
unittest.TextTestRunner(verbosity=2).run(alltests_suite)
|
|
@ -1,26 +0,0 @@
|
||||||
from sleekxmpp.plugins.stanza_pubsub import *
|
|
||||||
|
|
||||||
def testAffiliations():
|
|
||||||
iq = Iq()
|
|
||||||
aff1 = Affiliation()
|
|
||||||
aff1['node'] = 'testnode'
|
|
||||||
aff1['affiliation'] = 'owner'
|
|
||||||
aff2 = Affiliation()
|
|
||||||
aff2['node'] = 'testnode2'
|
|
||||||
aff2['affiliation'] = 'publisher'
|
|
||||||
iq['pubsub']['affiliations'].append(aff1)
|
|
||||||
iq['pubsub']['affiliations'].append(aff2)
|
|
||||||
print(iq)
|
|
||||||
iq2 = Iq(None, ET.fromstring("""<iq id="0"><pubsub xmlns="http://jabber.org/protocol/pubsub"><affiliations><affiliation node="testnode" affiliation="owner" /><affiliation node="testnode2" affiliation="publisher" /></affiliations></pubsub></iq>"""))
|
|
||||||
iq3 = Iq()
|
|
||||||
values = iq2.getValues()
|
|
||||||
print(values)
|
|
||||||
iq3.setValues(values)
|
|
||||||
print("-"*8)
|
|
||||||
print(iq3.keys())
|
|
||||||
|
|
||||||
print(iq3)
|
|
||||||
print(str(iq) == str(iq2) == str(iq3))
|
|
||||||
|
|
||||||
|
|
||||||
testAffiliations()
|
|
19
tests/test_messagestanzas.py
Normal file
19
tests/test_messagestanzas.py
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
class testmessagestanzas(unittest.TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
import sleekxmpp.stanza.message as m
|
||||||
|
self.m = m
|
||||||
|
|
||||||
|
def testGroupchatReplyRegression(self):
|
||||||
|
"Regression groupchat reply should be to barejid"
|
||||||
|
msg = self.m.Message()
|
||||||
|
msg['to'] = 'me@myserver.tld'
|
||||||
|
msg['from'] = 'room@someservice.someserver.tld/somenick'
|
||||||
|
msg['type'] = 'groupchat'
|
||||||
|
msg['body'] = "this is a message"
|
||||||
|
msg.reply()
|
||||||
|
self.failUnless(str(msg['to']) == 'room@someservice.someserver.tld')
|
||||||
|
|
||||||
|
suite = unittest.TestLoader().loadTestsFromTestCase(testmessagestanzas)
|
28
tests/test_pubsubstanzas.py
Normal file
28
tests/test_pubsubstanzas.py
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
class testpubsubstanzas(unittest.TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
import sleekxmpp.plugins.stanza_pubsub as ps
|
||||||
|
self.ps = ps
|
||||||
|
|
||||||
|
def testAffiliations(self):
|
||||||
|
"Testing iq/pubsub/affiliations/affiliation stanzas"
|
||||||
|
iq = self.ps.Iq()
|
||||||
|
aff1 = self.ps.Affiliation()
|
||||||
|
aff1['node'] = 'testnode'
|
||||||
|
aff1['affiliation'] = 'owner'
|
||||||
|
aff2 = self.ps.Affiliation()
|
||||||
|
aff2['node'] = 'testnode2'
|
||||||
|
aff2['affiliation'] = 'publisher'
|
||||||
|
iq['pubsub']['affiliations'].append(aff1)
|
||||||
|
iq['pubsub']['affiliations'].append(aff2)
|
||||||
|
xmlstring = """<iq id="0"><pubsub xmlns="http://jabber.org/protocol/pubsub"><affiliations><affiliation node="testnode" affiliation="owner" /><affiliation node="testnode2" affiliation="publisher" /></affiliations></pubsub></iq>"""
|
||||||
|
iq2 = self.ps.Iq(None, self.ps.ET.fromstring(xmlstring))
|
||||||
|
iq3 = self.ps.Iq()
|
||||||
|
values = iq2.getValues()
|
||||||
|
iq3.setValues(values)
|
||||||
|
self.failUnless(xmlstring == str(iq) == str(iq2) == str(iq3))
|
||||||
|
|
||||||
|
|
||||||
|
suite = unittest.TestLoader().loadTestsFromTestCase(testpubsubstanzas)
|
Loading…
Reference in a new issue