Skip to content

Commit c1d5967

Browse files
committed
Merge pull request #82 from shotgunsoftware/ticket/28817_fix_api_tests_for_new_features
Ticket/28817 fix api tests for new features
2 parents a64c55b + 7963a06 commit c1d5967

File tree

3 files changed

+108
-79
lines changed

3 files changed

+108
-79
lines changed

shotgun_api3/shotgun.py

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -137,23 +137,46 @@ def __init__(self, host, meta):
137137
self._ensure_json_supported()
138138

139139

140-
def _ensure_json_supported(self):
141-
"""Checks the server version supports the JSON api, raises an
140+
def _ensure_support(self, feature, raise_hell=True):
141+
"""Checks the server version supports a given feature, raises an
142142
exception if it does not.
143143
144-
:raises ShotgunError: The current server version does not support json
144+
:param feature: dict supported version and human label { 'version': (int, int, int), 'label': str }
145+
146+
:raises ShotgunError: The current server version does not [feature]
145147
"""
146-
if not self.version or self.version < (2, 4, 0):
147-
raise ShotgunError("JSON API requires server version 2.4 or "\
148-
"higher, server is %s" % (self.version,))
148+
149+
if not self.version or self.version < feature['version']:
150+
if raise_hell:
151+
raise ShotgunError(
152+
"%s requires server version %s or higher, "\
153+
"server is %s" % (feature['label'], _version_str(feature['version']), _version_str(self.version))
154+
)
155+
return False
156+
else:
157+
return True
158+
159+
160+
def _ensure_json_supported(self):
161+
"""Wrapper for ensure_support"""
162+
self._ensure_support({
163+
'version': (2, 4, 0),
164+
'label': 'JSON API'
165+
})
149166

150167
def ensure_include_archived_projects(self):
151-
"""Checks the server version support include_archived_projects parameter
152-
to find.
153-
"""
154-
if not self.version or self.version < (5, 3, 14):
155-
raise ShotgunError("The include_archived_projects flag requires server version 5.3.14 or "\
156-
"higher, server is %s" % (self.version,))
168+
"""Wrapper for ensure_support"""
169+
self._ensure_support({
170+
'version': (5, 3, 14),
171+
'label': 'include_archived_projects parameter'
172+
})
173+
174+
def ensure_per_project_customization(self):
175+
"""Wrapper for ensure_support"""
176+
return self._ensure_support({
177+
'version': (5, 4, 4),
178+
'label': 'project parameter'
179+
}, True)
157180

158181

159182
def __str__(self):
@@ -627,6 +650,15 @@ def _construct_read_parameters(self,
627650
params['sorts'] = sort_list
628651
return params
629652

653+
654+
def _add_project_param(self, params, project_entity):
655+
656+
if project_entity and self.server_caps.ensure_per_project_customization():
657+
params["project"] = project_entity
658+
659+
return params
660+
661+
630662
def summarize(self,
631663
entity_type,
632664
filters,
@@ -1005,12 +1037,7 @@ def schema_entity_read(self, project_entity=None):
10051037

10061038
params = {}
10071039

1008-
if project_entity:
1009-
if not self.server_caps.version or self.server_caps.version < (5, 4, 4):
1010-
raise ShotgunError("Per project schema operations require server "\
1011-
"version 5.4.4 or higher, server is %s" % (self.server_caps.version,))
1012-
else:
1013-
params["project"] = project_entity
1040+
params = self._add_project_param(params, project_entity)
10141041

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

10301057
params = {}
10311058

1032-
if project_entity:
1033-
if not self.server_caps.version or self.server_caps.version < (5, 4, 4):
1034-
raise ShotgunError("Per project schema operations require server "\
1035-
"version 5.4.4 or higher, server is %s" % (self.server_caps.version,))
1036-
else:
1037-
params["project"] = project_entity
1038-
1059+
params = self._add_project_param(params, project_entity)
1060+
10391061
if params:
10401062
return self._call_rpc("schema_read", params)
10411063
else:
@@ -1062,15 +1084,11 @@ def schema_field_read(self, entity_type, field_name=None, project_entity=None):
10621084
params = {
10631085
"type": entity_type,
10641086
}
1087+
10651088
if field_name:
10661089
params["field_name"] = field_name
1067-
1068-
if project_entity:
1069-
if not self.server_caps.version or self.server_caps.version < (5, 4, 4):
1070-
raise ShotgunError("Per project schema operations require server "\
1071-
"version 5.4.4 or higher, server is %s" % (self.server_caps.version,))
1072-
else:
1073-
params["project"] = project_entity
1090+
1091+
params = self._add_project_param(params, project_entity)
10741092

10751093
return self._call_rpc("schema_field_read", params)
10761094

@@ -2208,4 +2226,6 @@ def _translate_filters_simple(sg_filter):
22082226

22092227
return condition
22102228

2211-
2229+
def _version_str(version):
2230+
"""Converts a tuple of int's to a '.' separated str"""
2231+
return '.'.join(map(str, version))

