def test_notify(self): r = Notifier(Context()) self.assertEqual(r.route, '/notify') self.assertTrue(r.queue is not None) self.assertEqual(r.notification, None) with self.assertRaises(Exception): r.get() queue = Queue() r = Notifier(Context(), queue=queue, route='/test') self.assertEqual(r.queue, queue) self.assertEqual(r.notification, None) self.assertEqual(r.get(), 'OK') self.assertEqual(queue.get(), '/test') queue = Queue() r = Notifier(Context(), queue=queue, notification='signal') self.assertEqual(r.queue, queue) self.assertEqual(r.notification, 'signal') self.assertEqual(r.get(), 'OK') self.assertEqual(queue.get(), 'signal') self.assertEqual(r.post(), 'OK') self.assertEqual(queue.get(), 'signal') self.assertEqual(r.put(), 'OK') self.assertEqual(queue.get(), 'signal') self.assertEqual(r.delete(), 'OK') self.assertEqual(queue.get(), 'signal')
def test_wrapper(self): r = Wrapper(Context()) with self.assertRaises(AttributeError): r.get() with self.assertRaises(AttributeError): r.post() with self.assertRaises(AttributeError): r.put() with self.assertRaises(AttributeError): r.delete() def hook(): return 'hello' def hook_patched(): return 'world' r = Wrapper(callable=hook, route='/wrapped') self.assertEqual(r.route, '/wrapped') self.assertTrue(r.callable is not None) self.assertEqual(r.get(), 'hello') self.assertEqual(r.post(), 'hello') self.assertEqual(r.put(), 'hello') self.assertEqual(r.delete(), 'hello') r.callable = hook_patched self.assertEqual(r.get(), 'world') self.assertEqual(r.post(), 'world') self.assertEqual(r.put(), 'world') self.assertEqual(r.delete(), 'world') context = Context() class Callable(object): def __init__(self, context): self.context = context def hook(self, **kwargs): self.context.set('signal', 'wrapped!') return 'OK' callable = Callable(context) r = Wrapper(context=context, callable=callable.hook, route='/wrapped') self.assertEqual(r.route, '/wrapped') self.assertEqual(r.callable, callable.hook) self.assertEqual(context.get('signal'), None) self.assertEqual(r.get(), 'OK') self.assertEqual(r.post(), 'OK') self.assertEqual(r.put(), 'OK') self.assertEqual(r.delete(), 'OK') self.assertEqual(context.get('signal'), 'wrapped!')
def test_text(self): r = Text(Context()) self.assertEqual(r.route, '/') self.assertEqual(r.page, None) self.assertEqual(r.get(), 'OK') r = Text(Context(), route='/hello', page='Hello world') self.assertEqual(r.route, '/hello') self.assertEqual(r.page, 'Hello world') self.assertEqual(r.get(), 'Hello world')
def test_configuration_2(self): logging.info('*** configure 2 ***') settings = { 'bot': { 'on_enter': 'Hello!', 'on_exit': 'Bye!', }, 'space': { 'title': 'Support channel', }, 'server': { 'url': 'http://to.nowhere/', 'trigger': '/trigger', 'hook': '/hook', 'binding': '0.0.0.0', 'port': 8080, }, } clear_env('CHANNEL_DEFAULT_PARTICIPANTS') context = Context(settings) engine = Engine(context=context, configure=True) self.assertEqual(engine.get('bot.on_enter'), 'Hello!') self.assertEqual(engine.get('bot.on_exit'), 'Bye!') self.assertEqual(engine.get('space.title'), 'Support channel') self.assertEqual(engine.get('space.participants'), None) self.assertEqual(engine.get('server.url'), 'http://to.nowhere/') self.assertEqual(engine.get('server.hook'), '/hook') self.assertEqual(engine.get('server.trigger'), '/trigger') self.assertEqual(engine.get('server.binding'), None) self.assertEqual(engine.get('server.port'), 8080)
def setUp(self): self.context = Context() self.ears = Queue() self.fan = Queue() self.space = SparkSpace(context=self.context, ears=self.ears, fan=self.fan)
def test_concurrency(self): def worker(id, context): for i in range(4): r = random.random() time.sleep(r) value = context.increment('gauge') logging.info('worker %d:counter=%d' % (id, value)) logging.info('worker %d:done' % id) logging.info('Creating a counter') self.counter = Context() logging.info('Launching incrementing workers') workers = [] for i in range(4): p = Process(target=worker, args=( i, self.counter, )) p.start() workers.append(p) logging.info('Waiting for worker threads') for p in workers: p.join() logging.info('Counter: %d' % self.counter.get('gauge')) self.assertEqual(self.counter.get('gauge'), 16)
def test_init(self): logging.info('*** init ***') settings = { 'localized': { 'hello world': "What'up, Doc?", 'another string': 'Bye!', }, 'space': { 'title': 'space name', 'participants': ['*****@*****.**'], }, 'server': { 'url': 'http://to.no.where', 'hook': '/hook', 'binding': '0.0.0.0', 'port': 8080, }, } context = Context(settings) my_localization = Localization(context) self.assertEqual(my_localization.context, context) self.assertEqual(my_localization._('hello world'), "What'up, Doc?") self.assertEqual(my_localization._('not localized'), 'not localized') self.assertEqual(my_localization.actual_strings, { 'hello world': "What'up, Doc?", 'not localized': 'not localized' })
def test_configuration(self): logging.info('*** Configuration test ***') settings = { 'server': { 'binding': '1.2.3.4', 'port': 8888, 'debug': True, }, } server = Server(context=my_context) self.assertEqual(server.context.get('server.binding'), None) self.assertEqual(server.context.get('server.port'), None) self.assertEqual(server.context.get('server.debug'), None) server.configure(settings) self.assertEqual(server.context.get('server.binding'), '1.2.3.4') self.assertEqual(server.context.get('server.port'), 8888) self.assertEqual(server.context.get('server.debug'), True) context = Context(settings) server = Server(context=context) self.assertEqual(server.context.get('server.binding'), '1.2.3.4') self.assertEqual(server.context.get('server.port'), 8888) self.assertEqual(server.context.get('server.debug'), True)
def test_load_commands_via_configure(self): logging.info('***** load_commands via configure') settings = { 'shell': {'commands': ['shellbot.commands.help', 'shellbot.commands.noop']}, 'localized': { 'help': 'aide', }, } context=Context(settings) l10n.set_context(context) shell = Shell(engine=self.engine) shell.configure(settings) self.assertEqual( shell.commands, ['*default', '*empty', '*upload', 'aide', 'echo', 'pass', 'sleep', 'version']) l10n.set_context(self.engine.context)
def setUp(self): self.context = Context(settings=my_settings) self.engine = Engine(context=self.context, mouth=Queue()) self.engine.factory = TodoFactory(self.engine.get('todos.items', [])) self.engine.bus = Bus(self.context) self.engine.bus.check() self.engine.publisher = self.engine.bus.publish() self.bot = self.engine.get_bot()
def setUp(self): self.context = Context() self.engine = Engine(context=self.context, mouth=Queue()) self.space = LocalSpace(context=self.context, ears=self.engine.ears) self.store = MemoryStore(context=self.context) self.machine = MyMachine() self.bot = MyBot(engine=self.engine) self.bot.machine = None
def test_from_base(self): w = ['world'] q = Queue() r = Route(Context(), arg_1='hello', arg_2=w, arg_3=q, route='/ping') self.assertEqual(r.arg_1, 'hello') self.assertEqual(r.arg_2, w) self.assertEqual(r.arg_3, q) self.assertEqual(r.route, '/ping')
def setUp(self): self.context = Context() self.engine = Engine(context=self.context, ears=Queue(), mouth=Queue()) self.engine.bus = Bus(self.context) self.engine.bus.check() self.engine.publisher = self.engine.bus.publish() self.space = LocalSpace(context=self.context, ears=self.engine.ears) self.store = MemoryStore(context=self.context) self.bot = ShellBot(engine=self.engine, space=self.space, store=self.store) self.channel = Channel({'id': '*id', 'title': '*title'})
def test_init(self): settings = { 'bot': { 'name': 'testy', 'version': '17.4.1' }, } self.context = Context(settings) self.assertEqual(self.context.get('bot.name'), 'testy') self.assertEqual(self.context.get('bot.version'), '17.4.1')
def test_base(self): settings = { u'hello': u'world', } context = Context(settings) r = Route(context) self.assertEqual(r.context.get('hello'), u'world') self.assertEqual(r.route, None) with self.assertRaises(NotImplementedError): r.get() with self.assertRaises(NotImplementedError): r.post() with self.assertRaises(NotImplementedError): r.put() with self.assertRaises(NotImplementedError): r.delete()
is_direct = False class MyBot(object): channel = MyChannel() id = '123' def __init__(self, engine): self.engine = engine def say(self, text, content=None, file=None): self.engine.mouth.put(Vibes(text, content, file)) my_context = Context() my_ears = Queue() my_mouth = Queue() my_space = Space(my_context) my_space.post_message = MagicMock() class CompositeTests(unittest.TestCase): def setUp(self): self.engine = MyEngine(context=my_context, ears=my_ears, mouth=my_mouth, space=my_space) self.engine.set( 'bot.id', "Y2lzY29zcGFyazovL3VzL1BFT1BMRS83YWYyZjcyYy0xZDk1LTQxZjAtYTcxNi00MjlmZmNmYmM0ZDg"
'message': u'Escalation to technical experts', 'participants': '*****@*****.**', }, { 'label': u'Level 3', 'message': u'Escalation to decision stakeholders', 'participants': '*****@*****.**', }, { 'label': u'Terminated', 'message': u'Process is closed, yet conversation can continue', }, ], } context = Context(settings) context.check('server.trigger', '/trigger') context.check('server.hook', '/hook') engine = Engine( # use Cisco Spark and setup the environment type='spark', context=context, configure=True, commands=['shellbot.commands.step', 'shellbot.commands.close'], machine_factory=MyFactory(steps=context.get('process.steps')), ears=Queue(), ) server = Server(context=context, check=True) # set web front-end server.add_route(
import json import logging import sys import time from shellbot import Context from shellbot.bus import Bus, Subscriber, Publisher Context.set_logger() # this should be ran manually for test purpose # you can run multiple instances of this program and see message replication # done by ZeroMQ bus = Bus(context=Context()) bus.check() subscriber = bus.subscribe(['topic_A', 'topic_B']) logging.info("Waiting for messages (non-blocking mode)") while True: time.sleep(0.01) message = subscriber.get() if message: logging.info("- receiving: {}".format(message)) if isinstance(message, dict): logging.info("- visible keys: {}".format(message.keys())) if str(message) == 'quit': logging.info("- stopping subscriber") break
def __init__(self, context=None): self.context = context if context else Context() self.lists = {}
def setUp(self): self.context = Context()
def test_vocabulary_localized(self): logging.info('***** localized vocabulary') settings = { 'localized': { 'help': 'aide', 'help <command>': u'aide <commande>', 'Show commands and usage': u'Liste les commandes et leur usage', 'Available commands:': 'Commandes disponibles :', "Sorry, I do not know how to handle '{}'": \ u"Désolé, je ne sais pas traiter '{}'", 'usage: {}': u'utilisation : {}', u'Thank you for the information shared!': \ u"Merci pour le partage d'informations !", }, } context=Context(settings) l10n.set_context(context) shell = Shell(engine=self.engine) shell.load_default_commands() self.assertEqual(len(shell.commands), 8) self.assertEqual(shell.line, None) self.assertEqual(shell.count, 0) shell.do('*inconnu*', received=self.message) self.assertEqual(shell.line, '*inconnu*') self.assertEqual(shell.count, 1) self.assertEqual(shell.engine.mouth.get().text, u"Désolé, je ne sais pas traiter '*inconnu*'") with self.assertRaises(Exception): shell.engine.mouth.get_nowait() shell.do('echo hello world', received=self.message) self.assertEqual(shell.line, 'echo hello world') self.assertEqual(shell.count, 2) self.assertEqual(shell.engine.mouth.get().text, 'hello world') with self.assertRaises(Exception): shell.engine.mouth.get_nowait() shell.do('aide aide', received=self.message) self.assertEqual(shell.line, 'aide aide') self.assertEqual(shell.count, 3) self.assertEqual(shell.engine.mouth.get().text, u'aide - Liste les commandes et leur usage\n' + u'utilisation : aide <commande>') with self.assertRaises(Exception): print(shell.engine.mouth.get_nowait()) shell.do('pass', received=self.message) self.assertEqual(shell.line, 'pass') self.assertEqual(shell.count, 4) with self.assertRaises(Exception): shell.engine.mouth.get_nowait() shell.do('sleep .0103', received=self.message) self.assertEqual(shell.line, 'sleep .0103') self.assertEqual(shell.count, 5) self.engine.set('worker.busy', True) shell.do('sleep .0201', received=self.message) self.assertEqual(shell.line, 'sleep .0201') self.assertEqual(shell.count, 6) self.engine.set('worker.busy', False) with self.assertRaises(Exception): print(shell.engine.mouth.get_nowait()) shell.do('version', received=self.message) self.assertEqual(shell.line, 'version') self.assertEqual(shell.count, 7) self.assertEqual(shell.engine.mouth.get().text, u'Shelly version *unknown*') with self.assertRaises(Exception): shell.engine.mouth.get_nowait() shell.do('', received=self.message) self.assertEqual(shell.line, '') self.assertEqual(shell.count, 8) self.assertEqual( shell.engine.mouth.get().text, u'Commandes disponibles :\n' + u'aide - Liste les commandes et leur usage') with self.assertRaises(Exception): print(shell.engine.mouth.get_nowait()) shell.do('', received=self.upload) self.assertEqual(shell.line, '') self.assertEqual(shell.count, 9) self.assertEqual( shell.engine.mouth.get().text, u"Merci pour le partage d'informations !") with self.assertRaises(Exception): print(shell.engine.mouth.get_nowait()) print("**************************") print(l10n.actual_strings) print("**************************") l10n.set_context(self.engine.context)
def setUp(self): self.context = Context() self.context.set('general.switch', 'on') self.context.set('bus.address', 'tcp://127.0.0.1:6666') self.bus = Bus(context=self.context)
def test_init_filter(self): self.context = Context(filter=lambda x, y: x + '...') self.context.apply({'my.var': 'my value'}) self.context.check('my.var', filter=True) self.assertEqual(self.context.get('my.var'), 'my value...')
def setUp(self): self.context = Context() self.engine = Engine(context=self.context, mouth=Queue()) self.engine.configure() self.bot = Bot(engine=self.engine)
def setUp(self): self.context = Context() self.engine = Engine(context=self.context, mouth=Queue()) self.space = LocalSpace(context=self.context) self.engine.space = self.space
def setUp(self): self.context = Context() self.db_name = os.path.join( os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'local', 'store.db')
def setUp(self): self.context = Context() self.space = Space(context=self.context)
def setUp(self): self.context = Context() self.engine = Engine(context=self.context, mouth=Queue()) self.store = MemoryStore(context=self.context) self.bot = Bot(engine=self.engine)