コード例 #1
0
    def test_run_method(self):
        connection = Connection(host='store.mybigcommerce.com', auth=('user', 'abcdef'))
        connection._session.request = MagicMock()

        # Call with nothing
        connection._run_method('GET', '')
        connection._session.request.assert_called_once_with('GET', 'https://store.mybigcommerce.com/api/v2/',
                                                            data=None, timeout=7.0, headers={})
        connection._session.request.reset_mock()

        # A simple request
        connection._run_method('GET', 'time')
        connection._session.request.assert_called_once_with('GET', 'https://store.mybigcommerce.com/api/v2/time',
                                                            data=None, timeout=7.0, headers={})
        connection._session.request.reset_mock()

        # A request with data
        data = {
            'name': 'Shirt',
            'price': 25.00
        }

        connection._run_method('POST', '/products', data)

        connection._session.request.assert_called_once_with('POST', 'https://store.mybigcommerce.com/api/v2/products',
                                                            data=json.dumps(data), timeout=7.0,
                                                            headers={'Content-Type': 'application/json'})
        connection._session.request.reset_mock()

        # A request with filters
        connection._run_method('GET', '/orders', query={'limit': 50})
        connection._session.request.assert_called_once_with('GET',
                                                            'https://store.mybigcommerce.com/api/v2/orders?limit=50',
                                                            data=None, timeout=7.0, headers={})
        connection._session.request.reset_mock()
コード例 #2
0
    def test_handle_response(self):
        connection = Connection('store.mybigcommerce.com', ('user', 'abcdef'))
        # A normal, 200-ok response
        data = {
            'name': 'Shirt'
        }
        res = MagicMock()
        res.headers = {'Content-Type': 'application/json'}
        res.status_code = 200
        res.content = json.dumps(data)
        res.json.return_value = data
        self.assertEqual(connection._handle_response('products/1', res), data)

        res.status_code = 500
        self.assertRaisesHttpException(ServerException,
                                       lambda: connection._handle_response('products/1', res),
                                       # Test all of the properties of a HttpException
                                       500,
                                       {'Content-Type': 'application/json'},
                                       json.dumps(data))

        res.status_code = 404
        self.assertRaisesHttpException(ClientRequestException,
                                       lambda: connection._handle_response('products/1', res), 404)

        res.status_code = 301
        self.assertRaisesHttpException(RedirectionException,
                                       lambda: connection._handle_response('products/1', res), 301)
コード例 #3
0
    def test_handle_response(self):
        connection = Connection("store.mybigcommerce.com", ("user", "abcdef"))
        # A normal, 200-ok response
        data = {"name": "Shirt"}
        res = MagicMock()
        res.headers = {"Content-Type": "application/json"}
        res.status_code = 200
        res.content = json.dumps(data)
        res.json.return_value = data
        self.assertEqual(connection._handle_response("products/1", res), data)

        res.status_code = 500
        self.assertRaisesHttpException(
            ServerException,
            lambda: connection._handle_response("products/1", res),
            # Test all of the properties of a HttpException
            500,
            {"Content-Type": "application/json"},
            json.dumps(data),
        )

        res.status_code = 404
        self.assertRaisesHttpException(
            ClientRequestException, lambda: connection._handle_response("products/1", res), 404
        )

        res.status_code = 301
        self.assertRaisesHttpException(
            RedirectionException, lambda: connection._handle_response("products/1", res), 301
        )
