Skip to content

Commit

Permalink
updating tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brimoor committed Nov 2, 2023
1 parent 8e9064d commit 9ed5f75
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 31 deletions.
5 changes: 2 additions & 3 deletions fiftyone/operators/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ def raise_timeout_error(seconds):


def plugins_cache(func):
"""Decorator that returns cached function results as long as no
subdirectories of ``fo.config.plugins_dir`` have been modified since last
time.
"""Decorator that returns cached function results as long as no plugins
have been modified since last time.
"""

@wraps(func)
Expand Down
44 changes: 16 additions & 28 deletions tests/unittests/operators/decorators_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,18 @@


class DirStateTests(unittest.TestCase):
@patch("os.path.isdir")
@patch("os.path.getmtime")
def test_dir_state_non_existing_dir(self, mock_getmtime, mock_isdir):
mock_isdir.return_value = False
def test_dir_state_non_existing_dir(self):
dirpath = "/non/existing/dir"
try:
result = dir_state(dirpath)
except Exception as e:
self.fail(e)
mock_isdir.assert_called_once_with(dirpath)
result = dir_state(dirpath)
self.assertIsNone(result)
mock_getmtime.assert_not_called()

@patch("os.path.isdir")
@patch("os.path.getmtime")
def test_dir_state_existing_empty_dir(self, mock_getmtime, mock_isdir):
mock_isdir.return_value = True
dirpath = "/existing/empty/dir"
mock_getmtime.return_value = 1000

try:
result = dir_state(dirpath)
except Exception as e:
self.fail(e)
result = dir_state(dirpath)
mock_isdir.assert_called_once_with(dirpath)
mock_getmtime.assert_called_once_with(dirpath)
self.assertEqual(result, 1000)
Expand All @@ -64,11 +52,11 @@ def test_rgrs_dir_state_empty(self):
self.assertGreater(dir_state(tmpdirname), 0)

def test_rgrs_dir_state_change_with_delete(self):
plugin_paths = ["plugin1/file1.txt", "plugin2/file2.txt"]
plugin_paths = ["@org1/plugin1/file1.txt", "@org2/plugin2/file2.txt"]
with tempfile.TemporaryDirectory() as tmpdirname:
initial_dir_state = dir_state(tmpdirname)
for p in plugin_paths:
time.sleep(1)
time.sleep(0.01)
os.makedirs(os.path.join(tmpdirname, p))

# verify that max time is greater after adding files
Expand All @@ -77,24 +65,24 @@ def test_rgrs_dir_state_change_with_delete(self):

# verify that max time is greater after deleting files
shutil.rmtree(
os.path.join(tmpdirname, plugin_paths[0].split("/")[0])
os.path.join(tmpdirname, plugin_paths[0].rsplit("/", 1)[0])
)
dir_state2 = dir_state(tmpdirname)
self.assertGreaterEqual(dir_state2, dir_state1)
time.sleep(1)
time.sleep(0.01)

shutil.rmtree(
os.path.join(tmpdirname, plugin_paths[1].split("/")[0])
os.path.join(tmpdirname, plugin_paths[1].rsplit("/", 1)[0])
)
dir_state3 = dir_state(tmpdirname)
self.assertGreaterEqual(dir_state3, dir_state2)

def test_rgrs_dir_state_change_with_rename(self):
plugin_paths = ["plugin1/file1.txt", "plugin2/file2.txt"]
plugin_paths = ["@org1/plugin1/file1.txt", "@org2/plugin2/file2.txt"]
with tempfile.TemporaryDirectory() as tmpdirname:
initial_dir_state = dir_state(tmpdirname)
for p in plugin_paths:
time.sleep(1)
time.sleep(0.01)
os.makedirs(os.path.join(tmpdirname, p))

# verify that max time is greater after adding files
Expand All @@ -103,9 +91,9 @@ def test_rgrs_dir_state_change_with_rename(self):

# verify that max time is greater after renaming plugin dir
os.rename(
os.path.join(tmpdirname, plugin_paths[0].split("/")[0]),
os.path.join(tmpdirname, plugin_paths[0].rsplit("/", 1)[0]),
os.path.join(
tmpdirname, plugin_paths[0].split("/")[0] + "renamed"
tmpdirname, plugin_paths[0].rsplit("/", 1)[0] + "renamed"
),
)
dir_state2 = dir_state(tmpdirname)
Expand All @@ -117,7 +105,7 @@ async def dummy_coroutine_fn(duration):
return "Success"


@coroutine_timeout(seconds=2)
@coroutine_timeout(seconds=0.2)
async def timeout_dummy_coroutine_fn(duration):
return await dummy_coroutine_fn(duration)

Expand All @@ -128,14 +116,14 @@ def non_coroutine_fn():

class TestCoroutineTimeoutDecorator(unittest.TestCase):
def test_successful_execution(self):
result = asyncio.run(timeout_dummy_coroutine_fn(1))
result = asyncio.run(timeout_dummy_coroutine_fn(0.1))
self.assertEqual(result, "Success")

def test_timeout_exception(self):
with self.assertRaises(TimeoutError):
asyncio.run(timeout_dummy_coroutine_fn(3))
asyncio.run(timeout_dummy_coroutine_fn(0.3))

def test_non_coroutine_function(self):
decorated_function = coroutine_timeout(2)(non_coroutine_fn)
decorated_function = coroutine_timeout(0.2)(non_coroutine_fn)
with self.assertRaises(TypeError):
asyncio.run(decorated_function())

0 comments on commit 9ed5f75

Please sign in to comment.