From 4a66d5e95f14df99a2eb372fbcf9459560ac2481 Mon Sep 17 00:00:00 2001 From: Jeff Knupp Date: Sat, 12 Apr 2014 12:42:37 -0400 Subject: [PATCH] tests pass --- sandman/sandman.py | 12 ++++---- tests/test_foreign_keys.py | 4 +-- tests/test_sandman.py | 60 +++++++++++++++++++------------------- 3 files changed, 37 insertions(+), 39 deletions(-) diff --git a/sandman/sandman.py b/sandman/sandman.py index 0ac2f88..86e1388 100644 --- a/sandman/sandman.py +++ b/sandman/sandman.py @@ -19,9 +19,8 @@ FORWARDED_EXCEPTION_MESSAGE = 'Request could not be completed. Exception: [{}]' FORBIDDEN_EXCEPTION_MESSAGE = """Method [{}] not acceptable for resource \ type [{}]. Acceptable methods: [{}]""" -UNSUPPORTED_CONTENT_TYPE_MESSAGE = """Content-type [{}] not supported. -Supported values for 'Content-type': {}""".format( - '{}', str(ACCEPTABLE_CONTENT_TYPES)) +UNSUPPORTED_CONTENT_TYPE_MESSAGE = 'Content-type [{types}] not supported.' +#UNSUPPORTED_CONTENT_TYPE_MESSAGE += """\nSupported values for 'Content-type': {}""".format(ACCEPTABLE_CONTENT_TYPES) def _perform_database_action(action, *args): """Call session.*action* with the given *args*. @@ -179,7 +178,7 @@ def get_resource_data(incoming_request): else: # HTTP 415: Unsupported Media Type raise InvalidAPIUsage(415, - UNSUPPORTED_CONTENT_TYPE_MESSAGE.format( + UNSUPPORTED_CONTENT_TYPE_MESSAGE.format(types= incoming_request.headers['Content-type'])) def endpoint_class(collection): @@ -382,7 +381,7 @@ def put_resource(collection, key): _perform_database_action('add', resource) except IntegrityError as exception: raise InvalidAPIUsage(422, FORWARDED_EXCEPTION_MESSAGE.format( - exception.message)) + exception)) return no_content_response() @app.route('/', methods=['POST']) @@ -423,7 +422,7 @@ def delete_resource(collection, key): _perform_database_action('delete', resource) except IntegrityError as exception: raise InvalidAPIUsage(422, FORWARDED_EXCEPTION_MESSAGE.format( - exception.message)) + exception)) return no_content_response() @app.route('//', methods=['GET']) @@ -477,7 +476,6 @@ def get_collection(collection): start = stop = None - print request.args if request.args and 'page' in request.args: page = int(request.args['page']) results_per_page = app.config.get('RESULTS_PER_PAGE', 20) diff --git a/tests/test_foreign_keys.py b/tests/test_foreign_keys.py index 479aadd..f2cfcc1 100644 --- a/tests/test_foreign_keys.py +++ b/tests/test_foreign_keys.py @@ -33,12 +33,12 @@ def setup_method(self, _): def test_get(self): """Test simple HTTP GET, enough to cover all cases for now.""" response = self.app.get('/job_schedules') - assert len(json.loads(response.data)[u'resources']) == 1 + assert len(json.loads(response.get_data(as_text=True))[u'resources']) == 1 def test_date_time(self): """Test serializing a datetime object works properly.""" response = self.app.get('/date_times') - assert len(json.loads(response.data)[u'resources']) == 1 + assert len(json.loads(response.get_data(as_text=True))[u'resources']) == 1 def teardown_method(self, _): """Remove the database file copied during setup.""" diff --git a/tests/test_sandman.py b/tests/test_sandman.py index 0e388e5..72fe912 100644 --- a/tests/test_sandman.py +++ b/tests/test_sandman.py @@ -46,7 +46,7 @@ def get_response(self, uri, status_code, params=None, has_data=True, response = self.app.get(uri, query_string=params, headers=headers) assert response.status_code == status_code if has_data: - assert response.data + assert response.get_data(as_text=True) return response def post_response(self): @@ -56,47 +56,47 @@ def post_response(self): content_type='application/json', data=json.dumps({u'Name': u'Jeff Knupp'})) assert response.status_code == 201 - assert json.loads(response.data)[u'Name'] == u'Jeff Knupp' + assert json.loads(response.get_data(as_text=True))[u'Name'] == u'Jeff Knupp' return response @staticmethod def is_html_response(response): """Return True if *response* is an HTML response""" - assert 'text/html' in response.headers['Content-type'] - return '' in response.data + assert 'text/html' in str(response.headers['Content-type']) + return '' in str(response.get_data(as_text=True)) class TestSandmanBasicVerbs(TestSandmanBase): """Test the basic HTTP verbs (e.g. "PUT", "GET", etc.)""" def test_get(self): """Test simple HTTP GET""" response = self.get_response('/artists', 200) - assert len(json.loads(response.data)[u'resources']) == 275 + assert len(json.loads(response.get_data(as_text=True))[u'resources']) == 275 def test_get_with_filter(self): """Test simple HTTP GET""" response = self.get_response('/artists', 200, params={'Name': 'AC/DC'}) - assert len(json.loads(response.data)[u'resources']) == 1 + assert len(json.loads(response.get_data(as_text=True))[u'resources']) == 1 def test_get_with_like_filter(self): """Test simple HTTP GET""" response = self.get_response('/artists', 200, params={'Name': '%AC%DC%'}) - assert len(json.loads(response.data)[u'resources']) == 1 + assert len(json.loads(response.get_data(as_text=True))[u'resources']) == 1 def test_get_with_sort(self): """Test simple HTTP GET""" response = self.get_response('/artists', 200, params={'sort': 'Name'}) - assert json.loads(response.data)[u'resources'][0]['Name'] == 'A Cor Do Som' + assert json.loads(response.get_data(as_text=True))[u'resources'][0]['Name'] == 'A Cor Do Som' def test_get_attribute(self): """Test simple HTTP GET""" response = self.get_response('/artists/1/Name', 200) - assert json.loads(response.data)[u'Name'] == 'AC/DC' + assert json.loads(response.get_data(as_text=True))[u'Name'] == 'AC/DC' def test_get_object_attribute(self): """Test simple HTTP GET""" response = self.get_response('/tracks/347', 200) response = self.get_response('/tracks/347/Genre', 200) - assert json.loads(response.data)[u'Name'] == 'Rock' + assert json.loads(response.get_data(as_text=True))[u'Name'] == 'Rock' def test_get_link_header_for_resource(self): """Does GETing a resource return the 'Link' header field?""" @@ -108,12 +108,12 @@ def test_get_link_header_for_resource(self): def test_get_expanded_resource(self): """Does GETing a resource return the 'Link' header field?""" response = self.get_response('/tracks/1', 200, params={'expand': 1}) - assert 'album' in json.loads(response.data) + assert 'album' in json.loads(response.get_data(as_text=True)) def test_post(self): """Test simple HTTP POST""" response = self.post_response() - assert json.loads(response.data) == { + assert json.loads(response.get_data(as_text=True)) == { 'ArtistId': 276, 'Name': 'Jeff Knupp', 'self': '/artists/276', @@ -130,7 +130,7 @@ def test_posted_uri(self): """Make sure 'uri' in the links returned in response actually points to new resource created during POST.""" post_response = self.post_response() - as_json = json.loads(post_response.data) + as_json = json.loads(post_response.get_data(as_text=True)) assert as_json == { 'ArtistId': 276, 'Name': 'Jeff Knupp', @@ -148,9 +148,9 @@ def test_patch_new_resource(self): content_type='application/json', data=json.dumps({u'Name': u'Jeff Knupp'})) assert response.status_code == 201 - assert type(response.data) == str - assert json.loads(response.data)['Name'] == u'Jeff Knupp' - assert json.loads(response.data)['self'] == '/artists/276' + assert type(response.get_data(as_text=True)) == str + assert json.loads(response.get_data(as_text=True))['Name'] == u'Jeff Knupp' + assert json.loads(response.get_data(as_text=True))['self'] == '/artists/276' def test_patch_existing_resource(self): """Send HTTP PATCH for an existing resource (should be updated).""" @@ -160,9 +160,9 @@ def test_patch_existing_resource(self): assert response.status_code == 204 response = self.get_response('/artists/275', 200) assert json.loads( - response.data.decode('utf-8'))[u'Name'] == u'Jeff Knupp' + response.get_data(as_text=True))[u'Name'] == u'Jeff Knupp' assert json.loads( - response.data.decode('utf-8'))[u'ArtistId'] == 275 + response.get_data(as_text=True))[u'ArtistId'] == 275 def test_delete_resource(self): """Test DELETEing a resource.""" @@ -197,9 +197,9 @@ def test_put_resource(self): assert response.status_code == 204 response = self.get_response('/tracks/1', 200) assert json.loads( - response.data.decode('utf-8'))[u'Name'] == u'Some New Album' + response.get_data(as_text=True))[u'Name'] == u'Some New Album' assert json.loads( - response.data.decode('utf-8'))[u'Composer'] is None + response.get_data(as_text=True))[u'Composer'] is None def test_put_unknown_resource(self): """Test HTTP PUTing a resource that doesn't exist. Should give 404.""" @@ -233,7 +233,7 @@ class TestSandmanUserDefinitions(TestSandmanBase): def test_user_defined_endpoint(self): """Make sure user-defined endpoint exists.""" response = self.get_response('/styles', 200) - assert len(json.loads(response.data)[u'resources']) == 25 + assert len(json.loads(response.get_data(as_text=True))[u'resources']) == 25 def test_user_validation_reject(self): """Test user-defined validation which on request which should be @@ -288,7 +288,7 @@ def test_unsupported_collection_method(self): def test_pagination(self): """Can we get paginated results?""" response = self.get_response('/artists', 200, params={'page': 2}) - assert len(json.loads(response.data)['resources']) == 20 + assert len(json.loads(response.get_data(as_text=True))['resources']) == 20 class TestSandmanContentTypes(TestSandmanBase): """Sandman tests related to content types""" @@ -317,18 +317,18 @@ def test_get_html_non_existant_resource(self): def test_get_html_collection(self): """Test getting HTML version of a collection rather than JSON.""" - response = self.app.get('/artists', + response = self.get_response('/artists', 200, headers={'Accept': 'text/html'}) assert self.is_html_response(response) - assert 'Aerosmith' in response.data + assert 'Aerosmith' in response.get_data(as_text=True) def test_get_json(self): """Test explicitly getting the JSON version of a resource.""" response = self.get_response('/artists', 200, headers={'Accept': 'application/json'}) - assert len(json.loads(response.data)[u'resources']) == 275 + assert len(json.loads(response.get_data(as_text=True))[u'resources']) == 275 def test_get_unknown_url(self): """Test sending a GET request to a URL that would match the @@ -363,7 +363,7 @@ def test_post_html_response(self): headers={'Accept': 'text/html'}, data={u'Name': u'Jeff Knupp'}) assert response.status_code == 201 - assert 'Jeff Knupp' in response.data + assert 'Jeff Knupp' in str(response.get_data(as_text=True)) def test_post_html_with_charset(self): """Test POSTing a resource with a Content-Type that specifies a @@ -374,7 +374,7 @@ def test_post_html_with_charset(self): headers={'Accept': 'text/html'}, data={u'Name': u'Jeff Knupp'}) assert response.status_code == 201 - assert 'Jeff Knupp' in response.data + assert 'Jeff Knupp' in str(response.get_data(as_text=True)) def test_post_no_json_data(self): """Test POSTing a resource with no JSON data.""" @@ -432,7 +432,7 @@ def test_admin_collection_view(self): # If related tables are being loaded correctly, Tracks will have a # Mediatype column, at least one of which has the value 'MPEG audio # file'. - assert 'MPEG audio file' in response.data + assert 'MPEG audio file' in str(response.get_data(as_text=True)) def test_admin_default_str_repr(self): """Ensure default ``__str__`` implementations works in the admin.""" @@ -441,7 +441,7 @@ def test_admin_default_str_repr(self): # If related tables are being loaded correctly, Tracks will have a # Genre column, but should display the GenreId and not the name ('Jazz' # is the genre for many results on the third page - assert 'Jazz' not in response.data + assert 'Jazz' not in str(response.get_data(as_text=True)) #pylint: disable=invalid-name def test_admin_default_str_repr_different_table_class_name(self): @@ -450,4 +450,4 @@ def test_admin_default_str_repr_different_table_class_name(self): table name).""" response = self.get_response('/admin/styleview/', 200) - assert 'Genre' not in response.data + assert 'Genre' not in str(response.get_data(as_text=True))