diff --git a/test/integ_tests/test_create_quantum_job.py b/test/integ_tests/test_create_quantum_job.py index 640cb1688..2de29885e 100644 --- a/test/integ_tests/test_create_quantum_job.py +++ b/test/integ_tests/test_create_quantum_job.py @@ -198,11 +198,6 @@ def test_completed_quantum_job(aws_session, capsys): assert data in log_data -@pytest.mark.xfail( - (sys.version_info.major, sys.version_info.minor) != decorator_python_version, - raises=RuntimeError, - reason="Python version mismatch", -) def test_decorator_job(): class MyClass: attribute = "value" @@ -210,56 +205,65 @@ class MyClass: def __str__(self): return f"MyClass({self.attribute})" - @hybrid_job( - device=Devices.Amazon.SV1, - include_modules="job_test_script", - dependencies=str(Path("test", "integ_tests", "requirements.txt")), - input_data=str(Path("test", "integ_tests", "requirements")), + define_job_contexts = ( + pytest.raises( + RuntimeError, match="Python version must match between local environment and container." + ) + if (sys.version_info.major, sys.version_info.minor) != decorator_python_version + else () ) - def decorator_job(a, b: int, c=0, d: float = 1.0, **extras): - save_job_result(job_test_script.job_helper()) - with open(Path(get_input_data_dir()) / "requirements.txt", "r") as f: - assert f.readlines() == ["pytest\n"] - with open(Path("test", "integ_tests", "requirements.txt"), "r") as f: - assert f.readlines() == ["pytest\n"] - assert dir(pytest) - assert a.attribute == "value" - assert b == 2 - assert c == 0 - assert d == 5 - assert extras["extra_arg"] == "extra_value" - - hp_file = os.environ["AMZN_BRAKET_HP_FILE"] - with open(hp_file, "r") as f: - hyperparameters = json.load(f) - assert hyperparameters == { - "a": "MyClass{value}", - "b": "2", - "c": "0", - "d": "5", - "extra_arg": "extra_value", - } - - with open("test/output_file.txt", "w") as f: - f.write("hello") - - job = decorator_job(MyClass(), 2, d=5, extra_arg="extra_value") - assert job.result()["status"] == "SUCCESS" + with define_job_contexts: - current_dir = Path.cwd() - with tempfile.TemporaryDirectory() as temp_dir: - os.chdir(temp_dir) - try: - job.download_result() - with open(Path(job.name, "test", "output_file.txt"), "r") as f: - assert f.read() == "hello" - assert ( - Path(job.name, "results.json").exists() - and Path(job.name, "test").exists() - and not Path(job.name, "test", "integ_tests").exists() - ) - finally: - os.chdir(current_dir) + @hybrid_job( + device=Devices.Amazon.SV1, + include_modules="job_test_script", + dependencies=str(Path("test", "integ_tests", "requirements.txt")), + input_data=str(Path("test", "integ_tests", "requirements")), + ) + def decorator_job(a, b: int, c=0, d: float = 1.0, **extras): + save_job_result(job_test_script.job_helper()) + with open(Path(get_input_data_dir()) / "requirements.txt", "r") as f: + assert f.readlines() == ["pytest\n"] + with open(Path("test", "integ_tests", "requirements.txt"), "r") as f: + assert f.readlines() == ["pytest\n"] + assert dir(pytest) + assert a.attribute == "value" + assert b == 2 + assert c == 0 + assert d == 5 + assert extras["extra_arg"] == "extra_value" + + hp_file = os.environ["AMZN_BRAKET_HP_FILE"] + with open(hp_file, "r") as f: + hyperparameters = json.load(f) + assert hyperparameters == { + "a": "MyClass{value}", + "b": "2", + "c": "0", + "d": "5", + "extra_arg": "extra_value", + } + + with open("test/output_file.txt", "w") as f: + f.write("hello") + + job = decorator_job(MyClass(), 2, d=5, extra_arg="extra_value") + assert job.result()["status"] == "SUCCESS" + + current_dir = Path.cwd() + with tempfile.TemporaryDirectory() as temp_dir: + os.chdir(temp_dir) + try: + job.download_result() + with open(Path(job.name, "test", "output_file.txt"), "r") as f: + assert f.read() == "hello" + assert ( + Path(job.name, "results.json").exists() + and Path(job.name, "test").exists() + and not Path(job.name, "test", "integ_tests").exists() + ) + finally: + os.chdir(current_dir) @pytest.mark.xfail(