From f09be89978a8e29a438b2741caca9b03255db779 Mon Sep 17 00:00:00 2001 From: timwis Date: Sat, 21 Nov 2015 09:48:24 -0500 Subject: [PATCH] Use http for mock protocol, add test for set_public(). #3 --- sodapy/__init__.py | 8 ++++---- tests/test_data/empty.txt | 0 tests/test_soda.py | 32 ++++++++++++++++++++++++++++---- 3 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 tests/test_data/empty.txt diff --git a/sodapy/__init__.py b/sodapy/__init__.py index 74d1f35..4904c51 100644 --- a/sodapy/__init__.py +++ b/sodapy/__init__.py @@ -62,7 +62,7 @@ def __init__(self, domain, app_token, username=None, password=None, session_adapter["adapter"]) self.uri_prefix = session_adapter["prefix"] else: - self.uri_prefix = "https" + self.uri_prefix = "https://" def authentication_validation(self, username, password, access_token): ''' @@ -230,20 +230,20 @@ def _perform_request(self, request_type, resource, **kwargs): raise Exception("Unknown request type. Supported request types are" ": {0}".format(", ".join(request_type_methods))) - uri = "{0}://{1}{2}".format(self.uri_prefix, self.domain, resource) + uri = "{0}{1}{2}".format(self.uri_prefix, self.domain, resource) # set a timeout, just to be safe kwargs["timeout"] = 10 response = getattr(self.session, request_type)(uri, **kwargs) - + # handle errors if response.status_code not in (200, 202): _raise_for_status(response) # when responses have no content body (ie. delete, set_public), simply # return the whole response - if not len(response.text): + if not response.text: return response # for other request types, return most useful data diff --git a/tests/test_data/empty.txt b/tests/test_data/empty.txt new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_soda.py b/tests/test_soda.py index 0f39cdd..4bf0e31 100644 --- a/tests/test_soda.py +++ b/tests/test_soda.py @@ -7,7 +7,7 @@ import json -PREFIX = "mock" +PREFIX = "http://" DOMAIN = "fakedomain.com" PATH = "/songs.json" APPTOKEN = "FakeAppToken" @@ -113,7 +113,7 @@ def test_delete(): client = Socrata(DOMAIN, APPTOKEN, username=USERNAME, password=PASSWORD, session_adapter=mock_adapter) - uri = "{0}://{1}{2}".format(PREFIX, DOMAIN, PATH) + uri = "{0}{1}{2}".format(PREFIX, DOMAIN, PATH) adapter.register_uri("DELETE", uri, status_code=200) response = client.delete(PATH) assert response.status_code == 200 @@ -159,12 +159,36 @@ def test_create(): assert len(response.get("id")) == 9 client.close() +def test_set_public(): + mock_adapter = {} + mock_adapter["prefix"] = PREFIX + adapter = requests_mock.Adapter() + mock_adapter["adapter"] = adapter + client = Socrata(DOMAIN, APPTOKEN, username=USERNAME, password=PASSWORD, + session_adapter=mock_adapter) + + response_data = "empty.txt" + resource = "/api/views" + PATH + set_up_mock(adapter, "PUT", response_data, 200, resource=resource) + + response = client.set_public(PATH) + + request = adapter.request_history[0] + assert "method" in request.qs + assert "value" in request.qs + + assert response.status_code == 200 + def set_up_mock(adapter, method, response, response_code, reason="OK", auth=None, resource=PATH): path = os.path.join(TEST_DATA_PATH, response) with open(path, "rb") as f: - body = json.load(f) - uri = "{0}://{1}{2}".format(PREFIX, DOMAIN, resource) + try: + body = json.load(f) + except ValueError: + body = None + + uri = "{0}{1}{2}".format(PREFIX, DOMAIN, resource) headers = { "content-type": "application/json; charset=utf-8" }