tests: Move monkeypatch statements to fixtures in test_version_update.py

This commit is contained in:
Shreya Malviya 2021-09-14 16:04:06 +05:30
parent 90c6392e16
commit 686f65e4f4
1 changed files with 20 additions and 16 deletions

View File

@ -6,46 +6,50 @@ from monkey_island.cc.services.version_update import VersionUpdateService
@pytest.fixture
def deployment_info_file_path(data_for_tests_dir):
return os.path.join(data_for_tests_dir, "deployment.json")
def deployment_info_file_path(monkeypatch, data_for_tests_dir):
path = os.path.join(data_for_tests_dir, "deployment.json")
monkeypatch.setattr(os.path, "join", lambda *args: path)
return path
@pytest.fixture
def ghost_file():
return "ghost_file"
def ghost_file(monkeypatch):
path = "ghost_file"
monkeypatch.setattr(os.path, "join", lambda *args: path)
return path
@pytest.fixture
def key_error_deployment_info_file_path(data_for_tests_dir):
return os.path.join(data_for_tests_dir, "deployment_key_error.json")
def key_error_deployment_info_file_path(monkeypatch, data_for_tests_dir):
path = os.path.join(data_for_tests_dir, "deployment_key_error.json")
monkeypatch.setattr(os.path, "join", lambda *args: path)
return path
@pytest.fixture
def flawed_deployment_info_file_path(data_for_tests_dir):
return os.path.join(data_for_tests_dir, "deployment_flawed")
def flawed_deployment_info_file_path(monkeypatch, data_for_tests_dir):
path = os.path.join(data_for_tests_dir, "deployment_flawed")
monkeypatch.setattr(os.path, "join", lambda *args: path)
return path
def test_get_deployment_field_from_file(deployment_info_file_path, monkeypatch):
monkeypatch.setattr(os.path, "join", lambda *args: deployment_info_file_path)
def test_get_deployment_field_from_file(deployment_info_file_path):
deployment = VersionUpdateService().get_deployment_from_file(deployment_info_file_path)
assert deployment == "develop"
def test_get_deployment_field_from_nonexistent_file(ghost_file, monkeypatch):
monkeypatch.setattr(os.path, "join", lambda *args: ghost_file)
def test_get_deployment_field_from_nonexistent_file(ghost_file):
deployment = VersionUpdateService().get_deployment_from_file(ghost_file)
assert deployment == "unknown"
def test_get_deployment_field_key_error(key_error_deployment_info_file_path, monkeypatch):
monkeypatch.setattr(os.path, "join", lambda *args: key_error_deployment_info_file_path)
def test_get_deployment_field_key_error(key_error_deployment_info_file_path):
deployment = VersionUpdateService().get_deployment_from_file(
key_error_deployment_info_file_path
)
assert deployment == "unknown"
def test_get_deployment_field_from_flawed_json_file(flawed_deployment_info_file_path, monkeypatch):
monkeypatch.setattr(os.path, "join", lambda *args: flawed_deployment_info_file_path)
def test_get_deployment_field_from_flawed_json_file(flawed_deployment_info_file_path):
deployment = VersionUpdateService().get_deployment_from_file(flawed_deployment_info_file_path)
assert deployment == "unknown"