Skip to content

Commit

Permalink
Use pytest for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Feb 21, 2018
1 parent 7230947 commit a1026ad
Show file tree
Hide file tree
Showing 62 changed files with 1,760 additions and 1,841 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
.tox/
.coverage
.cache
.pytest_cache
nosetests.xml
coverage.xml
htmlcov/
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ before_install:
install:
- pip install -U setuptools pip
- pip install unittest2 mock pycurl flake8 tox-travis coveralls
- pip install pycurl flake8 coveralls
- python setup.py clean --all
- python setup.py install

script:
- if [[ $TRAVIS_PYTHON_VERSION == '2.7' ]]; then flake8 stripe tests; fi
- python -W all -bb -W error::BytesWarning -m coverage.__main__ run setup.py test
- python -W all -bb -W error::BytesWarning setup.py test --addopts "--cov=stripe"

after_success:
coveralls
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,17 @@ instructions for installing via Homebrew and other methods):

tox -e py27

Run all tests in a single file:

tox -e py27 -- tests/api_resources/abstract/test_updateable_api_resource.py

Run a single test suite:

tox -e py27 -- --test-suite tests.api_resources.abstract.test_updateable_api_resource.UpdateableAPIResourceTests
tox -e py27 -- tests/api_resources/abstract/test_updateable_api_resource.py::TestUpdateableAPIResource

Run a single test:

tox -e py27 -- --test-suite tests.api_resources.abstract.test_updateable_api_resource.UpdateableAPIResourceTests.test_save
tox -e py27 -- tests/api_resources/abstract/test_updateable_api_resource.py::TestUpdateableAPIResource::test_save

Run the linter with:

