Skip to content

Commit

Permalink
Mock calls to _request without actually making them. #4
Browse files Browse the repository at this point in the history
  • Loading branch information
thejunglejane committed Nov 4, 2015
1 parent 9023b62 commit 8b22c00
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 26 deletions.
21 changes: 11 additions & 10 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ def readme():
return f.read()

setup(
name = 'pynigma',
packages = ['pynigma'],
version = __version__,
install_requires = ['requests'],
description = 'A Python client for the Enigma API.',
author = 'Jane Stewart Adams',
author_email = '[email protected]',
license = 'MIT',
url = 'https://github.com/thejunglejane/pynigma',
download_url = 'https://github.com/thejunglejane/pynigma/tarball/0.0.2'
name='pynigma',
packages=['pynigma', 'tests'],
version=__version__,
install_requires=['requests', 'mock'],
description='A Python client for the Enigma API.',
author='Jane Stewart Adams',
author_email='[email protected]',
license='MIT',
url='https://github.com/thejunglejane/pynigma',
download_url='https://github.com/thejunglejane/pynigma/tarball/{v}'.format(
v=__version__)
)
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__all__ = ['test_client']
60 changes: 44 additions & 16 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pynigma import client
import decimal
import mock
import random
import string
import unittest
Expand Down Expand Up @@ -73,41 +74,52 @@ def test_request_invalid_params(self):
resource='stats', datapath='us.gov.whitehouse.salaries.2011', **{'invalid': ''})
self.assertIsNone(self.new_client.request_url)

def test_request_success(self):
'''Does _request() return decoded JSON when the request is valid?
'''
self.assertIsInstance(self.new_client.get_data(
datapath='us.gov.whitehouse.salaries.2011'), dict)

def test_request_success_keys(self):
'''Does _request() return decoded JSON with the expected keys when the
request is valid?
'''
self.assertEquals(self.new_client.get_data(
datapath='us.gov.whitehouse.salaries.2011').keys(), ['info', 'datapath', 'success', 'result'])

def test_get_limits_datapath_failure(self):
'''Does get_limits() raise a TypeError when a datapath is passed?
'''
with self.assertRaises(TypeError):
self.new_client.get_limits(
datapath='us.gov.whitehouse.salaries.2011')

@mock.patch.object(client.EnigmaAPI, '_request', autospec=True)
def test_get_limits(self, mock_method):
'''Does get_limits() call _request with resource='limits'?
'''
self.new_client.get_limits()
mock_method.assert_called_with(
self.new_client, datapath=None, resource='limits')

def test_get_data_no_datapath_failure(self):
'''Does get_data() raise a TypeError when no datapath is passed?
'''
with self.assertRaises(TypeError):
self.new_client.get_data()

@mock.patch.object(client.EnigmaAPI, '_request', autospec=True)
def test_get_data(self, mock_method):
'''Does get_data() call _request with resource='data'?
'''
self.new_client.get_data(datapath='us.gov.whitehouse.salaries.2011')
mock_method.assert_called_with(
self.new_client, datapath='us.gov.whitehouse.salaries.2011', resource='data')

def test_get_metadata_no_datapath_failure(self):
'''Does get_metadata() raise a TypeError when no datapath is passed?
'''
with self.assertRaises(TypeError):
self.new_client.get_metadata()

def test_get_metadata_python_data_type(self):
'''Does get_metadata() return a dictionary key corresponding to the
Python data type of a column?
@mock.patch.object(client.EnigmaAPI, '_request', autospec=True)
def test_get_metadata(self, mock_method):
'''Does get_metadata() call _request with resource='metadata'?
'''
self.new_client.get_metadata(datapath='us.gov.whitehouse.salaries.2011')
mock_method.assert_called_with(
self.new_client, datapath='us.gov.whitehouse.salaries.2011', resource='meta')

def test_map_metadata_data_type_python_data_type(self):
'''Does _map_metadata_data_type() return a dictionary key corresponding
to the Python data type of a column?
'''
metadata = self.new_client.get_metadata(
datapath='us.gov.whitehouse.visitor-list')
Expand All @@ -129,8 +141,24 @@ def test_get_stats_no_datapath_failure(self):
with self.assertRaises(TypeError):
self.new_client.get_stats()

@mock.patch.object(client.EnigmaAPI, '_request', autospec=True)
def test_get_stats(self, mock_method):
'''Does get_stats() call _request with resource='stats'?
'''
self.new_client.get_stats(datapath='us.gov.whitehouse.salaries.2011')
mock_method.assert_called_with(
self.new_client, datapath='us.gov.whitehouse.salaries.2011', resource='stats')

def test_get_export_no_datapath_failure(self):
'''Does get_export() raise a TypeError when no datapath is passed?
'''
with self.assertRaises(TypeError):
self.new_client.get_export()

@mock.patch.object(client.EnigmaAPI, '_request', autospec=True)
def test_get_export(self, mock_method):
'''Does get_export() call _request with resource='export'?
'''
self.new_client.get_export(datapath='us.gov.whitehouse.salaries.2011')
mock_method.assert_called_with(
self.new_client, datapath='us.gov.whitehouse.salaries.2011', resource='export')

0 comments on commit 8b22c00

Please sign in to comment.