From a35fce27875d9db80dce1976b6f8ee8c00ecfe0a Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 21 Feb 2022 16:23:06 +0300 Subject: Fix a myriad of linter issues * Use `with` in place of plain `open` * Use f-strings in place of `str.format()` * Remove string interpolation from queries - provide data as query parameters * other minor fixes --- tests/performance/perf_query.py | 6 ++--- tests/unit/computations/test_correlation.py | 8 +++---- tests/unit/computations/test_dictify_by_samples.py | 26 +++++++++++++--------- tests/unit/db/test_datasets.py | 17 +++++++------- tests/unit/test_data_helpers.py | 2 +- tests/unit/test_db_utils.py | 2 +- tests/unit/test_file_utils.py | 3 ++- 7 files changed, 34 insertions(+), 30 deletions(-) (limited to 'tests') diff --git a/tests/performance/perf_query.py b/tests/performance/perf_query.py index 12cb944..c22dcf5 100644 --- a/tests/performance/perf_query.py +++ b/tests/performance/perf_query.py @@ -42,7 +42,7 @@ def query_executor(query: str, def fetch_probeset_query(dataset_name: str): """contains queries for datasets""" - query = """SELECT * from ProbeSetData + query = f"""SELECT * from ProbeSetData where StrainID in (4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 17, 18, 19, 20, 21, 22, 24, 25, 26, 28, 29, 30, 31, 35, 36, 37, 39, 98, 99, 100, 103, @@ -53,8 +53,8 @@ def fetch_probeset_query(dataset_name: str): and id in (SELECT ProbeSetXRef.DataId FROM (ProbeSet, ProbeSetXRef, ProbeSetFreeze) WHERE ProbeSetXRef.ProbeSetFreezeId = ProbeSetFreeze.Id - and ProbeSetFreeze.Name = '{}' - and ProbeSet.Id = ProbeSetXRef.ProbeSetId)""".format(dataset_name) + and ProbeSetFreeze.Name = '{dataset_name}' + and ProbeSet.Id = ProbeSetXRef.ProbeSetId)""" return query diff --git a/tests/unit/computations/test_correlation.py b/tests/unit/computations/test_correlation.py index 69d4c52..267ced3 100644 --- a/tests/unit/computations/test_correlation.py +++ b/tests/unit/computations/test_correlation.py @@ -1,10 +1,10 @@ """Module contains the tests for correlation""" +import math from unittest import TestCase from unittest import mock +from collections import namedtuple import pytest -from collections import namedtuple -import math from numpy.testing import assert_almost_equal from gn3.computations.correlations import normalize_values @@ -58,12 +58,12 @@ class DataBase(QueryableMixin): """expects the expectede results value to be an array""" self.password = password self.db_name = db_name - self.__query_options = None + self.__query_options = None # pylint: disable=[W0238] self.results_generator(expected_results) def execute(self, query_options): """method to execute an sql query""" - self.__query_options = query_options + self.__query_options = query_options # pylint: disable=[W0238] return 1 def cursor(self): diff --git a/tests/unit/computations/test_dictify_by_samples.py b/tests/unit/computations/test_dictify_by_samples.py index 8a1332f..5cd3eca 100644 --- a/tests/unit/computations/test_dictify_by_samples.py +++ b/tests/unit/computations/test_dictify_by_samples.py @@ -1,7 +1,11 @@ +"""Property tests for `gn3.computations.partial_correlations.dictify_by_samples` + function""" from math import isnan -import pytest from collections.abc import Sequence + +import pytest from hypothesis import given, strategies as st + from gn3.computations.partial_correlations import dictify_by_samples @@ -53,22 +57,22 @@ def check_values(samples, values, variances, row): for smp, val, var in zip(samples, values, variances) if smp != "") -non_empty_samples = st.lists( +generated_non_empty_samples = st.lists( st.text(min_size=1, max_size=15).map( lambda s: s.strip())) -empty_samples = st.text( +generated_empty_samples = st.text( alphabet=" \t\n\r\f\v", min_size=1, max_size=15).filter( lambda s: len(s.strip()) == 0) -values = st.lists(st.floats()) -variances = st.lists(st.one_of(st.none(), st.floats())) -other = st.lists(st.integers()) +generated_values = st.lists(st.floats()) +generated_variances = st.lists(st.one_of(st.none(), st.floats())) +generated_other = st.lists(st.integers()) @pytest.mark.unit_test @given(svv=st.tuples( - st.lists(non_empty_samples), - st.lists(values), - st.lists(variances), - st.lists(other))) + st.lists(generated_non_empty_samples), + st.lists(generated_values), + st.lists(generated_variances), + st.lists(generated_other))) def test_dictifify_by_samples_with_nonempty_samples_strings(svv): """ Test for `dictify_by_samples`. @@ -94,7 +98,7 @@ def test_dictifify_by_samples_with_nonempty_samples_strings(svv): @pytest.mark.unit_test @given(svv=st.tuples( st.lists( - st.lists(empty_samples,min_size=1), + st.lists(generated_empty_samples,min_size=1), min_size=1), st.lists(st.lists(st.floats(), min_size=1), min_size=1), st.lists( diff --git a/tests/unit/db/test_datasets.py b/tests/unit/db/test_datasets.py index 0b24489..e4abd2f 100644 --- a/tests/unit/db/test_datasets.py +++ b/tests/unit/db/test_datasets.py @@ -15,14 +15,14 @@ class TestDatasetsDBFunctions(TestCase): @pytest.mark.unit_test def test_retrieve_dataset_name(self): """Test that the function is called correctly.""" - for trait_type, thresh, trait_name, dataset_name, columns, table, expected in [ - ["ProbeSet", 9, "probesetTraitName", "probesetDatasetName", + for trait_type, thresh, dataset_name, columns, table, expected in [ + ["ProbeSet", 9, "probesetDatasetName", "Id, Name, FullName, ShortName, DataScale", "ProbeSetFreeze", {"dataset_id": None, "dataset_name": "probesetDatasetName", "dataset_fullname": "probesetDatasetName"}], - ["Geno", 3, "genoTraitName", "genoDatasetName", + ["Geno", 3, "genoDatasetName", "Id, Name, FullName, ShortName", "GenoFreeze", {}], - ["Publish", 6, "publishTraitName", "publishDatasetName", + ["Publish", 6, "publishDatasetName", "Id, Name, FullName, ShortName", "PublishFreeze", {}]]: db_mock = mock.MagicMock() with self.subTest(trait_type=trait_type): @@ -30,16 +30,15 @@ class TestDatasetsDBFunctions(TestCase): cursor.fetchone.return_value = {} self.assertEqual( retrieve_dataset_name( - trait_type, thresh, trait_name, dataset_name, db_mock), + trait_type, thresh, dataset_name, db_mock), expected) cursor.execute.assert_called_once_with( - "SELECT {cols} " - "FROM {table} " + f"SELECT {columns} " + f"FROM {table} " "WHERE public > %(threshold)s AND " "(Name = %(name)s " "OR FullName = %(name)s " - "OR ShortName = %(name)s)".format( - table=table, cols=columns), + "OR ShortName = %(name)s)", {"threshold": thresh, "name": dataset_name}) @pytest.mark.unit_test diff --git a/tests/unit/test_data_helpers.py b/tests/unit/test_data_helpers.py index b6de42e..e7c3ae9 100644 --- a/tests/unit/test_data_helpers.py +++ b/tests/unit/test_data_helpers.py @@ -37,7 +37,7 @@ class TestDataHelpers(TestCase): (13, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], ((0, 1, 2, 3, 4, 5, 6, 7, 8, 9), ))): with self.subTest(n=count, items=items): - self.assertEqual(partition_all(count, items), expected) + self.assertEqual(tuple(partition_all(count, items)), expected) @pytest.mark.unit_test def test_parse_csv_line(self): diff --git a/tests/unit/test_db_utils.py b/tests/unit/test_db_utils.py index dd0cd5d..96ee68f 100644 --- a/tests/unit/test_db_utils.py +++ b/tests/unit/test_db_utils.py @@ -2,9 +2,9 @@ from unittest import TestCase from unittest import mock +from types import SimpleNamespace import pytest -from types import SimpleNamespace from gn3.db_utils import database_connector from gn3.db_utils import parse_db_url diff --git a/tests/unit/test_file_utils.py b/tests/unit/test_file_utils.py index 77fea88..7048d43 100644 --- a/tests/unit/test_file_utils.py +++ b/tests/unit/test_file_utils.py @@ -92,7 +92,8 @@ extracting the file""" test_dir = "/tmp/QmQPeNsJPyVWPFDVHb77w8G42Fvo15z4bG2X8D2GhfbSXc-test" if not os.path.exists(test_dir): os.mkdir(test_dir) - open(f"{test_dir}/genotype.txt", "a").close() + with open(f"{test_dir}/genotype.txt", "a", encoding="utf8"): + pass file_loc = cache_ipfs_file( ipfs_file=("/ipfs/" "QmQPeNsJPyVWPFDVHb" -- cgit v1.2.3