Solved remaining unittest issues with py3

This commit is contained in:
Griatch 2019-03-19 19:29:33 +01:00
parent 37151e6b89
commit 5bf2280036
3 changed files with 34 additions and 22 deletions

View file

@ -73,7 +73,7 @@ class SlowExit(DefaultExit):
traversing_object.msg("You start moving %s at a %s." % (self.key, move_speed)) traversing_object.msg("You start moving %s at a %s." % (self.key, move_speed))
# create a delayed movement # create a delayed movement
deferred = utils.delay(move_delay, callback=move_callback) deferred = utils.delay(move_delay, move_callback)
# we store the deferred on the character, this will allow us # we store the deferred on the character, this will allow us
# to abort the movement. We must use an ndb here since # to abort the movement. We must use an ndb here since
# deferreds cannot be pickled. # deferreds cannot be pickled.

View file

@ -8,7 +8,7 @@ import sys
import datetime import datetime
from django.test import override_settings from django.test import override_settings
from evennia.commands.default.tests import CommandTest from evennia.commands.default.tests import CommandTest
from evennia.utils.test_resources import EvenniaTest from evennia.utils.test_resources import EvenniaTest, mockdelay, mockdeferLater
from mock import Mock, patch from mock import Mock, patch
# Testing of rplanguage module # Testing of rplanguage module
@ -809,13 +809,19 @@ from evennia.contrib import slow_exit
slow_exit.MOVE_DELAY = {"stroll": 0, "walk": 0, "run": 0, "sprint": 0} slow_exit.MOVE_DELAY = {"stroll": 0, "walk": 0, "run": 0, "sprint": 0}
def _cancellable_mockdelay(time, callback, *args, **kwargs):
callback(*args, **kwargs)
return Mock()
class TestSlowExit(CommandTest): class TestSlowExit(CommandTest):
@patch("evennia.utils.delay", _cancellable_mockdelay)
def test_exit(self): def test_exit(self):
exi = create_object(slow_exit.SlowExit, key="slowexit", location=self.room1, destination=self.room2) exi = create_object(slow_exit.SlowExit, key="slowexit", location=self.room1, destination=self.room2)
exi.at_traverse(self.char1, self.room2) exi.at_traverse(self.char1, self.room2)
self.call(slow_exit.CmdSetSpeed(), "walk", "You are now walking.") self.call(slow_exit.CmdSetSpeed(), "walk", "You are now walking.")
self.call(slow_exit.CmdStop(), "", "You stop moving.") self.call(slow_exit.CmdStop(), "", "You stop moving.")
# test talking npc contrib # test talking npc contrib
@ -858,23 +864,8 @@ from twisted.internet.base import DelayedCall
DelayedCall.debug = True DelayedCall.debug = True
def _mockdelay(tim, func, *args, **kwargs):
func(*args, **kwargs)
return MagicMock()
def _mockdeferLater(reactor, timedelay, callback, *args, **kwargs):
callback(*args, **kwargs)
return MagicMock()
class TestTutorialWorldObjects(TwistedTestCase, CommandTest): class TestTutorialWorldObjects(TwistedTestCase, CommandTest):
def setUp(self):
self.patch(sys.modules['evennia.contrib.tutorial_world.objects'], 'delay', _mockdelay)
self.patch(sys.modules['evennia.scripts.taskhandler'], 'deferLater', _mockdeferLater)
super(TestTutorialWorldObjects, self).setUp()
def test_tutorialobj(self): def test_tutorialobj(self):
obj1 = create_object(tutobjects.TutorialObject, key="tutobj") obj1 = create_object(tutobjects.TutorialObject, key="tutobj")
obj1.reset() obj1.reset()
@ -885,6 +876,7 @@ class TestTutorialWorldObjects(TwistedTestCase, CommandTest):
readable.db.readable_text = "Text to read" readable.db.readable_text = "Text to read"
self.call(tutobjects.CmdRead(), "book", "You read book:\n Text to read", obj=readable) self.call(tutobjects.CmdRead(), "book", "You read book:\n Text to read", obj=readable)
def test_climbable(self): def test_climbable(self):
climbable = create_object(tutobjects.Climbable, key="tree", location=self.room1) climbable = create_object(tutobjects.Climbable, key="tree", location=self.room1)
self.call(tutobjects.CmdClimb(), "tree", "You climb tree. Having looked around, you climb down again.", obj=climbable) self.call(tutobjects.CmdClimb(), "tree", "You climb tree. Having looked around, you climb down again.", obj=climbable)
@ -894,11 +886,15 @@ class TestTutorialWorldObjects(TwistedTestCase, CommandTest):
obelisk = create_object(tutobjects.Obelisk, key="obelisk", location=self.room1) obelisk = create_object(tutobjects.Obelisk, key="obelisk", location=self.room1)
self.assertEqual(obelisk.return_appearance(self.char1).startswith("|cobelisk("), True) self.assertEqual(obelisk.return_appearance(self.char1).startswith("|cobelisk("), True)
@patch("evennia.contrib.tutorial_world.objects.delay", mockdelay)
@patch("evennia.scripts.taskhandler.deferLater", mockdeferLater)
def test_lightsource(self): def test_lightsource(self):
light = create_object(tutobjects.LightSource, key="torch", location=self.room1) light = create_object(tutobjects.LightSource, key="torch", location=self.room1)
self.call(tutobjects.CmdLight(), "", "A torch on the floor flickers and dies.|You light torch.", obj=light) self.call(tutobjects.CmdLight(), "", "A torch on the floor flickers and dies.|You light torch.", obj=light)
self.assertFalse(light.pk) self.assertFalse(light.pk)
@patch("evennia.contrib.tutorial_world.objects.delay", mockdelay)
@patch("evennia.scripts.taskhandler.deferLater", mockdeferLater)
def test_crumblingwall(self): def test_crumblingwall(self):
wall = create_object(tutobjects.CrumblingWall, key="wall", location=self.room1) wall = create_object(tutobjects.CrumblingWall, key="wall", location=self.room1)
self.assertFalse(wall.db.button_exposed) self.assertFalse(wall.db.button_exposed)
@ -917,9 +913,6 @@ class TestTutorialWorldObjects(TwistedTestCase, CommandTest):
# we patch out the delay, so these are closed immediately # we patch out the delay, so these are closed immediately
self.assertFalse(wall.db.button_exposed) self.assertFalse(wall.db.button_exposed)
self.assertFalse(wall.db.exit_open) self.assertFalse(wall.db.exit_open)
wall.reset()
wall.delete()
return wall.deferred
def test_weapon(self): def test_weapon(self):
weapon = create_object(tutobjects.Weapon, key="sword", location=self.char1) weapon = create_object(tutobjects.Weapon, key="sword", location=self.char1)

