Merge branch '6-merge-unit-tests-into-same-branch' into 4-increase-test-coverage-for-scriptstickethandlerpy
This commit is contained in:
commit
695ea655fb
1 changed files with 100 additions and 3 deletions
|
|
@ -3,12 +3,15 @@ from unittest import TestCase, mock
|
||||||
from parameterized import parameterized
|
from parameterized import parameterized
|
||||||
|
|
||||||
from evennia import DefaultScript
|
from evennia import DefaultScript
|
||||||
|
from evennia.objects.objects import DefaultObject
|
||||||
from evennia.scripts.models import ObjectDoesNotExist, ScriptDB
|
from evennia.scripts.models import ObjectDoesNotExist, ScriptDB
|
||||||
from evennia.scripts.scripts import DoNothing, ExtendedLoopingCall
|
from evennia.scripts.scripts import DoNothing, ExtendedLoopingCall
|
||||||
from evennia.utils.create import create_script
|
from evennia.utils.create import create_script
|
||||||
from evennia.utils.test_resources import BaseEvenniaTest
|
from evennia.utils.test_resources import BaseEvenniaTest
|
||||||
from evennia.scripts.tickerhandler import TickerHandler
|
from evennia.scripts.tickerhandler import TickerHandler
|
||||||
|
from evennia.scripts.monitorhandler import MonitorHandler
|
||||||
|
import inspect
|
||||||
|
from evennia.scripts.manager import ScriptDBManager
|
||||||
|
|
||||||
class TestScript(BaseEvenniaTest):
|
class TestScript(BaseEvenniaTest):
|
||||||
def test_create(self):
|
def test_create(self):
|
||||||
|
|
@ -29,7 +32,48 @@ class Test_improve_coverage(TestCase):
|
||||||
with self.assertRaises(RuntimeError):
|
with self.assertRaises(RuntimeError):
|
||||||
th=TickerHandler()
|
th=TickerHandler()
|
||||||
th.remove(callback=1)
|
th.remove(callback=1)
|
||||||
|
|
||||||
|
def test_not_obj_return_empty_list(self):
|
||||||
|
manager_obj = ScriptDBManager()
|
||||||
|
returned_list = manager_obj.get_all_scripts_on_obj(False)
|
||||||
|
self.assertEqual(returned_list, [])
|
||||||
|
|
||||||
|
class ListIntervalsScript(DefaultScript):
|
||||||
|
"""
|
||||||
|
A script that does nothing. Used to test listing of script with nonzero intervals.
|
||||||
|
"""
|
||||||
|
def at_script_creation(self):
|
||||||
|
"""
|
||||||
|
Setup the script
|
||||||
|
"""
|
||||||
|
self.key = "interval_test"
|
||||||
|
self.desc = "This is an empty placeholder script."
|
||||||
|
self.interval = 1
|
||||||
|
self.repeats = 1
|
||||||
|
|
||||||
|
class TestScriptHandler(BaseEvenniaTest):
|
||||||
|
"""
|
||||||
|
Test the ScriptHandler class.
|
||||||
|
|
||||||
|
"""
|
||||||
|
def setUp(self):
|
||||||
|
self.obj, self.errors = DefaultObject.create("test_object")
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
self.obj.delete()
|
||||||
|
|
||||||
|
def test_start(self):
|
||||||
|
"Check that ScriptHandler start function works correctly"
|
||||||
|
self.obj.scripts.add(ListIntervalsScript)
|
||||||
|
self.num = self.obj.scripts.start(self.obj.scripts.all()[0].key)
|
||||||
|
self.assertTrue(self.num == 1)
|
||||||
|
|
||||||
|
def test_list_script_intervals(self):
|
||||||
|
"Checks that Scripthandler __str__ function lists script intervals correctly"
|
||||||
|
self.obj.scripts.add(ListIntervalsScript)
|
||||||
|
self.str = str(self.obj.scripts)
|
||||||
|
self.assertTrue("None/1" in self.str)
|
||||||
|
self.assertTrue("1 repeats" in self.str)
|
||||||
|
|
||||||
class TestScriptDB(TestCase):
|
class TestScriptDB(TestCase):
|
||||||
"Check the singleton/static ScriptDB object works correctly"
|
"Check the singleton/static ScriptDB object works correctly"
|
||||||
|
|
@ -63,11 +107,9 @@ class TestScriptDB(TestCase):
|
||||||
# Check the script is not recreated as a side-effect
|
# Check the script is not recreated as a side-effect
|
||||||
self.assertFalse(self.scr in ScriptDB.objects.get_all_scripts())
|
self.assertFalse(self.scr in ScriptDB.objects.get_all_scripts())
|
||||||
|
|
||||||
|
|
||||||
class TestExtendedLoopingCall(TestCase):
|
class TestExtendedLoopingCall(TestCase):
|
||||||
"""
|
"""
|
||||||
Test the ExtendedLoopingCall class.
|
Test the ExtendedLoopingCall class.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@mock.patch("evennia.scripts.scripts.LoopingCall")
|
@mock.patch("evennia.scripts.scripts.LoopingCall")
|
||||||
|
|
@ -100,3 +142,58 @@ class TestExtendedLoopingCall(TestCase):
|
||||||
loopcall.__call__.assert_not_called()
|
loopcall.__call__.assert_not_called()
|
||||||
self.assertEqual(loopcall.interval, 20)
|
self.assertEqual(loopcall.interval, 20)
|
||||||
loopcall._scheduleFrom.assert_called_with(121)
|
loopcall._scheduleFrom.assert_called_with(121)
|
||||||
|
|
||||||
|
def dummy_func():
|
||||||
|
return 0
|
||||||
|
class TestMonitorHandler(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.handler = MonitorHandler()
|
||||||
|
|
||||||
|
def test_add(self):
|
||||||
|
obj = mock.Mock()
|
||||||
|
fieldname = "db_add"
|
||||||
|
callback = dummy_func
|
||||||
|
idstring = "test"
|
||||||
|
self.assertEquals(inspect.isfunction(callback),True)
|
||||||
|
|
||||||
|
self.handler.add(obj, fieldname, callback, idstring=idstring)
|
||||||
|
|
||||||
|
self.assertIn(fieldname, self.handler.monitors[obj])
|
||||||
|
self.assertIn(idstring, self.handler.monitors[obj][fieldname])
|
||||||
|
self.assertEqual(self.handler.monitors[obj][fieldname][idstring], (callback, False, {}))
|
||||||
|
|
||||||
|
def test_remove(self):
|
||||||
|
obj = mock.Mock()
|
||||||
|
fieldname = 'db_remove'
|
||||||
|
callback = dummy_func
|
||||||
|
idstring = 'test_remove'
|
||||||
|
|
||||||
|
self.handler.add(obj,fieldname,callback,idstring=idstring)
|
||||||
|
self.assertIn(fieldname,self.handler.monitors[obj])
|
||||||
|
self.assertEqual(self.handler.monitors[obj][fieldname][idstring], (callback, False, {}))
|
||||||
|
|
||||||
|
self.handler.remove(obj,fieldname,idstring=idstring)
|
||||||
|
self.assertEquals(self.handler.monitors[obj][fieldname], {})
|
||||||
|
|
||||||
|
def test_add_with_invalid_callback_does_not_work(self):
|
||||||
|
obj = mock.Mock()
|
||||||
|
fieldname = "db_key"
|
||||||
|
callback = "not_a_function"
|
||||||
|
|
||||||
|
self.handler.add(obj, fieldname, callback)
|
||||||
|
self.assertNotIn(fieldname, self.handler.monitors[obj])
|
||||||
|
|
||||||
|
""" def test_add_raise_exception(self):
|
||||||
|
obj = mock.Mock()
|
||||||
|
fieldname = "db_add"
|
||||||
|
callback = 1
|
||||||
|
idstring = "test"
|
||||||
|
# self.assertEquals(inspect.isfunction(callback),True)
|
||||||
|
self.assertRaises(Exception,self.handler.add,obj, fieldname, callback, idstring=idstring)
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue