Skip to content

Commit

Permalink
Try mocking one more time
Browse files Browse the repository at this point in the history
  • Loading branch information
EmanElsaban committed Nov 29, 2024
1 parent 66ff06d commit 1a3638c
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions tests/serialize/runstate/dynamodb_state_store_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,7 @@ def large_object():
}


@pytest.fixture(autouse=True)
def mock_config_functions():
with mock.patch("tron.config.static_config.get_config_watcher") as mock_get_config_watcher:
mock_get_config_watcher.return_value = mock.Mock()
yield


@pytest.mark.usefixtures("store", "small_object", "large_object", "mock_config_functions")
@pytest.mark.usefixtures("store", "small_object", "large_object")
class TestDynamoDBStateStore:
@pytest.mark.parametrize("read_json", [False, True])
def test_save(self, store, small_object, large_object, read_json):
Expand All @@ -159,7 +152,9 @@ def test_save(self, store, small_object, large_object, read_json):
]
mock_config = {"read_json.enable": read_json}
mock_configuration = staticconf.testing.MockConfiguration(mock_config, namespace="tron")
with mock_configuration:
with mock_configuration, mock.patch("tron.config.static_config.load_yaml_file", autospec=True), mock.patch(
"tron.config.static_config.build_configuration_watcher", autospec=True
):
vals = store.restore(keys)
for key, value in key_value_pairs:
assert_equal(vals[key], value)
Expand Down Expand Up @@ -199,7 +194,10 @@ def test_delete_if_val_is_none(self, store, small_object, large_object):
store.build_key("job_state", "two"),
store.build_key("job_run_state", "four"),
]
vals = store.restore(keys)
with mock.patch("tron.config.static_config.load_yaml_file", autospec=True), mock.patch(
"tron.config.static_config.build_configuration_watcher", autospec=True
):
vals = store.restore(keys)
assert vals == {keys[1]: small_object}

def test_save_more_than_4KB(self, store, small_object, large_object):
Expand All @@ -215,7 +213,10 @@ def test_save_more_than_4KB(self, store, small_object, large_object):
assert store.save_errors == 0
keys = [store.build_key("job_state", "two")]

vals = store.restore(keys)
with mock.patch("tron.config.static_config.load_yaml_file", autospec=True), mock.patch(
"tron.config.static_config.build_configuration_watcher", autospec=True
):
vals = store.restore(keys)
for key, value in key_value_pairs:
assert_equal(vals[key], value)

Expand All @@ -231,7 +232,9 @@ def test_restore_more_than_4KB(self, store, small_object, large_object, read_jso

mock_config = {"read_json.enable": read_json}
mock_configuration = staticconf.testing.MockConfiguration(mock_config, namespace="tron")
with mock_configuration:
with mock_configuration, mock.patch("tron.config.static_config.load_yaml_file", autospec=True), mock.patch(
"tron.config.static_config.build_configuration_watcher", autospec=True
):
vals = store.restore(keys)
for key in keys:
assert_equal(vals[key], large_object)
Expand All @@ -247,7 +250,9 @@ def test_restore(self, store, small_object, large_object, read_json):
assert store.save_errors == 0
mock_config = {"read_json.enable": read_json}
mock_configuration = staticconf.testing.MockConfiguration(mock_config, namespace="tron")
with mock_configuration:
with mock_configuration, mock.patch("tron.config.static_config.load_yaml_file", autospec=True), mock.patch(
"tron.config.static_config.build_configuration_watcher", autospec=True
):
vals = store.restore(keys)
for key in keys:
assert_equal(vals[key], small_object)
Expand Down Expand Up @@ -310,7 +315,10 @@ def test_retry_reading(self, store, small_object, large_object):
return_value=unprocessed_value,
) as mock_failed_read:
try:
store.restore(keys)
with mock.patch("tron.config.static_config.load_yaml_file", autospec=True), mock.patch(
"tron.config.static_config.build_configuration_watcher", autospec=True
):
store.restore(keys)
except Exception:
assert_equal(mock_failed_read.call_count, 11)

Expand All @@ -322,6 +330,8 @@ def test_restore_exception_propagation(self, store, small_object):
mock_future.result.side_effect = Exception("mocked exception")
with mock.patch("concurrent.futures.Future", return_value=mock_future, autospec=True):
with mock.patch("concurrent.futures.as_completed", return_value=[mock_future], autospec=True):
with pytest.raises(Exception) as exec_info:
with pytest.raises(Exception) as exec_info, mock.patch(
"tron.config.static_config.load_yaml_file", autospec=True
), mock.patch("tron.config.static_config.build_configuration_watcher", autospec=True):
store.restore(keys)
assert str(exec_info.value) == "mocked exception"

0 comments on commit 1a3638c

Please sign in to comment.