Expanded command test-suite functionality and added basic testing methods for commands in "System" category.

This commit is contained in:
Griatch 2011-02-21 17:47:02 +00:00
parent 5464487c93
commit ef59b97881
2 changed files with 57 additions and 10 deletions

View file

@ -21,6 +21,7 @@ except ImportError:
from django.conf import settings from django.conf import settings
from src.utils import create from src.utils import create
from src.server import session, sessionhandler from src.server import session, sessionhandler
from src.config.models import ConfigValue
#------------------------------------------------------------ #------------------------------------------------------------
# Command testing # Command testing
@ -44,7 +45,19 @@ class FakeSession(session.Session):
pass pass
def msg(self, message, data=None): def msg(self, message, data=None):
if message.startswith("Traceback (most recent call last):"): if message.startswith("Traceback (most recent call last):"):
#retval = "Traceback last line: %s" % message.split('\n')[-4:]
raise AssertionError(message) raise AssertionError(message)
if self.player.character.ndb.return_string != None:
return_list = self.player.character.ndb.return_string
if hasattr(return_list, '__iter__'):
rstring = return_list.pop(0)
self.player.character.ndb.return_string = return_list
else:
rstring = return_list
self.player.character.ndb.return_string = None
if not message.startswith(rstring):
retval = "Returned message ('%s') != desired message ('%s')" % (message, rstring)
raise AssertionError(retval)
if VERBOSE: if VERBOSE:
print message print message
@ -57,17 +70,22 @@ class CommandTest(TestCase):
""" """
def setUp(self): def setUp(self):
"sets up the testing environment" "sets up the testing environment"
c = ConfigValue(db_key="default_home", db_value="2")
c.save()
self.room1 = create.create_object(settings.BASE_ROOM_TYPECLASS, key="room1") self.room1 = create.create_object(settings.BASE_ROOM_TYPECLASS, key="room1")
self.room2 = create.create_object(settings.BASE_ROOM_TYPECLASS, key="room2") self.room2 = create.create_object(settings.BASE_ROOM_TYPECLASS, key="room2")
# create a faux player/character for testing. # create a faux player/character for testing.
self.char1 = create.create_player("TestingPlayer", "testplayer@test.com", "testpassword", location=self.room1) self.char1 = create.create_player("TestingPlayer", "testplayer@test.com", "testpassword", location=self.room1)
self.char1.player.user.is_superuser = True self.char1.player.user.is_superuser = True
self.char1.ndb.return_string = None
sess = FakeSession() sess = FakeSession()
sess.connectionMade() sess.connectionMade()
sess.session_login(self.char1.player) sess.session_login(self.char1.player)
# create second player and some objects # create second player and some objects
self.char2 = create.create_object(settings.BASE_CHARACTER_TYPECLASS, key="char2", location=self.room1) self.char2 = create.create_object(settings.BASE_CHARACTER_TYPECLASS, key="char2", location=self.room1)
self.char2.ndb.return_string = None
self.obj1 = create.create_object(settings.BASE_OBJECT_TYPECLASS, key="obj1", location=self.room1) self.obj1 = create.create_object(settings.BASE_OBJECT_TYPECLASS, key="obj1", location=self.room1)
self.obj2 = create.create_object(settings.BASE_OBJECT_TYPECLASS, key="obj2", location=self.room1) self.obj2 = create.create_object(settings.BASE_OBJECT_TYPECLASS, key="obj2", location=self.room1)
self.exit1 = create.create_object(settings.BASE_EXIT_TYPECLASS, key="exit1", location=self.room1) self.exit1 = create.create_object(settings.BASE_EXIT_TYPECLASS, key="exit1", location=self.room1)
@ -86,7 +104,7 @@ class CommandTest(TestCase):
cmd.obj = self.char1 cmd.obj = self.char1
return cmd return cmd
def execute_cmd(self, raw_string): def execute_cmd(self, raw_string, wanted_return_string=None):
""" """
Creates the command through faking a normal command call; Creates the command through faking a normal command call;
This also mangles the input in various ways to test if the command This also mangles the input in various ways to test if the command
@ -100,9 +118,13 @@ class CommandTest(TestCase):
self.char1.execute_cmd(test1) self.char1.execute_cmd(test1)
self.char1.execute_cmd(test2) self.char1.execute_cmd(test2)
self.char1.execute_cmd(test3) self.char1.execute_cmd(test3)
# actual call # actual call, we potentially check so return is ok.
self.char1.execute_cmd(raw_string) self.char1.ndb.return_string = wanted_return_string
try:
self.char1.execute_cmd(raw_string)
except AssertionError, e:
self.fail(e)
self.char1.ndb.return_string = None
#------------------------------------------------------------ #------------------------------------------------------------
# Default set Command testing # Default set Command testing
#------------------------------------------------------------ #------------------------------------------------------------
@ -123,7 +145,32 @@ class TestNick(CommandTest):
def test_call(self): def test_call(self):
self.execute_cmd("nickname testalias = testaliasedstring") self.execute_cmd("nickname testalias = testaliasedstring")
self.assertEquals("testaliasedstring", self.char1.nicks.get("testalias", None)) self.assertEquals("testaliasedstring", self.char1.nicks.get("testalias", None))
# system.py tests
# system.py command tests
class TestPy(CommandTest):
def test_call(self):
self.execute_cmd("@py 1+2", [">>> 1+2", "<<< 3"])
class TestListScripts(CommandTest):
def test_call(self):
self.execute_cmd("@scripts")
class TestListObjects(CommandTest):
def test_call(self):
self.execute_cmd("@objects")
class TestListService(CommandTest):
def test_call(self):
self.execute_cmd("@service")
class TestVersion(CommandTest):
def test_call(self):
self.execute_cmd("@version")
class TestTime(CommandTest):
def test_call(self):
self.execute_cmd("@time")
class TestList(CommandTest):
def test_call(self):
self.execute_cmd("@list")
class TestPs(CommandTest): class TestPs(CommandTest):
def test_call(self): def test_call(self):
self.execute_cmd("@ps") self.execute_cmd("@ps","\n{wNon-timed scripts")
class TestStats(CommandTest):
def test_call(self):
self.execute_cmd("@stats")

