Changed unittest suite to use dummy sessions.

This commit is contained in:
Griatch 2013-09-22 16:29:02 +02:00
parent 4659ddbfc3
commit bbba695380
6 changed files with 66 additions and 37 deletions

View file

@ -214,7 +214,6 @@ def format_script_list(scripts):
table.align = 'r'
for script in scripts:
nextrep = script.time_until_next_repeat()
print type(script),
table.add_row([script.id,
script.obj.key if (hasattr(script, 'obj') and script.obj) else "<Global>",
script.key,

View file

@ -15,8 +15,11 @@ main test suite started with
import re
from django.conf import settings
from django.utils.unittest import TestCase
from src.server.serversession import ServerSession
from src.objects.objects import Object, Character
from src.players.player import Player
from src.utils import create, utils, ansi
from src.server.sessionhandler import SESSIONS
from django.db.models.signals import pre_save
from src.server.caches import field_pre_save
@ -30,6 +33,19 @@ _RE = re.compile(r"^\+|-+\+|\+-+|--*|\|", re.MULTILINE)
# Command testing
# ------------------------------------------------------------
def dummy_data_out(self, text=None, **kwargs):
pass
SESSIONS.data_out = dummy_data_out
class TestObjectClass(Object):
def msg(self, text="", **kwargs):
"test message"
pass
class TestCharacterClass(Character):
def msg(self, text="", **kwargs):
"test message"
if self.player:
self.player.msg(text=text, **kwargs)
class TestPlayerClass(Player):
def msg(self, text="", **kwargs):
"test message"
@ -48,22 +64,30 @@ class CommandTest(TestCase):
CID = 0 # we must set a different CID in every test to avoid unique-name collisions creating the objects
def setUp(self):
"sets up testing environment"
self.player = create.create_player("TestPlayer%i" % self.CID, "test@test.com", "testpassword", typeclass=TestPlayerClass)
self.player2 = create.create_player("TestPlayer%ib" % self.CID, "test@test.com", "testpassword", typeclass=TestPlayerClass)
self.room1 = create.create_object("src.objects.objects.Room", key="Room%i"%self.CID)
self.room1.db.desc = "room_desc"
self.room2 = create.create_object("src.objects.objects.Room", key="Room%ib" % self.CID)
self.obj1 = create.create_object("src.objects.objects.Object", key="Obj%i" % self.CID, location=self.room1, home=self.room1)
self.obj2 = create.create_object("src.objects.objects.Object", key="Obj%ib" % self.CID, location=self.room1, home=self.room1)
self.char1 = create.create_object("src.objects.objects.Character", key="Char%i" % self.CID, location=self.room1, home=self.room1)
self.char2 = create.create_object("src.objects.objects.Character", key="Char%ib" % self.CID, location=self.room1, home=self.room1)
self.script = create.create_script("src.scripts.scripts.Script", key="Script%i" % self.CID)
self.player = create.create_player("TestPlayer%i" % self.CID, "test@test.com", "testpassword", typeclass=TestPlayerClass)
self.player2 = create.create_player("TestPlayer%ib" % self.CID, "test@test.com", "testpassword", typeclass=TestPlayerClass)
self.player.permissions.add("Immortals")
self.obj1 = create.create_object(TestObjectClass, key="Obj%i" % self.CID, location=self.room1, home=self.room1)
self.obj2 = create.create_object(TestObjectClass, key="Obj%ib" % self.CID, location=self.room1, home=self.room1)
self.char1 = create.create_object(TestCharacterClass, key="Char%i" % self.CID, location=self.room1, home=self.room1)
self.char2 = create.create_object(TestCharacterClass, key="Char%ib" % self.CID, location=self.room1, home=self.room1)
self.char1.player = self.player
self.char1.sessid = 1
self.char2.player = self.player2
self.script = create.create_script("src.scripts.scripts.Script", key="Script%i" % self.CID)
self.player.permissions.add("Immortals")
def call(self, cmdobj, args, msg=None, cmdset=None, noansi=True):
# set up a fake session
global SESSIONS
session = ServerSession()
session.init_session("telnet", ("localhost", "testmode"), SESSIONS)
session.sessid = self.CID
SESSIONS.portal_connect(session.get_sync_data())
SESSIONS.login(SESSIONS.session_from_sessid(self.CID), self.player, testmode=True)
def call(self, cmdobj, args, msg=None, cmdset=None, noansi=True, caller=None):
"""
Test a command by assigning all the needed
properties to cmdobj and running
@ -74,15 +98,17 @@ class CommandTest(TestCase):
The msgreturn value is compared to eventual
output sent to caller.msg in the game
"""
cmdobj.caller = self.char1
cmdobj.caller = caller if caller else self.char1
#print "call:", cmdobj.key, cmdobj.caller, caller if caller else cmdobj.caller.player
#print "perms:", cmdobj.caller.permissions.all()
cmdobj.cmdstring = cmdobj.key
cmdobj.args = args
cmdobj.cmdset = cmdset
cmdobj.sessid = 1
cmdobj.session = None
cmdobj.sessid = self.CID
cmdobj.session = SESSIONS.session_from_sessid(self.CID)
cmdobj.player = self.player
cmdobj.raw_string = cmdobj.key + " " + args
cmdobj.obj = self.char1
cmdobj.obj = caller if caller else self.char1
# test
self.char1.player.ndb.stored_msg = []
cmdobj.at_pre_cmd()
@ -160,19 +186,19 @@ class TestAdmin(CommandTest):
from src.commands.default import player
class TestPlayer(CommandTest):
CID = 5
def test_cmds(self):
self.call(player.CmdOOCLook(), "", "Account TestPlayer5 (you are OutofCharacter)")
self.call(player.CmdIC(), "Char5","Char5 is now acted from another")
self.call(player.CmdOOC(), "", "You are already")
self.call(player.CmdPassword(), "testpassword = testpassword", "Password changed.")
self.call(player.CmdEncoding(), "", "Default encoding:")
self.call(player.CmdWho(), "", "Players:")
self.call(player.CmdQuit(), "", "Quitting. Hope to see you soon again.")
self.call(player.CmdSessions(), "", "Your current session(s):")
self.call(player.CmdColorTest(), "ansi", "ANSI colors:")
self.call(player.CmdCharCreate(), "Test1=Test char","Created new character Test1. Use @ic Test1 to enter the game")
self.call(player.CmdQuell(), "", "Quelling Player permissions (Immortals). Use @unquell to get them back.")
CID = 5
def test_cmds(self):
self.call(player.CmdOOCLook(), "", "Account TestPlayer5 (you are OutofCharacter)", caller=self.player)
self.call(player.CmdPassword(), "testpassword = testpassword", "Password changed.", caller=self.player)
self.call(player.CmdEncoding(), "", "Default encoding:", caller=self.player)
self.call(player.CmdWho(), "", "Players:", caller=self.player)
self.call(player.CmdQuit(), "", "Quitting. Hope to see you soon again.", caller=self.player)
self.call(player.CmdSessions(), "", "Your current session(s):", caller=self.player)
self.call(player.CmdColorTest(), "ansi", "ANSI colors:", caller=self.player)
self.call(player.CmdCharCreate(), "Test1=Test char","Created new character Test1. Use @ic Test1 to enter the game", caller=self.player)
self.call(player.CmdQuell(), "", "Quelling Player permissions (immortals). Use @unquell to get them back.", caller=self.player)
self.call(player.CmdIC(), "Char5","Char5 is now acted from another", caller=self.player)
self.call(player.CmdOOC(), "", "You are already", caller=self.player)
from src.commands.default import building
class TestBuilding(CommandTest):

View file

@ -29,7 +29,7 @@ from src.commands.cmdsethandler import CmdSetHandler
from src.commands import cmdhandler
from src.scripts.scripthandler import ScriptHandler
from src.utils import logger
from src.utils.utils import make_iter, to_unicode, variable_from_module, inherits_from
from src.utils.utils import make_iter, to_str, to_unicode, variable_from_module, inherits_from
from django.utils.translation import ugettext as _
@ -675,7 +675,7 @@ class ObjectDB(TypedObject):
if not _SESSIONS:
from src.server.sessionhandler import SESSIONS as _SESSIONS
text = utils.to_str(text, force_string=True) if text else ""
text = to_str(text, force_string=True) if text else ""
if "data" in kwargs:
# deprecation warning
@ -705,8 +705,7 @@ class ObjectDB(TypedObject):
contents = _GA(self, "contents")
if exclude:
exclude = make_iter(exclude)
contents = [obj for obj in contents
if (obj not in exclude and obj not in exclude)]
contents = [obj for obj in contents if obj not in exclude]
for obj in contents:
obj.msg(message, from_obj=from_obj, **kwargs)

View file

@ -29,6 +29,7 @@ from src.typeclasses.models import TypedObject, TagHandler, NickHandler, AliasHa
from src.commands.cmdsethandler import CmdSetHandler
from src.commands import cmdhandler
from src.utils import utils
from src.utils.utils import to_str
from django.utils.translation import ugettext as _
@ -271,7 +272,7 @@ class PlayerDB(TypedObject, AbstractUser):
if isinstance(data, dict):
kwargs.update(data)
text = utils.to_str(text, force_string=True) if text else ""
text = to_str(text, force_string=True) if text else ""
if from_obj:
# call hook
try:

View file

@ -204,11 +204,14 @@ class ServerSessionHandler(SessionHandler):
data="")
# server-side access methods
def login(self, session, player):
def login(self, session, player, testmode=False):
"""
Log in the previously unloggedin session and the player we by
now should know is connected to it. After this point we
assume the session to be logged in one way or another.
testmode - this is used by unittesting for faking login without
any AMP being actually active
"""
# we have to check this first before uid has been assigned
@ -241,7 +244,8 @@ class ServerSessionHandler(SessionHandler):
session.logged_in = True
# sync the portal to the session
sessdata = session.get_sync_data()
self.server.amp_protocol.call_remote_PortalAdmin(session.sessid,
if not testmode:
self.server.amp_protocol.call_remote_PortalAdmin(session.sessid,
operation=SLOGIN,
data=sessdata)
player.at_post_login(sessid=session.sessid)

View file

@ -552,7 +552,7 @@ class TagHandler(object):
def all(self):
"Get all tags in this handler"
return [p[0] for p in _GA(self.obj, self._m2m_fieldname).all().values_list("db_key")]
return [to_str(p[0]) for p in _GA(self.obj, self._m2m_fieldname).all().values_list("db_key")]
def __str__(self):
return ",".join(self.all())