Skip to content

Commit

Permalink
tests: make compliant to Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
breakthewall committed Mar 7, 2024
1 parent fe0d3cb commit e37444b
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 79 deletions.
20 changes: 20 additions & 0 deletions tests/functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from tempfile import (
_get_candidate_names,
_get_default_tempdir
)
from os import (
path as os_path,
remove as os_remove
)
def tmp_filepath(suffix=''):
return os_path.join(
_get_default_tempdir(),
next(_get_candidate_names())
) + suffix

def clean_file(fn):
if os_path.exists(fn):
# f.close()
os_remove(fn)


65 changes: 35 additions & 30 deletions tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from icfree.converter.converter import concentrations_to_volumes
from icfree.converter.args import build_args_parser

from tests.functions import tmp_filepath, clean_file


__DATA_FOLDER = os_path.join(
os_path.dirname(os_path.realpath(__file__)),
Expand Down Expand Up @@ -121,32 +123,34 @@ def setUp(self):
)

def test_valid_input(self):
with NamedTemporaryFile(mode='w', suffix='.tsv') as temp_file:
args = [
parameters,
sampling_concentrations,
'-v', '1000',
'-o', temp_file.name,
'-r', 'echo'
]
main(args)
actual = pd_read_csv(temp_file.name, sep='\t')
expected = pd_read_csv(sampling_volumes, sep='\t')
pd_testing.assert_frame_equal(actual, expected)
tmp_fn = tmp_filepath('.tsv')
args = [
parameters,
sampling_concentrations,
'-v', '1000',
'-o', tmp_fn,
'-r', 'echo'
]
main(args)
actual = pd_read_csv(tmp_fn, sep='\t')
expected = pd_read_csv(sampling_volumes, sep='\t')
pd_testing.assert_frame_equal(actual, expected)
clean_file(tmp_fn)

def test_other_robot(self):
with NamedTemporaryFile(mode='w', suffix='.tsv') as temp_file:
args = [
parameters,
sampling_concentrations,
'-v', '1000',
'-o', temp_file.name,
'-r', 'other'
]
main(args)
actual = pd_read_csv(temp_file.name, sep='\t')
expected = pd_read_csv(sampling_volumes_other, sep='\t')
pd_testing.assert_frame_equal(actual, expected)
tmp_fn = tmp_filepath('.tsv')
args = [
parameters,
sampling_concentrations,
'-v', '1000',
'-o', tmp_fn,
'-r', 'other'
]
main(args)
actual = pd_read_csv(tmp_fn, sep='\t')
expected = pd_read_csv(sampling_volumes_other, sep='\t')
pd_testing.assert_frame_equal(actual, expected)
clean_file(tmp_fn)

def test_system_exit(self):
args = [
Expand All @@ -161,9 +165,10 @@ def test_system_exit(self):
main(args)

def test_subprocess_call(self):
with NamedTemporaryFile(mode='w', suffix='.tsv') as temp_file:
cmd = 'python -m icfree.converter {} {} -o {} -v 1000 -r echo'.format(parameters, sampling_concentrations, temp_file.name)
sp_run(cmd.split())
actual = pd_read_csv(temp_file.name)
expected = pd_read_csv(sampling_volumes)
pd_testing.assert_frame_equal(actual, expected)
tmp_fn = tmp_filepath('.tsv')
cmd = 'python -m icfree.converter {} {} -o {} -v 1000 -r echo'.format(parameters, sampling_concentrations, tmp_fn)
sp_run(cmd.split())
actual = pd_read_csv(tmp_fn)
expected = pd_read_csv(sampling_volumes)
pd_testing.assert_frame_equal(actual, expected)
clean_file(tmp_fn)
50 changes: 27 additions & 23 deletions tests/test_instructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
build_args_parser
)

from tests.functions import tmp_filepath, clean_file


class TestParseSrcPlateTypeOption(TestCase):

Expand Down Expand Up @@ -58,33 +60,35 @@ def test_generate_instructions(self):
})
plate_types = "ALL:384PP_AQ_GP3;Component_1:384PP_AQ_GP4"

tmp_sfn = tmp_filepath()
tmp_dfn = tmp_filepath()
# Write source and destination dataframes to CSV temp files
with NamedTemporaryFile() as source_file, NamedTemporaryFile() as destination_file:
source_df.to_csv(source_file.name, index=False)
destination_df.to_csv(destination_file.name, index=False)
source_df.to_csv(tmp_sfn, index=False)
destination_df.to_csv(tmp_dfn, index=False)

# Generate instructions
instructions = generate_instructions(
[source_file.name], [destination_file.name], src_plate_type=plate_types
)
# Generate instructions
instructions = generate_instructions(
[tmp_sfn], [tmp_dfn], src_plate_type=plate_types
)

# Define expected instructions as DataFrame
expected_instructions = DataFrame({
'Source Plate Name': ['Source[1]', 'Source[1]', 'Source[1]', 'Source[1]'],
'Source Plate Type': ['384PP_AQ_GP4', '384PP_AQ_GP4', '384PP_AQ_GP3', '384PP_AQ_GP3'],
'Source Well': ['{A2;B1}', '{A2;B1}', 'A1', 'A1'],
'Destination Plate Name': ['Destination[1]', 'Destination[1]', 'Destination[1]', 'Destination[1]'],
'Destination Well': ['A2', 'B1', 'A1', 'B2'],
'Transfer Volume': [10, 20, 5, 15],
'Sample ID': ['Component_1', 'Component_1', 'Component_2', 'Component_2']
})

