Skip to content

Commit e861767

Browse files
Refactor find_one_await_thumbnail invocation
1 parent d16f91b commit e861767

File tree

2 files changed

+54
-50
lines changed

2 files changed

+54
-50
lines changed

tests/base.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import random
55
import re
6+
import time
67
import unittest
78

89
from . import mock
@@ -26,6 +27,11 @@ def skip(f):
2627
return lambda self: None
2728

2829

30+
THUMBNAIL_MAX_ATTEMPTS = 30
31+
THUMBNAIL_RETRY_INTERVAL = 10
32+
TRANSIENT_IMAGE_PATH = "images/status/transient"
33+
34+
2935
class TestBase(unittest.TestCase):
3036
'''Base class for tests.
3137
@@ -366,6 +372,20 @@ def gen_entity(self, entity_type, **kwargs):
366372
rv = self.sg.delete(entity_type, entity["id"])
367373
assert rv == True
368374

375+
def find_one_await_thumbnail(self, entity_type, filters, fields=["image"], thumbnail_field_name="image", **kwargs):
376+
attempts = 0
377+
result = self.sg.find_one(entity_type, filters, fields=fields, **kwargs)
378+
while attempts < THUMBNAIL_MAX_ATTEMPTS:
379+
result = self.sg.find_one(entity_type, filters, fields=fields, **kwargs)
380+
if TRANSIENT_IMAGE_PATH in result.get(thumbnail_field_name, ""):
381+
return result
382+
383+
time.sleep(THUMBNAIL_RETRY_INTERVAL)
384+
attempts += 1
385+
else:
386+
if self.config.jenkins:
387+
self.skipTest("Jenkins test timed out waiting for thumbnail")
388+
369389

370390
class HumanUserAuthLiveTestBase(LiveTestBase):
371391
'''

tests/test_api.py

Lines changed: 34 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@
4040

4141
from . import base
4242

43-
THUMBNAIL_MAX_ATTEMPTS = 30
44-
THUMBNAIL_RETRY_INTERAL = 10
45-
TRANSIENT_IMAGE_PATH = "images/status/transient"
46-
4743

