Skip to content

Commit fddf9b9

Browse files
committed
Add tests
1 parent 7d2bb43 commit fddf9b9

File tree

10 files changed

+167
-63
lines changed

10 files changed

+167
-63
lines changed

.travis.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ os:
44
- linux
55
python:
66
- '2.7'
7-
- '3.3'
8-
- '3.4'
97
- '3.5'
108
- '3.6'
11-
- pypy
129
install:
1310
- pip install -U pip
1411
- pip install -U setuptools

recho/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
from pbr.version import VersionInfo
1+
from pkg_resources import get_distribution, DistributionNotFound
22

3-
__version__ = VersionInfo('recho').semantic_version().release_string()
3+
try:
4+
__version__ = get_distribution(__name__).version
5+
except DistributionNotFound: # pragma: no cover
6+
__version__ = '0.0.0'

recho/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def main():
101101
timestamps[args.redditor] = latest_timestamp.timestamp()
102102
with open(args.timestamp_file, 'w') as w: # pylint: disable=C0103
103103
w.write(json.dumps(timestamps, indent=4, sort_keys=True))
104-
except:
104+
except Exception:
105105
# Log to sentry, if configured
106106
if 'sentry' in config:
107107
key, secret = config['sentry']['key'], config['sentry']['secret']

recho/recho.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
from datetime import datetime as dt
33

44
import praw
5-
from retry import retry
65
from slacker import Slacker
7-
from requests.exceptions import ConnectionError # pylint: disable=redefined-builtin
86
from praw.models.reddit.comment import Comment
97
from praw.models.reddit.submission import Submission
108

11-
from recho import __version__ as recho_version
12-
from recho.reddit import RedditComment, RedditSubmission
9+
from . import __version__ as recho_version
10+
from .reddit import RedditComment, RedditSubmission
1311

1412
TIME_PERIOD = 'week'
1513

@@ -18,16 +16,6 @@ class RechoError(Exception):
1816
pass
1917

2018

21-
@retry(ConnectionError, tries=3, delay=1, backoff=2)
22-
def _get_comments(redditor):
23-
return redditor.get_comments(time=TIME_PERIOD)
24-
25-
26-
@retry(ConnectionError, tries=3, delay=1, backoff=2)
27-
def _get_submitted(redditor):
28-
return redditor.get_submitted(time=TIME_PERIOD)
29-
30-
3119
def build_user_agent():
3220
recho_version_trunk = recho_version.split("+")[0]
3321
user_agent = '{0}:recho:{1} (by /u/IHKAS1984)'

requirements.txt

Lines changed: 0 additions & 6 deletions
This file was deleted.

setup.cfg

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,3 @@
1-
[metadata]
2-
name = recho
3-
author = Levi Noecker
4-
author-email = [email protected]
5-
summary = Post the Reddit users's latest comment(s) to a specific Slack channel
6-
description-file = README.rst
7-
home-page = https://github.com/DankCity/recho
8-
license = MIT
9-
keywords = reddit slack bot slackbot recho chat chatbot
10-
classifier =
11-
Development Status :: 4 - Beta
12-
Intended Audience :: Developers
13-
Natural Language :: English
14-
License :: OSI Approved :: MIT License
15-
Operating System :: OS Independent
16-
Topic :: Communications :: Chat
17-
Programming Language :: Python :: 2
18-
Programming Language :: Python :: 2.7
19-
Programming Language :: Python :: 3
20-
Programming Language :: Python :: 3.5
21-
Programming Language :: Python :: 3.6
22-
Programming Language :: Python :: Implementation :: CPython
23-
24-
[files]
25-
packages =
26-
recho
27-
28-
[entry_points]
29-
console_scripts =
30-
recho = recho.cli:main
31-
321
[tool:pytest]
332
testpaths = tests
343

setup.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,49 @@
11
"""
22
Use setup tools to setup recho as a standard python package
33
"""
4-
from setuptools import setup
4+
import os
5+
from setuptools import find_packages, setup
6+
7+
# Get the long description from the README file
8+
HERE = os.path.dirname(os.path.abspath(__file__))
9+
with open(os.path.join(HERE, 'README.rst')) as f:
10+
LONG_DESCRIPTION = f.read()
11+
512

613
setup(
7-
setup_requires=['pbr>=1.9', 'setuptools>=17.1'],
8-
pbr=True,
14+
name="recho",
15+
author='Levi Noecker',
16+
author_email='[email protected]',
17+
url='https://github.com/levi-rs/recho',
18+
description=LONG_DESCRIPTION,
19+
licence='MIT',
20+
packages=find_packages(),
21+
use_scm_version={'root': '.', 'relative_to': __file__},
22+
setup_requires=['setuptools_scm'],
23+
install_requires=[
24+
'configparser==3.5.0',
25+
'praw>=5.3.0',
26+
'raven',
27+
'slacker==0.9.30',
28+
],
29+
keywords='reddit slack bot slackbot recho chat chatbot',
30+
classifiers=[
31+
'Development Status :: 4 - Beta',
32+
'Intended Audience :: Developers',
33+
'Natural Language :: English',
34+
'License :: OSI Approved :: MIT License',
35+
'Operating System :: OS Independent',
36+
'Topic :: Communications :: Chat',
37+
'Programming Language :: Python :: 2',
38+
'Programming Language :: Python :: 2.7',
39+
'Programming Language :: Python :: 3',
40+
'Programming Language :: Python :: 3.5',
41+
'Programming Language :: Python :: 3.6',
42+
'Programming Language :: Python :: Implementation :: CPython',
43+
],
44+
entry_points={
45+
'console_scripts': [
46+
'recho=recho.cli:main',
47+
]
48+
},
949
)