Expand Down
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[aliases]
test=pytest
8 changes: 6 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@
'stripe.api_resources.abstract'],
package_data={'stripe': ['data/ca-certificates.crt']},
install_requires=install_requires,
test_suite='tests',
tests_require=['unittest2', 'mock'],
setup_requires=['pytest-runner'],
tests_require=[
'pytest >= 3.4',
'pytest-mock >= 1.7',
'pytest-cov >= 2.5',
],
classifiers=[
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
Expand Down
4 changes: 4 additions & 0 deletions stripe/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ def __init__(self, timeout=80, session=None, **kwargs):
self._timeout = timeout
self._session = session or requests.Session()

def __del__(self):
if self._session:
self._session.close()

def request(self, method, url, headers, post_data=None):
kwargs = {}
if self._verify_ssl_certs:
Expand Down
62 changes: 30 additions & 32 deletions tests/api_resources/abstract/test_api_resource.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
from __future__ import absolute_import, division, print_function

import stripe
from tests.helper import StripeTestCase

import pytest

class MyResource(stripe.api_resources.abstract.APIResource):
pass
import stripe


class APIResourceTests(StripeTestCase):
class TestAPIResource(object):
class MyResource(stripe.api_resources.abstract.APIResource):
pass

def test_retrieve_and_refresh(self):
def test_retrieve_and_refresh(self, request_mock):
url = '/v1/myresources/foo%2A'
self.stub_request(
request_mock.stub_request(
'get',
url,
{
Expand All @@ -22,25 +21,25 @@ def test_retrieve_and_refresh(self):
rheaders={'request-id': 'req_id'}
)

res = MyResource.retrieve('foo*', myparam=5)
res = self.MyResource.retrieve('foo*', myparam=5)

self.assert_requested(
request_mock.assert_requested(
'get',
url,
{
'myparam': 5,
},
None
)
self.assertEqual('scrobble', res.bobble)
self.assertEqual('foo2', res.id)
self.assertEqual('sk_test_123', res.api_key)
assert res.bobble == 'scrobble'
assert res.id == 'foo2'
assert res.api_key == 'sk_test_123'

self.assertTrue(res.last_response is not None)
self.assertEqual('req_id', res.last_response.request_id)
assert res.last_response is not None
assert res.last_response.request_id == 'req_id'

url = '/v1/myresources/foo2'
self.stub_request(
request_mock.stub_request(
'get',
url,
{
Expand All @@ -50,16 +49,17 @@ def test_retrieve_and_refresh(self):

res = res.refresh()

self.assert_requested(
request_mock.assert_requested(
'get',
url,
{
'myparam': 5,
},
None
)
self.assertEqual(5, res.frobble)
self.assertRaises(KeyError, res.__getitem__, 'bobble')
assert res.frobble == 5
with pytest.raises(KeyError):
res['bobble']

def test_convert_to_stripe_object(self):
sample = {
Expand All @@ -81,28 +81,26 @@ def test_convert_to_stripe_object(self):
sample, 'akey', None, None)

# Types
self.assertTrue(isinstance(converted,
stripe.stripe_object.StripeObject))
self.assertTrue(isinstance(converted.adict, stripe.Charge))
self.assertEqual(1, len(converted.alist))
self.assertTrue(isinstance(converted.alist[0], stripe.Customer))
assert isinstance(converted, stripe.stripe_object.StripeObject)
assert isinstance(converted.adict, stripe.Charge)
assert len(converted.alist) == 1
assert isinstance(converted.alist[0], stripe.Customer)

# Values
self.assertEqual('bar', converted.foo)
self.assertEqual(42, converted.adict.id)
self.assertEqual('chilango', converted.alist[0].name)
assert converted.foo == 'bar'
assert converted.adict.id == 42
assert converted.alist[0].name == 'chilango'

# Stripping
# TODO: We should probably be stripping out this property
# self.assertRaises(AttributeError, getattr, converted.adict, 'object')

def test_convert_array_to_dict(self):
out = stripe.util.convert_array_to_dict([{"foo": "bar"}])
self.assertEqual({"0": {"foo": "bar"}}, out)
self.assertEqual({"f": "b"},
stripe.util.convert_array_to_dict({"f": "b"}))
assert out == {"0": {"foo": "bar"}}
assert stripe.util.convert_array_to_dict({"f": "b"}) == {"f": "b"}

def test_raise_on_incorrect_id_type(self):
for obj in [None, 1, 3.14, dict(), list(), set(), tuple(), object()]:
self.assertRaises(stripe.error.InvalidRequestError,
MyResource.retrieve, obj)
with pytest.raises(stripe.error.InvalidRequestError):
self.MyResource.retrieve(obj)
40 changes: 17 additions & 23 deletions tests/api_resources/abstract/test_createable_api_resource.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
from __future__ import absolute_import, division, print_function

import stripe
from tests.helper import StripeTestCase


class MyCreatable(stripe.api_resources.abstract.CreateableAPIResource):
pass
class TestCreateableAPIResource(object):
class MyCreatable(stripe.api_resources.abstract.CreateableAPIResource):
pass


class CreateableAPIResourceTests(StripeTestCase):

def test_create(self):
self.stub_request(
def test_create(self, request_mock):
request_mock.stub_request(
'post',
'/v1/mycreatables',
{
Expand All @@ -21,21 +18,21 @@ def test_create(self):
rheaders={'request-id': 'req_id'}
)

res = MyCreatable.create()
res = self.MyCreatable.create()

self.assert_requested(
request_mock.assert_requested(
'post',
'/v1/mycreatables',
{}
)
self.assertTrue(isinstance(res, stripe.Charge))
self.assertEqual('bar', res.foo)
assert isinstance(res, stripe.Charge)
assert res.foo == 'bar'

self.assertTrue(res.last_response is not None)
self.assertEqual('req_id', res.last_response.request_id)
assert res.last_response is not None
assert res.last_response.request_id == 'req_id'

def test_idempotent_create(self):
self.stub_request(
def test_idempotent_create(self, request_mock):
request_mock.stub_request(
'post',
'/v1/mycreatables',
{
Expand All @@ -45,16 +42,13 @@ def test_idempotent_create(self):
rheaders={'idempotency-key': 'foo'}
)

res = MyCreatable.create(idempotency_key='foo')
res = self.MyCreatable.create(idempotency_key='foo')

self.assert_requested(
request_mock.assert_requested(
'post',
'/v1/mycreatables',
{},
{'Idempotency-Key': 'foo'}
)
self.assertTrue(isinstance(res, stripe.Charge))
self.assertEqual('bar', res.foo)

self.assertTrue(res.last_response is not None)
self.assertEqual('foo', res.last_response.idempotency_key)
assert isinstance(res, stripe.Charge)
assert res.foo == 'bar'
26 changes: 12 additions & 14 deletions tests/api_resources/abstract/test_deletable_api_resource.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
from __future__ import absolute_import, division, print_function

import stripe
from tests.helper import StripeTestCase


class MyDeletable(stripe.api_resources.abstract.DeletableAPIResource):
pass
class TestDeletableAPIResource(object):
class MyDeletable(stripe.api_resources.abstract.DeletableAPIResource):
pass


class DeletableAPIResourceTests(StripeTestCase):
def test_delete(self):
self.stub_request(
def test_delete(self, request_mock):
request_mock.stub_request(
'delete',
'/v1/mydeletables/mid',
{
Expand All @@ -20,18 +18,18 @@ def test_delete(self):
rheaders={'request-id': 'req_id'}
)

obj = MyDeletable.construct_from({
obj = self.MyDeletable.construct_from({
'id': 'mid'
}, 'mykey')

self.assertTrue(obj is obj.delete())
self.assert_requested(
assert obj is obj.delete()
request_mock.assert_requested(
'delete',
'/v1/mydeletables/mid',
{}
)
self.assertEqual(True, obj.deleted)
self.assertEqual('mid', obj.id)
assert obj.deleted is True
assert obj.id == 'mid'

self.assertTrue(obj.last_response is not None)
self.assertEqual('req_id', obj.last_response.request_id)
assert obj.last_response is not None
assert obj.last_response.request_id == 'req_id'
30 changes: 13 additions & 17 deletions tests/api_resources/abstract/test_listable_api_resource.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
from __future__ import absolute_import, division, print_function

import stripe
from tests.helper import StripeTestCase


class MyListable(stripe.api_resources.abstract.ListableAPIResource):
pass
class TestListableAPIResource(object):
class MyListable(stripe.api_resources.abstract.ListableAPIResource):
pass


class ListableAPIResourceTests(StripeTestCase):

def test_all(self):
self.stub_request(
def test_all(self, request_mock):
request_mock.stub_request(
'get',
'/v1/mylistables',
{
Expand All @@ -32,17 +29,16 @@ def test_all(self):
rheaders={'request-id': 'req_id'}
)

res = MyListable.list()
self.assert_requested(
res = self.MyListable.list()
request_mock.assert_requested(
'get',
'/v1/mylistables',
{}
)
self.assertEqual(2, len(res.data))
self.assertTrue(all(isinstance(obj, stripe.Charge)
for obj in res.data))
self.assertEqual('jose', res.data[0].name)
self.assertEqual('curly', res.data[1].name)
assert len(res.data) == 2
assert all(isinstance(obj, stripe.Charge) for obj in res.data)
assert res.data[0].name == 'jose'
assert res.data[1].name == 'curly'

self.assertTrue(res.last_response is not None)
self.assertEqual('req_id', res.last_response.request_id)
assert res.last_response is not None
assert res.last_response.request_id == 'req_id'
Loading

0 comments on commit a1026ad

Please sign in to comment.