4844
class TestShotgunApi(base.LiveTestBase):
4945
def setUp(self):
@@ -383,11 +379,10 @@ def test_upload_thumbnail_in_create(self):
383379
data = {'image': path, 'code': 'Test Version',
384380
'project': self.project}
385381
new_version = self.sg.create("Version", data, return_fields=['image'])
386-
new_version = find_one_await_thumbnail(
387-
self.sg,
382+
new_version = self.find_one_await_thumbnail(
388383
"Version",
389384
[["id", "is", new_version["id"]]],
390-
fields=["image", "project", "type", "id"]
385+
fields=["image", "project", "type", "id"],
391386
)
392387

393388
self.assertTrue(new_version is not None)
@@ -434,7 +429,9 @@ def test_upload_thumbnail_for_version(self):
434429

435430
# check result on version
436431
version_with_thumbnail = self.sg.find_one('Version', [['id', 'is', self.version['id']]])
437-
version_with_thumbnail = find_one_await_thumbnail(self.sg, 'Version', [['id', 'is', self.version['id']]])
432+
version_with_thumbnail = self.find_one_await_thumbnail(
433+
"Version", [["id", "is", self.version["id"]]]
434+
)
438435

439436
self.assertEqual(version_with_thumbnail.get('type'), 'Version')
440437
self.assertEqual(version_with_thumbnail.get('id'), self.version['id'])
@@ -461,7 +458,9 @@ def test_upload_thumbnail_for_task(self):
461458

462459
# check result on version
463460
task_with_thumbnail = self.sg.find_one('Task', [['id', 'is', self.task['id']]])
464-
task_with_thumbnail = find_one_await_thumbnail(self.sg, 'Task', [['id', 'is', self.task['id']]])
461+
task_with_thumbnail = self.find_one_await_thumbnail(
462+
"Task", [["id", "is", self.task["id"]]]
463+
)
465464

466465
self.assertEqual(task_with_thumbnail.get('type'), 'Task')
467466
self.assertEqual(task_with_thumbnail.get('id'), self.task['id'])
@@ -557,12 +556,11 @@ def test_linked_thumbnail_url(self):
557556

558557
thumb_id = self.sg.upload_thumbnail("Project", self.version['project']['id'], path)
559558

560-
response_version_with_project = find_one_await_thumbnail(
561-
self.sg,
559+
response_version_with_project = self.find_one_await_thumbnail(
562560
"Version",
563561
[["id", "is", self.version["id"]]],
564562
fields=["id", "code", "project.Project.image"],
565-
thumbnail_field_name="project.Project.image"
563+
thumbnail_field_name="project.Project.image",
566564
)
567565

568566
if self.sg.server_caps.version and self.sg.server_caps.version >= (3, 3, 0):
@@ -597,30 +595,28 @@ def share_thumbnail_retry(*args, **kwargs):
597595
# the thumbnail to finish processing.
598596
thumbnail_id = None
599597
attempts = 0
600-
while attempts < THUMBNAIL_MAX_ATTEMPTS and thumbnail_id is None:
598+
while attempts < base.THUMBNAIL_MAX_ATTEMPTS and thumbnail_id is None:
601599
try:
602600
thumbnail_id = self.sg.share_thumbnail(*args, **kwargs)
603601
attempts += 1
604602
except shotgun_api3.ShotgunError:
605-
time.sleep(THUMBNAIL_RETRY_INTERAL)
603+
time.sleep(base.THUMBNAIL_RETRY_INTERVAL)
606604
return thumbnail_id
607605

608606
this_dir, _ = os.path.split(__file__)
609607
path = os.path.abspath(os.path.expanduser(os.path.join(this_dir, "sg_logo.jpg")))
610608

611609
# upload thumbnail to first entity and share it with the rest
612610
share_thumbnail_retry([self.version, self.shot], thumbnail_path=path)
613-
response_version_thumbnail = find_one_await_thumbnail(
614-
self.sg,
611+
response_version_thumbnail = self.find_one_await_thumbnail(
615612
'Version',
616613
[['id', 'is', self.version['id']]],
617-
fields=['id', 'code', 'image']
614+
fields=['id', 'code', 'image'],
618615
)
619-
response_shot_thumbnail = find_one_await_thumbnail(
620-
self.sg,
616+
response_shot_thumbnail = self.find_one_await_thumbnail(
621617
'Shot',
622618
[['id', 'is', self.shot['id']]],
623-
fields=['id', 'code', 'image']
619+
fields=['id', 'code', 'image'],
624620
)
625621

626622
shot_url = urllib.parse.urlparse(response_shot_thumbnail.get('image'))
@@ -632,23 +628,20 @@ def share_thumbnail_retry(*args, **kwargs):
632628
# share thumbnail from source entity with entities
633629
self.sg.upload_thumbnail("Version", self.version['id'], path)
634630
share_thumbnail_retry([self.asset, self.shot], source_entity=self.version)
635-
response_version_thumbnail = find_one_await_thumbnail(
636-
self.sg,
631+
response_version_thumbnail = self.find_one_await_thumbnail(
637632
'Version',
638633
[['id', 'is', self.version['id']]],
639-
fields=['id', 'code', 'image']
634+
fields=['id', 'code', 'image'],
640635
)
641-
response_shot_thumbnail = find_one_await_thumbnail(
642-
self.sg,
636+
response_shot_thumbnail = self.find_one_await_thumbnail(
643637
'Shot',
644638
[['id', 'is', self.shot['id']]],
645-
fields=['id', 'code', 'image']
639+
fields=['id', 'code', 'image'],
646640
)
647-
response_asset_thumbnail = find_one_await_thumbnail(
648-
self.sg,
641+
response_asset_thumbnail = self.find_one_await_thumbnail(
649642
'Asset',
650643
[['id', 'is', self.asset['id']]],
651-
fields=['id', 'code', 'image']
644+
fields=['id', 'code', 'image'],
652645
)
653646

654647
shot_url = urllib.parse.urlparse(response_shot_thumbnail.get('image'))
@@ -856,7 +849,6 @@ def test_work_schedule(self):
856849
self.assertRaises(shotgun_api3.ShotgunError, self.sg.work_schedule_read,
857850
start_date_obj, end_date_obj, project, user)
858851

859-
860852
resp = self.sg.work_schedule_update('2012-01-02', False, 'Studio Holiday')
861853
expected = {
862854
'date': '2012-01-02',
@@ -1007,7 +999,7 @@ def test_set_date(self):
1007999

10081000
def test_set_date_time(self):
10091001
if self.config.jenkins:
1010-
self.skipTest("Skipping test on Jenkins. locked_until not updating.")
1002+
self.skipTest("Jenkins. locked_until not updating.")
10111003
entity = 'HumanUser'
10121004
entity_id = self.human_user['id']
10131005
field_name = 'locked_until'
@@ -1202,7 +1194,7 @@ def setUp(self):
12021194

12031195
def test_convert_to_utc(self):
12041196
if self.config.jenkins:
1205-
self.skipTest("Skipping test on Jenkins. locked_until not updating.")
1197+
self.skipTest("Jenkins. locked_until not updating.")
12061198
sg_utc = shotgun_api3.Shotgun(self.config.server_url,
12071199
http_proxy=self.config.http_proxy,
12081200
convert_datetimes_to_utc=True,
@@ -1212,7 +1204,7 @@ def test_convert_to_utc(self):
12121204

12131205
def test_no_convert_to_utc(self):
12141206
if self.config.jenkins:
1215-
self.skipTest("Skipping test on Jenkins. locked_until not updating.")
1207+
self.skipTest("Jenkins. locked_until not updating.")
12161208
sg_no_utc = shotgun_api3.Shotgun(self.config.server_url,
12171209
http_proxy=self.config.http_proxy,
12181210
convert_datetimes_to_utc=False,
@@ -2164,7 +2156,7 @@ def test_human_user_sudo_auth_fails(self):
21642156
Request fails on server because user has no permission to Sudo.
21652157
"""
21662158
if self.config.jenkins:
2167-
self.skipTest("Skipping test on Jenkins. locked_until not updating.")
2159+
self.skipTest("Jenkins. locked_until not updating.")
21682160

21692161
if not self.sg.server_caps.version or self.sg.server_caps.version < (5, 3, 12):
21702162
return
@@ -2221,7 +2213,10 @@ def test_humanuser_upload_thumbnail_for_version(self):
22212213
self.assertTrue(isinstance(thumb_id, int))
22222214

22232215
# check result on version
2224-
version_with_thumbnail = find_one_await_thumbnail(self.sg, 'Version', [['id', 'is', self.version['id']]])
2216+
version_with_thumbnail = self.find_one_await_thumbnail(
2217+
"Version",
2218+
[["id", "is", self.version["id"]]],
2219+
)
22252220

22262221
self.assertEqual(version_with_thumbnail.get('type'), 'Version')
22272222
self.assertEqual(version_with_thumbnail.get('id'), self.version['id'])
@@ -2278,7 +2273,10 @@ def test_humanuser_upload_thumbnail_for_version(self):
22782273
self.assertTrue(isinstance(thumb_id, int))
22792274

22802275
# check result on version
2281-
version_with_thumbnail = find_one_await_thumbnail(self.sg, 'Version', [['id', 'is', self.version['id']]])
2276+
version_with_thumbnail = self.find_one_await_thumbnail(
2277+
"Version",
2278+
[["id", "is", self.version["id"]]],
2279+
)
22822280

22832281
self.assertEqual(version_with_thumbnail.get('type'), 'Version')
22842282
self.assertEqual(version_with_thumbnail.get('id'), self.version['id'])
@@ -3040,19 +3038,5 @@ def _get_path(url):
30403038
return url.path
30413039

30423040

3043-
def find_one_await_thumbnail(sg, entity_type, filters, fields=["image"], thumbnail_field_name="image", **kwargs):
3044-
attempts = 0
3045-
result = sg.find_one(entity_type, filters, fields=fields, **kwargs)
3046-
while (
3047-
attempts < THUMBNAIL_MAX_ATTEMPTS
3048-
and result[thumbnail_field_name]
3049-
and TRANSIENT_IMAGE_PATH in result[thumbnail_field_name]
3050-
):
3051-
time.sleep(THUMBNAIL_RETRY_INTERAL)
3052-
result = sg.find_one(entity_type, filters, fields=fields, **kwargs)
3053-
attempts += 1
3054-
return result
3055-
3056-
30573041
if __name__ == '__main__':
30583042
unittest.main()

0 commit comments

Comments
 (0)