# Sort dataframes for comparison
instructions = instructions.sort_values(by=['Sample ID'], ignore_index=True)
expected_instructions = expected_instructions.sort_values(by=['Sample ID'], ignore_index=True)
# Compare instructions with expected instructions
self.assertTrue(instructions.equals(expected_instructions))
# Define expected instructions as DataFrame
expected_instructions = DataFrame({
'Source Plate Name': ['Source[1]', 'Source[1]', 'Source[1]', 'Source[1]'],
'Source Plate Type': ['384PP_AQ_GP4', '384PP_AQ_GP4', '384PP_AQ_GP3', '384PP_AQ_GP3'],
'Source Well': ['{A2;B1}', '{A2;B1}', 'A1', 'A1'],
'Destination Plate Name': ['Destination[1]', 'Destination[1]', 'Destination[1]', 'Destination[1]'],
'Destination Well': ['A2', 'B1', 'A1', 'B2'],
'Transfer Volume': [10, 20, 5, 15],
'Sample ID': ['Component_1', 'Component_1', 'Component_2', 'Component_2']
})

# Sort dataframes for comparison
instructions = instructions.sort_values(by=['Sample ID'], ignore_index=True)
expected_instructions = expected_instructions.sort_values(by=['Sample ID'], ignore_index=True)
# Compare instructions with expected instructions
self.assertTrue(instructions.equals(expected_instructions))
clean_file(tmp_sfn)
clean_file(tmp_dfn)

class TestGetPlateIndex(TestCase):
def setUp(self):
Expand Down
64 changes: 38 additions & 26 deletions tests/test_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
from scipy.stats import qmc
from itertools import product
from tempfile import NamedTemporaryFile
from os import path as os_path
from os import (
path as os_path,
name as os_name
)
from subprocess import run as sp_run

from icfree.sampler.sampler import (
Expand All @@ -25,6 +28,8 @@
from icfree.sampler.args import build_args_parser
from icfree.sampler.__main__ import main

from tests.functions import tmp_filepath, clean_file


DATA_FOLDER = os_path.join(
os_path.dirname(os_path.realpath(__file__)),
Expand Down Expand Up @@ -52,7 +57,6 @@
)



class TestExtractSpecs(unittest.TestCase):
def test_valid_input(self):
"""Test with a valid specification string."""
Expand Down Expand Up @@ -691,19 +695,20 @@ def test_invalid_file_path(self):
load_parameters_file('nonexistent_file.csv')

def test_empty_file(self):
with NamedTemporaryFile(mode='w') as temp_file:
with NamedTemporaryFile(mode='r', suffix='.csv') as temp_file:
with self.assertRaises(pd.errors.EmptyDataError):
load_parameters_file(temp_file.name)

def test_with_dead_volume(self):
parameters_df = self.parameters_df.copy()
# Add deadVolume column with no value
parameters_df['deadVolume'] = ""
with NamedTemporaryFile(mode='w', suffix='.tsv') as temp_file:
parameters_df.to_csv(temp_file.name, index=False, sep='\t')
data = load_parameters_file(temp_file.name)
parameters_df['deadVolume'] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
pd.testing.assert_frame_equal(data, parameters_df)
tmp_fn = tmp_filepath(suffix='.tsv')
parameters_df.to_csv(tmp_fn, index=False, sep='\t')
data = load_parameters_file(tmp_fn)
clean_file(tmp_fn)
parameters_df['deadVolume'] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
pd.testing.assert_frame_equal(data, parameters_df)


class TestArgsParser(unittest.TestCase):
Expand Down Expand Up @@ -750,18 +755,21 @@ def setUp(self):
)

def test_valid_input(self):
with NamedTemporaryFile(mode='w', suffix='.csv') as temp_file:
args = [
input_file,
'-o', temp_file.name,
'--nb-samples', '40',
'--method', 'lhs',
'--seed', '42'
]
main(args)
actual = pd.read_csv(temp_file.name)
expected = pd.read_csv(ref_output_file)
pd.testing.assert_frame_equal(actual, expected)
# Do not use NamedTemporaryFile as opening a file
# already opened is not supported on Windows
tmp_fn = tmp_filepath()
args = [
input_file,
'-o', tmp_fn,
'--nb-samples', '40',
'--method', 'lhs',
'--seed', '42'
]
main(args)
actual = pd.read_csv(tmp_fn)
clean_file(tmp_fn)
expected = pd.read_csv(ref_output_file)
pd.testing.assert_frame_equal(actual, expected)

def test_missing_input(self):
with self.assertRaises(SystemExit):
Expand Down Expand Up @@ -794,6 +802,9 @@ def test_system_exit(self):
main(args)

def test_without_ouput_file(self):
# Do not run on Windows
if os_name == 'nt':
return
import sys
with NamedTemporaryFile(mode='w', suffix='.csv') as temp_file:
# simulate a redirection of the output to a file
Expand All @@ -812,9 +823,10 @@ def test_without_ouput_file(self):
pd.testing.assert_frame_equal(actual, expected)

def test_subprocess_call(self):
with NamedTemporaryFile(mode='w', suffix='.csv') as temp_file:
cmd = 'python -m icfree.sampler {} -o {} --nb-samples 40 --method lhs --seed 42'.format(input_file, temp_file.name)
sp_run(cmd.split())
actual = pd.read_csv(temp_file.name)
expected = pd.read_csv(ref_output_file)
pd.testing.assert_frame_equal(actual, expected)
tmp_fn = tmp_filepath()
cmd = 'python -m icfree.sampler {} -o {} --nb-samples 40 --method lhs --seed 42 --silent'.format(input_file, tmp_fn)
sp_run(cmd.split())
actual = pd.read_csv(tmp_fn)
clean_file(tmp_fn)
expected = pd.read_csv(ref_output_file)
pd.testing.assert_frame_equal(actual, expected)

0 comments on commit e37444b

Please sign in to comment.