mirror of
https://github.com/nicolabs/nicobot.git
synced 2025-09-07 05:14:01 +02:00
+ unit test for askbot
~ removed unused class arguments ~ fixed : imports are now relative so they can be imported from tests
This commit is contained in:
parent
b7215b1c1f
commit
f91738abcb
7
.travis.yml
Normal file
7
.travis.yml
Normal file
|
@ -0,0 +1,7 @@
|
|||
language: python
|
||||
python:
|
||||
- "3.6"
|
||||
install:
|
||||
- pip install -r requirements.txt
|
||||
script:
|
||||
- python -m unittest discover
|
|
@ -1,4 +1,4 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from bot import Bot
|
||||
from chatter import Chatter
|
||||
from helpers import *
|
||||
from .bot import Bot
|
||||
from .chatter import Chatter
|
||||
from .helpers import *
|
||||
|
|
|
@ -18,15 +18,15 @@ import yaml
|
|||
import urllib.request
|
||||
|
||||
# Own classes
|
||||
from helpers import *
|
||||
from bot import Bot
|
||||
from bot import ArgsHelper as BotArgsHelper
|
||||
from console import ConsoleChatter
|
||||
from jabber import JabberChatter
|
||||
from jabber import arg_parser as jabber_arg_parser
|
||||
from signalcli import SignalChatter
|
||||
from signalcli import ArgsHelper as SignalArgsHelper
|
||||
from stealth import StealthChatter
|
||||
from .helpers import *
|
||||
from .bot import Bot
|
||||
from .bot import ArgsHelper as BotArgsHelper
|
||||
from .console import ConsoleChatter
|
||||
from .jabber import JabberChatter
|
||||
from .jabber import arg_parser as jabber_arg_parser
|
||||
from .signalcli import SignalChatter
|
||||
from .signalcli import ArgsHelper as SignalArgsHelper
|
||||
from .stealth import StealthChatter
|
||||
|
||||
|
||||
# Default configuration (some defaults still need to be set up after command line has been parsed)
|
||||
|
@ -55,7 +55,7 @@ class AskBot(Bot):
|
|||
patterns : a list of 2-element list/tuple as [name,pattern]
|
||||
"""
|
||||
|
||||
def __init__( self, chatter, message, output=sys.stdout, err=sys.stderr, patterns=[], max_count=-1 ):
|
||||
def __init__( self, chatter, message, patterns=[], max_count=-1 ):
|
||||
|
||||
# TODO Implement a global session timeout after which the bot exits
|
||||
self.status = {
|
||||
|
@ -66,8 +66,6 @@ class AskBot(Bot):
|
|||
|
||||
self.chatter = chatter
|
||||
self.message = message
|
||||
self.output = output
|
||||
self.err = err
|
||||
self.max_count = max_count
|
||||
self.patterns = []
|
||||
for pattern in patterns:
|
||||
|
|
|
@ -8,10 +8,10 @@ import signal
|
|||
import sys
|
||||
|
||||
|
||||
from console import ConsoleChatter
|
||||
from jabber import JabberChatter
|
||||
from signalcli import SignalChatter
|
||||
from stealth import StealthChatter
|
||||
from .console import ConsoleChatter
|
||||
from .jabber import JabberChatter
|
||||
from .signalcli import SignalChatter
|
||||
from .stealth import StealthChatter
|
||||
|
||||
|
||||
class Bot:
|
||||
|
@ -162,7 +162,7 @@ class ArgsHelper:
|
|||
# By default (or if backend == "console"), will read from stdin or a given file and output to console
|
||||
else:
|
||||
logging.debug("Console backend selected")
|
||||
chatter = ConsoleChatter(args.input_file,sys.stdout)
|
||||
chatter = ConsoleChatter(args.input_file)
|
||||
|
||||
if args.stealth:
|
||||
chatter = StealthChatter(chatter)
|
||||
|
|
|
@ -2,17 +2,17 @@
|
|||
|
||||
import logging
|
||||
import sys
|
||||
from chatter import Chatter
|
||||
|
||||
from .chatter import Chatter
|
||||
|
||||
|
||||
class ConsoleChatter(Chatter):
|
||||
"""
|
||||
Bot engine that reads from a stream and outputs to another
|
||||
Bot engine that reads messages from a stream (stdin by default)
|
||||
"""
|
||||
|
||||
def __init__( self, input=sys.stdin, output=sys.stdout ):
|
||||
def __init__( self, input=sys.stdin ):
|
||||
self.input = input
|
||||
self.output = output
|
||||
self.exit = False
|
||||
|
||||
def start( self, bot ):
|
||||
|
|
|
@ -15,8 +15,8 @@ from slixmpp_omemo import UndecidedException, UntrustedException, NoAvailableSes
|
|||
from omemo.exceptions import MissingBundleException
|
||||
|
||||
# Own classes
|
||||
from chatter import Chatter
|
||||
from helpers import *
|
||||
from .chatter import Chatter
|
||||
from .helpers import *
|
||||
|
||||
|
||||
|
||||
|
@ -311,8 +311,9 @@ class JabberChatter(Chatter):
|
|||
Sends the given message using the underlying implemented chat protocol
|
||||
"""
|
||||
logging.debug(">>> %s",message)
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.run_until_complete(self.xmpp.encrypted_send( body=message, recipient=self.recipient ))
|
||||
#loop = asyncio.get_event_loop()
|
||||
# loop.run_until_complete(self.xmpp.encrypted_send( body=message, recipient=self.recipient ))
|
||||
asyncio.ensure_future( self.xmpp.encrypted_send( body=message, recipient=self.recipient ) )
|
||||
|
||||
def stop( self ):
|
||||
"""
|
||||
|
|
|
@ -15,8 +15,8 @@ import subprocess
|
|||
import sys
|
||||
import time
|
||||
|
||||
from chatter import Chatter
|
||||
from helpers import *
|
||||
from .chatter import Chatter
|
||||
from .helpers import *
|
||||
|
||||
|
||||
# Generic timeout for signal-cli commands to return (actually only 'send' because 'receive' uses its own timeout)
|
||||
|
|
|
@ -2,7 +2,8 @@
|
|||
|
||||
import logging
|
||||
import sys
|
||||
from chatter import Chatter
|
||||
|
||||
from .chatter import Chatter
|
||||
|
||||
|
||||
class StealthChatter(Chatter):
|
||||
|
|
|
@ -22,15 +22,15 @@ import yaml
|
|||
import urllib.request
|
||||
|
||||
# Own classes
|
||||
from helpers import *
|
||||
from bot import Bot
|
||||
from bot import ArgsHelper as BotArgsHelper
|
||||
from console import ConsoleChatter
|
||||
from jabber import JabberChatter
|
||||
from jabber import arg_parser as jabber_arg_parser
|
||||
from signalcli import SignalChatter
|
||||
from signalcli import ArgsHelper as SignalArgsHelper
|
||||
from stealth import StealthChatter
|
||||
from .helpers import *
|
||||
from .bot import Bot
|
||||
from .bot import ArgsHelper as BotArgsHelper
|
||||
from .console import ConsoleChatter
|
||||
from .jabber import JabberChatter
|
||||
from .jabber import arg_parser as jabber_arg_parser
|
||||
from .signalcli import SignalChatter
|
||||
from .signalcli import ArgsHelper as SignalArgsHelper
|
||||
from .stealth import StealthChatter
|
||||
|
||||
|
||||
|
||||
|
|
45
test_askbot.py
Normal file
45
test_askbot.py
Normal file
|
@ -0,0 +1,45 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import unittest
|
||||
|
||||
from nicobot.askbot import AskBot
|
||||
from nicobot.console import ConsoleChatter
|
||||
|
||||
|
||||
class TestAskbot(unittest.TestCase):
|
||||
|
||||
def test_end_on_yes(self):
|
||||
bot = AskBot(
|
||||
chatter = ConsoleChatter( input=["Yes !"] ),
|
||||
message = "ça va ?",
|
||||
patterns=[
|
||||
[ "yes", r'(?i)\b(yes|ok)\b' ],
|
||||
[ "no", r'(?i)\bno\b' ],
|
||||
[ "cancel", r'(?i)\b(cancel|abort)\b' ]
|
||||
],
|
||||
max_count=-1
|
||||
)
|
||||
result = bot.run()
|
||||
expected = {'max_count': False, 'events': [{'message': 'Yes !', 'matched_patterns': ['yes']}]}
|
||||
self.assertEqual(expected,result)
|
||||
|
||||
|
||||
def test_dont_end_on_coucou(self):
|
||||
bot = AskBot(
|
||||
chatter = ConsoleChatter( input=["Coucou","Yes !"] ),
|
||||
message = "ça va ?",
|
||||
patterns=[
|
||||
[ "yes", r'(?i)\b(yes|ok)\b' ],
|
||||
[ "no", r'(?i)\bno\b' ],
|
||||
[ "cancel", r'(?i)\b(cancel|abort)\b' ]
|
||||
],
|
||||
max_count=-1
|
||||
)
|
||||
result = bot.run()
|
||||
expected = {'max_count': False, 'events': [{'message': 'Coucou', 'matched_patterns': []}, {'message': 'Yes !', 'matched_patterns': ['yes']}]}
|
||||
self.assertEqual(expected,result)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Loading…
Reference in a new issue