Add tags= keywords to all create functions, with unit tests. Resolves #2160

This commit is contained in:
Griatch 2020-07-18 21:36:03 +02:00
parent e7b91e11e7
commit e5b96a2b79
3 changed files with 108 additions and 6 deletions

View file

@ -51,6 +51,8 @@ class DefaultChannel(ChannelDB, metaclass=TypeclassBase):
self.attributes.add("keep_log", cdict["keep_log"])
if cdict.get("desc"):
self.attributes.add("desc", cdict["desc"])
if cdict.get("tags"):
self.tags.batch_add(*cdict["tags"])
def basetype_setup(self):
# delayed import of the channelhandler

View file

@ -313,7 +313,7 @@ script = create_script
#
def create_help_entry(key, entrytext, category="General", locks=None, aliases=None):
def create_help_entry(key, entrytext, category="General", locks=None, aliases=None, tags=None):
"""
Create a static help entry in the help database. Note that Command
help entries are dynamic and directly taken from the __doc__
@ -326,7 +326,8 @@ def create_help_entry(key, entrytext, category="General", locks=None, aliases=No
entrytext (str): The body of te help entry
category (str, optional): The help category of the entry.
locks (str, optional): A lockstring to restrict access.
aliases (list of str): List of alternative (likely shorter) keynames.
aliases (list of str, optional): List of alternative (likely shorter) keynames.
tags (lst, optional): List of tags or tuples `(tag, category)`.
Returns:
help (HelpEntry): A newly created help entry.
@ -344,7 +345,9 @@ def create_help_entry(key, entrytext, category="General", locks=None, aliases=No
if locks:
new_help.locks.add(locks)
if aliases:
new_help.aliases.add(aliases)
new_help.aliases.add(make_iter(aliases))
if tags:
new_help.tags.batch_add(*tags)
new_help.save()
return new_help
except IntegrityError:
@ -366,7 +369,8 @@ help_entry = create_help_entry
# Comm system methods
def create_message(senderobj, message, channels=None, receivers=None, locks=None, header=None):
def create_message(senderobj, message, channels=None, receivers=None,
locks=None, tags=None, header=None):
"""
Create a new communication Msg. Msgs represent a unit of
database-persistent communication between entites.
@ -382,6 +386,7 @@ def create_message(senderobj, message, channels=None, receivers=None, locks=None
receivers (Object, Account, str or list): An Account/Object to send
to, or a list of them. May be Account objects or accountnames.
locks (str): Lock definition string.
tags (list): A list of tags or tuples `(tag, category)`.
header (str): Mime-type or other optional information for the message
Notes:
@ -408,6 +413,9 @@ def create_message(senderobj, message, channels=None, receivers=None, locks=None
new_message.receivers = receiver
if locks:
new_message.locks.add(locks)
if tags:
new_message.tags.batch_add(*tags)
new_message.save()
return new_message
@ -416,7 +424,7 @@ message = create_message
create_msg = create_message
def create_channel(key, aliases=None, desc=None, locks=None, keep_log=True, typeclass=None):
def create_channel(key, aliases=None, desc=None, locks=None, keep_log=True, typeclass=None, tags=None):
"""
Create A communication Channel. A Channel serves as a central hub
for distributing Msgs to groups of people without specifying the
@ -435,6 +443,7 @@ def create_channel(key, aliases=None, desc=None, locks=None, keep_log=True, type
keep_log (bool): Log channel throughput.
typeclass (str or class): The typeclass of the Channel (not
often used).
tags (list): A list of tags or tuples `(tag, category)`.
Returns:
channel (Channel): A newly created channel.
@ -451,7 +460,7 @@ def create_channel(key, aliases=None, desc=None, locks=None, keep_log=True, type
# store call signature for the signal
new_channel._createdict = dict(
key=key, aliases=aliases, desc=desc, locks=locks, keep_log=keep_log
key=key, aliases=aliases, desc=desc, locks=locks, keep_log=keep_log, tags=tags
)
# this will trigger the save signal which in turn calls the

View file

@ -3,6 +3,7 @@ Tests of create functions
"""
from django.test import TestCase
from evennia.utils.test_resources import EvenniaTest
from evennia.scripts.scripts import DefaultScript
from evennia.utils import create
@ -76,3 +77,93 @@ class TestCreateScript(EvenniaTest):
assert script.repeats == 1
assert script.key == "test_script"
script.stop()
class TestCreateHelpEntry(TestCase):
help_entry = """
Qui laborum voluptas quis commodi ipsum quo temporibus eum. Facilis
assumenda facilis architecto in corrupti. Est placeat eum amet qui beatae
reiciendis. Accusamus vel aspernatur ab ex. Quam expedita sed expedita
consequuntur est dolorum non exercitationem.
Ipsa vel ut dolorem voluptatem adipisci velit. Sit odit temporibus mollitia
illum ipsam placeat. Rem et ipsum dolor. Hic eum tempore excepturi qui veniam
magni.
Excepturi quam repellendus inventore excepturi fugiat quo quasi molestias.
Nostrum ut assumenda enim a. Repellat quis omnis est officia accusantium. Fugit
facere qui aperiam. Perspiciatis commodi dolores ipsam nemo consequatur
quisquam qui non. Adipisci et molestias voluptatum est sed fugiat facere.
"""
def test_create_help_entry__simple(self):
entry = create.create_help_entry("testentry", self.help_entry, category="Testing")
self.assertEqual(entry.key, "testentry")
self.assertEqual(entry.entrytext, self.help_entry)
self.assertEqual(entry.help_category, "Testing")
# creating same-named entry should not work (must edit existing)
self.assertFalse(create.create_help_entry("testentry", "testtext"))
def test_create_help_entry__complex(self):
locks = "foo:false();bar:true()"
aliases = ['foo', 'bar', 'tst']
tags = [("tag1", "help"), ("tag2", "help"), ("tag3", "help")]
entry = create.create_help_entry("testentry", self.help_entry, category="Testing",
locks=locks, aliases=aliases, tags=tags)
self.assertTrue(all(lock in entry.locks.all() for lock in locks.split(";")))
self.assertEqual(list(entry.aliases.all()).sort(), aliases.sort())
self.assertEqual(entry.tags.all(return_key_and_category=True), tags)
class TestCreateMessage(EvenniaTest):
msgtext = """
Qui laborum voluptas quis commodi ipsum quo temporibus eum. Facilis
assumenda facilis architecto in corrupti. Est placeat eum amet qui beatae
reiciendis. Accusamus vel aspernatur ab ex. Quam expedita sed expedita
consequuntur est dolorum non exercitationem.
"""
def test_create_msg__simple(self):
msg = create.create_message(self.char1, self.msgtext, header="TestHeader")
self.assertEqual(msg.message, self.msgtext)
self.assertEqual(msg.header, "TestHeader")
self.assertEqual(msg.senders, [self.char1])
def test_create_msg__channel(self):
chan1 = create.create_channel("DummyChannel1")
chan2 = create.create_channel("DummyChannel2")
msg = create.create_message(self.char1, self.msgtext, channels=[chan1, chan2], header="TestHeader")
self.assertEqual(list(msg.channels), [chan1, chan2])
def test_create_msg__custom(self):
locks = "foo:false();bar:true()"
tags = ["tag1", "tag2", "tag3"]
msg = create.create_message(self.char1, self.msgtext, header="TestHeader",
receivers=[self.char1, self.char2], locks=locks, tags=tags)
self.assertEqual(msg.receivers, [self.char1, self.char2])
self.assertTrue(all(lock in msg.locks.all() for lock in locks.split(";")))
self.assertEqual(msg.tags.all(), tags)
class TestCreateChannel(TestCase):
def test_create_channel__simple(self):
chan = create.create_channel("TestChannel1", desc="Testing channel")
self.assertEqual(chan.key, "TestChannel1")
self.assertEqual(chan.db.desc, "Testing channel")
def test_create_channel__complex(self):
locks = "foo:false();bar:true()"
tags = ["tag1", "tag2", "tag3"]
aliases = ['foo', 'bar', 'tst']
chan = create.create_channel("TestChannel2", desc="Testing channel",
aliases=aliases, locks=locks, tags=tags)
self.assertTrue(all(lock in chan.locks.all() for lock in locks.split(";")))
self.assertEqual(chan.tags.all(), tags)
self.assertEqual(list(chan.aliases.all()).sort(), aliases.sort())