Skip to content

Commit

Permalink
Switched to pytest from sure/nose to support pypy
Browse files Browse the repository at this point in the history
  • Loading branch information
coagulant committed May 7, 2013
1 parent 9db1dbb commit e9247de
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 50 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Expand Up @@ -3,11 +3,11 @@ python:
- 2.6
- 2.7
- 3.3
- pypy
install:
- python setup.py install
- pip install coveralls
script:
- python setup.py test
- coverage run --source=cleanweb setup.py -q nosetests
after_script:
- coveralls
- coverage run --source=cleanweb setup.py -q test
after_success:
- coveralls
12 changes: 11 additions & 1 deletion CHANGELOG
@@ -1,8 +1,18 @@
2.1 (07-05-2013)
================
Pypy support


2.0 (29-04-2013)
================
Fixed PyPI version


1.1 (18-02-2013)
================
Python 3 support


1.0 (17-02-2013)
================
Initial release
Initial release
29 changes: 23 additions & 6 deletions setup.py
@@ -1,25 +1,40 @@
from os import path
import codecs
from setuptools import setup
from setuptools.command.test import test as TestCommand
import sys


read = lambda filepath: codecs.open(filepath, 'r', 'utf-8').read()


class PyTest(TestCommand):
def finalize_options(self):
TestCommand.finalize_options(self)
self.test_args = []
self.test_suite = True

def run_tests(self):
#import here, cause outside the eggs aren't loaded
import pytest
errno = pytest.main(self.test_args)
sys.exit(errno)


setup(
name='cleanweb',
version='2.0',
version='2.1',
author='Ilya Baryshev',
author_email='baryshev@gmail.com',
py_modules=['cleanweb'],
url='https://github.com/coagulant/cleanweb',
license='MIT',
description="Python wrapper for cleanweb API of Yandex to fight spam.",
long_description=read(path.join(path.dirname(__file__), 'README.rst')),
install_requires=["requests>=1.0.0",],
tests_require=['nose', 'httpretty==0.5.9', 'sure'],
test_suite = "nose.collector",
classifiers = [
install_requires=["requests>=1.0.0"],
tests_require=['pytest', 'httpretty==0.5.9'],
cmdclass={'test': PyTest},
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
Expand All @@ -28,5 +43,7 @@
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: Implementation :: CPython',
'Programming Language :: Python :: Implementation :: PyPy',
],
)
)
77 changes: 39 additions & 38 deletions test.py → test_cleanweb.py
@@ -1,8 +1,10 @@
# coding: utf-8
from __future__ import unicode_literals
from unittest import TestCase
import sure

from httpretty import HTTPretty, PY3, parse_qs
import pytest

from cleanweb import Cleanweb, CleanwebError


Expand All @@ -21,23 +23,25 @@ def assertBodyQueryString(self, **kwargs):
qs = parse_qs(HTTPretty.last_request.body.decode('utf-8'))
else:
qs = dict((key, [values[0].decode('utf-8')]) for key, values in parse_qs(HTTPretty.last_request.body).items())
kwargs.should.be.equal(qs)
assert kwargs == qs


class Api(HttprettyCase):

def test_raises_exception_when_instantiated_with_no_key(self):
Cleanweb.when.called_with().should.throw(CleanwebError,
"Cleanweb needs API key to operate. Get it here: http://api.yandex.ru/cleanweb/form.xml")
with pytest.raises(CleanwebError) as excinfo:
Cleanweb()
assert excinfo.value.message == "Cleanweb needs API key to operate. Get it here: http://api.yandex.ru/cleanweb/form.xml"

def test_xml_error_is_handled(self):
error_repsonse = """
<!DOCTYPE get-captcha-result PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<error key="key-not-registered"><message>Provided API key not registered</message></error>"""
HTTPretty.register_uri(HTTPretty.GET, "http://cleanweb-api.yandex.ru/1.0/get-captcha", body=error_repsonse,
status=403)
Cleanweb(key='xxx').get_captcha.when.called_with().should.throw(CleanwebError,
'Provided API key not registered (key-not-registered)')
with pytest.raises(CleanwebError) as excinfo:
Cleanweb(key='xxx').get_captcha()
assert excinfo.value.message == 'Provided API key not registered (key-not-registered)'


class CheckSpam(HttprettyCase):
Expand Down Expand Up @@ -65,33 +69,32 @@ def setUp(self):
def test_is_not_spam(self):
HTTPretty.register_uri(HTTPretty.POST, "http://cleanweb-api.yandex.ru/1.0/check-spam",
body=self.ham_response)
Cleanweb(key='yyy').check_spam(body='Питон').should.be.equal({
assert Cleanweb(key='yyy').check_spam(body='Питон') == {
'id': '123456789abcd',
'spam_flag': False,
'links': []
})
HTTPretty.last_request.method.should.be.equal("POST")
}
assert HTTPretty.last_request.method == "POST"
self.assertBodyQueryString(**{'body-plain': ['Питон']})
HTTPretty.last_request.should.have.property('querystring').being.equal({
'key': ['yyy'],
})
assert HTTPretty.last_request.querystring == {'key': ['yyy']}

