Skip to content

Ticket/28817 fix api tests for new features #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 53 additions & 33 deletions shotgun_api3/shotgun.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,23 +137,46 @@ def __init__(self, host, meta):
self._ensure_json_supported()


def _ensure_json_supported(self):
"""Checks the server version supports the JSON api, raises an
def _ensure_support(self, feature, raise_hell=True):
"""Checks the server version supports a given feature, raises an
exception if it does not.

:raises ShotgunError: The current server version does not support json
:param feature: dict supported version and human label { 'version': (int, int, int), 'label': str }

:raises ShotgunError: The current server version does not [feature]
"""
if not self.version or self.version < (2, 4, 0):
raise ShotgunError("JSON API requires server version 2.4 or "\
"higher, server is %s" % (self.version,))

if not self.version or self.version < feature['version']:
if raise_hell:
raise ShotgunError(
"%s requires server version %s or higher, "\
"server is %s" % (feature['label'], _version_str(feature['version']), _version_str(self.version))
)
return False
else:
return True


def _ensure_json_supported(self):
"""Wrapper for ensure_support"""
self._ensure_support({
'version': (2, 4, 0),
'label': 'JSON API'
})

def ensure_include_archived_projects(self):
"""Checks the server version support include_archived_projects parameter
to find.
"""
if not self.version or self.version < (5, 3, 14):
raise ShotgunError("The include_archived_projects flag requires server version 5.3.14 or "\
"higher, server is %s" % (self.version,))
"""Wrapper for ensure_support"""
self._ensure_support({
'version': (5, 3, 14),
'label': 'include_archived_projects parameter'
})

def ensure_per_project_customization(self):
"""Wrapper for ensure_support"""
return self._ensure_support({
'version': (5, 4, 4),
'label': 'project parameter'
}, True)


def __str__(self):
Expand Down Expand Up @@ -627,6 +650,15 @@ def _construct_read_parameters(self,
params['sorts'] = sort_list
return params


def _add_project_param(self, params, project_entity):

if project_entity and self.server_caps.ensure_per_project_customization():
params["project"] = project_entity

return params


def summarize(self,
entity_type,
filters,
Expand Down Expand Up @@ -1005,12 +1037,7 @@ def schema_entity_read(self, project_entity=None):

params = {}

if project_entity:
if not self.server_caps.version or self.server_caps.version < (5, 4, 4):
raise ShotgunError("Per project schema operations require server "\
"version 5.4.4 or higher, server is %s" % (self.server_caps.version,))
else:
params["project"] = project_entity
params = self._add_project_param(params, project_entity)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@manneohrstrom, I'd been tasked w/ fixing this, and only saw much later that you'd fixed the project aspect of this...
So this is the conflict resolution, I hope you don't mind I kept "mine", since I'd already cherry-picked my ensure_... changes over from #79...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries at all - this was just a quick fix to get the tests working on older servers. Looks like you have done a much more holistic pass, trying to clean things up properly (which is totally awesome!). So please ignore/discard any of my changes in favour of your proper changes. This was just a quick fix to make sure we can release the API with all tests passing.


if params:
return self._call_rpc("schema_entity_read", params)
Expand All @@ -1029,13 +1056,8 @@ def schema_read(self, project_entity=None):

params = {}

if project_entity:
if not self.server_caps.version or self.server_caps.version < (5, 4, 4):
raise ShotgunError("Per project schema operations require server "\
"version 5.4.4 or higher, server is %s" % (self.server_caps.version,))
else:
params["project"] = project_entity

params = self._add_project_param(params, project_entity)

if params:
return self._call_rpc("schema_read", params)
else:
Expand All @@ -1062,15 +1084,11 @@ def schema_field_read(self, entity_type, field_name=None, project_entity=None):
params = {
"type": entity_type,
}

if field_name:
params["field_name"] = field_name

if project_entity:
if not self.server_caps.version or self.server_caps.version < (5, 4, 4):
raise ShotgunError("Per project schema operations require server "\
"version 5.4.4 or higher, server is %s" % (self.server_caps.version,))
else:
params["project"] = project_entity

params = self._add_project_param(params, project_entity)

return self._call_rpc("schema_field_read", params)

Expand Down Expand Up @@ -2208,4 +2226,6 @@ def _translate_filters_simple(sg_filter):

return condition


def _version_str(version):
"""Converts a tuple of int's to a '.' separated str"""
return '.'.join(map(str, version))
78 changes: 42 additions & 36 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1567,57 +1567,63 @@ class TestSessionTokenAuth(base.SessionTokenAuthLiveTestBase):
"""
Testing the session token based authentication method
"""

def test_humanuser_find(self):
"""Called find, find_one for known entities as session token based user"""
filters = []
filters.append(['project', 'is', self.project])
filters.append(['id', 'is', self.version['id']])

fields = ['id']
if self.sg.server_caps.version >= (5, 4, 1):

versions = self.sg.find("Version", filters, fields=fields)
filters = []
filters.append(['project', 'is', self.project])
filters.append(['id', 'is', self.version['id']])

self.assertTrue(isinstance(versions, list))
version = versions[0]
self.assertEqual("Version", version["type"])
self.assertEqual(self.version['id'], version["id"])
fields = ['id']

version = self.sg.find_one("Version", filters, fields=fields)
self.assertEqual("Version", version["type"])
self.assertEqual(self.version['id'], version["id"])
versions = self.sg.find("Version", filters, fields=fields)