tests/test_api.py

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,57 +1567,63 @@ class TestSessionTokenAuth(base.SessionTokenAuthLiveTestBase):
15671567
"""
15681568
Testing the session token based authentication method
15691569
"""
1570-
1570+
15711571
def test_humanuser_find(self):
15721572
"""Called find, find_one for known entities as session token based user"""
1573-
filters = []
1574-
filters.append(['project', 'is', self.project])
1575-
filters.append(['id', 'is', self.version['id']])
15761573

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

1579-
versions = self.sg.find("Version", filters, fields=fields)
1576+
filters = []
1577+
filters.append(['project', 'is', self.project])
1578+
filters.append(['id', 'is', self.version['id']])
15801579

1581-
self.assertTrue(isinstance(versions, list))
1582-
version = versions[0]
1583-
self.assertEqual("Version", version["type"])
1584-
self.assertEqual(self.version['id'], version["id"])
1580+
fields = ['id']
15851581

1586-
version = self.sg.find_one("Version", filters, fields=fields)
1587-
self.assertEqual("Version", version["type"])
1588-
self.assertEqual(self.version['id'], version["id"])
1582+
versions = self.sg.find("Version", filters, fields=fields)
1583+
1584+
self.assertTrue(isinstance(versions, list))
1585+
version = versions[0]
1586+
self.assertEqual("Version", version["type"])
1587+
self.assertEqual(self.version['id'], version["id"])
1588+
1589+
version = self.sg.find_one("Version", filters, fields=fields)
1590+
self.assertEqual("Version", version["type"])
1591+
self.assertEqual(self.version['id'], version["id"])
15891592

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

1597-
# upload thumbnail
1598-
thumb_id = self.sg.upload_thumbnail("Version",
1599-
self.version['id'], path)
1600-
self.assertTrue(isinstance(thumb_id, int))
1596+
if self.sg.server_caps.version >= (5, 4, 1):
16011597

1602-
# check result on version
1603-
version_with_thumbnail = self.sg.find_one('Version',
1604-
[['id', 'is', self.version['id']]],
1605-
fields=['image'])
1598+
this_dir, _ = os.path.split(__file__)
1599+
path = os.path.abspath(os.path.expanduser(
1600+
os.path.join(this_dir,"sg_logo.jpg")))
1601+
size = os.stat(path).st_size
16061602

1607-
self.assertEqual(version_with_thumbnail.get('type'), 'Version')
1608-
self.assertEqual(version_with_thumbnail.get('id'), self.version['id'])
1603+
# upload thumbnail
1604+
thumb_id = self.sg.upload_thumbnail("Version",
1605+
self.version['id'], path)
1606+
self.assertTrue(isinstance(thumb_id, int))
16091607

