Skip to content

Commit

Permalink
Merge pull request #465 from materialsproject/test-err-msg
Browse files Browse the repository at this point in the history
Test error messages
  • Loading branch information
janosh authored Oct 22, 2023
2 parents d7e6760 + ab71d1d commit 85aabc5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 38 deletions.
18 changes: 7 additions & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -151,26 +151,22 @@ select = [
"W", # pycodestyle warning
"YTT", # flake8-2020
]
# PLR0911: too-many-return-statements
# PLR0912: too-many-branches
# PLR0913: too-many-arguments
# PLR0915: too-many-statements
ignore = [
"B028",
"PLR0911",
"PLR0912",
"PLR0913",
"PLR0915",
"PLR0911", # too-many-return-statements
"PLR0912", # too-many-branches
"PLR0913", # too-many-arguments
"PLR0915", # too-many-statements
"PLW0603",
"RUF013",
]
pydocstyle.convention = "numpy"
isort.known-first-party = ["jobflow"]

[tool.ruff.per-file-ignores]
# F401: unused import
"__init__.py" = ["F401"]
# D: pydocstyle
# PLR2004: magic-value-comparison
# PT004: pytest-missing-fixture-name-underscore
# PLR0915: too-many-statements
# PT011: pytest-raises-too-broad TODO fix these, should not be ignored
"**/tests/*" = ["B018", "D", "PLR0915", "PLR2004", "PT004", "PT011"]
"**/tests/*" = ["D", "PLR2004", "PT004"]
44 changes: 29 additions & 15 deletions tests/core/test_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def test_flow_of_flows_init():
# test all jobflow included needed to generate outputs
add_job = get_test_job()
sub_flow = Flow([add_job], output=add_job.output)
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="jobs array does not contain all jobs"):
Flow([], output=sub_flow.output)

# test flow given rather than outputs
Expand All @@ -216,13 +216,13 @@ def test_flow_of_flows_init():
add_job = get_test_job()
sub_flow = Flow([add_job], output=add_job.output)
Flow([sub_flow])
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="already belongs to another flow"):
Flow([sub_flow])

# test that two of the same flow cannot be used in the same flow
add_job = get_test_job()
sub_flow = Flow([add_job], output=add_job.output)
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="jobs array contains multiple jobs/flows"):
Flow([sub_flow, sub_flow])


Expand Down Expand Up @@ -252,7 +252,7 @@ def test_flow_job_mixed():
add_job = get_test_job()
add_job2 = get_test_job()
subflow = Flow([add_job2], output=add_job2.output)
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="jobs array does not contain all jobs"):
Flow([add_job], output=[add_job.output, subflow.output])


Expand Down Expand Up @@ -456,7 +456,7 @@ def test_iterflow():
add_job2 = get_test_job()
add_job1.function_args = (2, add_job2.output)
flow = Flow([add_job1, add_job2], order=JobOrder.LINEAR)
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Job connectivity contains cycles"):
list(flow.iterflow())

# test with external reference
Expand Down Expand Up @@ -615,7 +615,10 @@ def test_get_flow():
# test all jobs included for graph to work
job1 = Job(add, function_args=(1, 2))
job2 = Job(add, function_args=(job1.output.value, 2))
with pytest.raises(ValueError):
with pytest.raises(
ValueError,
match="The following jobs were not found in the jobs array and are needed to ",
):
get_flow(job2, allow_external_references=False)

job1 = Job(add, function_args=(1, 2))
Expand Down Expand Up @@ -683,18 +686,18 @@ def test_add_jobs():
assert len(flow1.jobs) == 2
assert add_job2.hosts == [flow1.uuid]

with pytest.raises(ValueError):
with pytest.raises(ValueError, match="jobs array contains multiple jobs/flows"):
flow1.add_jobs(add_job2)

add_job3 = get_test_job()
Flow(add_job3)

# job belongs to another Flow
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="already belongs to another flow"):
flow1.add_jobs(add_job3)

add_job4 = get_test_job()
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="jobs array contains multiple jobs/flows"):
flow1.add_jobs([add_job4, add_job4])

# nested flows
Expand All @@ -709,7 +712,9 @@ def test_add_jobs():
flow1 = Flow([get_test_flow()])
flow2 = Flow([flow1])
flow3 = Flow([flow2])
with pytest.raises(ValueError):
with pytest.raises(
ValueError, match="circular dependency: Flow .+ contains the current Flow"
):
flow1.add_jobs(flow3)

# test passing single job to @jobs setter
Expand All @@ -730,9 +735,12 @@ def test_remove_jobs():
assert len(flow1.jobs) == 1
assert flow1.jobs[0].uuid is add_job2.uuid