def test_is_spam(self):
HTTPretty.register_uri(HTTPretty.POST, "http://cleanweb-api.yandex.ru/1.0/check-spam",
body=self.spam_response)
spam_text = 'ШОК! Видео скачать без СМС! http://cnn.com http://yandex.ru'
Cleanweb(key='yyy').check_spam(subject=spam_text, body='123',
ip='10.178.33.2', name='Vasia', body_type='html').should.be.equal({
spam_or_ham = Cleanweb(key='yyy').check_spam(subject=spam_text, body='123',
ip='10.178.33.2', name='Vasia', body_type='html')
assert spam_or_ham == {
'id': '123456789efgh',
'spam_flag': True,
'links': [('http://cnn.com', True), ('http://yandex.ru', False)]
})
HTTPretty.last_request.method.should.be.equal("POST")
}
assert HTTPretty.last_request.method == "POST"
self.assertBodyQueryString(**{'ip': ['10.178.33.2'], 'body-html': ['123'],
'subject-plain': [spam_text], 'name': ['Vasia']})
HTTPretty.last_request.should.have.property('querystring').being.equal({
assert HTTPretty.last_request.querystring == {
'key': ['yyy'],
})
}


class GetCaptcha(HttprettyCase):
Expand All @@ -109,23 +112,21 @@ def setUp(self):
def test_can_be_obtained_without_msg_id(self):
HTTPretty.register_uri(HTTPretty.GET, "http://cleanweb-api.yandex.ru/1.0/get-captcha",
body=self.valid_response)
Cleanweb(key='xxx').get_captcha().should.be.equal(
{'captcha': 'abcd12345',
'url': 'http://i.captcha.yandex.net/image?key=abcd12345'})
HTTPretty.last_request.should.have.property("querystring").being.equal({
assert Cleanweb(key='xxx').get_captcha() == {'captcha': 'abcd12345',
'url': 'http://i.captcha.yandex.net/image?key=abcd12345'}
assert HTTPretty.last_request.querystring == {
"key": ["xxx"],
})
}

def test_can_be_obtained_with_msg_id(self):
HTTPretty.register_uri(HTTPretty.GET, "http://cleanweb-api.yandex.ru/1.0/get-captcha",
body=self.valid_response)
Cleanweb(key='xxx').get_captcha(id='somekindofmsgid').should.be.equal(
{'captcha': 'abcd12345',
'url': 'http://i.captcha.yandex.net/image?key=abcd12345'})
HTTPretty.last_request.should.have.property("querystring").being.equal({
Cleanweb(key='xxx').get_captcha(id='somekindofmsgid') == {'captcha': 'abcd12345',
'url': 'http://i.captcha.yandex.net/image?key=abcd12345'}
assert HTTPretty.last_request.querystring == {
"key": ["xxx"],
"id": ["somekindofmsgid"]
})
}


class CheckCaptcha(HttprettyCase):
Expand All @@ -149,36 +150,36 @@ def test_valid_captcha_no_msg_is_ok(self):
HTTPretty.register_uri(HTTPretty.GET,
"http://cleanweb-api.yandex.ru/1.0/check-captcha",
body=self.valid_response)
Cleanweb(key='xxx').check_captcha(captcha='abcd12345', value='48151632').should.be.true
HTTPretty.last_request.should.have.property("querystring").being.equal({
assert Cleanweb(key='xxx').check_captcha(captcha='abcd12345', value='48151632')
assert HTTPretty.last_request.querystring == {
"key": ["xxx"],
"captcha": ["abcd12345"],
"value": ["48151632"],
})
}

def test_valid_captcha_msg_is_ok(self):
HTTPretty.register_uri(HTTPretty.GET,
"http://cleanweb-api.yandex.ru/1.0/check-captcha",
body=self.valid_response)
Cleanweb(key='xxx').check_captcha(id='somekindofmsgid', captcha='abcd12345', value='48151632').should.be.true
HTTPretty.last_request.should.have.property("querystring").being.equal({
assert Cleanweb(key='xxx').check_captcha(id='somekindofmsgid', captcha='abcd12345', value='48151632')
assert HTTPretty.last_request.querystring == {
"key": ["xxx"],
"captcha": ["abcd12345"],
"value": ["48151632"],
"id": ["somekindofmsgid"]
})
}

def test_invalid_captcha(self):
HTTPretty.register_uri(HTTPretty.GET,
"http://cleanweb-api.yandex.ru/1.0/check-captcha",
body=self.invalid_response)
Cleanweb(key='xxx').check_captcha(id='somekindofmsgid', captcha='abcd12345', value='000').should.be.false
HTTPretty.last_request.should.have.property("querystring").being.equal({
assert Cleanweb(key='xxx').check_captcha(id='somekindofmsgid', captcha='abcd12345', value='000') == False
assert HTTPretty.last_request.querystring == {
"key": ["xxx"],
"captcha": ["abcd12345"],
"value": ["000"],
"id": ["somekindofmsgid"]
})
}


class Complain(HttprettyCase):
Expand All @@ -202,4 +203,4 @@ def test_spam_is_spam(self):
"http://cleanweb-api.yandex.ru/1.0/complain",
body=self.valid_response)
Cleanweb(key='zzz').complain(id='somekindofmsgid', is_spam=False)
self.assertBodyQueryString(spamtype=['ham'], id=['somekindofmsgid'])
self.assertBodyQueryString(spamtype=['ham'], id=['somekindofmsgid'])
2 changes: 1 addition & 1 deletion tox.ini
Expand Up @@ -2,5 +2,5 @@
envlist = py26, py27, py33

[testenv]
deps = nose
deps = pytest
commands = {envpython} setup.py nosetests

0 comments on commit e9247de

Please sign in to comment.