Move unit tests in src module hierarchy

- Python cannot merge a same module from to different folder so merge 'src' and 'tests' folder into one.

darcs-hash:20070513141150-86b55-892f79fdc18fb69f5b8513ea7a31b01f8eb68f0e.gz
This commit is contained in:
David Rousselie
2007-05-13 16:11:50 +02:00
parent 046bade075
commit 74aa7b02df
14 changed files with 164 additions and 150 deletions

View File

@@ -0,0 +1,15 @@
"""JCL test module"""
__revision__ = ""
import unittest
from jcl.jabber.tests import component, feeder
def suite():
suite = unittest.TestSuite()
suite.addTest(component.suite())
suite.addTest(feeder.suite())
return suite
if __name__ == '__main__':
unittest.main(defaultTest='suite')

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,186 @@
# -*- coding: utf-8 -*-
##
## test_feeder.py
## Login : David Rousselie <dax@happycoders.org>
## Started on Wed Aug 9 21:34:26 2006 David Rousselie
## $Id$
##
## Copyright (C) 2006 David Rousselie
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##
import unittest
import os
import threading
import time
import sys
from sqlobject import *
from sqlobject.dbconnection import TheURIOpener
from pyxmpp.message import Message
from jcl.jabber.component import JCLComponent
from jcl.jabber.feeder import FeederComponent, Feeder, Sender
from jcl.model.account import Account
from jcl.model import account
from jcl.model.tests.account import ExampleAccount, Example2Account
from jcl.jabber.tests.component import JCLComponent_TestCase, MockStream
if sys.platform == "win32":
DB_PATH = "/c|/temp/test.db"
else:
DB_PATH = "/tmp/test.db"
DB_URL = DB_PATH #+ "?debug=1&debugThreading=1"
class FeederComponent_TestCase(JCLComponent_TestCase):
def setUp(self):
if os.path.exists(DB_PATH):
os.unlink(DB_PATH)
self.comp = FeederComponent("jcl.test.com",
"password",
"localhost",
"5347",
'sqlite://' + DB_URL)
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
Account.createTable(ifNotExists = True)
ExampleAccount.createTable(ifNotExists = True)
Example2Account.createTable(ifNotExists = True)
del account.hub.threadConnection
def tearDown(self):
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
Account.dropTable(ifExists = True)
ExampleAccount.dropTable(ifExists = True)
Example2Account.dropTable(ifExists = True)
del TheURIOpener.cachedURIs['sqlite://' + DB_URL]
account.hub.threadConnection.close()
del account.hub.threadConnection
if os.path.exists(DB_PATH):
os.unlink(DB_PATH)
def test_run(self):
self.comp.time_unit = 1
self.comp.stream = MockStream()
self.comp.stream_class = MockStream
run_thread = threading.Thread(target = self.comp.run, \
name = "run_thread")
run_thread.start()
time.sleep(1)
self.comp.running = False
self.assertTrue(self.comp.stream.connection_started)
time.sleep(JCLComponent.timeout + 1)
threads = threading.enumerate()
self.assertEquals(len(threads), 1)
self.assertTrue(self.comp.stream.connection_stopped)
if self.comp.queue.qsize():
raise self.comp.queue.get(0)
def test_run_ni_handle_tick(self):
# handle_tick is implemented in FeederComponent
# so no need to check for NotImplemented raise assertion
self.assertTrue(True)
def test_handle_tick(self):
class AccountFeeder(Feeder):
def feed(self, _account):
return ["user_jid: " + _account.user_jid, \
"jid: " + _account.jid]
class MessageAccountSender(Sender):
def send(self, _account, data):
self.component.stream.send(Message(\
from_jid = _account.jid, \
to_jid = _account.user_jid, \
subject = "Simple Message for account " + _account.name, \
body = data))
self.comp.stream = MockStream()
self.comp.stream_class = MockStream
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
account11 = Account(user_jid = "user1@test.com", \
name = "account11", \
jid = "account11@jcl.test.com")
account12 = Account(user_jid = "user1@test.com", \
name = "account12", \
jid = "account12@jcl.test.com")
account2 = Account(user_jid = "user2@test.com", \
name = "account2", \
jid = "account2@jcl.test.com")
self.comp.feeder = AccountFeeder(self.comp)
self.comp.sender = MessageAccountSender(self.comp)
self.comp.handle_tick()
messages_sent = self.comp.stream.sent
self.assertEquals(len(messages_sent), 6)
self.assertEqual(messages_sent[0].get_from(), "account11@jcl.test.com")
self.assertEqual(messages_sent[0].get_to(), "user1@test.com")
self.assertEqual(messages_sent[0].get_subject(), \
"Simple Message for account account11")
self.assertEqual(messages_sent[0].get_body(), \
"user_jid: user1@test.com")
self.assertEqual(messages_sent[1].get_from(), "account11@jcl.test.com")
self.assertEqual(messages_sent[1].get_to(), "user1@test.com")
self.assertEqual(messages_sent[1].get_subject(), \
"Simple Message for account account11")
self.assertEqual(messages_sent[1].get_body(), \
"jid: account11@jcl.test.com")
self.assertEqual(messages_sent[2].get_from(), "account12@jcl.test.com")
self.assertEqual(messages_sent[2].get_to(), "user1@test.com")
self.assertEqual(messages_sent[2].get_subject(), \
"Simple Message for account account12")
self.assertEqual(messages_sent[2].get_body(), \
"user_jid: user1@test.com")
self.assertEqual(messages_sent[3].get_from(), "account12@jcl.test.com")
self.assertEqual(messages_sent[3].get_to(), "user1@test.com")
self.assertEqual(messages_sent[3].get_subject(), \
"Simple Message for account account12")
self.assertEqual(messages_sent[3].get_body(), \
"jid: account12@jcl.test.com")
self.assertEqual(messages_sent[4].get_from(), "account2@jcl.test.com")
self.assertEqual(messages_sent[4].get_to(), "user2@test.com")
self.assertEqual(messages_sent[4].get_subject(), \
"Simple Message for account account2")
self.assertEqual(messages_sent[4].get_body(), \
"user_jid: user2@test.com")
self.assertEqual(messages_sent[5].get_from(), "account2@jcl.test.com")
self.assertEqual(messages_sent[5].get_to(), "user2@test.com")
self.assertEqual(messages_sent[5].get_subject(), \
"Simple Message for account account2")
self.assertEqual(messages_sent[5].get_body(), \
"jid: account2@jcl.test.com")
class Feeder_TestCase(unittest.TestCase):
def test_feed_exist(self):
feeder = Feeder()
self.assertRaises(NotImplementedError, feeder.feed, None)
class Sender_TestCase(unittest.TestCase):
def test_send_exist(self):
sender = Sender()
self.assertRaises(NotImplementedError, sender.send, None, None)
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(FeederComponent_TestCase, 'test'))
suite.addTest(unittest.makeSuite(Feeder_TestCase, 'test'))
suite.addTest(unittest.makeSuite(Sender_TestCase, 'test'))
return suite
if __name__ == '__main__':
unittest.main(defaultTest='suite')

