From 42df56bf004026478a1c5c85e2f2e05f0311aef5 Mon Sep 17 00:00:00 2001 From: bethac07 Date: Wed, 20 Nov 2024 10:11:07 -0500 Subject: [PATCH] Updated tests --- tests/conftest.py | 55 +++++++++++++++++++++++++++++++++ tests/test___main__.py | 17 +++++----- tests/test_append_illum_cols.py | 44 +++++++++----------------- tests/test_transformer.py | 36 ++++++++++++--------- 4 files changed, 99 insertions(+), 53 deletions(-) create mode 100644 tests/conftest.py diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..4f358ac --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,55 @@ +import pytest + +@pytest.fixture(scope="module", + params=[ + ( + 'tests/data/Version1/images/', + 'tests/data/Version1/images/Index.idx.xml', + 'tests/data/Version1/config_v1.yml', + 'tests/data/Version1/load_data_version1.csv', + { + "488 long": "OrigRNA", + "Alexa 488": "OrigER", + "Alexa 568": "OrigAGP", + "Alexa 647": "OrigMito", + "HOECHST 33342": "OrigDNA", + "Brightfield": "OrigBrightfield" + }, + ), + ( + 'tests/data/Version5/images/', + 'tests/data/Version5/images/Index.idx.xml', + 'tests/data/Version5/config_v5.yml', + 'tests/data/Version5/load_data_version5.csv', + { + "Brightfield high": "OrigBrightfield", + "Alexa 568": "OrigAGP", + "Alexa 647": "OrigMito", + "Alexa 488": "OrigER", + "488 650": "OrigRNA", + "Digital Phase Contrast": "OrigDPC" + }, + ), + ( + 'tests/data/Version7/images/', + 'tests/data/Version7/images/Index.xml', + 'tests/data/Version7/config_v7.yml', + 'tests/data/Version7/load_data_version7.csv', + { + "HOECHST 33342": "OrigDNA", + "Alexa 568": "OrigAGP", + "Alexa 647": "OrigMito", + "Alexa 488": "OrigER", + "Alexa 488 Em 650": "OrigRNA", + "Brightfield": "OrigBrightfield", + "405ex, 570-630-em)": "OrigActin" + }, + ), + ], + ids=["v1", "v5", "v7"],) +def test_files(request): + index_directory, index_file, config_file, output_file, expected_channels = request.param + test_dict = {'index_directory':index_directory,'index_file':index_file, + 'config_file':config_file,'output_file':output_file, + 'expected_channels':expected_channels} + return test_dict diff --git a/tests/test___main__.py b/tests/test___main__.py index 56fb6e2..4c4f7bc 100644 --- a/tests/test___main__.py +++ b/tests/test___main__.py @@ -3,15 +3,14 @@ import src.pe2loaddata.__main__ -def test_with_file(): +def test_with_file(test_files): runner = click.testing.CliRunner() command = src.pe2loaddata.__main__.main - result = runner.invoke(command, [ - "tests/data/config.yml", + test_files["config_file"], "--index-file", - "tests/data/images/Index.idx.xml", + test_files["index_file"], "test.csv" ]) @@ -20,23 +19,23 @@ def test_with_file(): os.remove("test.csv") -def test_with_directory(): +def test_with_directory(test_files): runner = click.testing.CliRunner() command = src.pe2loaddata.__main__.main result = runner.invoke(command, [ - "tests/data/config.yml", + test_files["config_file"], "--index-directory", - "tests/data/images/", + test_files["index_directory"], "test.csv" ]) assert result.exit_code == 0 os.remove("test.csv") -def test_with_import(): +def test_with_import(test_files): assert not os.path.exists("test.csv") - src.pe2loaddata.__main__.headless("tests/data/config.yml","test.csv",index_file="tests/data/images/Index.idx.xml",index_directory=os.curdir) + src.pe2loaddata.__main__.headless(test_files["config_file"],"test.csv",index_file=test_files["index_file"],index_directory=test_files["index_directory"]) assert os.path.exists("test.csv") os.remove("test.csv") \ No newline at end of file diff --git a/tests/test_append_illum_cols.py b/tests/test_append_illum_cols.py index 2afd40a..ce7416e 100644 --- a/tests/test_append_illum_cols.py +++ b/tests/test_append_illum_cols.py @@ -45,21 +45,14 @@ def test_check_dir_arg(): except ArgumentTypeError: assert True -def test_load_config(): - pathname = "./tests/data/config.yml" +def test_load_config(test_files): + pathname = test_files["config_file"] assert os.path.exists(pathname) channels = load_config(pathname) - expected_channels = { - "488 long": "OrigRNA", - "Alexa 488": "OrigER", - "Alexa 568": "OrigAGP", - "Alexa 647": "OrigMito", - "HOECHST 33342": "OrigDNA", - "Brightfield": "OrigBrightfield" - } + expected_channels = test_files["expected_channels"] assert channels == expected_channels @@ -69,27 +62,18 @@ def test_parse_args(): pass -def test_main(): - config_file = "./tests/data/config.yml" - input_csv = "./tests/data/load_data.csv" - illum_filetype = ".mat" +def test_main(test_files): + config_file = test_files["config_file"] + input_csv = test_files["output_file"] + illum_filetype = ".npy" nrows = sum(1 for _ in open(input_csv)) - 1 - - - expected_channels = { - "488 long": "OrigRNA", - "Alexa 488": "OrigER", - "Alexa 568": "OrigAGP", - "Alexa 647": "OrigMito", - "HOECHST 33342": "OrigDNA", - "Brightfield": "OrigBrightfield" - } + expected_channels = test_files["expected_channels"] assert os.path.exists(config_file) channels = load_config(config_file) illum_directory = "tests/data/illum/" - plate_id = "BR00100044" + plate_id = "BR00121482" assert nrows != {} assert channels == expected_channels @@ -102,16 +86,16 @@ def test_main(): os.remove('illum.csv') -def test_write_csv(): - config_file = "./tests/data/config.yml" - input_csv = "tests/data/load_data.csv" - illum_filetype = ".mat" +def test_write_csv(test_files): + config_file = test_files["config_file"] + input_csv = test_files["output_file"] + illum_filetype = ".npy" nrows = sum(1 for _ in open(input_csv)) - 1 channels = load_config(config_file) illum_directory = "tests/data/illum/" - plate_id = "BR00100044" + plate_id = "BR00121482" with open('illum.csv', 'w') as fd: writer = csv.writer(fd, lineterminator='\n') diff --git a/tests/test_transformer.py b/tests/test_transformer.py index fd619c6..0ce1b6a 100644 --- a/tests/test_transformer.py +++ b/tests/test_transformer.py @@ -49,21 +49,14 @@ def test_parse_args(): pass -def test_load_config(): - pathname = "./tests/data/config.yml" +def test_load_config(test_files): + pathname = test_files["config_file"] assert os.path.exists(pathname) channels, metadata = load_config(pathname) - expected_channels = { - "488 long": "OrigRNA", - "Alexa 488": "OrigER", - "Alexa 568": "OrigAGP", - "Alexa 647": "OrigMito", - "HOECHST 33342": "OrigDNA", - "Brightfield": "OrigBrightfield" - } + expected_channels = test_files["expected_channels"] expected_metadata = { "AbsPositionZ": "AbsPositionZ", @@ -95,8 +88,8 @@ def test_load_config(): assert metadata == expected_metadata -def test_write_csv(): - config_file = "./tests/data/config.yml" +def test_write_csv(test_files): + config_file = test_files["config_file"] assert os.path.exists(config_file) @@ -104,7 +97,7 @@ def test_write_csv(): channels = dict([(str(k).replace(" ", ""), v) for (k, v) in channels.items()]) - index_file = "./tests/data/images/Index.idx.xml" + index_file = test_files["index_file"] handler = Handler() @@ -117,7 +110,7 @@ def test_write_csv(): paths = {} - index_directory = "./tests/data/images" + index_directory = test_files["index_directory"] for filename in os.listdir(index_directory): paths[filename] = index_directory @@ -126,5 +119,20 @@ def test_write_csv(): writer = csv.writer(fd, lineterminator='\n') write_csv(writer, images, plates, wells, maps, channels, metadata, paths) + + gt = [] + created = [] + + with open(test_files["output_file"]) as csvfile: + reader = csv.DictReader(csvfile) + for row in reader: + gt.append(row) + + with open("example.csv") as csvfile: + reader = csv.DictReader(csvfile) + for row in reader: + created.append(row) + + assert gt == created os.remove("example.csv")