Skip to content

Commit

Permalink
Several integrational setup wizard tests were added.
Browse files Browse the repository at this point in the history
  • Loading branch information
quasiyoke committed Jan 9, 2018
1 parent 5c8c683 commit 1199459
Show file tree
Hide file tree
Showing 11 changed files with 461 additions and 113 deletions.
8 changes: 6 additions & 2 deletions randtalkbot/stranger.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ async def _add_bonuses(self, bonuses_delta):
self.bonus_count += bonuses_delta
self.save()
bonuses_notifications_muted = getattr(self, '_bonuses_notifications_muted', False)

if not bonuses_notifications_muted:
await self._notify_about_bonuses(bonuses_delta)

Expand Down Expand Up @@ -261,6 +262,7 @@ def mute_bonuses_notifications(self):
# pylint: disable=attribute-defined-outside-init
self._bonuses_notifications_muted = True
asyncio.get_event_loop().create_task(self._unmute_bonuses_notifications(self.bonus_count))
LOGGER.debug('Bonuses notifications were muted for %d', self.id)

async def _unmute_bonuses_notifications(self, last_bonuses_count):
await asyncio.sleep(type(self).UNMUTE_BONUSES_NOTIFICATIONS_DELAY)
Expand Down Expand Up @@ -433,11 +435,13 @@ async def _reward_inviter(self):
return

talk = self.get_talk()
threshold_messages_count = 1

if talk is None or talk.partner1_sent != 1 or talk.partner2_sent != 1:
if talk is None or talk.partner1_sent != threshold_messages_count or \
talk.partner2_sent != threshold_messages_count:
return

LOGGER.debug('Rewarding stranger %d', self.id)
LOGGER.debug('Rewarding inviter of %d', self.id)
self.was_invited_as = self.sex
self.save()
sex_ratio = StatsService.get_instance().get_stats().get_sex_ratio()
Expand Down
11 changes: 11 additions & 0 deletions randtalkbot/stranger_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ def get_cached_stranger(self, stranger):
return self._strangers_cache[stranger.id]
except KeyError:
self._strangers_cache[stranger.id] = stranger

if stranger.invited_by is not None:
if stranger.invited_by.invited_by_id == stranger.id:
LOGGER.error(
'Circular reference between invited strangers %d and %d',
stranger.id,
stranger.invited_by_id,
)
else:
stranger.invited_by = self.get_cached_stranger(stranger.invited_by)

return stranger

def get_cache_size(self):
Expand Down
5 changes: 5 additions & 0 deletions randtalkbot/stranger_setup_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ async def _prompt(self):
' at special keyboard.'
)

LOGGER.debug(
'Sending languages setup notification `%s`. Keyboard: %s',
prompt,
keyboard,
)
await self._sender.send_notification(
prompt,
languages_enumeration,
Expand Down
4 changes: 4 additions & 0 deletions telepot_testing/aio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def get_future(seed_tuple):
Future: Future awaiting for received updates.
"""
async def wait_loop():
LOGGER.debug('Handler\'s first call: %s', update)
await handler.on_message(update)

while True:
Expand Down Expand Up @@ -132,4 +133,7 @@ async def sendMessage(
if disable_notification is not None:
update['disable_notification'] = disable_notification

if reply_markup is not None:
update['reply_markup'] = reply_markup

send_update(update)
29 changes: 23 additions & 6 deletions telepot_testing/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
UPDATES_FUTURES = []
UPDATES_TIMEOUT = 1

async def assert_sent_message(chat_id, text, disable_notification=None):
async def assert_sent_message(chat_id, text, disable_notification=None, reply_markup=None):
expected_update = {
'chat': {
'id': chat_id,
Expand All @@ -26,6 +26,9 @@ async def assert_sent_message(chat_id, text, disable_notification=None):
if disable_notification is not None:
expected_update['disable_notification'] = disable_notification

if reply_markup is not None:
expected_update['reply_markup'] = reply_markup

await assert_sent_update(expected_update)

async def assert_sent_update(expected_update):
Expand Down Expand Up @@ -64,16 +67,25 @@ async def get_sent_update():
expected_update_repr = pformat(expected_update)

if actual_update != expected_update:
raise AssertionError(
f'Expected sent update `{actual_update_repr}` to be equal `{expected_update_repr}`',
)
reason = f'Expected sent update `{actual_update_repr}`' \
f'\nto be equal `{expected_update_repr}`'
LOGGER.error(reason)
raise AssertionError(reason)

async def finalize():
LOGGER.debug('Finalizing')

if UPDATES_FUTURES:
try:
if UPDATES_FUTURES:
raise AssertionError('Updates\' futures weren\'t awaited')

for future in SENT_FUTURES:
if future.done():
result = future.result()
raise AssertionError(f'Sent future wasn\'t awaited: {result}')
finally:
SENT_FUTURES.clear()
UPDATES_FUTURES.clear()
raise AssertionError('Updates\' futures weren\'t awaited')

def get_first_not_done_future(futures):
for future in futures:
Expand Down Expand Up @@ -129,4 +141,9 @@ def receive_message(chat_id, text):
})

def send_update(update):
LOGGER.debug(
'Futures count: %d. Sending update to some (probably new) future: %s',
len(SENT_FUTURES),
update,
)
get_first_not_done_future(SENT_FUTURES).set_result(update)
35 changes: 19 additions & 16 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,26 @@ def raise_assertion_error(key, actual_value, expected_value):
if isinstance(expected_value, datetime.datetime):
if abs(actual_value - expected_value) > TIME_TOLERANCE:
raise_assertion_error(key, actual_value, expected_value)
elif hasattr(expected_value, 'match'): # Regular expression pattern.
if expected_value.match(actual_value) is None:
raise AssertionError(
f'Value of `{key}` is `{actual_value}` and doesn\'t match'
f' `{expected_value}`',
)
elif actual_value != expected_value:
raise_assertion_error(key, actual_value, expected_value)

strangers_dicts = db_dict.get('strangers')

if strangers_dicts:
for stranger_dict in strangers_dicts:
stranger_instance = Stranger.get(id=stranger_dict['id'])
assert_model(stranger_instance, stranger_dict)

talks_dicts = db_dict.get('talks')

if talks_dicts:
for talk_dict in talks_dicts:
talk_instance = Talk.get(id=talk_dict['id'])
assert_model(talk_instance, talk_dict)
for model_name, models_dicts in db_dict.items():
if model_name == 'strangers':
for stranger_dict in models_dicts:
stranger_instance = Stranger.get(id=stranger_dict['id'])
assert_model(stranger_instance, stranger_dict)
elif model_name == 'talks':
for talk_dict in models_dicts:
talk_instance = Talk.get(id=talk_dict['id'])
assert_model(talk_instance, talk_dict)
else:
raise AssertionError(f'Unknown model name: `{model_name}`')

def get_configuration_mock():
configuration = Mock()
Expand Down Expand Up @@ -84,13 +88,12 @@ def run(ctx):

def finalize(ctx):
ctx.database.drop_tables([Stranger, Talk])
ctx.task.cancel()
loop = asyncio.get_event_loop()
loop.run_until_complete(finalize_telepot())

for task in asyncio.Task.all_tasks():
task.cancel()

loop = asyncio.get_event_loop()
loop.run_until_complete(finalize_telepot())
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()

Expand Down
Loading

0 comments on commit 1199459

Please sign in to comment.