コード例 #4
0
    def test_run_method(self):
        connection = Connection(host='store.mybigcommerce.com', auth=('user', 'abcdef'))
        connection._session.request = MagicMock()

        # Call with nothing
        connection._run_method('GET', '')
        connection._session.request.assert_called_once_with('GET', 'https://store.mybigcommerce.com/api/v2/',
                                                            data=None, timeout=7.0, headers={})
        connection._session.request.reset_mock()

        # A simple request
        connection._run_method('GET', 'time')
        connection._session.request.assert_called_once_with('GET', 'https://store.mybigcommerce.com/api/v2/time',
                                                            data=None, timeout=7.0, headers={})
        connection._session.request.reset_mock()

        # A request with data
        data = {
            'name': 'Shirt',
            'price': 25.00
        }

        connection._run_method('POST', '/products', data)

        connection._session.request.assert_called_once_with('POST', 'https://store.mybigcommerce.com/api/v2/products',
                                                            data=json.dumps(data), timeout=7.0,
                                                            headers={'Content-Type': 'application/json'})
        connection._session.request.reset_mock()

        # A request with filters
        connection._run_method('GET', '/orders', query={'limit': 50})
        connection._session.request.assert_called_once_with('GET',
                                                            'https://store.mybigcommerce.com/api/v2/orders?limit=50',
                                                            data=None, timeout=7.0, headers={})
        connection._session.request.reset_mock()
コード例 #5
0
    def test_handle_response(self):
        connection = Connection('store.mybigcommerce.com', ('user', 'abcdef'))
        # A normal, 200-ok response
        data = {
            'name': 'Shirt'
        }
        res = MagicMock()
        res.headers = {'Content-Type': 'application/json'}
        res.status_code = 200
        res.content = json.dumps(data)
        res.json.return_value = data
        self.assertEqual(connection._handle_response('products/1', res), data)

        res.status_code = 500
        self.assertRaisesHttpException(ServerException,
                                       lambda: connection._handle_response('products/1', res),
                                       # Test all of the properties of a HttpException
                                       500,
                                       {'Content-Type': 'application/json'},
                                       json.dumps(data))

        res.status_code = 404
        self.assertRaisesHttpException(ClientRequestException,
                                       lambda: connection._handle_response('products/1', res), 404)

        res.status_code = 301
        self.assertRaisesHttpException(RedirectionException,
                                       lambda: connection._handle_response('products/1', res), 301)
コード例 #6
0
    def test_run_method(self):
        connection = Connection(host="store.mybigcommerce.com", auth=("user", "abcdef"))
        connection._session.request = MagicMock()

        # Call with nothing
        connection._run_method("GET", "")
        connection._session.request.assert_called_once_with(
            "GET", "https://store.mybigcommerce.com/api/v2/", data=None, timeout=7.0, headers={}
        )
        connection._session.request.reset_mock()

        # A simple request
        connection._run_method("GET", "time")
        connection._session.request.assert_called_once_with(
            "GET", "https://store.mybigcommerce.com/api/v2/time", data=None, timeout=7.0, headers={}
        )
        connection._session.request.reset_mock()

        # A request with data
        data = {"name": "Shirt", "price": 25.00}

        connection._run_method("POST", "/products", data)

        connection._session.request.assert_called_once_with(
            "POST",
            "https://store.mybigcommerce.com/api/v2/products",
            data=json.dumps(data),
            timeout=7.0,
            headers={"Content-Type": "application/json"},
        )
        connection._session.request.reset_mock()

        # A request with filters
        connection._run_method("GET", "/orders", query={"limit": 50})
        connection._session.request.assert_called_once_with(
            "GET", "https://store.mybigcommerce.com/api/v2/orders?limit=50", data=None, timeout=7.0, headers={}
        )
        connection._session.request.reset_mock()
コード例 #7
0
 def test_create(self):
     connection = Connection(host='store.mybigcommerce.com', auth=('user', 'abcdef'))
     self.assertTupleEqual(connection._session.auth, ('user', 'abcdef'))
コード例 #8
0
 def test_full_path(self):
     connection = Connection(host='store.mybigcommerce.com', auth=('user', 'abcdef'))
     self.assertEqual(connection.full_path('time'), 'https://store.mybigcommerce.com/api/v2/time')
コード例 #9
0
 def test_full_path(self):
     connection = Connection(host='store.mybigcommerce.com', auth=('user', 'abcdef'))
     self.assertEqual(connection.full_path('time'), 'https://store.mybigcommerce.com/api/v2/time')
コード例 #10
0
 def test_full_path(self):
     connection = Connection(host="store.mybigcommerce.com", auth=("user", "abcdef"))
     self.assertEqual(connection.full_path("time"), "https://store.mybigcommerce.com/api/v2/time")