From 74044f3c7985308b4996da3a52f91c5c20a19194 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 14 Feb 2022 06:56:32 +0300 Subject: Use pytest's "mark" feature to categorise tests Use pytest's `mark` feature to explicitly categorise the tests and run them per category --- tests/unit/computations/test_correlation.py | 18 ++++++++++++++++++ tests/unit/computations/test_dictify_by_samples.py | 1 + tests/unit/computations/test_diff.py | 3 +++ tests/unit/computations/test_gemma.py | 8 +++++++- tests/unit/computations/test_parsers.py | 4 ++++ tests/unit/computations/test_partial_correlations.py | 9 +++++++++ tests/unit/computations/test_qtlreaper.py | 4 ++++ tests/unit/computations/test_rqtl.py | 2 ++ tests/unit/computations/test_slink.py | 13 +++++++++++++ tests/unit/computations/test_wgcna.py | 6 ++++++ 10 files changed, 67 insertions(+), 1 deletion(-) (limited to 'tests/unit/computations') diff --git a/tests/unit/computations/test_correlation.py b/tests/unit/computations/test_correlation.py index 7523d99..69d4c52 100644 --- a/tests/unit/computations/test_correlation.py +++ b/tests/unit/computations/test_correlation.py @@ -2,6 +2,7 @@ from unittest import TestCase from unittest import mock +import pytest from collections import namedtuple import math from numpy.testing import assert_almost_equal @@ -91,6 +92,7 @@ class DataBase(QueryableMixin): class TestCorrelation(TestCase): """Class for testing correlation functions""" + @pytest.mark.unit_test def test_normalize_values(self): """Function to test normalizing values """ @@ -106,6 +108,7 @@ class TestCorrelation(TestCase): results = normalize_values(a_values, b_values) self.assertEqual(list(zip(*list(results))), expected_result) + @pytest.mark.unit_test @mock.patch("gn3.computations.correlations.compute_corr_coeff_p_value") @mock.patch("gn3.computations.correlations.normalize_values") def test_compute_sample_r_correlation(self, norm_vals, compute_corr): @@ -130,6 +133,7 @@ class TestCorrelation(TestCase): self.assertEqual(bicor_results, ("1412_at", 0.8, 0.21, 7)) + @pytest.mark.unit_test def test_filter_shared_sample_keys(self): """Function to tests shared key between two dicts""" @@ -157,6 +161,7 @@ class TestCorrelation(TestCase): self.assertEqual(list(zip(*list(results))), [filtered_this_samplelist, filtered_target_samplelist]) + @pytest.mark.unit_test @mock.patch("gn3.computations.correlations.compute_sample_r_correlation") @mock.patch("gn3.computations.correlations.filter_shared_sample_keys") def test_compute_all_sample(self, filter_shared_samples, sample_r_corr): @@ -199,6 +204,7 @@ class TestCorrelation(TestCase): corr_method="pearson", trait_vals=('1.23', '6.565', '6.456'), target_samples_vals=('6.266', '6.565', '6.456')) + @pytest.mark.unit_test @mock.patch("gn3.computations.correlations.compute_corr_coeff_p_value") def test_tissue_correlation_for_trait(self, mock_compute_corr_coeff): """Test given a primary tissue values for a trait and and a list of\ @@ -217,6 +223,7 @@ class TestCorrelation(TestCase): self.assertEqual(tissue_results, expected_tissue_results) + @pytest.mark.unit_test @mock.patch("gn3.computations.correlations.fetch_lit_correlation_data") @mock.patch("gn3.computations.correlations.map_to_mouse_gene_id") def test_lit_correlation_for_trait(self, mock_mouse_gene_id, fetch_lit_data): @@ -244,6 +251,7 @@ class TestCorrelation(TestCase): self.assertEqual(lit_results, expected_results) + @pytest.mark.unit_test def test_fetch_lit_correlation_data(self): """Test for fetching lit correlation data from\ the database where the input and mouse geneid are none @@ -257,6 +265,7 @@ class TestCorrelation(TestCase): self.assertEqual(results, ("1", 0)) + @pytest.mark.unit_test def test_fetch_lit_correlation_data_db_query(self): """Test for fetching lit corr coefficent givent the input\ input trait mouse gene id and mouse gene id @@ -274,6 +283,7 @@ class TestCorrelation(TestCase): self.assertEqual(expected_results, lit_results) + @pytest.mark.unit_test def test_query_lit_correlation_for_db_empty(self): """Test that corr coeffient returned is 0 given the\ db value if corr coefficient is empty @@ -289,6 +299,7 @@ class TestCorrelation(TestCase): self.assertEqual(lit_results, ("16", 0)) + @pytest.mark.unit_test def test_query_formatter(self): """Test for formatting a query given the query string and also the\ values @@ -316,6 +327,7 @@ class TestCorrelation(TestCase): self.assertEqual(formatted_query, expected_formatted_query) + @pytest.mark.unit_test def test_query_formatter_no_query_values(self): """Test for formatting a query where there are no\ string placeholder @@ -325,6 +337,7 @@ class TestCorrelation(TestCase): self.assertEqual(formatted_query, query) + @pytest.mark.unit_test def test_map_to_mouse_gene_id(self): """Test for converting a gene id to mouse geneid\ given a species which is not mouse @@ -348,6 +361,7 @@ class TestCorrelation(TestCase): self.assertEqual(results, expected_results) + @pytest.mark.unit_test @mock.patch("gn3.computations.correlations.lit_correlation_for_trait") def test_compute_all_lit_correlation(self, mock_lit_corr): """Test for compute all lit correlation which acts\ @@ -368,6 +382,7 @@ class TestCorrelation(TestCase): self.assertEqual(lit_correlation_results, expected_mocked_lit_results) + @pytest.mark.unit_test @mock.patch("gn3.computations.correlations.tissue_correlation_for_trait") @mock.patch("gn3.computations.correlations.process_trait_symbol_dict") def test_compute_all_tissue_correlation(self, process_trait_symbol, mock_tissue_corr): @@ -411,6 +426,7 @@ class TestCorrelation(TestCase): self.assertEqual(results, expected_results) + @pytest.mark.unit_test def test_map_shared_keys_to_values(self): """test helper function needed to integrate with genenenetwork2\ given a a samplelist containing dataset sampelist keys\ @@ -431,6 +447,7 @@ class TestCorrelation(TestCase): self.assertEqual(results, expected_results) + @pytest.mark.unit_test def test_process_trait_symbol_dict(self): """test for processing trait symbol dict\ and fetch tissue values from tissue value dict\ @@ -449,6 +466,7 @@ class TestCorrelation(TestCase): self.assertEqual(results, [expected_results]) + @pytest.mark.unit_test def test_compute_correlation(self): """Test that the new correlation function works the same as the original from genenetwork1.""" diff --git a/tests/unit/computations/test_dictify_by_samples.py b/tests/unit/computations/test_dictify_by_samples.py index decc095..8a1332f 100644 --- a/tests/unit/computations/test_dictify_by_samples.py +++ b/tests/unit/computations/test_dictify_by_samples.py @@ -63,6 +63,7 @@ values = st.lists(st.floats()) variances = st.lists(st.one_of(st.none(), st.floats())) other = st.lists(st.integers()) +@pytest.mark.unit_test @given(svv=st.tuples( st.lists(non_empty_samples), st.lists(values), diff --git a/tests/unit/computations/test_diff.py b/tests/unit/computations/test_diff.py index e4f5dde..128fb60 100644 --- a/tests/unit/computations/test_diff.py +++ b/tests/unit/computations/test_diff.py @@ -2,6 +2,8 @@ import unittest import os +import pytest + from gn3.computations.diff import generate_diff TESTDIFF = """3,4c3,4 @@ -19,6 +21,7 @@ TESTDIFF = """3,4c3,4 class TestDiff(unittest.TestCase): """Test cases for computations.diff""" + @pytest.mark.unit_test def test_generate_diff(self): """Test that the correct diff is generated""" data = os.path.join(os.path.dirname(__file__).split("unit")[0], diff --git a/tests/unit/computations/test_gemma.py b/tests/unit/computations/test_gemma.py index 73dd5eb..b36a93e 100644 --- a/tests/unit/computations/test_gemma.py +++ b/tests/unit/computations/test_gemma.py @@ -1,7 +1,9 @@ """Test cases for procedures defined in computations.gemma""" import unittest - from unittest import mock + +import pytest + from gn3.computations.gemma import generate_gemma_cmd from gn3.computations.gemma import generate_hash_of_string from gn3.computations.gemma import generate_pheno_txt_file @@ -9,6 +11,7 @@ from gn3.computations.gemma import generate_pheno_txt_file class TestGemma(unittest.TestCase): """Test cases for computations.gemma module""" + @pytest.mark.unit_test def test_generate_pheno_txt_file(self): """Test that the pheno text file is generated correctly""" open_mock = mock.mock_open() @@ -26,11 +29,13 @@ class TestGemma(unittest.TestCase): mock.call("BXD07 438.700\n") ]) + @pytest.mark.unit_test def test_generate_hash_of_string(self): """Test that a string is hashed correctly""" self.assertEqual(generate_hash_of_string("I^iQP&TlSR^z"), "hMVRw8kbEp49rOmoIkhMjA") + @pytest.mark.unit_test @mock.patch("gn3.computations.gemma.get_hash_of_files") def test_compute_k_values_without_loco(self, mock_get_hash): """Test computing k values without loco""" @@ -52,6 +57,7 @@ class TestGemma(unittest.TestCase): "-gk > /tmp/my-token/my-hash-output.json") }) + @pytest.mark.unit_test @mock.patch("gn3.computations.gemma.get_hash_of_files") def test_generate_gemma_cmd_with_loco(self, mock_get_hash): """Test computing k values with loco""" diff --git a/tests/unit/computations/test_parsers.py b/tests/unit/computations/test_parsers.py index b51b0bf..f05f766 100644 --- a/tests/unit/computations/test_parsers.py +++ b/tests/unit/computations/test_parsers.py @@ -2,17 +2,21 @@ import unittest import os +import pytest + from gn3.computations.parsers import parse_genofile class TestParsers(unittest.TestCase): """Test cases for some various parsers""" + @pytest.mark.unit_test def test_parse_genofile_without_existing_file(self): """Assert that an error is raised if the genotype file is absent""" self.assertRaises(FileNotFoundError, parse_genofile, "/non-existent-file") + @pytest.mark.unit_test def test_parse_genofile_with_existing_file(self): """Test that a genotype file is parsed correctly""" samples = ["bxd1", "bxd2"] diff --git a/tests/unit/computations/test_partial_correlations.py b/tests/unit/computations/test_partial_correlations.py index 3690ca4..ee17659 100644 --- a/tests/unit/computations/test_partial_correlations.py +++ b/tests/unit/computations/test_partial_correlations.py @@ -3,6 +3,7 @@ from unittest import TestCase import pandas +import pytest from numpy.testing import assert_allclose from gn3.computations.partial_correlations import ( @@ -98,6 +99,7 @@ dictified_control_samples = ( class TestPartialCorrelations(TestCase): """Class for testing partial correlations computation functions""" + @pytest.mark.unit_test def test_control_samples(self): """Test that the control_samples works as expected.""" self.assertEqual( @@ -112,6 +114,7 @@ class TestPartialCorrelations(TestCase): (None, None, None)), (6, 4, 3))) + @pytest.mark.unit_test def test_dictify_by_samples(self): """ Test that `dictify_by_samples` generates the appropriate dict @@ -142,6 +145,7 @@ class TestPartialCorrelations(TestCase): (6, 4, 3))), dictified_control_samples) + @pytest.mark.unit_test def test_fix_samples(self): """ Test that `fix_samples` returns only the common samples @@ -187,6 +191,7 @@ class TestPartialCorrelations(TestCase): (None, None, None, None, None, None, None, None, None, None, None, None, None))) + @pytest.mark.unit_test def test_find_identical_traits(self): """ Test `gn3.partial_correlations.find_identical_traits`. @@ -219,6 +224,7 @@ class TestPartialCorrelations(TestCase): self.assertEqual( find_identical_traits(primn, primv, contn, contv), expected) + @pytest.mark.unit_test def test_tissue_correlation_error(self): """ Test that `tissue_correlation` raises specific exceptions for particular @@ -253,6 +259,7 @@ class TestPartialCorrelations(TestCase): with self.assertRaises(error, msg=error_msg): tissue_correlation(primary, target, method) + @pytest.mark.unit_test def test_tissue_correlation(self): # pylint: disable=R0201 """ Test that the correct correlation values are computed for the given: @@ -269,6 +276,7 @@ class TestPartialCorrelations(TestCase): assert_allclose( tissue_correlation(primary, target, method), expected) + @pytest.mark.unit_test def test_good_dataset_samples_indexes(self): """ Test that `good_dataset_samples_indexes` returns correct indices. @@ -279,6 +287,7 @@ class TestPartialCorrelations(TestCase): ("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l")), (0, 4, 8, 10)) + @pytest.mark.unit_test def test_build_data_frame(self): """ Check that the function builds the correct data frame. diff --git a/tests/unit/computations/test_qtlreaper.py b/tests/unit/computations/test_qtlreaper.py index 742d106..607f4a6 100644 --- a/tests/unit/computations/test_qtlreaper.py +++ b/tests/unit/computations/test_qtlreaper.py @@ -1,5 +1,6 @@ """Module contains tests for gn3.computations.qtlreaper""" from unittest import TestCase +import pytest from gn3.computations.qtlreaper import ( parse_reaper_main_results, organise_reaper_main_results, @@ -9,6 +10,7 @@ from tests.unit.sample_test_data import organised_trait_1 class TestQTLReaper(TestCase): """Class for testing qtlreaper interface functions.""" + @pytest.mark.unit_test def test_parse_reaper_main_results(self): """Test that the main results file is parsed correctly.""" self.assertEqual( @@ -67,6 +69,7 @@ class TestQTLReaper(TestCase): } ]) + @pytest.mark.unit_test def test_parse_reaper_permutation_results(self): """Test that the permutations results file is parsed correctly.""" self.assertEqual( @@ -77,6 +80,7 @@ class TestQTLReaper(TestCase): 5.63874, 5.71346, 5.71936, 5.74275, 5.76764, 5.79815, 5.81671, 5.82775, 5.89659, 5.92117, 5.93396, 5.93396, 5.94957]) + @pytest.mark.unit_test def test_organise_reaper_main_results(self): """Check that results are organised correctly.""" self.assertEqual( diff --git a/tests/unit/computations/test_rqtl.py b/tests/unit/computations/test_rqtl.py index 955d0ab..51df281 100644 --- a/tests/unit/computations/test_rqtl.py +++ b/tests/unit/computations/test_rqtl.py @@ -2,10 +2,12 @@ import unittest from unittest import mock +import pytest from gn3.computations.rqtl import generate_rqtl_cmd class TestRqtl(unittest.TestCase): """Test cases for computations.rqtl module""" + @pytest.mark.unit_test @mock.patch("gn3.computations.rqtl.generate_hash_of_string") @mock.patch("gn3.computations.rqtl.get_hash_of_files") def test_generate_rqtl_command(self, mock_get_hash_files, mock_generate_hash_string): diff --git a/tests/unit/computations/test_slink.py b/tests/unit/computations/test_slink.py index 995393b..276133a 100644 --- a/tests/unit/computations/test_slink.py +++ b/tests/unit/computations/test_slink.py @@ -1,6 +1,8 @@ """Module contains tests for slink""" from unittest import TestCase +import pytest + from gn3.computations.slink import slink from gn3.computations.slink import nearest from gn3.computations.slink import LengthError @@ -9,6 +11,7 @@ from gn3.computations.slink import MirrorError class TestSlink(TestCase): """Class for testing slink functions""" + @pytest.mark.unit_test def test_nearest_expects_list_of_lists(self): """Test that function only accepts a list of lists.""" # This might be better handled with type-hints and mypy @@ -18,6 +21,7 @@ class TestSlink(TestCase): with self.assertRaises(ValueError, msg="Expected list or tuple"): nearest(item, 1, 1) + @pytest.mark.unit_test def test_nearest_does_not_allow_empty_lists(self): """Test that function does not accept an empty list, or any of the child lists to be empty.""" @@ -29,6 +33,7 @@ class TestSlink(TestCase): with self.assertRaises(ValueError): nearest(lst, 1, 1) + @pytest.mark.unit_test def test_nearest_expects_children_are_same_length_as_parent(self): """Test that children lists are same length as parent list.""" for lst in [[[0, 1]], @@ -40,6 +45,7 @@ class TestSlink(TestCase): with self.assertRaises(LengthError): nearest(lst, 1, 1) + @pytest.mark.unit_test def test_nearest_expects_member_is_zero_distance_from_itself(self): """Test that distance of a member from itself is zero""" for lst in [[[1]], @@ -50,6 +56,7 @@ class TestSlink(TestCase): with self.assertRaises(ValueError): nearest(lst, 1, 1) + @pytest.mark.unit_test def test_nearest_expects_distance_atob_is_equal_to_distance_btoa(self): """Test that the distance from member A to member B is the same as that from member B to member A.""" @@ -60,6 +67,7 @@ class TestSlink(TestCase): with self.assertRaises(MirrorError): nearest(lst, 1, 1) + @pytest.mark.unit_test def test_nearest_expects_zero_or_positive_distances(self): """Test that all distances are either zero, or greater than zero.""" # Based on: @@ -74,6 +82,7 @@ class TestSlink(TestCase): with self.assertRaises(ValueError, msg="Distances should be positive."): nearest(lst, 1, 1) + @pytest.mark.unit_test def test_nearest_returns_shortest_distance_given_coordinates_to_both_group_members(self): """Test that the shortest distance is returned.""" # This test is named wrong - at least I think it is, from the expected results @@ -234,6 +243,7 @@ class TestSlink(TestCase): with self.subTest(lst=lst): self.assertEqual(nearest(lst, i, j), expected) + @pytest.mark.unit_test def test_nearest_gives_shortest_distance_between_list_of_members_and_member(self): """Test that the shortest distance is returned.""" for members_distances, members_list, member_coordinate, expected_distance in [ @@ -260,6 +270,7 @@ class TestSlink(TestCase): members_distances, member_coordinate, members_list), expected_distance) + @pytest.mark.unit_test def test_nearest_returns_shortest_distance_given_two_lists_of_members(self): """Test that the shortest distance is returned.""" for members_distances, members_list, member_list2, expected_distance in [ @@ -289,12 +300,14 @@ class TestSlink(TestCase): members_distances, member_list2, members_list), expected_distance) + @pytest.mark.unit_test def test_slink_wrong_data_returns_empty_list(self): """Test that empty list is returned for wrong data.""" for data in [1, "test", [], 2.945, nearest, [0]]: with self.subTest(data=data): self.assertEqual(slink(data), []) + @pytest.mark.unit_test def test_slink_with_data(self): """Test slink with example data, and expected results for each data sample.""" diff --git a/tests/unit/computations/test_wgcna.py b/tests/unit/computations/test_wgcna.py index 5f23a86..3130374 100644 --- a/tests/unit/computations/test_wgcna.py +++ b/tests/unit/computations/test_wgcna.py @@ -2,6 +2,8 @@ from unittest import TestCase from unittest import mock +import pytest + from gn3.computations.wgcna import dump_wgcna_data from gn3.computations.wgcna import compose_wgcna_cmd from gn3.computations.wgcna import call_wgcna_script @@ -10,6 +12,7 @@ from gn3.computations.wgcna import call_wgcna_script class TestWgcna(TestCase): """test class for wgcna""" + @pytest.mark.unit_test @mock.patch("gn3.computations.wgcna.process_image") @mock.patch("gn3.computations.wgcna.run_cmd") @mock.patch("gn3.computations.wgcna.compose_wgcna_cmd") @@ -95,6 +98,7 @@ class TestWgcna(TestCase): self.assertEqual(results, expected_output) + @pytest.mark.unit_test @mock.patch("gn3.computations.wgcna.run_cmd") @mock.patch("gn3.computations.wgcna.compose_wgcna_cmd") @mock.patch("gn3.computations.wgcna.dump_wgcna_data") @@ -117,6 +121,7 @@ class TestWgcna(TestCase): self.assertEqual(call_wgcna_script( "input_file.R", ""), expected_error) + @pytest.mark.unit_test def test_compose_wgcna_cmd(self): """test for composing wgcna cmd""" wgcna_cmd = compose_wgcna_cmd( @@ -124,6 +129,7 @@ class TestWgcna(TestCase): self.assertEqual( wgcna_cmd, "Rscript ./scripts/wgcna.r /tmp/wgcna.json") + @pytest.mark.unit_test @mock.patch("gn3.computations.wgcna.TMPDIR", "/tmp") @mock.patch("gn3.computations.wgcna.uuid.uuid4") def test_create_json_file(self, file_name_generator): -- cgit v1.2.3