def test_api_server_is_unreachable_raises_server_busy_or_offline_exception(self, sleep):
     sleep.return_value = True # we don't need to really sleep
     request = FakeApi().create_request('Wrong Value')
     request.get_inventory()
     # we expect an exception because the "server" isn't returning a valid response
     with self.assertRaises(ServerBusyOrOfflineException):
         request.call()
 def test_api_server_is_unreachable_raises_server_busy_or_offline_exception(self, sleep):
     sleep.return_value = True # we don't need to really sleep
     request = FakeApi().create_request('Wrong Value')
     request.get_inventory()
     # we expect an exception because the "server" isn't returning a valid response
     with self.assertRaises(ServerBusyOrOfflineException):
         request.call()
    def test_multiple_requests(self):
        request = FakeApi().create_request()
        request.get_inventory(test='awesome')
        request.fort_details()

        good_return_value = {'responses': {'GET_INVENTORY': {}, 'FORT_DETAILS': {}}, 'status_code': 0}
        request._call.return_value = good_return_value

        result = request.call()
        self.assertEqual(result, good_return_value)
    def test_multiple_requests(self):
        api = FakeApi()
        api.get_inventory(test='awesome')
        api.fort_details()

        good_return_value = {'responses': {'GET_INVENTORY': {}, 'FORT_DETAILS': {}}, 'status_code': 0}
        api.setApiReturnValue(good_return_value)

        result = api.call()
        self.assertEqual(result, good_return_value)
    def test_multiple_requests(self):
        request = FakeApi().create_request()
        request.get_inventory(test='awesome')
        request.fort_details()

        good_return_value = {'responses': {'GET_INVENTORY': {}, 'FORT_DETAILS': {}}, 'status_code': 0}
        request._call.return_value = good_return_value

        result = request.call()
        self.assertEqual(result, good_return_value)
    def test_return_value_is_valid(self):
        request = FakeApi().create_request() # we set the return value below
        request.get_inventory(test='awesome')

        request_caller = request.request_callers[0] # only one request
        self.assertEqual(request_caller.upper(), 'GET_INVENTORY')

        good_return_value = {'responses': {request_caller.upper(): {}}, 'status_code': 0}
        request._call.return_value = good_return_value

        result = request.call()
        self.assertEqual(result, good_return_value)
        self.assertEqual(len(request.request_callers), 0, 'request_callers must be empty')
    def test_return_value_is_valid(self):
        api = FakeApi() # we set the return value below
        api.get_inventory(test='awesome')

        request = api.request_callers[0] # only one request
        self.assertEqual(request.upper(), 'GET_INVENTORY')

        good_return_value = {'responses': {request.upper(): {}}, 'status_code': 0}
        api.setApiReturnValue(good_return_value)

        result = api.call()
        self.assertEqual(result, good_return_value)
        self.assertEqual(len(api.request_callers), 0, 'request_callers must be empty')
    def test_return_value_is_valid(self):
        request = FakeApi().create_request() # we set the return value below
        request.get_inventory(test='awesome')

        request_caller = request.request_callers[0] # only one request
        self.assertEqual(request_caller.upper(), 'GET_INVENTORY')

        good_return_value = {'responses': {request_caller.upper(): {}}, 'status_code': 0}
        request._call.return_value = good_return_value

        result = request.call()
        self.assertEqual(result, good_return_value)
        self.assertEqual(len(request.request_callers), 0, 'request_callers must be empty')
 def test_mocked_call(self):
     request = FakeApi().create_request(True)
     request.is_response_valid = MagicMock(return_value=True)
     request.get_inventory(test='awesome')
     result = request.call()
     self.assertTrue(result)
 def test_mocked_call(self):
     request = FakeApi().create_request(True)
     request.is_response_valid = MagicMock(return_value=True)
     request.get_inventory(test='awesome')
     result = request.call()
     self.assertTrue(result)
 def returnApi(ret_value):
     api = FakeApi(ret_value)
     api.get_inventory(test='awesome')
     return api
 def test_mocked_call(self):
     api = FakeApi(True)
     api._is_response_valid = MagicMock(return_value=True)
     api.get_inventory(test='awesome')
     result = api.call()
     self.assertTrue(result)