View File

@@ -0,0 +1,14 @@
"""JCL test module"""
__revision__ = ""
import unittest
from jcl.model.tests import account
def suite():
suite = unittest.TestSuite()
suite.addTest(account.suite())
return suite
if __name__ == '__main__':
unittest.main(defaultTest='suite')

View File

@@ -0,0 +1,260 @@
##
## test_account.py
## Login : David Rousselie <dax@happycoders.org>
## Started on Wed Nov 22 19:32:53 2006 David Rousselie
## $Id$
##
## Copyright (C) 2006 David Rousselie
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##
import unittest
import sys
import os
from sqlobject import *
from sqlobject.dbconnection import TheURIOpener
from jcl.jabber.error import FieldError
from jcl.model import account
from jcl.model.account import Account, PresenceAccount
class ExampleAccount(Account):
login = StringCol(default = "")
password = StringCol(default = None)
store_password = BoolCol(default = True)
waiting_password_reply = BoolCol(default = False)
test_enum = EnumCol(default = "choice1", enumValues = ["choice1", "choice2", "choice3"])
test_int = IntCol(default = 42)
def _get_register_fields(cls, real_class = None):
def password_post_func(password):
if password is None or password == "":
return None
return password
if real_class is None:
real_class = cls
return Account.get_register_fields(real_class) + \
[("login", "text-single", None, \
lambda field_value, default_func: account.mandatory_field(field_value), \
lambda : ""), \
("password", "text-private", None, \
lambda field_value, default_func: password_post_func(field_value), \
lambda : ""), \
("store_password", "boolean", None, account.default_post_func, \
lambda : True), \
("test_enum", "list-single", ["choice1", "choice2", "choice3"], \
account.default_post_func, \
lambda : "choice2"), \
("test_int", "text-single", None, account.int_post_func, \
lambda : 44)]
get_register_fields = classmethod(_get_register_fields)
class Example2Account(Account):
test_new_int = IntCol(default = 42)
def _get_register_fields(cls, real_class = None):
if real_class is None:
real_class = cls
return Account.get_register_fields(real_class) + \
[("test_new_int", "text-single", None, account.int_post_func, \
lambda : 43)]
get_register_fields = classmethod(_get_register_fields)
class PresenceAccountExample(PresenceAccount):
DO_SOMETHING_ELSE = 2
possibles_actions = [PresenceAccount.DO_NOTHING, \
PresenceAccount.DO_SOMETHING, \
DO_SOMETHING_ELSE]
def _get_presence_actions_fields(cls):
"""See PresenceAccount._get_presence_actions_fields
"""
return {'chat_action': (cls.possibles_actions, \
PresenceAccountExample.DO_SOMETHING_ELSE), \
'online_action': (cls.possibles_actions, \
PresenceAccountExample.DO_SOMETHING_ELSE), \
'away_action': (cls.possibles_actions, \
PresenceAccountExample.DO_SOMETHING_ELSE), \
'xa_action': (cls.possibles_actions, \
PresenceAccountExample.DO_SOMETHING_ELSE), \
'dnd_action': (cls.possibles_actions, \
PresenceAccountExample.DO_SOMETHING_ELSE), \
'offline_action': (cls.possibles_actions, \
PresenceAccountExample.DO_SOMETHING_ELSE)}
get_presence_actions_fields = classmethod(_get_presence_actions_fields)
test_new_int = IntCol(default = 42)
def _get_register_fields(cls, real_class = None):
if real_class is None:
real_class = cls
return PresenceAccount.get_register_fields(real_class) + \
[("test_new_int", "text-single", None, account.int_post_func, \
lambda : 43)]
get_register_fields = classmethod(_get_register_fields)
if sys.platform == "win32":
DB_PATH = "/c|/temp/test.db"
else:
DB_PATH = "/tmp/test.db"
DB_URL = DB_PATH# + "?debug=1&debugThreading=1"
class AccountModule_TestCase(unittest.TestCase):
def test_default_post_func(self):
result = account.default_post_func("test", None)
self.assertEquals(result, "test")
def test_default_post_func_default_value(self):
result = account.default_post_func("", lambda : "test")
self.assertEquals(result, "test")
def test_default_post_func_default_value2(self):
result = account.default_post_func(None, lambda : "test")
self.assertEquals(result, "test")
def test_int_post_func(self):
result = account.int_post_func("42", None)
self.assertEquals(result, 42)
def test_int_post_func_default_value(self):
result = account.int_post_func("", lambda : 42)
self.assertEquals(result, 42)
def test_int_post_func_default_value(self):
result = account.int_post_func(None, lambda : 42)
self.assertEquals(result, 42)
def test_mandatory_field_empty(self):
self.assertRaises(FieldError, \
account.mandatory_field, \
"")
def test_mandatory_field_none(self):
self.assertRaises(FieldError, \
account.mandatory_field, \
None)
def test_mandatory_field_empty(self):
self.assertEquals(account.mandatory_field("value"), \
"value")
class Account_TestCase(unittest.TestCase):
def setUp(self):
if os.path.exists(DB_PATH):
os.unlink(DB_PATH)
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
Account.createTable(ifNotExists = True)
ExampleAccount.createTable(ifNotExists = True)
del account.hub.threadConnection
def tearDown(self):
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
ExampleAccount.dropTable(ifExists = True)
Account.dropTable(ifExists = True)
del TheURIOpener.cachedURIs['sqlite://' + DB_URL]
account.hub.threadConnection.close()
del account.hub.threadConnection
if os.path.exists(DB_PATH):
os.unlink(DB_PATH)
def test_set_status(self):
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
account11 = Account(user_jid = "test1@test.com", \
name = "account11", \
jid = "account11@jcl.test.com")
account11.status = account.OFFLINE
self.assertEquals(account11.status, account.OFFLINE)
# TODO : test first_check attribute
del account.hub.threadConnection
def test_set_status_live_password(self):
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
account11 = ExampleAccount(user_jid = "test1@test.com", \
name = "account11", \
jid = "account11@jcl.test.com", \
login = "mylogin", \
password = "mypassword", \
store_password = False, \
test_enum = "choice3", \
test_int = 21)
account11.waiting_password_reply = True
account11.status = account.OFFLINE
self.assertEquals(account11.status, account.OFFLINE)
self.assertEquals(account11.waiting_password_reply, False)
self.assertEquals(account11.password, None)
del account.hub.threadConnection
class PresenceAccount_TestCase(unittest.TestCase):
def setUp(self):
if os.path.exists(DB_PATH):
os.unlink(DB_PATH)
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
Account.createTable(ifNotExists = True)
PresenceAccount.createTable(ifNotExists = True)
PresenceAccountExample.createTable(ifNotExists = True)
self.account = PresenceAccountExample(\
user_jid = "test1@test.com", \
name = "account11", \
jid = "account11@jcl.test.com")
del account.hub.threadConnection
def tearDown(self):
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
PresenceAccountExample.dropTable(ifExists = True)
PresenceAccount.dropTable(ifExists = True)
Account.dropTable(ifExists = True)
del TheURIOpener.cachedURIs['sqlite://' + DB_URL]
account.hub.threadConnection.close()
del account.hub.threadConnection
if os.path.exists(DB_PATH):
os.unlink(DB_PATH)
def test_get_presence_actions_fields(self):
fields = PresenceAccount.get_presence_actions_fields()
(possibles_actions, chat_default_action) = fields["chat_action"]
self.assertEquals(chat_default_action, PresenceAccount.DO_SOMETHING)
self.assertEquals(possibles_actions, PresenceAccount.possibles_actions)
(possibles_actions, online_default_action) = fields["online_action"]
self.assertEquals(online_default_action, PresenceAccount.DO_SOMETHING)
self.assertEquals(possibles_actions, PresenceAccount.possibles_actions)
def test_possibles_actions(self):
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
for (field_name, \
field_type, \
possibles_actions, \
post_func, \
default_func) in self.account.get_register_fields()[1:]:
if possibles_actions is not None:
for possible_action in possibles_actions:
self.assertEquals(post_func(possible_action, default_func),
int(possible_action))
self.assertTrue(str(default_func()) in possibles_actions)
del account.hub.threadConnection
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(AccountModule_TestCase, 'test'))
suite.addTest(unittest.makeSuite(Account_TestCase, 'test'))
suite.addTest(unittest.makeSuite(PresenceAccount_TestCase, 'test'))
return suite
if __name__ == '__main__':
unittest.main(defaultTest='suite')