with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Only indices between 0 and the number of"):
flow1.remove_jobs(-1)
with pytest.raises(ValueError):
with pytest.raises(
ValueError,
match="Only indices between 0 and the number of the jobs are accepted",
):
flow1.remove_jobs(10)

# test removing two jobs
Expand All @@ -750,7 +758,9 @@ def test_remove_jobs():
add_job2 = get_test_job()
flow2 = Flow([add_job1, add_job2], output=add_job2.output)

with pytest.raises(ValueError):
with pytest.raises(
ValueError, match="Removed Jobs/Flows are referenced in the output of the Flow"
):
flow2.remove_jobs(1)

# test removing a flow
Expand All @@ -771,7 +781,9 @@ def test_remove_jobs():
flow_inner = Flow([add_job1, add_job2])
flow = Flow([flow_inner, add_job3], output=flow_inner.jobs[0].output)

with pytest.raises(ValueError):
with pytest.raises(
ValueError, match="Removed Jobs/Flows are referenced in the output of the Flow"
):
flow.remove_jobs(0)

# test removing a job in a flow containing another flow
Expand All @@ -797,7 +809,9 @@ def test_set_output():
flow.output = add_job1.output
assert flow.output.uuid == add_job1.uuid

with pytest.raises(ValueError):
with pytest.raises(
ValueError, match="jobs array does not contain all jobs needed for flow output"
):
flow.output = [add_job3.output]


Expand Down
24 changes: 18 additions & 6 deletions tests/core/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ def test_job_init():
assert test_job._kwargs == {"data": "output", "graphs": "graph"}

# check giving True for multiple stores fails
with pytest.raises(ValueError):
with pytest.raises(
ValueError, match="Cannot select True for multiple additional stores"
):
Job(function=add, function_args=(1,), data=True, graphs=True)

# test changing job name (test needed due to setattr override
Expand Down Expand Up @@ -260,7 +262,9 @@ def return_arg(arg):
ref = OutputReference("xyz")
config = JobConfig(on_missing_references=OnMissing.ERROR)
test_job = Job(return_arg, function_args=(ref,), config=config)
with pytest.raises(ValueError):
with pytest.raises(
ValueError, match="Could not resolve reference - xyz not in store"
):
test_job.run(memory_jobstore)

config = JobConfig(on_missing_references=OnMissing.NONE)
Expand Down Expand Up @@ -535,7 +539,10 @@ def test_response():
assert response_original == response_processed

# test Response and another output
with pytest.raises(ValueError):
with pytest.raises(
ValueError,
match="Response cannot be returned in combination with other outputs",
):
Response.from_job_returns([response_original, 5])

# test schema
Expand Down Expand Up @@ -909,15 +916,20 @@ def add_schema_replace(a, b):
assert response.replace.jobs[-1].output_schema.__name__ == "AddSchema"

test_job = add_schema_bad(5, 6)
with pytest.raises(ValueError):
with pytest.raises(
ValueError,
match="Expected output to be AddSchema or dict but got output type of int",
):
test_job.run(memory_jobstore)

test_job = add_schema_wrong_key(5, 6)
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="1 validation error for AddSchema\n"):
test_job.run(memory_jobstore)

test_job = add_schema_no_output(5, 6)
with pytest.raises(ValueError):
with pytest.raises(
ValueError, match="Expected output of type AddSchema but got no output"
):
test_job.run(memory_jobstore)


Expand Down
13 changes: 7 additions & 6 deletions tests/core/test_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,25 +186,26 @@ class MySchema(BaseModel):
assert new_ref.output_schema is None

with pytest.raises(AttributeError):
ref.a.uuid
_ = ref.a.uuid

with pytest.raises(AttributeError):
ref["a"].uuid
_ = ref["a"].uuid

with pytest.raises(AttributeError):
ref[1].uuid
_ = ref[1].uuid

# check valid nested schemas
assert ref.nested.s.uuid == "123"
with pytest.raises(AttributeError):
ref.nested.m.uuid
_ = ref.nested.m.uuid

assert ref.nested.nested.n.uuid == "123"
with pytest.raises(AttributeError):
ref.nested.nested.m.uuid
_ = ref.nested.nested.m.uuid

assert ref.nested.nested_opt.n.uuid == "123"
with pytest.raises(AttributeError):
ref.nested.nested_opt.m.uuid
_ = ref.nested.nested_opt.m.uuid

# Union, List and Dict are currently not recognized by their inner type
# but check that there is no problem with them
Expand Down

0 comments on commit 85aabc5

Please sign in to comment.