Added logging, fixed tests

This commit is contained in:
Michael Lazar
2017-03-28 22:50:51 -07:00
parent 87576a6dec
commit 92a221305c
7 changed files with 42 additions and 23 deletions

View File

@@ -2,9 +2,7 @@ beautifulsoup4==4.5.1
decorator==4.0.10 decorator==4.0.10
kitchen==1.2.4 kitchen==1.2.4
mailcap-fix==0.1.3 mailcap-fix==0.1.3
praw==3.6.0
requests==2.11.0 requests==2.11.0
six==1.10.0 six==1.10.0
update-checker==0.11 pytest==3.0.7
pytest vcrpy==1.10.5
vcrpy

View File

@@ -12,6 +12,7 @@ import six
import requests import requests
from . import docs from . import docs
from . import packages
from .packages import praw from .packages import praw
from .config import Config, copy_default_config, copy_default_mailcap from .config import Config, copy_default_config, copy_default_mailcap
from .oauth import OAuthHelper from .oauth import OAuthHelper
@@ -111,6 +112,14 @@ def main():
warnings.warn(text) warnings.warn(text)
config['ascii'] = True config['ascii'] = True
# Check the praw version
if packages.__praw_bundled__:
_logger.info('Using packaged PRAW distribution')
_logger.info('PRAW commit hash %s' % packages.__praw_hash__)
else:
_logger.info('Packaged PRAW not found, falling back to system distribution')
_logger.info('PRAW version %s' % praw.__version__)
# Construct the reddit user agent # Construct the reddit user agent
user_agent = docs.AGENT.format(version=__version__) user_agent = docs.AGENT.format(version=__version__)

View File

@@ -1,7 +1,7 @@
""" """
This stub allows the end-user to fallback to their system installation of praw This stub allows the the package to fallback to their system installation of
if the bundled package missing. This technique was inspired by the requests praw if the bundled package is missing. This technique was inspired by the
library and how it handles dependencies. requests library and how it handles dependencies.
Reference: Reference:
https://github.com/kennethreitz/requests/blob/master/requests/packages/__init__.py https://github.com/kennethreitz/requests/blob/master/requests/packages/__init__.py
@@ -11,6 +11,7 @@ import sys
__praw_hash__ = 'a632ff005fc09e74a8d3d276adc10aa92638962c' __praw_hash__ = 'a632ff005fc09e74a8d3d276adc10aa92638962c'
__praw_bundled__ = True
try: try:
@@ -21,3 +22,4 @@ except ImportError:
msg = 'Invalid PRAW version {0}, exiting'.format(praw.__version__) msg = 'Invalid PRAW version {0}, exiting'.format(praw.__version__)
raise RuntimeError(msg) raise RuntimeError(msg)
sys.modules['%s.praw' % __name__] = praw sys.modules['%s.praw' % __name__] = praw
__praw_bundled__ = False

View File

@@ -174,7 +174,7 @@ def reddit(vcr, request):
os.remove(filename) os.remove(filename)
with vcr.use_cassette(cassette_name): with vcr.use_cassette(cassette_name):
with patch('praw.Reddit.get_access_information'): with patch('rtv.packages.praw.Reddit.get_access_information'):
reddit = praw.Reddit(user_agent='rtv test suite', reddit = praw.Reddit(user_agent='rtv test suite',
decode_html_entities=False, decode_html_entities=False,
disable_update_check=True) disable_update_check=True)

8
tests/test_packages.py Normal file
View File

@@ -0,0 +1,8 @@
from rtv import packages
def test_praw3_package():
# Sanity check that the package was installed
assert packages.praw
assert len(packages.__praw_hash__) == 40
assert packages.__praw_bundled__ is True

View File

