# Path: shippo/test/helper.py
# def create_mock_shipment(asynchronous=False, api_key=None):
#     to_address = shippo.Address.create(api_key=api_key, **TO_ADDRESS)
#     from_address = shippo.Address.create(api_key=api_key, **FROM_ADDRESS)
#     parcel = shippo.Parcel.create(api_key=api_key, **DUMMY_PARCEL)
#     SHIPMENT = DUMMY_SHIPMENT.copy()
#     SHIPMENT['address_from'] = from_address.object_id
#     SHIPMENT['address_to'] = to_address.object_id
#     SHIPMENT['parcels'] = [parcel.object_id]
#     SHIPMENT['asynchronous'] = asynchronous
#     shipment = shippo.Shipment.create(api_key=api_key, **SHIPMENT)
#     return shipment
# 
# DUMMY_ADDRESS = {
#     "name": "Laura Behrens Wu",
#     "company": "Shippo",
#     "street1": "Clayton St.",
#     "street_no": "215",
#     "street2": "",
#     "city": "San Francisco",
#     "state": "CA",
#     "zip": "94117",
#     "country": "US",
#     "phone": "+1 555 341 9393",
#     "metadata": "Customer ID 123456"
# }
# 
# INVALID_ADDRESS = {
#     "name": "Laura Behrens Wu",
#     "company": "Shippo",
#     "street2": "",
#     "city": "San Francisco",
#     "state": "CA",
#     "country": "US",
#     "phone": "+1 555 341 9393",
#     "metadata": "Customer ID 123456"
# }
# 
# class ShippoTestCase(TestCase):
#     RESTORE_ATTRIBUTES = ('api_version', 'api_key')
# 
#     def setUp(self):
#         super(ShippoTestCase, self).setUp()
# 
#         self._shippo_original_attributes = {}
# 
#         for attr in self.RESTORE_ATTRIBUTES:
#             self._shippo_original_attributes[attr] = getattr(
#                 shippo.config, attr)
# 
#         api_base = os.environ.get('SHIPPO_API_BASE')
#         if api_base:
#             shippo.config.api_base = api_base
# 
#         shippo.config.api_key = os.environ.get(
#             'SHIPPO_API_KEY', '51895b669caa45038110fd4074e61e0d')
#         shippo.config.api_version = os.environ.get(
#             'SHIPPO_API_VERSION', '2018-02-08')
# 
#     def tearDown(self):
#         super(ShippoTestCase, self).tearDown()
# 
#         for attr in self.RESTORE_ATTRIBUTES:
#             setattr(shippo.config, attr,
#                     self._shippo_original_attributes[attr])
# 
#     # Python < 2.7 compatibility
#     def assertRaisesRegexp(self, exception, regexp, callable, *args, **kwargs):
#         try:
#             callable(*args, **kwargs)
#         except exception as err:
#             if regexp is None:
#                 return True
# 
#             if isinstance(regexp, str):
#                 regexp = re.compile(regexp)
#             if not regexp.search(str(err)):
#                 raise self.failureException('"%s" does not match "%s"' %
#                                             (regexp.pattern, str(err)))
#         else:
#             raise self.failureException(
#                 '%s was not raised' % (exception.__name__,))


import unittest2
import shippo
from mock import patch
from shippo.test.helper import (
    create_mock_shipment,
    DUMMY_ADDRESS,
    INVALID_ADDRESS,
    ShippoTestCase,
)
    request_client = shippo.http_client.RequestsClient

    def setUp(self):
        super(FunctionalTests, self).setUp()

        def get_http_client(*args, **kwargs):
            return self.request_client(*args, **kwargs)

        self.client_patcher = patch(
            'shippo.http_client.new_default_http_client')

        client_mock = self.client_patcher.start()
        client_mock.side_effect = get_http_client

    def tearDown(self):
        super(FunctionalTests, self).tearDown()

        self.client_patcher.stop()

    def test_dns_failure(self):
        api_base = shippo.config.api_base
        try:
            shippo.config.api_base = 'https://my-invalid-domain.ireallywontresolve/v1'
            self.assertRaises(shippo.error.APIConnectionError,
                              shippo.Address.create)
        finally:
            shippo.config.api_base = api_base

    def test_run(self):
        try:

