def test_cleanup(): baseplate = Lego(None, threading.Lock()) baseplate.add_child(Lego) child = baseplate.children[0] child.stop() baseplate.cleanup() assert(len(baseplate.children) == 0) # nosec
def test_add_child(): baseplate = Lego(None, threading.Lock()) baseplate.add_child(Lego) child = baseplate.children[0] assert(isinstance(child, pykka.ActorRef)) # nosec child_proxy = child.proxy() child_proxy.add_child(Lego) child_children = child_proxy.children.get() assert(isinstance(child_children[0], pykka.ActorRef)) # nosec child_children[0].stop() child.stop()
def test_receive_logs(): log_file_name = 'test_logging.log' lego = Lego(None, threading.Lock(), log_file_name) message = Message('Test Message 1', Metadata(None).__dict__, True).__dict__ lego.on_receive(message) with open(log_file_name, mode='r') as f: log = json.loads(f.read()) assert log == message # nosec os.remove(log_file_name)
def test_on_receive_informs_children(): log_file_name = 'test_child_informed.log' baseplate = Lego(None, threading.Lock()) child = Lego.start(baseplate, threading.Lock(), log_file_name) baseplate.children.append(child) message = Message('Test Message 1', Metadata(None).__dict__, True).__dict__ baseplate.on_receive(message) child.stop() with open(log_file_name, mode='r') as f: log = json.loads(f.read()) os.remove(log_file_name) assert log == message # nosec
def test_reply(): # Setup baseplate = Lego.start(None, threading.Lock()) baseplate_proxy = baseplate.proxy() urn = baseplate.actor_urn meta = Metadata(source=urn).__dict__ message = Message(text='0', metadata=meta, should_log=True).__dict__ log_file_name = 'test_reply.log' children = baseplate_proxy.children.get() for child in children: print(child.actor_urn) # Test baseplate_proxy.add_child(ReplyTestingPingLego, log_file_name) baseplate.tell(message) time.sleep(1) # Cleanup children = baseplate_proxy.children.get() for child in children: child.stop() baseplate.stop() with open(log_file_name, mode='r') as f: log = json.loads(f.read()) os.remove(log_file_name) assert log['text'] == '1' # nosec
def _make_testing_connector(temp_file=None): lock = threading.Lock() baseplate = Lego(None, lock) if temp_file is None: testing_connector = TestingConnector(baseplate, lock) else: testing_connector = TestingConnector(baseplate, lock, temp_file) return testing_connector # nosec
def test_initialization(): source = Lego(None, threading.Lock()) metadata = Metadata(source) message = Message('a message', metadata) assert (message.text == 'a message') # nosec assert (message.metadata == metadata) # nosec message = Message('a message', metadata, True) assert (message.text == 'a message') # nosec assert (message.metadata == metadata) # nosec assert (message.should_log) # nosec
def test_cleanup(): baseplate = Lego(None, threading.Lock()) baseplate.add_child(Lego) child = baseplate.children[0] child.stop() baseplate.cleanup() assert (len(baseplate.children) == 0) # nosec
def test_reply(self): log_file_name = 'test_reply.log' baseplate = Lego.start(None, threading.Lock()) baseplate_proxy = baseplate.proxy() baseplate_proxy.add_child(ReplyTestingPingLego, log_file_name) baseplate_proxy.add_child(ReplyTestingPongLego) baseplate.tell(Message('0', Metadata(None).__dict__, True).__dict__) time.sleep(1) children = baseplate_proxy.children.get() for child in children: child.stop() baseplate.stop() with open(log_file_name, mode='r') as f: log = json.loads(f.read()) os.remove(log_file_name) assert log['text'] == '4' # nosec
def test_initialization(): baseplate = Lego.start(None, threading.Lock()) baseplate_proxy = baseplate.proxy() baseplate_proxy.add_child(IRC, # nosec channels=['#foo'], nickname='test_nick', server='foo.testing', port=6667, use_ssl=False, username='******', password='******') # Cleanup children = baseplate_proxy.children.get() for child in children: child.stop() baseplate.stop()
def test_initialization(): lego = Lego(None, threading.Lock()) message = Message('Test Message', Metadata(lego)) thread = Lego.HandlerThread(lego.handle, message) assert thread.handler == lego.handle # nosec assert thread.message == message # nosec
def test_handle(self): lego = Lego(None, threading.Lock()) assert(lego.handle(self.make_message()) is None) # nosec
def test_listening_for(self): lego = Lego(None, threading.Lock()) message = self.make_message() assert not lego.listening_for(message) # nosec
def test_on_failure(): lego = Lego(None, threading.Lock()) lego.on_failure("Exception Type", "Exception Value", "Traceback") assert True # nosec
def test_default_init_values(): lock = threading.Lock() baseplate = Lego(None, lock) lego = Lego(baseplate, baseplate.lock) assert lego.log_file is None # nosec
def test_get_name(): lego = Lego(None, threading.Lock()) assert lego.get_name() == '?' # nosec
def test_get_help(): lego = Lego(None, threading.Lock()) assert lego.get_help() == '' # nosec
def test_initialization(): source = Lego(None, threading.Lock()) dest = Lego(None, threading.Lock()) metadata = Metadata(source, dest) assert (metadata.source == source) # nosec assert (metadata.dest == dest) # nosec
config = configparser.ConfigParser() config.read('config.ini.example') logger = logging.getLogger() logger.setLevel(logging.DEBUG) ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) # create formatter and add it to the handlers formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') ch.setFormatter(formatter) # add the handlers to the logger logger.addHandler(ch) # Initialize lock and baseplate lock = threading.Lock() baseplate = Lego.start(None, lock) baseplate_proxy = baseplate.proxy() # Add children baseplate_proxy.add_child( IRC, channels=[ channel.strip() for channel in config.get("irc1", "channel").split(",") ], nickname=config['irc1']['username'], server=config['irc1']['host'], port=int(config['irc1']['port']), use_ssl=config.getboolean('irc1', 'ssl'), username=config['irc1']['username'], password=config['irc1']['password']) baseplate_proxy.add_child(Help)
def bot_init(): global baseplate_proxy # Initialize lock and baseplate lock = threading.Lock() baseplate = Lego.start(None, lock) baseplate_proxy = baseplate.proxy()
def test_lock_required(self): with self.assertRaises(LegoError): lego = Lego(None, None) # noqa: F841
def test_default_init_values(): source = Lego(None, threading.Lock()) metadata = Metadata(source) message = Message('a message', metadata) assert (not message.should_log) # nosec
def initialize_baseplate(self): lock = threading.Lock() self.baseplate = Lego.start(None, lock) self.baseplate_proxy = self.baseplate.proxy()
def test_handle(self): lego = Lego(None, threading.Lock()) assert (lego.handle(self.make_message()) is None) # nosec
import sys import threading from Legobot.Lego import Lego from mock import patch LOCAL_PATH = os.path.join( os.path.abspath(os.path.dirname(__file__)), '..', 'Local' ) sys.path.append(LOCAL_PATH) from shakespeare import Shakespeare # noqa: E402 LOCK = threading.Lock() BASEPLATE = Lego.start(None, LOCK) LEGO = Shakespeare(BASEPLATE, LOCK) def test_listening_for(): assert LEGO.listening_for({'text': 'shake'}) is False assert LEGO.listening_for({'text': '!shake moin'}) is True assert LEGO.listening_for({'text': '!shake'}) is True assert LEGO.listening_for({'text': '!Shake'}) is False def test_get_quote(): default = 'Not so much brain as ear wax.' assert LEGO._get_quote('') == default assert LEGO._get_quote('bad search') == default assert LEGO._get_quote('scurvy') == '11. I scorn you, scurvy companion.'
def _make_message(): source = Lego(None, threading.Lock()) metadata = Metadata(source) message = Message('blah', metadata) return message
from Legobot.Legos.Help import Help config = configparser.ConfigParser() config.read('config.ini') logger = logging.getLogger() logger.setLevel(logging.DEBUG) ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) # create formatter and add it to the handlers formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') ch.setFormatter(formatter) # add the handlers to the logger logger.addHandler(ch) # Initialize lock and baseplate lock = threading.Lock() baseplate = Lego.start(None, lock) baseplate_proxy = baseplate.proxy() # Add children baseplate_proxy.add_child(IRC, channels=[channel.strip() for channel in config.get( "irc1", "channel").split(",")], nickname=config['irc1']['username'], server=config['irc1']['host'], port=int(config['irc1']['port']), use_ssl=config.getboolean('irc1', 'ssl'), username=config['irc1']['username'], password=config['irc1']['password']) baseplate_proxy.add_child(Help)
def test_default_init_values(): source = Lego(None, threading.Lock()) metadata = Metadata(source) assert (metadata.dest is None) # nosec