Page MenuHomePhabricator
Paste P3270

flow_thanks_tests.py v1.0
ActivePublic

Authored by darthbhyrava on Jun 19 2016, 2:08 PM.
# -*- coding: utf-8 -*-
"""Test suite for the flow based thanks script."""
#
# (C) Pywikibot team, 2016
#
# Distributed under the terms of the MIT license.
#
from __future__ import absolute_import, unicode_literals
import pywikibot
from scripts.flow_thanks import thank_flow_post
from pywikibot.flow import Board, Topic, Post
from tests.aspects import unittest, TestCase
class TestFlowThank(TestCase):
"""Test thanks for revisions."""
family = 'test'
code = 'test'
def test_topic (self):
"""
Method to test thanks for a Flow post
Note: This approach requires a new topic and flow post to be added atop
the Talk:Sandbox page between reruns, and will fail otherwise.
Alternatively, one could devise a method to use the post_ids list
in a better way.
"""
# A list which will store ids of posts from Talk:Sandbox, which can be
# thanked.
post_ids = []
site = pywikibot.Site()
# Getting the 'Talk:Sandbox' flow page on test:test
page = Board(self.site, 'Talk:Sandbox')
# Getting the a generator for topics on that page.
topics_list = page.topics(limit=5)
# The exception handling feature has been added to deal with the
# anomalous nature of 'Topic:Sel4ipu6maqlbm0d' which throws up a error.
#TODO: Fix the above.
try:
# Extracting each topic, and then the posts in each topic,
# and then appending these post_ids to a list.
for topic in topics_list:
post_list = topic.replies()
for post in post_list:
post_ids.append(post.uuid)
except Exception as a:
pass
# Checking log before the thanks action
log_initial = site.logevents(logtype='thanks', total=1)
for x in log_initial:
initial_id = x.logid()
# Thanking the most recent post on Talk:Sandbox
thank_flow_post(post_ids[0])
# Checking log for new thanks, and then comparing with earlier log.
log_final = site.logevents(logtype='thanks', total=1)
for x in log_final:
final_id = x.logid()
# Asserting that the ids are unequal, implying a successful
# thanks action.
self.assertNotEqual(initial_id, final_id)
if __name__ == "__main__":
unittest.main()

Event Timeline

What exceptions are being ignored on line 57?

As I mentioned in the meeting, total=1 in the second half is a bug. Just using assertNotEquals does not verify that the thanks was successful. Another user could have done a thanks . You need to check that your test user did the thanks.

In P3270#15483, @jayvdb wrote:

What exceptions are being ignored on line 57?

As I mentioned in the meeting, total=1 in the second half is a bug. Just using assertNotEquals does not verify that the thanks was successful. Another user could have done a thanks . You need to check that your test user did the thanks.

Exception handling is an error that is cropping up specifically for Topic:Sel4ipu6maqlbm0d on the Talk:Sandbox page on test:test, as I mentioned in the email. The exact error is as follows:

$ python -m unittest -v tests.flow_thanks_tests
tests: max_retries reduced from 25 to 1
test_topic (tests.flow_thanks_tests.TestFlowThank) ... ERROR
 11.651s 
======================================================================
ERROR: test_topic (tests.flow_thanks_tests.TestFlowThank)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "tests/flow_thanks_tests.py", line 53, in test_topic
    for topic in topics_list:
  File "pywikibot/flow.py", line 137, in topics
    topic = Topic.from_topiclist_data(self, root, data)
  File "pywikibot/flow.py", line 210, in from_topiclist_data
    topic._root = Post.fromJSON(topic, root_uuid, topiclist_data)
  File "pywikibot/flow.py", line 355, in fromJSON
    post = cls(page, post_uuid)
  File "pywikibot/flow.py", line 330, in __init__
    raise NoPage(page, 'Topic must exist: %s')
NoPage: Topic must exist: [[test:Topic:Sel4ipu6maqlbm0d]]

----------------------------------------------------------------------
Ran 1 test in 14.823s

FAILED (errors=1)

We had discussed total=1 for thanks_tests. Here, for flow_thanks_tests, there are no direct means of extracting the user and checking for User.thanks_enabled, in which case we had agreed upon adding in a note and looking at it later. Hence I am not checking for User.thanks_enabled here.

Re total=1, the thanking user (the test user) is in the log. It is the recipient that isnt logged publically.

Please raise a task in phab about the NoPage in flow.py . as a general rule, never hide exceptions, and if you must hide an exception then specify exactly which exception should be ignored and add a very good code comment explaining why.