-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #547 from BCDA-APS/447-pytest
refactor from unittest to pytest
- Loading branch information
Showing
27 changed files
with
724 additions
and
855 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
""" | ||
Unit testing of plans. | ||
""" | ||
|
||
from ... import plans as APS_plans | ||
from ...plans import _COMMAND_HANDLER_ | ||
from ...plans import addDeviceDataAsStream | ||
from ...plans import execute_command_list | ||
from ...plans import get_command_list | ||
from ...plans import register_command_handler | ||
from ...plans import run_command_file | ||
from bluesky.simulators import summarize_plan | ||
|
||
import ophyd.sim | ||
import os | ||
import pytest | ||
import sys | ||
|
||
|
||
DATA_PATH = os.path.abspath( | ||
os.path.join(os.path.dirname(__file__), "..", "..", "tests") | ||
) | ||
|
||
|
||
def test_myoutput(capsys): # or use "capfd" for fd-level | ||
print("hello") | ||
sys.stderr.write("world\n") | ||
captured = capsys.readouterr() | ||
assert captured.out == "hello\n" | ||
assert captured.err == "world\n" | ||
print("next") | ||
captured = capsys.readouterr() | ||
assert captured.out == "next\n" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"objects, name, expected", | ||
[ | ||
([ophyd.sim.motor1], "test-device", " Read ['motor1']\n"), | ||
( | ||
[[ophyd.sim.motor2, ophyd.sim.motor3]], | ||
"test-device-list", | ||
" Read ['motor2', 'motor3']\n", | ||
), | ||
], | ||
) | ||
def test_addDeviceDataAsStream(objects, name, expected, capsys): | ||
summarize_plan(addDeviceDataAsStream(*objects, name)) | ||
|
||
captured = capsys.readouterr() | ||
assert captured.out == expected | ||
|
||
|
||
def test_register_action_handler(): | ||
assert summarize_plan != execute_command_list | ||
assert APS_plans._COMMAND_HANDLER_ == _COMMAND_HANDLER_ | ||
|
||
register_command_handler(summarize_plan) | ||
assert APS_plans._COMMAND_HANDLER_ != _COMMAND_HANDLER_ # FIXME: | ||
assert APS_plans._COMMAND_HANDLER_ == summarize_plan | ||
assert APS_plans._COMMAND_HANDLER_ != execute_command_list | ||
|
||
register_command_handler() | ||
assert APS_plans._COMMAND_HANDLER_ == _COMMAND_HANDLER_ | ||
assert APS_plans._COMMAND_HANDLER_ == execute_command_list | ||
|
||
|
||
def test_get_command_list(capsys): | ||
filename = os.path.join(DATA_PATH, "actions.txt") | ||
assert os.path.exists(filename) | ||
|
||
expected = [ | ||
"sample_slits 0 0 0.4 1.2", | ||
"preusaxstune", | ||
"FlyScan 0 0 0 blank", | ||
'FlyScan 5 2 0 "empty container"', | ||
"SAXS 0 0 0 blank", | ||
] | ||
|
||
commands = get_command_list(filename) | ||
assert len(commands) == len(expected) | ||
for r, e in zip(commands, expected): | ||
assert r[-1] == e | ||
|
||
|
||
def test_run_command_file_text(capsys): | ||
filename = os.path.join(DATA_PATH, "actions.txt") | ||
assert os.path.exists(filename) | ||
|
||
expected = [ | ||
f"Command file: {filename}", | ||
"====== ============ ========================", | ||
"line # action parameters ", | ||
"====== ============ ========================", | ||
"5 sample_slits 0, 0, 0.4, 1.2 ", | ||
"7 preusaxstune ", | ||
"10 FlyScan 0, 0, 0, blank ", | ||
"11 FlyScan 5, 2, 0, empty container", | ||
"12 SAXS 0, 0, 0, blank ", | ||
"====== ============ ========================", | ||
"", | ||
"file line 5: sample_slits 0 0 0.4 1.2", | ||
"no handling for line 5: sample_slits 0 0 0.4 1.2", | ||
"file line 7: preusaxstune", | ||
"no handling for line 7: preusaxstune", | ||
"file line 10: FlyScan 0 0 0 blank", | ||
"no handling for line 10: FlyScan 0 0 0 blank", | ||
'file line 11: FlyScan 5 2 0 "empty container"', | ||
'no handling for line 11: FlyScan 5 2 0 "empty container"', | ||
"file line 12: SAXS 0 0 0 blank", | ||
"no handling for line 12: SAXS 0 0 0 blank", | ||
] | ||
|
||
summarize_plan(run_command_file(filename)) | ||
|
||
captured = capsys.readouterr() | ||
assert isinstance(captured.out, str) | ||
|
||
received = captured.out.strip().split("\n") | ||
assert len(received) == len(expected) | ||
|
||
for r, e in zip(received[1:], expected[1:]): # FIXME: first row? | ||
assert r.find("\n") == -1 | ||
assert r == e | ||
|
||
|
||
def test_run_command_file_Excel(capsys): | ||
filename = os.path.join(DATA_PATH, "actions.xlsx") | ||
assert os.path.exists(filename) | ||
|
||
expected = [ | ||
f"Command file: {filename}", | ||
"====== ============ ===========================", | ||
"line # action parameters ", | ||
"====== ============ ===========================", | ||
"1 mono_shutter open ", | ||
"2 USAXSscan 45.07, 98.3, 0, Water Blank", | ||
"3 saxsExp 45.07, 98.3, 0, Water Blank", | ||
"4 waxwsExp 45.07, 98.3, 0, Water Blank", | ||
"5 USAXSscan 12, 12, 1.2, plastic ", | ||
"6 USAXSscan 12, 37, 0.1, Al foil ", | ||
"7 mono_shutter close ", | ||
"====== ============ ===========================", | ||
"", | ||
"file line 1: ['mono_shutter', 'open', None, None, None]", | ||
"no handling for line 1: ['mono_shutter', 'open', None, None, None]", | ||
"file line 2: ['USAXSscan', 45.07, 98.3, 0, 'Water Blank']", | ||
"no handling for line 2: ['USAXSscan', 45.07, 98.3, 0, 'Water Blank']", | ||
"file line 3: ['saxsExp', 45.07, 98.3, 0, 'Water Blank']", | ||
"no handling for line 3: ['saxsExp', 45.07, 98.3, 0, 'Water Blank']", | ||
"file line 4: ['waxwsExp', 45.07, 98.3, 0, 'Water Blank']", | ||
"no handling for line 4: ['waxwsExp', 45.07, 98.3, 0, 'Water Blank']", | ||
"file line 5: ['USAXSscan', 12, 12, 1.2, 'plastic']", | ||
"no handling for line 5: ['USAXSscan', 12, 12, 1.2, 'plastic']", | ||
"file line 6: ['USAXSscan', 12, 37, 0.1, 'Al foil']", | ||
"no handling for line 6: ['USAXSscan', 12, 37, 0.1, 'Al foil']", | ||
"file line 7: ['mono_shutter', 'close', None, None, None]", | ||
"no handling for line 7: ['mono_shutter', 'close', None, None, None]", | ||
] | ||
|
||
summarize_plan(run_command_file(filename)) | ||
|
||
captured = capsys.readouterr() | ||
assert isinstance(captured.out, str) | ||
|
||
received = captured.out.strip().split("\n") | ||
assert len(received) == len(expected) | ||
|
||
for r, e in zip(received[1:], expected[1:]): # FIXME: first row? | ||
assert r.find("\n") == -1 | ||
assert r == e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
""" | ||
Test the command list support. | ||
""" | ||
|
||
import os | ||
import pyRestTable | ||
import pytest | ||
|
||
from ...plans import CommandFileReadError | ||
from ...plans import get_command_list | ||
from ...utils import command_list_as_table | ||
|
||
DATA_PATH = os.path.abspath( | ||
os.path.join(os.path.dirname(__file__), "..", "..", "tests") | ||
) | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def text_command_file(): | ||
text_command_file = os.path.join(DATA_PATH, "actions.txt") | ||
return text_command_file | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def xl_command_file(): | ||
xl_command_file = os.path.join(DATA_PATH, "actions.xlsx") | ||
return xl_command_file | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def xl_file(): | ||
xl_file = os.path.join(DATA_PATH, "demo3.xlsx") | ||
return xl_file | ||
|
||
|
||
def test_testfile_exists(text_command_file, xl_command_file, xl_file): | ||
assert os.path.exists(text_command_file) | ||
assert os.path.exists(xl_command_file) | ||
assert os.path.exists(xl_file) | ||
|
||
|
||
def compare_tables_as_str(expected, received): | ||
e_lines = expected.strip().splitlines() | ||
r_lines = received.strip().splitlines() | ||
assert len(e_lines) == len(r_lines) | ||
for expect, got in zip(e_lines, r_lines): | ||
assert expect.strip() == got.strip() | ||
|
||
|
||
def test_ExcelFile_commands(xl_file): | ||
# just a spreadsheet for testing (early version of a command file) | ||
commands = get_command_list(xl_file) | ||
assert len(commands) == 9 # rows | ||
|
||
table = command_list_as_table(commands, show_raw=False) | ||
assert isinstance(table, pyRestTable.Table) | ||
received = str(table).strip() | ||
expected = """ | ||
====== ====== ========================================= | ||
line # action parameters | ||
====== ====== ========================================= | ||
1 row1 91, 26, 85, None, blank, 8 | ||
2 row2 9, 39, 29, 85, sample, 60 | ||
3 row3 54, None, 38, 3, blank, 76 | ||
4 row4 71, 36, 95, 83, foil, 12 | ||
5 row5 55, 75, 59, 84, DNA, 34 | ||
6 row6 18, 49, 31, 34, lecithin, 47 | ||
7 row7 37, None, None, None, a big mix of stuff | ||
8 row8 37, 80, 79, 45, salt water, 36 | ||
9 row9 72, 98, 67, 89, surprises, 49 | ||
====== ====== ========================================= | ||
""".strip() | ||
compare_tables_as_str(expected, received) | ||
|
||
|
||
def test_ExcelCommandList(xl_command_file): | ||
commands = get_command_list(xl_command_file) | ||
assert len(commands) == 7 | ||
table = command_list_as_table(commands, show_raw=False) | ||
received = str(table).strip() | ||
expected = """ | ||
====== ============ =========================== | ||
line # action parameters | ||
====== ============ =========================== | ||
1 mono_shutter open | ||
2 USAXSscan 45.07, 98.3, 0, Water Blank | ||
3 saxsExp 45.07, 98.3, 0, Water Blank | ||
4 waxwsExp 45.07, 98.3, 0, Water Blank | ||
5 USAXSscan 12, 12, 1.2, plastic | ||
6 USAXSscan 12, 37, 0.1, Al foil | ||
7 mono_shutter close | ||
====== ============ =========================== | ||
""".strip() | ||
compare_tables_as_str(expected, received) | ||
|
||
|
||
def test_TextCommandList(text_command_file): | ||
commands = get_command_list(text_command_file) | ||
assert len(commands) == 5 | ||
table = command_list_as_table(commands, show_raw=False) | ||
received = str(table).strip() | ||
expected = """ | ||
====== ============ ======================== | ||
line # action parameters | ||
====== ============ ======================== | ||
5 sample_slits 0, 0, 0.4, 1.2 | ||
7 preusaxstune | ||
10 FlyScan 0, 0, 0, blank | ||
11 FlyScan 5, 2, 0, empty container | ||
12 SAXS 0, 0, 0, blank | ||
====== ============ ======================== | ||
""".strip() | ||
compare_tables_as_str(expected, received) | ||
|
||
|
||
def test_TextCommandListRaw(text_command_file): | ||
commands = get_command_list(text_command_file) | ||
assert len(commands) == 5 | ||
table = command_list_as_table(commands, show_raw=True) | ||
received = str(table).strip() | ||
expected = """ | ||
====== ============ ======================== ===================================== | ||
line # action parameters raw input | ||
====== ============ ======================== ===================================== | ||
5 sample_slits 0, 0, 0.4, 1.2 sample_slits 0 0 0.4 1.2 | ||
7 preusaxstune preusaxstune | ||
10 FlyScan 0, 0, 0, blank FlyScan 0 0 0 blank | ||
11 FlyScan 5, 2, 0, empty container FlyScan 5 2 0 "empty container" | ||
12 SAXS 0, 0, 0, blank SAXS 0 0 0 blank | ||
====== ============ ======================== ===================================== | ||
""".strip() | ||
compare_tables_as_str(expected, received) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"error, expected, item", | ||
[ | ||
(IOError, "file not found: ", os.path.join(DATA_PATH, "none such")), | ||
(CommandFileReadError, "could not read ", __file__), | ||
], | ||
) | ||
def test_known_exceptions(error, expected, item): | ||
with pytest.raises(error) as exc: | ||
get_command_list(item) | ||
assert str(exc.value).startswith(expected) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
""" | ||
Test the Excel support. | ||
""" | ||
|
||
import os | ||
import pytest | ||
|
||
from ...utils import ExcelDatabaseFileGeneric | ||
|
||
|
||
DATA_PATH = os.path.abspath( | ||
os.path.join(os.path.dirname(__file__), "..", "..", "tests") | ||
) | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def xl_file(): | ||
xl_file = os.path.join(DATA_PATH, "demo3.xlsx") | ||
return xl_file | ||
|
||
|
||
def test_testfile_exists(xl_file): | ||
assert os.path.exists(xl_file) | ||
|
||
|
||
def test_normal_read(xl_file): | ||
xl = ExcelDatabaseFileGeneric(xl_file) | ||
assert len(xl.db) == 9 # rows | ||
assert len(xl.db["0"]) == 7 # columns | ||
assert "Unnamed: 7" in xl.db["0"] | ||
assert xl.db["0"]["Unnamed: 7"] == 8.0 | ||
|
||
|
||
def test_ignore_extra_false(xl_file): | ||
xl = ExcelDatabaseFileGeneric(xl_file, ignore_extra=False) | ||
assert len(xl.db) == 16 # rows | ||
assert len(xl.db["0"]) == 9 # columns |
Oops, something went wrong.