Skip to content

Commit

Permalink
SG-31925 Fixup follow flaky tests (#304)
Browse files Browse the repository at this point in the history
Fix race condition
  • Loading branch information
julien-lang authored Sep 21, 2023
1 parent cad4735 commit aa6a948
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 54 deletions.
27 changes: 27 additions & 0 deletions tests/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Base class for ShotGrid API tests."""
import contextlib
import os
import random
import re
import unittest

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

@contextlib.contextmanager
def gen_entity(self, entity_type, **kwargs):
# Helper creator
if entity_type == "HumanUser":
if "login" not in kwargs:
kwargs["login"] = "test-python-api-{rnd}"

if "sg_status_list" not in kwargs:
kwargs["sg_status_list"] = "dis"

if "password_proxy" not in kwargs:
kwargs["password_proxy"] = self.config.human_password

item_rnd = random.randrange(100,999)
for k in kwargs:
if isinstance(kwargs[k], str):
kwargs[k] = kwargs[k].format(rnd=item_rnd)

entity = self.sg.create(entity_type, kwargs, return_fields=list(kwargs.keys()))
try:
yield entity
finally:
rv = self.sg.delete(entity_type, entity["id"])
assert rv == True


class HumanUserAuthLiveTestBase(LiveTestBase):
'''
Expand Down
131 changes: 77 additions & 54 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1694,40 +1694,46 @@ def test_include_archived_projects(self):


class TestFollow(base.LiveTestBase):
def setUp(self):
super(TestFollow, self).setUp()
self.sg.update('HumanUser', self.human_user['id'], {'projects': [self.project]})

# As the Follow entity isn't exposed directly, we clear out existing
# follows for the user before running our tests.
if self.sg.server_caps.version and self.sg.server_caps.version >= (7, 0, 12):
for entity in self.sg.following(self.human_user):
self.sg.unfollow(self.human_user, entity)

def test_follow_unfollow(self):
'''Test follow method'''

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

result = self.sg.follow(self.human_user, self.shot)
assert(result['followed'])
with self.gen_entity(
"HumanUser",
projects=[self.project],
) as human_user, self.gen_entity(
"Shot",
project=self.project,
) as shot:
result = self.sg.follow(human_user, shot)
assert(result['followed'])

result = self.sg.unfollow(self.human_user, self.shot)
assert(result['unfollowed'])
result = self.sg.unfollow(human_user, shot)
assert(result['unfollowed'])

def test_followers(self):
'''Test followers method'''

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

result = self.sg.follow(self.human_user, self.shot)
assert(result['followed'])
with self.gen_entity(
"HumanUser",
projects=[self.project],
) as human_user, self.gen_entity(
"Shot",
project=self.project,
) as shot:
result = self.sg.follow(human_user, shot)
assert(result['followed'])

result = self.sg.followers(shot)

result = self.sg.followers(self.shot)
self.assertEqual(1, len(result))
self.assertEqual(self.human_user['id'], result[0]['id'])
self.assertEqual(1, len(result))
self.assertEqual(human_user['id'], result[0]['id'])

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

result = self.sg.follow(self.human_user, self.shot)
assert(result['followed'])

result = self.sg.following(self.human_user)
self.assertEqual(1, len(result))
self.assertEqual(self.shot['id'], result[0]['id'])

result = self.sg.follow(self.human_user, self.task)
assert(result['followed'])

result = self.sg.following(self.human_user)
self.assertEqual(2, len(result))
result = self.sg.following(self.human_user, entity_type="Task")
self.assertEqual(1, len(result))
result = self.sg.following(self.human_user, entity_type="Shot")
self.assertEqual(1, len(result))

shot_project_id = self.sg.find_one("Shot",
[["id", "is", self.shot["id"]]],
["project.Project.id"])["project.Project.id"]
task_project_id = self.sg.find_one("Task",
[["id", "is", self.task["id"]]],
["project.Project.id"])["project.Project.id"]
project_count = 2 if shot_project_id == task_project_id else 1
result = self.sg.following(self.human_user, project={"type": "Project", "id": shot_project_id})
self.assertEqual(project_count, len(result))
result = self.sg.following(self.human_user, project={"type": "Project", "id": task_project_id})
self.assertEqual(project_count, len(result))
result = self.sg.following(self.human_user,
project={"type": "Project", "id": shot_project_id},
entity_type="Shot")
self.assertEqual(1, len(result))
result = self.sg.following(self.human_user,
project={"type": "Project", "id": task_project_id},
entity_type="Task")
self.assertEqual(1, len(result))
with self.gen_entity(
"HumanUser",
projects=[self.project],
) as human_user, self.gen_entity(
"Shot",
project=self.project,
) as shot, self.gen_entity(
"Task",
project=self.project,
) as task:
result = self.sg.follow(human_user, shot)
assert(result['followed'])

result = self.sg.following(human_user)

self.assertEqual(1, len(result))

result = self.sg.follow(human_user, task)
assert(result['followed'])

result = self.sg.following(human_user)

self.assertEqual(2, len(result))
result = self.sg.following(human_user, entity_type="Task")
self.assertEqual(1, len(result))
result = self.sg.following(human_user, entity_type="Shot")
self.assertEqual(1, len(result))

shot_project_id = self.sg.find_one("Shot",
[["id", "is", shot["id"]]],
["project.Project.id"])["project.Project.id"]
task_project_id = self.sg.find_one("Task",
[["id", "is", task["id"]]],
["project.Project.id"])["project.Project.id"]
project_count = 2 if shot_project_id == task_project_id else 1
result = self.sg.following(human_user, project={"type": "Project", "id": shot_project_id})
self.assertEqual(project_count, len(result))
result = self.sg.following(human_user, project={"type": "Project", "id": task_project_id})
self.assertEqual(project_count, len(result))
result = self.sg.following(human_user,
project={"type": "Project", "id": shot_project_id},
entity_type="Shot")
self.assertEqual(1, len(result))
result = self.sg.following(human_user,
project={"type": "Project", "id": task_project_id},
entity_type="Task")
self.assertEqual(1, len(result))


class TestErrors(base.TestBase):
Expand Down Expand Up @@ -1944,6 +1961,12 @@ class TestScriptUserSudoAuth(base.LiveTestBase):
def setUp(self):
super(TestScriptUserSudoAuth, self).setUp('ApiUser')

self.sg.update(
'HumanUser',
self.human_user['id'],
{'projects': [self.project]},
)

def test_user_is_creator(self):
"""
Test 'sudo_as_login' option: on create, ensure appropriate user is set in created-by
Expand Down

0 comments on commit aa6a948

Please sign in to comment.