View file

@ -3,9 +3,10 @@ Various helper resources for writing unittests.
""" """
import sys import sys
from twisted.internet.defer import Deferred
from django.conf import settings from django.conf import settings
from django.test import TestCase from django.test import TestCase
from mock import Mock from mock import Mock, patch
from evennia.objects.objects import DefaultObject, DefaultCharacter, DefaultRoom, DefaultExit from evennia.objects.objects import DefaultObject, DefaultCharacter, DefaultRoom, DefaultExit
from evennia.accounts.accounts import DefaultAccount from evennia.accounts.accounts import DefaultAccount
from evennia.scripts.scripts import DefaultScript from evennia.scripts.scripts import DefaultScript
@ -15,6 +16,18 @@ from evennia.utils import create
from evennia.utils.idmapper.models import flush_cache from evennia.utils.idmapper.models import flush_cache
# mocking of evennia.utils.utils.delay
def mockdelay(timedelay, callback, *args, **kwargs):
callback(*args, **kwargs)
return Deferred()
# mocking of twisted's deferLater
def mockdeferLater(reactor, timedelay, callback, *args, **kwargs):
callback(*args, **kwargs)
return Deferred()
def unload_module(module): def unload_module(module):
""" """
Reset import so one can mock global constants. Reset import so one can mock global constants.
@ -49,6 +62,11 @@ def unload_module(module):
del sys.modules[modulename] del sys.modules[modulename]
def _mock_deferlater(reactor, timedelay, callback, *args, **kwargs):
callback(*args, **kwargs)
return Deferred()
class EvenniaTest(TestCase): class EvenniaTest(TestCase):
""" """
Base test for Evennia, sets up a basic environment. Base test for Evennia, sets up a basic environment.
@ -60,6 +78,7 @@ class EvenniaTest(TestCase):
room_typeclass = DefaultRoom room_typeclass = DefaultRoom
script_typeclass = DefaultScript script_typeclass = DefaultScript
@patch("evennia.scripts.taskhandler.deferLater", _mock_deferlater)
def setUp(self): def setUp(self):
""" """
Sets up testing environment Sets up testing environment