Skip to content

Commit

Permalink
Configuration of tests, test data loading, setup
Browse files Browse the repository at this point in the history
  • Loading branch information
r-sierra committed Feb 19, 2018
1 parent 45ac8ab commit 6f179bc
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 65 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ nosetests.xml
htmlcov
coverage.xml

# Pytest cache
.pytest_cache

# PyCharm data
.idea

Expand Down
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include README.rst
include docs/*.txt
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Tienda Mobil Python API
-----------------------

To use simply do::

>>> import tienda_mobil
>>> api = tienda_mobil.Api('http://www.lvh.me', 'API-KEY')
14 changes: 14 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[pytest]
norecursedirs=
venv
*/python?.?/*
*/site-packages/*
.tox/*

with-coverage = true
cover-package = twitter
cover-html = true
cover-html-dir = htmlcov
cover-erase = true
cover-inclusive = true
cover-branches = true
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
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def extract_metaitem(meta):
version=extract_metaitem('version'),
license=extract_metaitem('license'),
description=extract_metaitem('description'),
long_description=read('README.rst'),
author=extract_metaitem('author'),
author_email=extract_metaitem('email'),
maintainer=extract_metaitem('author'),
Expand All @@ -57,7 +58,7 @@ def extract_metaitem(meta):
tests_require=['pytest'],
keywords='tienda_mobil api',
classifiers=[
'Development Status :: 5 - Production/Stable',
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'License :: OSI Approved :: Apache Software License',
'Operating System :: OS Independent',
Expand All @@ -66,5 +67,6 @@ def extract_metaitem(meta):
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
],
zip_safe=False
zip_safe=False,
include_package_data=True
)
5 changes: 3 additions & 2 deletions tienda_mobil/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import requests
from error import TiendaMobilError
from models import (
Expand Down Expand Up @@ -236,8 +237,8 @@ def _ParseAndCheck(self, response):
network outages it will return an HTML fail page.
Args:
json_data (str):
A python str created from the response content
response (requests.models <Response> class):
A response object created from the response content
Raises:
(tiendaMobil.TiendaMobilError): TiendaMobilError wrapping the error
Expand Down
2 changes: 1 addition & 1 deletion tienda_mobil/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def __init__(self, **kwargs):
def __repr__(self):
return "OrderPreview(ID={i}, Customer='{c}', TotalAmount='{a}')".format(
i=self.id,
c=self.customer['code'],
c=self.customer.code,
a=self.totalAmount)

@property
Expand Down
Empty file added tienda_mobil/tests/__init__.py
Empty file.
49 changes: 49 additions & 0 deletions tienda_mobil/tests/data/pending_orders.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"data": [
{
"id": "20488",
"type": "orders",
"attributes": {
"price-list": "R02-2018",
"customer": {
"code": "24899348",
"name": "Schmidt Analia Angelica"
},
"comment": "Una caja club",
"total-amount": "373.2",
"total-quantity": 12,
"businessman": "13008624"
}
},
{
"id": "20492",
"type": "orders",
"attributes": {
"price-list": "R02-2018",
"customer": {
"code": "31202377",
"name": "Gonzalez Claudia Noemi"
},
"comment": "",
"total-amount": "614.9",
"total-quantity": 20,
"businessman": "14076370"
}
},
{
"id": "20493",
"type": "orders",
"attributes": {
"price-list": "R02-2018",
"customer": {
"code": "05003446",
"name": "Rios Mirta Amalia"
},
"comment": "",
"total-amount": "579.0",
"total-quantity": 7,
"businessman": "14076370"
}
}
]
}
73 changes: 13 additions & 60 deletions tienda_mobil/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@
import unittest
import responses
import tienda_mobil
from tienda_mobil import TiendaMobilError

DEFAULT_URL = re.compile(r'https?://tiendamobil.com.ar/api/.*')
DEFAULT_URL = re.compile(r'https?://tiendamobil\.com\.ar/.*')

def readJSONFile(fname):
import os
import json
cwd = os.path.abspath(os.path.dirname(__file__))
with open(os.path.join(cwd, 'data', fname)) as f:
data = json.loads(f.read())
return data

class ApiTest(unittest.TestCase):

def setUp(self):
self.base_url = 'https://tiendamobil.com.ar/api'
self.base_url = 'https://tiendamobil.com.ar'
self.api = tienda_mobil.Api(base_url=self.base_url, api_key='test')

def testSetCredentials(self):
Expand All @@ -19,65 +25,12 @@ def testSetCredentials(self):
api = tienda_mobil.Api(base_url=self.base_url, api_key=test_key)
self.assertEqual(api._request_headers['authorization'], auth_header)

@responses.activate
def testBadGateway(self):
# Server Error
responses.add(GET, DEFAULT_URL, status=502)
with self.assertRaisesRegexp(TiendaMobilError, 'Bad Gateway'):
resp = self.api.GetPendingOrders()

# try:
# resp = self.api.GetPendingOrders()
# except tienda_mobil.TiendaMobilError as e:
# print e.message
# self.assertEqual(e.message, "Bad Gateway")

@responses.activate
def testUnauthorized(self):
# Application Error
responses.add(GET, DEFAULT_URL, status=401)
with self.assertRaisesRegexp(TiendaMobilError, 'Not authorized'):
resp = self.api.GetPendingOrders()

# try:
# resp = self.api.GetPendingOrders()
# except tienda_mobil.TiendaMobilError as e:
# print e.message
# self.assertEqual(e.message, "Not authorized.")

@responses.activate
def testBadRequest(self):
# Application Error
responses.add(GET, DEFAULT_URL, status=400)
with self.assertRaisesRegexp(TiendaMobilError, 'Bad Request'):
resp = self.api.GetPendingOrders()

# try:
# resp = self.api.GetPendingOrders()
# except tienda_mobil.TiendaMobilError as e:
# print e.message
# self.assertEqual(e.message, "Bad Request")

@responses.activate
def testUnprocessableEntity(self):
# Application Error
responses.add(GET, DEFAULT_URL, status=422)
with self.assertRaisesRegexp(TiendaMobilError, 'Unprocessable Entity'):
resp = self.api.GetPendingOrders()

# try:
# resp = self.api.GetPendingOrders()
# except tienda_mobil.TiendaMobilError as e:
# print e.message
# self.assertEqual(e.message, "Unprocessable Entity")

@responses.activate
def testGetPendingOrders(self):
with open('testdata/get_trends_current.json') as f:
resp_data = f.read()

url = '{0}/orders'.format(self.base_url)
responses.add(responses.GET, url, json=resp_data, status=200)
json_data = readJSONFile('pending_orders.json')
responses.add(responses.GET, DEFAULT_URL, json=json_data, status=200)

resp = self.api.GetPendingOrders()
self.assertEqual(3, len(resp))
self.assertTrue(type(resp[0]) is tienda_mobil.OrderPreview)
self.assertTrue(type(resp[0].customer) is tienda_mobil.Customer)

0 comments on commit 6f179bc

Please sign in to comment.