tests/__init__.py

Whitespace-only changes.

tests/test_recho.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
from datetime import datetime as dt, timedelta
2+
from mock import patch, create_autospec, MagicMock
3+
import time
4+
5+
from praw.models.reddit.comment import Comment
6+
from praw.models.reddit.submission import Submission
7+
import pytest
8+
9+
from recho import recho
10+
11+
CREDS = {'client_id': 'mock id', 'client_secret': 'mock secret'}
12+
SLACK = {'token': 'mock token', 'channel': 'mock channel'}
13+
TS = dt.utcnow() - timedelta(days=1)
14+
AUTHOR = 'mock author'
15+
TITLE = 'Mock Title ' + '0123456789' * 7
16+
LINK = "http://mock.link"
17+
TEXT = "mock body text"
18+
19+
20+
def make_ts(ts):
21+
return int((time.mktime(ts.timetuple())+ts.microsecond/1e6))
22+
23+
24+
@pytest.fixture()
25+
def com():
26+
comm = create_autospec(Comment)
27+
comm.created_utc = make_ts(TS + timedelta(days=1))
28+
comm.author = AUTHOR
29+
comm.title = TITLE
30+
comm.permalink = LINK
31+
comm.text = TEXT
32+
yield comm
33+
34+
35+
@pytest.fixture()
36+
def old_com():
37+
comm = create_autospec(Comment)
38+
comm.created_utc = make_ts(TS - timedelta(days=1))
39+
comm.author = AUTHOR
40+
comm.title = TITLE
41+
comm.permalink = LINK
42+
comm.text = TEXT
43+
yield comm
44+
45+
46+
@pytest.fixture()
47+
def sub():
48+
subm = create_autospec(Submission)
49+
subm.created_utc = make_ts(TS + timedelta(days=1))
50+
subm.author = AUTHOR
51+
subm.title = TITLE
52+
subm.permalink = LINK
53+
subm.text = TEXT
54+
yield subm
55+
56+
57+
@pytest.fixture()
58+
def old_sub():
59+
subm = create_autospec(Submission)
60+
subm.created_utc = make_ts(TS - timedelta(days=1))
61+
subm.author = AUTHOR
62+
subm.title = TITLE
63+
subm.permalink = LINK
64+
subm.text = TEXT
65+
yield subm
66+
67+
68+
@pytest.fixture()
69+
def new(com, old_com, sub, old_sub):
70+
new = sorted([com, old_com, sub, old_sub], key=lambda x: x.created_utc)
71+
return reversed(new)
72+
73+
74+
def test_build_user_agent():
75+
""" Validate user agent is built """
76+
assert "recho:" in recho.build_user_agent()
77+
78+
79+
@patch('recho.recho.praw')
80+
def test_get_posts_since(praw_mock, new):
81+
""" Validate can filter down to recent posts """
82+
praw_mock.Reddit.return_value = praw_mock
83+
praw_mock.redditor.return_value = praw_mock
84+
praw_mock.new.return_value = new
85+
posts = recho.get_posts_since(CREDS, 'mock name', TS)
86+
87+
assert len(posts) == 2
88+
89+
90+
@patch('recho.recho.praw')
91+
def test_get_posts_since_exc(praw_mock, new):
92+
""" Validate an exception is raised if unkown type is found """
93+
praw_mock.Reddit.return_value = praw_mock
94+
praw_mock.redditor.return_value = praw_mock
95+
unknown = MagicMock()
96+
unknown.created_utc = make_ts(TS + timedelta(days=1))
97+
praw_mock.new.return_value = [unknown, ]
98+
99+
with pytest.raises(recho.RechoError) as exc:
100+
recho.get_posts_since(CREDS, 'mock name', TS)
101+
102+
assert "Unknown comment/submission" in str(exc.value)
103+
104+
105+
@patch('recho.recho.Slacker')
106+
def test_post_to_slack(slack_mock, new):
107+
""" """
108+
slack_mock.return_value = slack_mock
109+
110+
recho.post_to_slack(SLACK, new)
111+
112+
assert slack_mock.chat.post_message.call_count == 4

tox.ini

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
[tox]
2-
skipdist = True
32
envlist = py{27,35,36},lint
43

54
[testenv]
65
commands =
7-
py.test -s --cov recho --cov-report term-missing --cov-report html --cov-report xml --junitxml={envdir}/junit.xml tests []
6+
py.test -s --cov recho --cov-report term-missing tests []
87
deps =
8+
mock
99
pytest>=3.0
1010
pytest-cov>=1.8.1
1111

1212
[testenv:lint]
13+
skipsdist = True
1314
deps =
14-
flake8==2.4.0
15+
flake8
1516
commands =
1617
flake8 setup.py recho tests
1718

0 commit comments

Comments
 (0)