# Path: shippo/test/helper.py
# def create_mock_shipment(asynchronous=False, api_key=None):
#     to_address = shippo.Address.create(api_key=api_key, **TO_ADDRESS)
#     from_address = shippo.Address.create(api_key=api_key, **FROM_ADDRESS)
#     parcel = shippo.Parcel.create(api_key=api_key, **DUMMY_PARCEL)
#     SHIPMENT = DUMMY_SHIPMENT.copy()
#     SHIPMENT['address_from'] = from_address.object_id
#     SHIPMENT['address_to'] = to_address.object_id
#     SHIPMENT['parcels'] = [parcel.object_id]
#     SHIPMENT['asynchronous'] = asynchronous
#     shipment = shippo.Shipment.create(api_key=api_key, **SHIPMENT)
#     return shipment
# 
# DUMMY_ADDRESS = {
#     "name": "Laura Behrens Wu",
#     "company": "Shippo",
#     "street1": "Clayton St.",
#     "street_no": "215",
#     "street2": "",
#     "city": "San Francisco",
#     "state": "CA",
#     "zip": "94117",
#     "country": "US",
#     "phone": "+1 555 341 9393",
#     "metadata": "Customer ID 123456"
# }
# 
# INVALID_ADDRESS = {
#     "name": "Laura Behrens Wu",
#     "company": "Shippo",
#     "street2": "",
#     "city": "San Francisco",
#     "state": "CA",
#     "country": "US",
#     "phone": "+1 555 341 9393",
#     "metadata": "Customer ID 123456"
# }
# 
# class ShippoTestCase(TestCase):
#     RESTORE_ATTRIBUTES = ('api_version', 'api_key')
# 
#     def setUp(self):
#         super(ShippoTestCase, self).setUp()
# 
#         self._shippo_original_attributes = {}
# 
#         for attr in self.RESTORE_ATTRIBUTES:
#             self._shippo_original_attributes[attr] = getattr(
#                 shippo.config, attr)
# 
#         api_base = os.environ.get('SHIPPO_API_BASE')
#         if api_base:
#             shippo.config.api_base = api_base
# 
#         shippo.config.api_key = os.environ.get(
#             'SHIPPO_API_KEY', '51895b669caa45038110fd4074e61e0d')
#         shippo.config.api_version = os.environ.get(
#             'SHIPPO_API_VERSION', '2018-02-08')
# 
#     def tearDown(self):
#         super(ShippoTestCase, self).tearDown()
# 
#         for attr in self.RESTORE_ATTRIBUTES:
#             setattr(shippo.config, attr,
#                     self._shippo_original_attributes[attr])
# 
#     # Python < 2.7 compatibility
#     def assertRaisesRegexp(self, exception, regexp, callable, *args, **kwargs):
#         try:
#             callable(*args, **kwargs)
#         except exception as err:
#             if regexp is None:
#                 return True
# 
#             if isinstance(regexp, str):
#                 regexp = re.compile(regexp)
#             if not regexp.search(str(err)):
#                 raise self.failureException('"%s" does not match "%s"' %
#                                             (regexp.pattern, str(err)))
#         else:
#             raise self.failureException(
#                 '%s was not raised' % (exception.__name__,))


# Path: shippo/test/integration/test_integration.py
import unittest2
import shippo
from mock import patch
from shippo.test.helper import (
    create_mock_shipment,
    DUMMY_ADDRESS,
    INVALID_ADDRESS,
    ShippoTestCase,
)

    request_client = shippo.http_client.RequestsClient

    def setUp(self):
        super(FunctionalTests, self).setUp()

        def get_http_client(*args, **kwargs):
            return self.request_client(*args, **kwargs)

        self.client_patcher = patch(
            'shippo.http_client.new_default_http_client')

        client_mock = self.client_patcher.start()
        client_mock.side_effect = get_http_client

    def tearDown(self):
        super(FunctionalTests, self).tearDown()

        self.client_patcher.stop()

    def test_dns_failure(self):
        api_base = shippo.config.api_base
        try:
            shippo.config.api_base = 'https://my-invalid-domain.ireallywontresolve/v1'
            self.assertRaises(shippo.error.APIConnectionError,
                              shippo.Address.create)
        finally:
            shippo.config.api_base = api_base

    def test_run(self):
        try:

            address = shippo.Address.create(**DUMMY_ADDRESS)