Permalink
Browse files

Add tests for resources/base.py

Increases test coverage to 82%
  • Loading branch information...
1 parent ef7a151 commit dcfd60f61e1197e2d8ccf249b528b5a91b9809f2 @tgsergeant tgsergeant committed Feb 10, 2014
Showing with 188 additions and 11 deletions.
  1. +12 −5 bigcommerce/resources/base.py
  2. +6 −6 tests/test_api.py
  3. +170 −0 tests/test_base.py
@@ -1,27 +1,34 @@
-# from bigcommerce import connection
-# from bigcommerce.exception import NotLoggedInException
-
-
class Mapping(dict):
"""
Mapping
provides '.' access to dictionary keys
"""
def __init__(self, mapping, *args, **kwargs):
+ """
+ Create a new mapping. Filters the mapping argument
+ to remove any elements that are already methods on the
+ object.
+
+ For example, Orders retains its `coupons` method, instead
+ of being replaced by the dict describing the coupons endpoint
+ """
filter_args = {k: mapping[k] for k in mapping if k not in dir(self)}
self.__dict__ = self
dict.__init__(self, filter_args, *args, **kwargs)
def __str__(self):
+ """
+ Display as a normal dict, but filter out underscored items first
+ """
return str({k: self.__dict__[k] for k in self.__dict__ if not k.startswith("_")})
def __repr__(self):
return "<%s at %s, %s>" % (type(self).__name__, hex(id(self)), str(self))
class ApiResource(Mapping):
- resource_name = ""
+ resource_name = "" # The identifier which describes this resource in urls
@classmethod
def _create_object(cls, response, connection=None):
View
@@ -40,17 +40,17 @@ def test_str_to_class(self):
self.assertRaises(AttributeError, lambda: bigcommerce.api.ApiResourceWrapper.str_to_class('ApiResourceWhichDoesNotExist'))
- def test_get_attr(self):
+ @patch.object(ApiResource, 'get')
+ def test_get_attr(self, patcher):
api = MagicMock()
api.connection = MagicMock()
- ApiResource.get = MagicMock()
- mock_return = Mock()
- ApiResource.get.return_value = mock_return
+ result = {'id': 1}
+ patcher.return_value = result
wrapper = bigcommerce.api.ApiResourceWrapper('ApiResource', api)
- self.assertEqual(wrapper.get(1), mock_return)
- ApiResource.get.assert_called_once_with(1, connection=api.connection)
+ self.assertEqual(wrapper.get(1), result)
+ patcher.assert_called_once_with(1, connection=api.connection)
View
@@ -0,0 +1,170 @@
+import unittest
+from bigcommerce.resources import Mapping, Orders, ApiResource, OrderShipments
+from bigcommerce.resources.orders import OrderCoupons
+from mock import MagicMock
+
+
+class TestMapping(unittest.TestCase):
+ def test_init(self):
+ result = {
+ 'coupons': {'url': 'blah'},
+ 'id': 1
+ }
+
+ map = Orders(result)
+ self.assertEqual(map.id, 1)
+ self.assertEqual(map['id'], 1)
+
+ self.assertNotIsInstance(map.coupons, dict)
+
+ def test_str(self):
+ map = Mapping({'id': 1, '_connection': MagicMock()})
+ self.assertEqual(str(map), str({'id': 1}))
+
+
+class TestApiResource(unittest.TestCase):
+ def test_create_object(self):
+ # Test with a single object
+ result = {'id': 1}
+ object = ApiResource._create_object(result, MagicMock())
+ self.assertEqual(object.id, 1)
+
+ # Test with a list
+ results = [{'id': 1}, {'id': 2}, {'id': 3}]
+ objects = ApiResource._create_object(results, MagicMock)
+ self.assertIsInstance(objects, list)
+ for object in objects:
+ self.assertIsNotNone(object.id)
+ self.assertIsNotNone(object._connection)
+
+ def test_get(self):
+ connection = MagicMock()
+ connection.make_request.return_value = {'id': 1}
+
+ result = Orders.get(1, connection)
+ self.assertIsInstance(result, Orders)
+ self.assertEqual(result.id, 1)
+
+ connection.make_request.assert_called_once_with('GET', 'orders/1', None, {}, {})
+
+
+class TestApiSubResource(unittest.TestCase):
+ def test_get(self):
+ connection = MagicMock()
+ connection.make_request.return_value = {'id': 2}
+
+ result = OrderCoupons.get(1, 2, connection)
+ self.assertIsInstance(result, OrderCoupons)
+ self.assertEqual(result.id, 2)
+
+ connection.make_request.assert_called_once_with('GET', 'orders/1/coupons/2', None, {}, {})
+
+ def test_parent_id(self):
+ coupon = OrderCoupons({'id': 2, 'order_id': 1})
+ self.assertEqual(coupon.parent_id(), 1)
+
+
+class TestCreateableApiResource(unittest.TestCase):
+ def test_create(self):
+ connection = MagicMock()
+ connection.make_request.return_value = {'id': 1}
+
+ result = Orders.create(connection, name="Hello")
+ self.assertIsInstance(result, Orders)
+ self.assertEqual(result.id, 1)
+ connection.make_request.assert_called_once_with('POST', 'orders', {'name': 'Hello'}, {}, {})
+
+
+class TestCreateableApiSubResource(unittest.TestCase):
+ def test_create(self):
+ connection = MagicMock()
+ connection.make_request.return_value = {'id': 2}
+
+ result = OrderShipments.create(1, connection, name="Hello")
+ self.assertIsInstance(result, OrderShipments)
+ self.assertEqual(result.id, 2)
+ connection.make_request.assert_called_once_with('POST', 'orders/1/shipments', {'name': 'Hello'}, {}, {})
+
+
+class TestListableApiResource(unittest.TestCase):
+ def test_all(self):
+ connection = MagicMock()
+ connection.make_request.return_value = [{'id': 1}, {'id': 2}]
+
+ result = Orders.all(connection, limit=2)
+ self.assertEqual(len(result), 2)
+ connection.make_request.assert_called_once_with('GET', 'orders', None, {'limit': 2}, {})
+
+
+class TestListableApiSubResource(unittest.TestCase):
+ def test_all(self):
+ connection = MagicMock()
+ connection.make_request.return_value = [{'id': 1}, {'id': 2}]
+
+ result = OrderCoupons.all(1, connection, limit=2)
+ self.assertEqual(len(result), 2)
+ connection.make_request.assert_called_once_with('GET', 'orders/1/coupons', None, {'limit': 2}, {})
+
+
+class TestUpdateableApiResource(unittest.TestCase):
+ def test_update(self):
+ connection = MagicMock()
+ connection.make_request.return_value = {'id': 1}
+
+ order = Orders({'id': 1}, _connection=connection)
+ new_order = order.update(name='order')
+ self.assertIsInstance(new_order, Orders)
+
+ connection.make_request.assert_called_once_with('PUT', 'orders/1', {'name': 'order'}, {}, {})
+
+
+class TestUpdateableApiSubResource(unittest.TestCase):
+ def test_update(self):
+ connection = MagicMock()
+ connection.make_request.return_value = {'id': 1}
+
+ order = OrderShipments({'id': 1, 'order_id': 2}, _connection=connection)
+ new_order = order.update(tracking_number='1234')
+ self.assertIsInstance(new_order, OrderShipments)
+
+ connection.make_request.assert_called_once_with('PUT', 'orders/2/shipments/1', {'tracking_number': '1234'},
+ {}, {})
+
+
+class TestDeleteableApiResource(unittest.TestCase):
+ def test_delete_all(self):
+ connection = MagicMock()
+ connection.make_request.return_value = {}
+
+ self.assertEqual(Orders.delete_all(connection), {})
+
+ connection.make_request.assert_called_once_with('DELETE', 'orders', None, {}, {})
+
+ def test_delete(self):
+ connection = MagicMock()
+ connection.make_request.return_value = {}
+
+ order = Orders({'id': 1}, _connection=connection)
+
+ self.assertEqual(order.delete(), {})
+
+ connection.make_request.assert_called_once_with('DELETE', 'orders/1', None, {}, {})
+
+
+class TestDeleteableApiSubResource(unittest.TestCase):
+ def test_delete_all(self):
+ connection = MagicMock()
+ connection.make_request.return_value = {}
+
+ self.assertEqual(OrderShipments.delete_all(1, connection=connection), {})
+
+ connection.make_request.assert_called_once_with('DELETE', 'orders/1/shipments', None, {}, {})
+
+ def test_delete(self):
+ connection = MagicMock()
+ connection.make_request.return_value = {}
+
+ shipment = OrderShipments({'id': 1, 'order_id': 2, '_connection': connection})
+ self.assertEqual(shipment.delete(), {})
+
+ connection.make_request.assert_called_once_with('DELETE', 'orders/2/shipments/1', None, {}, {})

0 comments on commit dcfd60f

Please sign in to comment.