From c7e7da4a4306eed0294845c4b5ed6a0508f5def7 Mon Sep 17 00:00:00 2001 From: Adolfo Portilla Date: Thu, 13 Jun 2019 16:53:17 -0700 Subject: [PATCH] Feat: Vehicle state error (#56) * Feat: vehicle state code error * Feat: Add errors to readme * Fix: failing test * Fix: failing e2e test * Bump version * Fix: rename errorCode to code * Fix: nits --- README.md | 3 +++ smartcar/__init__.py | 2 +- smartcar/exceptions.py | 9 ++++++++- tests/test_requester.py | 14 ++++++++++---- tests/tests_e2e/test_base.py | 2 +- 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index fb159a0c..3cba31a4 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,8 @@ odometer = vehicle.odometer()['data']['distance'] |500|smartcar.ServerException| |501|smartcar.NotCapableException| |504|smartcar.GatewayTimeoutException| + +Checkout our [Errors documentation][errors] to learn more. ## AuthClient @@ -366,3 +368,4 @@ Get a list of the user's vehicle ids [ci-image]: https://travis-ci.com/smartcar/python-sdk.svg?token=FcsopC3DdDmqUpnZsrwg&branch=master [pypi-url]: https://badge.fury.io/py/smartcar [pypi-image]: https://badge.fury.io/py/smartcar.svg +[errors]: https://smartcar.com/docs/api#errors diff --git a/smartcar/__init__.py b/smartcar/__init__.py index face8585..da240655 100644 --- a/smartcar/__init__.py +++ b/smartcar/__init__.py @@ -1,4 +1,4 @@ -__version__ = '3.0.1' +__version__ = '3.0.2' from .smartcar import (AuthClient, is_expired, get_user_id, get_vehicle_ids) from .vehicle import Vehicle diff --git a/smartcar/exceptions.py b/smartcar/exceptions.py index ae3de319..4fb02bad 100644 --- a/smartcar/exceptions.py +++ b/smartcar/exceptions.py @@ -20,7 +20,14 @@ class PermissionException(SmartcarException): class ResourceNotFoundException(SmartcarException): pass class StateException(SmartcarException): - pass + def __init__(self, response): + super(StateException, self).__init__(response) + json = response.json() + self.code = json['code'] + + def __str__(self): + return self.code + ': ' + self.message + class RateLimitingException(SmartcarException): pass class MonthlyLimitExceeded(SmartcarException): diff --git a/tests/test_requester.py b/tests/test_requester.py index ec654456..d30fe15a 100644 --- a/tests/test_requester.py +++ b/tests/test_requester.py @@ -7,14 +7,14 @@ class TestRequester(unittest.TestCase): EXPECTED = 'expected' URL = 'http://fake.url' - def queue(self, code, **kwargs): + def queue(self, status_code, **kwargs): """ queue up a fake response with the specified status code """ if not kwargs: json = { 'message': self.EXPECTED } else: json = kwargs - responses.add('GET', self.URL, status=code, json=json) + responses.add('GET', self.URL, status=status_code, json=json) def check(self, exception): @@ -68,8 +68,14 @@ def test_404(self): @responses.activate def test_409(self): - self.queue(409) - self.check(smartcar.StateException) + message = "Vehicle State Error" + code='VS_OO1' + self.queue(409, message=message, code=code) + try: + smartcar.requester.call('GET', self.URL) + except smartcar.StateException as err: + self.assertEqual(err.message, message) + self.assertEqual(err.code, code) @responses.activate def test_429(self): diff --git a/tests/tests_e2e/test_base.py b/tests/tests_e2e/test_base.py index 7082d1b3..006739c6 100644 --- a/tests/tests_e2e/test_base.py +++ b/tests/tests_e2e/test_base.py @@ -27,7 +27,7 @@ def get_code(driver, auth_url): tesla_button = WebDriverWait(driver, 10).until( EC.presence_of_element_located(( By.CSS_SELECTOR, - "a[data-make='TESLA']"))) + "button[data-make='TESLA']"))) tesla_button.click() username = uuid.uuid4()