diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/unit/computations/test_partial_correlations.py (renamed from tests/unit/test_partial_correlations.py) | 66 | ||||
-rw-r--r-- | tests/unit/test_data_helpers.py | 26 |
2 files changed, 89 insertions, 3 deletions
diff --git a/tests/unit/test_partial_correlations.py b/tests/unit/computations/test_partial_correlations.py index 60e54c1..f7217a9 100644 --- a/tests/unit/test_partial_correlations.py +++ b/tests/unit/computations/test_partial_correlations.py @@ -1,11 +1,13 @@ """Module contains tests for gn3.partial_correlations""" from unittest import TestCase -from gn3.partial_correlations import ( +from gn3.computations.partial_correlations import ( fix_samples, control_samples, dictify_by_samples, - find_identical_traits) + tissue_correlation, + find_identical_traits, + good_dataset_samples_indexes) sampleslist = ["B6cC3-1", "BXD1", "BXD12", "BXD16", "BXD19", "BXD2"] control_traits = ( @@ -209,3 +211,63 @@ class TestPartialCorrelations(TestCase): control_names=contn, control_values=contv): self.assertEqual( find_identical_traits(primn, primv, contn, contv), expected) + + def test_tissue_correlation_error(self): + """ + Test that `tissue_correlation` raises specific exceptions for particular + error conditions. + """ + for primary, target, method, error, error_msg in ( + ((1, 2, 3), (4, 5, 6, 7), "pearson", + AssertionError, + ( + "The lengths of the `primary_trait_values` and " + "`target_trait_values` must be equal")), + ((1, 2, 3), (4, 5, 6, 7), "spearman", + AssertionError, + ( + "The lengths of the `primary_trait_values` and " + "`target_trait_values` must be equal")), + ((1, 2, 3, 4), (5, 6, 7), "pearson", + AssertionError, + ( + "The lengths of the `primary_trait_values` and " + "`target_trait_values` must be equal")), + ((1, 2, 3, 4), (5, 6, 7), "spearman", + AssertionError, + ( + "The lengths of the `primary_trait_values` and " + "`target_trait_values` must be equal")), + ((1, 2, 3), (4, 5, 6), "nonexistentmethod", + AssertionError, + ( + "Method must be one of: pearson, spearman"))): + with self.subTest(primary=primary, target=target, method=method): + with self.assertRaises(error, msg=error_msg): + tissue_correlation(primary, target, method) + + def test_tissue_correlation(self): + """ + Test that the correct correlation values are computed for the given: + - primary trait + - target trait + - method + """ + for primary, target, method, expected in ( + ((12.34, 18.36, 42.51), (37.25, 46.25, 46.56), "pearson", + (0.6761779253, 0.5272701134)), + ((1, 2, 3, 4, 5), (5, 6, 7, 8, 7), "spearman", + (0.8207826817, 0.0885870053))): + with self.subTest(primary=primary, target=target, method=method): + self.assertEqual( + tissue_correlation(primary, target, method), expected) + + def test_good_dataset_samples_indexes(self): + """ + Test that `good_dataset_samples_indexes` returns correct indices. + """ + self.assertEqual( + good_dataset_samples_indexes( + ("a", "e", "i", "k"), + ("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l")), + (0, 4, 8, 10)) diff --git a/tests/unit/test_data_helpers.py b/tests/unit/test_data_helpers.py index 1eec3cc..39aea45 100644 --- a/tests/unit/test_data_helpers.py +++ b/tests/unit/test_data_helpers.py @@ -4,7 +4,7 @@ Test functions in gn3.data_helpers from unittest import TestCase -from gn3.data_helpers import partition_all +from gn3.data_helpers import partition_all, parse_csv_line class TestDataHelpers(TestCase): """ @@ -35,3 +35,27 @@ class TestDataHelpers(TestCase): ((0, 1, 2, 3, 4, 5, 6, 7, 8, 9), ))): with self.subTest(n=count, items=items): self.assertEqual(partition_all(count, items), expected) + + def test_parse_csv_line(self): + """ + Test parsing a single line from a CSV file + + Given: + - `line`: a line read from a csv file + - `delimiter`: the expected delimiter in the csv file + - `quoting`: the quoting enclosing each column in the csv file + When: + - `line` is parsed with the `parse_csv_file` with the given + parameters + Then: + - return a tuple of the columns in the CSV file, without the + delimiter and quoting + """ + for line, delimiter, quoting, expected in ( + ('"this","is","a","test"', ",", '"', ("this", "is", "a", "test")), + ('"this","is","a","test"', ",", None, ('"this"', '"is"', '"a"', '"test"'))): + with self.subTest(line=line, delimiter=delimiter, quoting=quoting): + self.assertEqual( + parse_csv_line( + line=line, delimiter=delimiter, quoting=quoting), + expected) |