18
src/jcl/tests/__init__.py Normal file
View File

@@ -0,0 +1,18 @@
"""JCL test module"""
__revision__ = ""
import unittest
from jcl.tests import lang
from jcl.jabber import tests as jabber
from jcl.model import tests as model
def suite():
suite = unittest.TestSuite()
suite.addTest(lang.suite())
suite.addTest(jabber.suite())
suite.addTest(model.suite())
return suite
if __name__ == '__main__':
unittest.main(defaultTest='suite')

70
src/jcl/tests/lang.py Normal file
View File

@@ -0,0 +1,70 @@
# -*- coding: UTF-8 -*-
##
## test_lang.py
## Login : David Rousselie <dax@happycoders.org>
## Started on Wed Nov 22 19:19:25 2006 David Rousselie
## $Id$
##
## Copyright (C) 2006 David Rousselie
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##
import unittest
from jcl.lang import Lang
from pyxmpp.iq import Iq
class Lang_TestCase(unittest.TestCase):
def setUp(self):
self.lang = Lang()
def tearDown(self):
self.lang = None
def test_get_lang_class_exist(self):
lang_class = self.lang.get_lang_class("fr")
self.assertEquals(lang_class, Lang.fr)
def test_get_lang_class_not_exist(self):
lang_class = self.lang.get_lang_class("not_exist")
self.assertEquals(lang_class, Lang.en)
def test_get_lang_class_long_code(self):
lang_class = self.lang.get_lang_class("fr_FR")
self.assertEquals(lang_class, Lang.fr)
def test_get_lang_from_node(self):
iq = Iq(from_jid = "test@test.com", \
to_jid = "test2@test.com", \
stanza_type = "get")
iq_node = iq.get_node()
iq_node.setLang("fr")
lang = self.lang.get_lang_from_node(iq_node)
self.assertEquals(lang, "fr")
def test_get_lang_class_from_node(self):
iq = Iq(from_jid = "test@test.com", \
to_jid = "test2@test.com", \
stanza_type = "get")
iq_node = iq.get_node()
iq_node.setLang("fr")
lang = self.lang.get_lang_class_from_node(iq_node)
self.assertEquals(lang, Lang.fr)
def suite():
return unittest.makeSuite(Lang_TestCase, 'test')
if __name__ == '__main__':
unittest.main(defaultTest='suite')