View file

@ -126,7 +126,7 @@ def gametime(format=False):
""" """
try: try:
script = ScriptDB.objects.get_all_scripts(GAME_TIME_SCRIPT)[0] script = ScriptDB.objects.get_all_scripts(GAME_TIME_SCRIPT)[0]
except KeyError: except (KeyError, IndexError):
logger.log_trace("GameTime script not found.") logger.log_trace("GameTime script not found.")
return return
# we return this as an integer (second-precision is good enough) # we return this as an integer (second-precision is good enough)
@ -141,7 +141,7 @@ def runtime(format=False):
""" """
try: try:
script = ScriptDB.objects.get_all_scripts(GAME_TIME_SCRIPT)[0] script = ScriptDB.objects.get_all_scripts(GAME_TIME_SCRIPT)[0]
except KeyError: except (KeyError, IndexError):
logger.log_trace("GameTime script not found.") logger.log_trace("GameTime script not found.")
return return
# we return this as an integer (second-precision is good enough) # we return this as an integer (second-precision is good enough)
@ -156,7 +156,7 @@ def uptime(format=False):
""" """
try: try:
script = ScriptDB.objects.get_all_scripts(GAME_TIME_SCRIPT)[0] script = ScriptDB.objects.get_all_scripts(GAME_TIME_SCRIPT)[0]
except KeyError: except (KeyError, IndexError):
logger.log_trace("GameTime script not found.") logger.log_trace("GameTime script not found.")
return return
# we return this as an integer (second-precision is good enough) # we return this as an integer (second-precision is good enough)