Skip to content

Commit aa6a948

Browse files
authored
SG-31925 Fixup follow flaky tests (#304)
Fix race condition
1 parent cad4735 commit aa6a948

File tree

2 files changed

+104
-54
lines changed

2 files changed

+104
-54
lines changed

tests/base.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Base class for ShotGrid API tests."""
2+
import contextlib
23
import os
4+
import random
35
import re
46
import unittest
57

@@ -339,6 +341,31 @@ def _setup_db(cls, config, sg):
339341
'linux_path': 'nowhere'}
340342
cls.local_storage = _find_or_create_entity(sg, 'LocalStorage', data, keys)
341343

344+
@contextlib.contextmanager
345+
def gen_entity(self, entity_type, **kwargs):
346+
# Helper creator
347+
if entity_type == "HumanUser":
348+
if "login" not in kwargs:
349+
kwargs["login"] = "test-python-api-{rnd}"
350+
351+
if "sg_status_list" not in kwargs:
352+
kwargs["sg_status_list"] = "dis"
353+
354+
if "password_proxy" not in kwargs:
355+
kwargs["password_proxy"] = self.config.human_password
356+
357+
item_rnd = random.randrange(100,999)
358+
for k in kwargs:
359+
if isinstance(kwargs[k], str):
360+
kwargs[k] = kwargs[k].format(rnd=item_rnd)
361+
362+
entity = self.sg.create(entity_type, kwargs, return_fields=list(kwargs.keys()))
363+
try:
364+
yield entity
365+
finally:
366+
rv = self.sg.delete(entity_type, entity["id"])
367+
assert rv == True
368+
342369

343370
class HumanUserAuthLiveTestBase(LiveTestBase):
344371
'''

tests/test_api.py

Lines changed: 77 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,40 +1694,46 @@ def test_include_archived_projects(self):
16941694

16951695

16961696
class TestFollow(base.LiveTestBase):
1697-
def setUp(self):
1698-
super(TestFollow, self).setUp()
1699-
self.sg.update('HumanUser', self.human_user['id'], {'projects': [self.project]})
1700-
1701-
# As the Follow entity isn't exposed directly, we clear out existing
1702-
# follows for the user before running our tests.
1703-
if self.sg.server_caps.version and self.sg.server_caps.version >= (7, 0, 12):
1704-
for entity in self.sg.following(self.human_user):
1705-
self.sg.unfollow(self.human_user, entity)
17061697

17071698
def test_follow_unfollow(self):
17081699
'''Test follow method'''
17091700

17101701
if not self.sg.server_caps.version or self.sg.server_caps.version < (5, 1, 22):
17111702
return
17121703

1713-
result = self.sg.follow(self.human_user, self.shot)
1714-
assert(result['followed'])
1704+
with self.gen_entity(
1705+
"HumanUser",
1706+
projects=[self.project],
1707+
) as human_user, self.gen_entity(
1708+
"Shot",
1709+
project=self.project,
1710+
) as shot:
1711+
result = self.sg.follow(human_user, shot)
1712+
assert(result['followed'])
17151713

1716-
result = self.sg.unfollow(self.human_user, self.shot)
1717-
assert(result['unfollowed'])
1714+
result = self.sg.unfollow(human_user, shot)
1715+
assert(result['unfollowed'])
17181716

17191717
def test_followers(self):
17201718
'''Test followers method'''
17211719

17221720
if not self.sg.server_caps.version or self.sg.server_caps.version < (5, 1, 22):
17231721
return
17241722

1725-
result = self.sg.follow(self.human_user, self.shot)
1726-
assert(result['followed'])
1723+
with self.gen_entity(
1724+
"HumanUser",
1725+
projects=[self.project],
1726+
) as human_user, self.gen_entity(
1727+
"Shot",
1728+
project=self.project,
1729+
) as shot:
1730+
result = self.sg.follow(human_user, shot)
1731+
assert(result['followed'])
1732+
1733+
result = self.sg.followers(shot)
17271734

1728-
result = self.sg.followers(self.shot)
1729-
self.assertEqual(1, len(result))
1730-
self.assertEqual(self.human_user['id'], result[0]['id'])
1735+
self.assertEqual(1, len(result))
1736+
self.assertEqual(human_user['id'], result[0]['id'])
17311737

17321738
def test_following(self):
17331739
'''Test following method'''
@@ -1736,42 +1742,53 @@ def test_following(self):
17361742
warnings.warn("Test bypassed because SG server used does not support this feature.", FutureWarning)
17371743
return
17381744

1739-
result = self.sg.follow(self.human_user, self.shot)
1740-
assert(result['followed'])
1741-
1742-
result = self.sg.following(self.human_user)
1743-
self.assertEqual(1, len(result))
1744-
self.assertEqual(self.shot['id'], result[0]['id'])
1745-
1746-
result = self.sg.follow(self.human_user, self.task)
1747-
assert(result['followed'])
1748-
1749-
result = self.sg.following(self.human_user)
1750-
self.assertEqual(2, len(result))
1751-
result = self.sg.following(self.human_user, entity_type="Task")
1752-
self.assertEqual(1, len(result))
1753-
result = self.sg.following(self.human_user, entity_type="Shot")
1754-
self.assertEqual(1, len(result))
1755-
1756-
shot_project_id = self.sg.find_one("Shot",
1757-
[["id", "is", self.shot["id"]]],
1758-
["project.Project.id"])["project.Project.id"]
1759-
task_project_id = self.sg.find_one("Task",
1760-
[["id", "is", self.task["id"]]],
1761-
["project.Project.id"])["project.Project.id"]
1762-
project_count = 2 if shot_project_id == task_project_id else 1
1763-
result = self.sg.following(self.human_user, project={"type": "Project", "id": shot_project_id})
1764-
self.assertEqual(project_count, len(result))
1765-
result = self.sg.following(self.human_user, project={"type": "Project", "id": task_project_id})
1766-
self.assertEqual(project_count, len(result))
1767-
result = self.sg.following(self.human_user,
1768-
project={"type": "Project", "id": shot_project_id},
1769-
entity_type="Shot")
1770-
self.assertEqual(1, len(result))
1771-
result = self.sg.following(self.human_user,
1772-
project={"type": "Project", "id": task_project_id},
1773-
entity_type="Task")
1774-
self.assertEqual(1, len(result))
1745+
with self.gen_entity(
1746+
"HumanUser",
1747+
projects=[self.project],
1748+
) as human_user, self.gen_entity(
1749+
"Shot",
1750+
project=self.project,
1751+
) as shot, self.gen_entity(
1752+
"Task",
1753+
project=self.project,
1754+
) as task:
1755+
result = self.sg.follow(human_user, shot)
1756+
assert(result['followed'])
1757+
1758+
result = self.sg.following(human_user)
1759+
1760+
self.assertEqual(1, len(result))
1761+
1762+
result = self.sg.follow(human_user, task)
1763+
assert(result['followed'])
1764+
1765+
result = self.sg.following(human_user)
1766+
1767+
self.assertEqual(2, len(result))
1768+
result = self.sg.following(human_user, entity_type="Task")
1769+
self.assertEqual(1, len(result))
1770+
result = self.sg.following(human_user, entity_type="Shot")
1771+
self.assertEqual(1, len(result))
1772+
1773+
shot_project_id = self.sg.find_one("Shot",
1774+
[["id", "is", shot["id"]]],
1775+
["project.Project.id"])["project.Project.id"]
1776+
task_project_id = self.sg.find_one("Task",
1777+
[["id", "is", task["id"]]],
1778+
["project.Project.id"])["project.Project.id"]
1779+
project_count = 2 if shot_project_id == task_project_id else 1
1780+
result = self.sg.following(human_user, project={"type": "Project", "id": shot_project_id})
1781+
self.assertEqual(project_count, len(result))
1782+
result = self.sg.following(human_user, project={"type": "Project", "id": task_project_id})
1783+
self.assertEqual(project_count, len(result))
1784+
result = self.sg.following(human_user,
1785+
project={"type": "Project", "id": shot_project_id},
1786+
entity_type="Shot")
1787+
self.assertEqual(1, len(result))
1788+
result = self.sg.following(human_user,
1789+
project={"type": "Project", "id": task_project_id},
1790+
entity_type="Task")
1791+
self.assertEqual(1, len(result))
17751792

17761793

17771794
class TestErrors(base.TestBase):
@@ -1944,6 +1961,12 @@ class TestScriptUserSudoAuth(base.LiveTestBase):
19441961
def setUp(self):
19451962
super(TestScriptUserSudoAuth, self).setUp('ApiUser')
19461963

1964+
self.sg.update(
1965+
'HumanUser',
1966+
self.human_user['id'],
1967+
{'projects': [self.project]},
1968+
)
1969+
19471970
def test_user_is_creator(self):
19481971
"""
19491972
Test 'sudo_as_login' option: on create, ensure appropriate user is set in created-by

0 commit comments

Comments
 (0)