1608+
# check result on version
1609+
version_with_thumbnail = self.sg.find_one('Version',
1610+
[['id', 'is', self.version['id']]],
1611+
fields=['image'])
16101612

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

1616-
# clear thumbnail
1617-
response_clear_thumbnail = self.sg.update("Version",
1618-
self.version['id'], {'image':None})
1619-
expected_clear_thumbnail = {'id': self.version['id'], 'image': None, 'type': 'Version'}
1620-
self.assertEqual(expected_clear_thumbnail, response_clear_thumbnail)
1616+
1617+
h = Http(".cache")
1618+
thumb_resp, content = h.request(version_with_thumbnail.get('image'), "GET")
1619+
self.assertEqual(thumb_resp['status'], '200')
1620+
self.assertEqual(thumb_resp['content-type'], 'image/jpeg')
1621+
1622+
# clear thumbnail
1623+
response_clear_thumbnail = self.sg.update("Version",
1624+
self.version['id'], {'image':None})
1625+
expected_clear_thumbnail = {'id': self.version['id'], 'image': None, 'type': 'Version'}
1626+
self.assertEqual(expected_clear_thumbnail, response_clear_thumbnail)
16211627

16221628

16231629
class TestProjectLastAccessedByCurrentUser(base.LiveTestBase):

tests/test_api_long.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,35 +105,38 @@ def test_schema(self):
105105
self.assertTrue(ret_val)
106106

107107
def test_schema_with_project(self):
108-
"""Called schema functions"""
108+
"""Called schema functions with project"""
109+
110+
project_entity = {'type': 'Project', 'id': 0}
109111

110112
if not self.sg.server_caps.version or self.sg.server_caps.version < (5, 4, 4):
113+
111114
# server does not support this!
112-
self.assertRaises(shotgun_api3.ShotgunError, self.sg.schema_entity_read, {'type': 'Project', 'id': 0})
113-
self.assertRaises(shotgun_api3.ShotgunError, self.sg.schema_read, {'type': 'Project', 'id': 0})
114-
self.assertRaises(shotgun_api3.ShotgunError, self.sg.schema_field_read, 'Version', None, {'type': 'Project', 'id': 0})
115-
self.assertRaises(shotgun_api3.ShotgunError, self.sg.schema_field_read, 'Version', 'user', {'type': 'Project', 'id': 0})
116-
115+
self.assertRaises(shotgun_api3.ShotgunError, self.sg.schema_entity_read, project_entity)
116+
self.assertRaises(shotgun_api3.ShotgunError, self.sg.schema_read, project_entity)
117+
self.assertRaises(shotgun_api3.ShotgunError, self.sg.schema_field_read, 'Version', None, project_entity)
118+
self.assertRaises(shotgun_api3.ShotgunError, self.sg.schema_field_read, 'Version', 'user', project_entity)
119+
117120
else:
118-
project_entity = {'type': 'Project', 'id': 0}
121+
119122
schema = self.sg.schema_entity_read(project_entity)
120123
self.assertTrue(schema, dict)
121124
self.assertTrue(len(schema) > 0)
122125
self.assertTrue('Project' in schema)
123126
self.assertTrue('visible' in schema['Project'])
124-
127+
125128
schema = self.sg.schema_read(project_entity)
126129
self.assertTrue(schema, dict)
127130
self.assertTrue(len(schema) > 0)
128131
self.assertTrue('Version' in schema)
129132
self.assertFalse('visible' in schema.keys())
130-
133+
131134
schema = self.sg.schema_field_read('Version', project_entity=project_entity)
132135
self.assertTrue(schema, dict)
133136
self.assertTrue(len(schema) > 0)
134137
self.assertTrue('user' in schema)
135138
self.assertTrue('visible' in schema['user'])
136-
139+
137140
schema = self.sg.schema_field_read('Version', 'user', project_entity)
138141
self.assertTrue(schema, dict)
139142
self.assertTrue(len(schema) > 0)

0 commit comments

Comments
 (0)