self.assertTrue(isinstance(versions, list))
version = versions[0]
self.assertEqual("Version", version["type"])
self.assertEqual(self.version['id'], version["id"])

version = self.sg.find_one("Version", filters, fields=fields)
self.assertEqual("Version", version["type"])
self.assertEqual(self.version['id'], version["id"])

def test_humanuser_upload_thumbnail_for_version(self):
"""simple upload thumbnail for version test as session based token user."""
this_dir, _ = os.path.split(__file__)
path = os.path.abspath(os.path.expanduser(
os.path.join(this_dir,"sg_logo.jpg")))
size = os.stat(path).st_size

# upload thumbnail
thumb_id = self.sg.upload_thumbnail("Version",
self.version['id'], path)
self.assertTrue(isinstance(thumb_id, int))
if self.sg.server_caps.version >= (5, 4, 1):

# check result on version
version_with_thumbnail = self.sg.find_one('Version',
[['id', 'is', self.version['id']]],
fields=['image'])
this_dir, _ = os.path.split(__file__)
path = os.path.abspath(os.path.expanduser(
os.path.join(this_dir,"sg_logo.jpg")))
size = os.stat(path).st_size

self.assertEqual(version_with_thumbnail.get('type'), 'Version')
self.assertEqual(version_with_thumbnail.get('id'), self.version['id'])
# upload thumbnail
thumb_id = self.sg.upload_thumbnail("Version",
self.version['id'], path)
self.assertTrue(isinstance(thumb_id, int))

# check result on version
version_with_thumbnail = self.sg.find_one('Version',
[['id', 'is', self.version['id']]],
fields=['image'])

h = Http(".cache")
thumb_resp, content = h.request(version_with_thumbnail.get('image'), "GET")
self.assertEqual(thumb_resp['status'], '200')
self.assertEqual(thumb_resp['content-type'], 'image/jpeg')
self.assertEqual(version_with_thumbnail.get('type'), 'Version')
self.assertEqual(version_with_thumbnail.get('id'), self.version['id'])

# clear thumbnail
response_clear_thumbnail = self.sg.update("Version",
self.version['id'], {'image':None})
expected_clear_thumbnail = {'id': self.version['id'], 'image': None, 'type': 'Version'}
self.assertEqual(expected_clear_thumbnail, response_clear_thumbnail)

h = Http(".cache")
thumb_resp, content = h.request(version_with_thumbnail.get('image'), "GET")
self.assertEqual(thumb_resp['status'], '200')
self.assertEqual(thumb_resp['content-type'], 'image/jpeg')

# clear thumbnail
response_clear_thumbnail = self.sg.update("Version",
self.version['id'], {'image':None})
expected_clear_thumbnail = {'id': self.version['id'], 'image': None, 'type': 'Version'}
self.assertEqual(expected_clear_thumbnail, response_clear_thumbnail)


class TestProjectLastAccessedByCurrentUser(base.LiveTestBase):
Expand Down
23 changes: 13 additions & 10 deletions tests/test_api_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,35 +105,38 @@ def test_schema(self):
self.assertTrue(ret_val)

def test_schema_with_project(self):
"""Called schema functions"""
"""Called schema functions with project"""

project_entity = {'type': 'Project', 'id': 0}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@manneohrstrom, I kept this definition outside of the if/else, to DRY up the assertRaises.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks much nicer than my code!


if not self.sg.server_caps.version or self.sg.server_caps.version < (5, 4, 4):

# server does not support this!
self.assertRaises(shotgun_api3.ShotgunError, self.sg.schema_entity_read, {'type': 'Project', 'id': 0})
self.assertRaises(shotgun_api3.ShotgunError, self.sg.schema_read, {'type': 'Project', 'id': 0})
self.assertRaises(shotgun_api3.ShotgunError, self.sg.schema_field_read, 'Version', None, {'type': 'Project', 'id': 0})
self.assertRaises(shotgun_api3.ShotgunError, self.sg.schema_field_read, 'Version', 'user', {'type': 'Project', 'id': 0})
self.assertRaises(shotgun_api3.ShotgunError, self.sg.schema_entity_read, project_entity)
self.assertRaises(shotgun_api3.ShotgunError, self.sg.schema_read, project_entity)
self.assertRaises(shotgun_api3.ShotgunError, self.sg.schema_field_read, 'Version', None, project_entity)
self.assertRaises(shotgun_api3.ShotgunError, self.sg.schema_field_read, 'Version', 'user', project_entity)

else:
project_entity = {'type': 'Project', 'id': 0}

schema = self.sg.schema_entity_read(project_entity)
self.assertTrue(schema, dict)
self.assertTrue(len(schema) > 0)
self.assertTrue('Project' in schema)
self.assertTrue('visible' in schema['Project'])

schema = self.sg.schema_read(project_entity)
self.assertTrue(schema, dict)
self.assertTrue(len(schema) > 0)
self.assertTrue('Version' in schema)
self.assertFalse('visible' in schema.keys())

schema = self.sg.schema_field_read('Version', project_entity=project_entity)
self.assertTrue(schema, dict)
self.assertTrue(len(schema) > 0)
self.assertTrue('user' in schema)
self.assertTrue('visible' in schema['user'])

schema = self.sg.schema_field_read('Version', 'user', project_entity)
self.assertTrue(schema, dict)
self.assertTrue(len(schema) > 0)
Expand Down