Skip to content

Commit

Permalink
test: pytest xfail python version mismatch (#751)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajberdy authored Oct 20, 2023
1 parent 6ba56a7 commit 1d9a8eb
Showing 1 changed file with 85 additions and 83 deletions.
168 changes: 85 additions & 83 deletions test/integ_tests/test_create_quantum_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,25 @@
import json
import os.path
import re
import sys
import tempfile
from pathlib import Path
from warnings import warn

import job_test_script
import pytest
from job_test_module.job_test_submodule.job_test_submodule_file import submodule_helper

from braket.aws.aws_quantum_job import AwsQuantumJob
from braket.devices import Devices
from braket.jobs import get_input_data_dir, hybrid_job, save_job_result
from braket.jobs import Framework, get_input_data_dir, hybrid_job, retrieve_image, save_job_result


@pytest.fixture
def decorator_python_version(aws_session):
image_uri = retrieve_image(Framework.BASE, aws_session.region)
tag = aws_session.get_full_image_tag(image_uri)
major_version, minor_version = re.search(r"-py(\d)(\d+)-", tag).groups()
return major_version, minor_version


def test_failed_quantum_job(aws_session, capsys):
Expand Down Expand Up @@ -190,53 +198,50 @@ 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"

def __str__(self):
return f"MyClass({self.attribute})"

try:

@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")

except RuntimeError as e:
if str(e).startswith("Python version must match between local environment and container."):
warn("skipping test due to python version mismatch")
return
raise e
@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"
Expand All @@ -257,50 +262,47 @@ def decorator_job(a, b: int, c=0, d: float = 1.0, **extras):
os.chdir(current_dir)


@pytest.mark.xfail(
(sys.version_info.major, sys.version_info.minor) != decorator_python_version,
raises=RuntimeError,
reason="Python version mismatch",
)
def test_decorator_job_submodule():
try:

@hybrid_job(
device=Devices.Amazon.SV1,
include_modules=[
@hybrid_job(
device=Devices.Amazon.SV1,
include_modules=[
"job_test_module",
],
dependencies=Path(
"test", "integ_tests", "job_test_module", "job_test_submodule", "requirements.txt"
),
input_data={
"my_input": str(Path("test", "integ_tests", "requirements.txt")),
"my_dir": str(Path("test", "integ_tests", "job_test_module")),
},
)
def decorator_job_submodule():
save_job_result(submodule_helper())
with open(Path(get_input_data_dir("my_input")) / "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"]
with open(
Path(get_input_data_dir("my_dir")) / "job_test_submodule" / "requirements.txt", "r"
) as f:
assert f.readlines() == ["pytest\n"]
with open(
Path(
"test",
"integ_tests",
"job_test_module",
],
dependencies=Path(
"test", "integ_tests", "job_test_module", "job_test_submodule", "requirements.txt"
"job_test_submodule",
"requirements.txt",
),
input_data={
"my_input": str(Path("test", "integ_tests", "requirements.txt")),
"my_dir": str(Path("test", "integ_tests", "job_test_module")),
},
)
def decorator_job_submodule():
save_job_result(submodule_helper())
with open(Path(get_input_data_dir("my_input")) / "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"]
with open(
Path(get_input_data_dir("my_dir")) / "job_test_submodule" / "requirements.txt", "r"
) as f:
assert f.readlines() == ["pytest\n"]
with open(
Path(
"test",
"integ_tests",
"job_test_module",
"job_test_submodule",
"requirements.txt",
),
"r",
) as f:
assert f.readlines() == ["pytest\n"]
assert dir(pytest)

except RuntimeError as e:
if str(e).startswith("Python version must match between local environment and container."):
warn("skipping test due to python version mismatch")
return
raise e
"r",
) as f:
assert f.readlines() == ["pytest\n"]
assert dir(pytest)

job = decorator_job_submodule()
assert job.result()["status"] == "SUCCESS"

0 comments on commit 1d9a8eb

Please sign in to comment.