Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oklahomer committed Aug 8, 2015
1 parent 98d8933 commit 82758a7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
37 changes: 33 additions & 4 deletions tests/test_hipchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@
import concurrent
from concurrent.futures import ALL_COMPLETED
from time import sleep
import pytest
import logging
import types

import pytest
from apscheduler.triggers.interval import IntervalTrigger
from sleekxmpp import ClientXMPP
from sleekxmpp.test import TestSocket
from sleekxmpp.stanza import Message
from sleekxmpp.exceptions import IqTimeout, IqError
from sleekxmpp.xmlstream import JID
from mock import MagicMock, call, patch
from sarah import CommandMessage

from sarah import CommandMessage, UserContext
from sarah.hipchat import HipChat, SarahHipChatException
import sarah.plugins.simple_counter
import types


# noinspection PyProtectedMember
Expand Down Expand Up @@ -174,7 +176,8 @@ def hipchat(self, request):
h = HipChat(nick='Sarah',
jid='test@localhost',
password='password',
plugins=(('sarah.plugins.simple_counter', {}),
plugins=(('sarah.plugins.hello', {}),
('sarah.plugins.simple_counter', {}),
('sarah.plugins.echo',)),
max_workers=4)
h.client.connect = lambda: True
Expand Down Expand Up @@ -236,6 +239,32 @@ def test_count_message(self, hipchat):
'__stash', {}).get('hipchat', {})
assert stash == {'123_homer@localhost/Oklahomer': {'ham': 2, 'egg': 1}}

def test_conversation(self, hipchat):
user_key = '123_homer@localhost/Oklahomer'

# Initial message
assert (hipchat.respond(user_key, '.hello') ==
"Hello. How are you feeling today?")

# Context is set
assert isinstance(hipchat.user_context_map.get(user_key), UserContext)

# Wrong formatted message results with help message
assert (hipchat.respond(user_key, "SomeBizarreText") ==
"Say Good or Bad, please.")

assert (hipchat.respond(user_key, "Bad") ==
"Are you sick?")

# Still in conversation
assert isinstance(hipchat.user_context_map.get(user_key), UserContext)

# The last yes/no question
assert hipchat.respond(user_key, "Yes")

# Context is removed
assert hipchat.user_context_map.get(user_key) is None


# noinspection PyUnresolvedReferences
class TestSessionStart(object):
Expand Down
25 changes: 24 additions & 1 deletion tests/test_hipchat_plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
from sarah import CommandMessage
from sarah import CommandMessage, UserContext
from sarah.plugins.echo import hipchat_echo
from sarah.plugins.hello import hipchat_hello, hipchat_user_feeling_good, \
hipchat_user_feeling_bad
from sarah.plugins.simple_counter import hipchat_count, hipchat_reset_count, \
reset_count
from sarah.plugins.bmw_quotes import hipchat_quote
Expand Down Expand Up @@ -74,3 +76,24 @@ def test_valid(self):
sender='123_homer@localhost/Oklahomer')
response = hipchat_quote(msg, {})
assert (response in sarah.plugins.bmw_quotes.quotes) is True


class TestHello(object):
def test__init(self):
msg = CommandMessage(original_text='.hello',
text='',
sender='spam@localhost/ham')
response = hipchat_hello(msg, {})
assert isinstance(response, UserContext) is True
assert response.message == "Hello. How are you feeling today?"
assert len(response.input_options) == 2
assert (response.input_options[0].next_step.__name__ ==
hipchat_user_feeling_good.__name__)
assert (response.input_options[1].next_step.__name__ ==
hipchat_user_feeling_bad.__name__)

assert (response.input_options[0].next_step("Good") ==
"Good to hear that.")

isinstance(response.input_options[1].next_step("Bad"),
UserContext) is True

0 comments on commit 82758a7

Please sign in to comment.