Skip to content
This repository has been archived by the owner on Jun 12, 2018. It is now read-only.

Commit

Permalink
Rewrite some of the tests as per Jeremy's suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Geddes committed Feb 21, 2014
1 parent 6577351 commit 216f447
Showing 1 changed file with 98 additions and 54 deletions.
152 changes: 98 additions & 54 deletions go/routers/application_multiplexer/tests/test_vumi_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class TestApplicationMultiplexerRouter(VumiTestCase):
]
}


@inlineCallbacks
def setUp(self):
self.router_helper = self.add_helper(
Expand Down Expand Up @@ -103,6 +102,24 @@ def check_state(self, router, state):
self.assertEqual(session, data,
msg="Unexpected session data")

@inlineCallbacks
def setup_session(self, user_id, data):
session_manager = yield self.router_worker.session_manager(
self.router_worker.CONFIG_CLASS(self.router_worker.config)
)
# Initialize session data
yield session_manager.save_session(user_id, data)

@inlineCallbacks
def assert_session_state(self, user_id, expected_session):
session_manager = yield self.router_worker.session_manager(
self.router_worker.CONFIG_CLASS(self.router_worker.config)
)
session = yield session_manager.load_session(user_id)
if 'created_at' in session:
del session['created_at']
self.assertEqual(session, expected_session,
msg="Unexpected session data")

def dynamic_config(self, fields):
config = self.router_worker.config.copy()
Expand Down Expand Up @@ -162,18 +179,20 @@ def test_state_start_to_select(self):
started=True,
config=self.ROUTER_CONFIG
)
yield self.check_state(router, {
'ri_inbound': (None, dict(from_addr='2323', session_event='new')),
'session': {},
'expect': {
'ri_outbound': ('Please select a choice.\n1) Flappy Bird', {}),
'session': {
'2323': {
'state': ApplicationMultiplexer.STATE_SELECT,
'endpoints': '["flappy-bird"]',
},
}
}
# msg sent from user
yield self.router_helper.ri.make_dispatch_inbound(
None,
router=router,
from_addr='123')

This comment has been minimized.

Copy link
@jerith

jerith Feb 21, 2014

Member

Rather put all the params on a single line if they fit:

yield self.router_helper.ri.make_dispatch_inbound(
    None, router=router, from_addr='123')

# assert that the user received a response
[msg] = self.router_helper.ri.get_dispatched_outbound()
self.assertEqual(msg['content'],
'Please select a choice.\n1) Flappy Bird')
# assert that session data updated correctly
yield self.assert_session_state('123', {
'state': ApplicationMultiplexer.STATE_SELECT,
'endpoints': '["flappy-bird"]',
})

@inlineCallbacks
Expand All @@ -182,26 +201,37 @@ def test_state_select_to_selected(self):
started=True,
config=self.ROUTER_CONFIG
)
yield self.check_state(router, {
'ri_inbound': ('1', dict(from_addr='2323',
session_event='resume')),
'ro_inbound': ('Flappy Flappy!', dict(session_event='resume')),
'session': {
'2323': {
'state': ApplicationMultiplexer.STATE_SELECT,
'endpoints': '["flappy-bird"]',
},
},
'expect': {
'ri_outbound': ('Flappy Flappy!', {}),
'session': {
'2323': {
'state': ApplicationMultiplexer.STATE_SELECTED,
'active_endpoint': 'flappy-bird',
'endpoints': '["flappy-bird"]',
},
}
}

yield self.setup_session('123', {
'state': ApplicationMultiplexer.STATE_SELECT,
'endpoints': '["flappy-bird"]',
})

# msg sent from user
msg = yield self.router_helper.ri.make_dispatch_inbound(
'1',
router=router,
from_addr='123',
session_event='resume'
)

# assert that message is forwarded to application
[msg] = self.router_helper.ro.get_dispatched_inbound()
self.assertEqual(msg['content'], None)
self.assertEqual(msg['session_event'], 'new')

# application sends reply
yield self.router_helper.ro.make_dispatch_reply(msg, 'Flappy Flappy!')

# assert that the user received a response
[msg] = self.router_helper.ri.get_dispatched_outbound()
self.assertEqual(msg['content'],
'Flappy Flappy!')

yield self.assert_session_state('123', {
'state': ApplicationMultiplexer.STATE_SELECTED,
'active_endpoint': 'flappy-bird',
'endpoints': '["flappy-bird"]',
})

@inlineCallbacks
Expand All @@ -210,27 +240,41 @@ def test_state_selected_to_selected(self):
started=True,
config=self.ROUTER_CONFIG
)
yield self.check_state(router, {
'ri_inbound': ('Up!', dict(from_addr='2323',
session_event='resume')),
'ro_inbound': ('Game Over!', dict(session_event='resume')),
'session': {
'2323': {
'state': ApplicationMultiplexer.STATE_SELECTED,
'active_endpoint': 'flappy-bird',
'endpoints': '["flappy-bird"]',
},
},
'expect': {
'ri_outbound': ('Game Over!', {}),
'session': {
'2323': {
'state': ApplicationMultiplexer.STATE_SELECTED,
'active_endpoint': 'flappy-bird',
'endpoints': '["flappy-bird"]',
},
}
}

yield self.setup_session('123', {
'state': ApplicationMultiplexer.STATE_SELECTED,
'active_endpoint': 'flappy-bird',
'endpoints': '["flappy-bird"]',
})

# msg sent from user
msg = yield self.router_helper.ri.make_dispatch_inbound(
'Up!',
router=router,
from_addr='123',
session_event='resume'
)

# assert that message is forwarded to application
[msg] = self.router_helper.ro.get_dispatched_inbound()
self.assertEqual(msg['content'], 'Up!')
self.assertEqual(msg['session_event'], 'resume')

# application sends reply
yield self.router_helper.ro.make_dispatch_reply(
msg,
'Game Over!\n1) Try Again!'
)

# assert that the user received a response
[msg] = self.router_helper.ri.get_dispatched_outbound()
self.assertEqual(msg['content'],
'Game Over!\n1) Try Again!')

yield self.assert_session_state('123', {
'state': ApplicationMultiplexer.STATE_SELECTED,
'active_endpoint': 'flappy-bird',
'endpoints': '["flappy-bird"]',
})

@inlineCallbacks
Expand Down

0 comments on commit 216f447

Please sign in to comment.