@@ -230,9 +230,9 @@ def test_submission_vote(submission_page, refresh_token):
submission_page.oauth.authorize() submission_page.oauth.authorize()
# Test voting on the submission # Test voting on the submission
with mock.patch('praw.objects.Submission.upvote') as upvote, \ with mock.patch('rtv.packages.praw.objects.Submission.upvote') as upvote, \
mock.patch('praw.objects.Submission.downvote') as downvote, \ mock.patch('rtv.packages.praw.objects.Submission.downvote') as downvote, \
mock.patch('praw.objects.Submission.clear_vote') as clear_vote: mock.patch('rtv.packages.praw.objects.Submission.clear_vote') as clear_vote:
data = submission_page.content.get(submission_page.nav.absolute_index) data = submission_page.content.get(submission_page.nav.absolute_index)
@@ -279,8 +279,8 @@ def test_submission_save(submission_page, refresh_token):
submission_page.oauth.authorize() submission_page.oauth.authorize()
# Test save on the submission # Test save on the submission
with mock.patch('praw.objects.Submission.save') as save, \ with mock.patch('rtv.packages.praw.objects.Submission.save') as save, \
mock.patch('praw.objects.Submission.unsave') as unsave: mock.patch('rtv.packages.praw.objects.Submission.unsave') as unsave:
data = submission_page.content.get(submission_page.nav.absolute_index) data = submission_page.content.get(submission_page.nav.absolute_index)
@@ -311,8 +311,8 @@ def test_submission_comment_save(submission_page, terminal, refresh_token):
submission_page.controller.trigger('j') submission_page.controller.trigger('j')
# Test save on the coment submission # Test save on the coment submission
with mock.patch('praw.objects.Comment.save') as save, \ with mock.patch('rtv.packages.praw.objects.Comment.save') as save, \
mock.patch('praw.objects.Comment.unsave') as unsave: mock.patch('rtv.packages.praw.objects.Comment.unsave') as unsave:
data = submission_page.content.get(submission_page.nav.absolute_index) data = submission_page.content.get(submission_page.nav.absolute_index)
@@ -339,8 +339,8 @@ def test_submission_comment(submission_page, terminal, refresh_token):
submission_page.oauth.authorize() submission_page.oauth.authorize()
# Leave a comment # Leave a comment
with mock.patch('praw.objects.Submission.add_comment') as add_comment, \ with mock.patch('rtv.packages.praw.objects.Submission.add_comment') as add_comment, \
mock.patch.object(terminal, 'open_editor') as open_editor, \ mock.patch.object(terminal, 'open_editor') as open_editor, \
mock.patch('time.sleep'): mock.patch('time.sleep'):
open_editor.return_value.__enter__.return_value = 'comment text' open_editor.return_value.__enter__.return_value = 'comment text'
submission_page.controller.trigger('c') submission_page.controller.trigger('c')
@@ -371,8 +371,8 @@ def test_submission_delete(submission_page, terminal, refresh_token):
# Spoof the author and try to delete again # Spoof the author and try to delete again
data = submission_page.content.get(submission_page.nav.absolute_index) data = submission_page.content.get(submission_page.nav.absolute_index)
data['author'] = submission_page.reddit.user.name data['author'] = submission_page.reddit.user.name
with mock.patch('praw.objects.Comment.delete') as delete, \ with mock.patch('rtv.packages.praw.objects.Comment.delete') as delete, \
mock.patch.object(terminal.stdscr, 'getch') as getch, \ mock.patch.object(terminal.stdscr, 'getch') as getch, \
mock.patch('time.sleep'): mock.patch('time.sleep'):
getch.return_value = ord('y') getch.return_value = ord('y')
submission_page.controller.trigger('d') submission_page.controller.trigger('d')
@@ -395,8 +395,8 @@ def test_submission_edit(submission_page, terminal, refresh_token):
# Spoof the submission and try to edit again # Spoof the submission and try to edit again
data = submission_page.content.get(submission_page.nav.absolute_index) data = submission_page.content.get(submission_page.nav.absolute_index)
data['author'] = submission_page.reddit.user.name data['author'] = submission_page.reddit.user.name
with mock.patch('praw.objects.Submission.edit') as edit, \ with mock.patch('rtv.packages.praw.objects.Submission.edit') as edit, \
mock.patch.object(terminal, 'open_editor') as open_editor, \ mock.patch.object(terminal, 'open_editor') as open_editor, \
mock.patch('time.sleep'): mock.patch('time.sleep'):
open_editor.return_value.__enter__.return_value = 'submission text' open_editor.return_value.__enter__.return_value = 'submission text'
@@ -411,7 +411,7 @@ def test_submission_edit(submission_page, terminal, refresh_token):
# Spoof the author and edit the comment # Spoof the author and edit the comment
data = submission_page.content.get(submission_page.nav.absolute_index) data = submission_page.content.get(submission_page.nav.absolute_index)
data['author'] = submission_page.reddit.user.name data['author'] = submission_page.reddit.user.name
with mock.patch('praw.objects.Comment.edit') as edit, \ with mock.patch('rtv.packages.praw.objects.Comment.edit') as edit, \
mock.patch.object(terminal, 'open_editor') as open_editor, \ mock.patch.object(terminal, 'open_editor') as open_editor, \
mock.patch('time.sleep'): mock.patch('time.sleep'):
open_editor.return_value.__enter__.return_value = 'comment text' open_editor.return_value.__enter__.return_value = 'comment text'

View File

@@ -30,7 +30,9 @@ def test_terminal_properties(terminal, config):
assert isinstance(terminal.guilded[0], six.text_type) assert isinstance(terminal.guilded[0], six.text_type)
terminal._display = None terminal._display = None
with mock.patch.dict('os.environ', {'DISPLAY': ''}): with mock.patch('rtv.terminal.sys') as sys, \
mock.patch.dict('os.environ', {'DISPLAY': ''}):
sys.platform = 'linux'
assert terminal.display is False assert terminal.display is False
terminal._display = None terminal._display = None