From 6b4bcc526c27a17f96704b54fbfa6a506b3a22f9 Mon Sep 17 00:00:00 2001 From: Alexanderlacuna Date: Thu, 5 Nov 2020 22:30:33 +0300 Subject: add test for samplelist and show_trait --- wqflask/tests/wqflask/show_trait/testSampleList.py | 18 +++++++ .../tests/wqflask/show_trait/test_show_trait.py | 55 ++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 wqflask/tests/wqflask/show_trait/testSampleList.py diff --git a/wqflask/tests/wqflask/show_trait/testSampleList.py b/wqflask/tests/wqflask/show_trait/testSampleList.py new file mode 100644 index 00000000..4cc8c4da --- /dev/null +++ b/wqflask/tests/wqflask/show_trait/testSampleList.py @@ -0,0 +1,18 @@ +import unittest +import re +from unittest import mock +from wqflask.show_trait.SampleList import natural_sort + + +class TestSampleList(unittest.TestCase): + def test_natural_sort(self): + "Sort the list into natural alphanumeric order." + + characters_list = ["z", "f", "q", "s", "t", "a", "g"] + names_list = ["temp1", "publish", "Sample", "Dataset"] + + natural_sort(characters_list) + natural_sort(names_list) + + self.assertEqual(characters_list, ["a", "f", "g", "q", "s", "t", "z"]) + self.assertEqual(names_list, ["Dataset", "Sample", "publish", "temp1"]) diff --git a/wqflask/tests/wqflask/show_trait/test_show_trait.py b/wqflask/tests/wqflask/show_trait/test_show_trait.py index 24150738..b1a91bbe 100644 --- a/wqflask/tests/wqflask/show_trait/test_show_trait.py +++ b/wqflask/tests/wqflask/show_trait/test_show_trait.py @@ -11,6 +11,7 @@ from wqflask.show_trait.show_trait import get_trait_units from wqflask.show_trait.show_trait import get_nearest_marker from wqflask.show_trait.show_trait import get_genotype_scales from wqflask.show_trait.show_trait import requests +from wqflask.show_trait.show_trait import get_scales_from_genofile class TraitObject: @@ -240,3 +241,57 @@ class TestTraits(unittest.TestCase): expected_results = {f"{file_location}": [["physic", "Mb"]]} self.assertEqual(get_genotype_scales(file_location), expected_results) mock_get_scales.assert_called_once_with(file_location) + + @mock.patch("wqflask.show_trait.show_trait.locate_ignore_error") + def test_get_scales_from_genofile_found(self, mock_location_ignore): + # test no complete to be continued with + # a .geno file + mock_location_ignore.return_value = True + mock_file_with_one_line = mock.mock_open( + read_data="Some data from opened file") + + mock_file = """#@scale and random data:is here_returned\n + This is second with spaced with tabs\n """ + mock_file_result = mock.mock_open(read_data=mock_file) + + with mock.patch("builtins.open", mock_file_with_one_line): + result = get_scales_from_genofile("~/data/file") + self.assertEqual(result, [['morgan', 'cM']]) + + with mock.patch("builtins.open", mock_file_result): + results = get_scales_from_genofile("~data/file_geno") + self.assertEqual(results, [['physic', 'Mb']]) + + @mock.patch("wqflask.show_trait.show_trait.locate_ignore_error") + def test_get_scales_from_genofile_found(self, mock_ingore_location): + """"add test for get scales from genofile where file is found""" + mock_ingore_location.return_value = True + geno_file = """ + #sample line with no @scales:other\n + #sample line @scales and :separated by semicolon\n + This attempts to check whether\n + """ + + geno_file_string = "@line start with @ and has @scale:morgan" + + file_location = "~/data/file.geno" + + mock_open_geno_file = mock.mock_open(read_data=geno_file) + with mock.patch("builtins.open", mock_open_geno_file): + results = get_scales_from_genofile(file_location) + self.assertEqual(results, [["morgan", "cM"]]) + + mock_open_string = mock.mock_open(read_data=geno_file_string) + + with mock.patch("builtins.open", mock_open_string): + result2 = get_scales_from_genofile(file_location) + self.assertEqual([['morgan', 'cM']], result2) + + @mock.patch("wqflask.show_trait.show_trait.locate_ignore_error") + def test_get_scales_from_genofile_not_found(self, mock_location_ignore): + mock_location_ignore.return_value = False + + expected_results = [["physic", "Mb"]] + results = get_scales_from_genofile("~/data/file") + mock_location_ignore.assert_called_once_with("~/data/file", "genotype") + self.assertEqual(results, expected_results) -- cgit v1.2.3 From 162e3e1acc1fea2548f7caa71fa3fc425c0a4ccd Mon Sep 17 00:00:00 2001 From: Alexanderlacuna Date: Thu, 5 Nov 2020 22:47:30 +0300 Subject: remove duplicates --- wqflask/tests/wqflask/show_trait/test_show_trait.py | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/wqflask/tests/wqflask/show_trait/test_show_trait.py b/wqflask/tests/wqflask/show_trait/test_show_trait.py index b1a91bbe..600baefb 100644 --- a/wqflask/tests/wqflask/show_trait/test_show_trait.py +++ b/wqflask/tests/wqflask/show_trait/test_show_trait.py @@ -242,25 +242,6 @@ class TestTraits(unittest.TestCase): self.assertEqual(get_genotype_scales(file_location), expected_results) mock_get_scales.assert_called_once_with(file_location) - @mock.patch("wqflask.show_trait.show_trait.locate_ignore_error") - def test_get_scales_from_genofile_found(self, mock_location_ignore): - # test no complete to be continued with - # a .geno file - mock_location_ignore.return_value = True - mock_file_with_one_line = mock.mock_open( - read_data="Some data from opened file") - - mock_file = """#@scale and random data:is here_returned\n - This is second with spaced with tabs\n """ - mock_file_result = mock.mock_open(read_data=mock_file) - - with mock.patch("builtins.open", mock_file_with_one_line): - result = get_scales_from_genofile("~/data/file") - self.assertEqual(result, [['morgan', 'cM']]) - - with mock.patch("builtins.open", mock_file_result): - results = get_scales_from_genofile("~data/file_geno") - self.assertEqual(results, [['physic', 'Mb']]) @mock.patch("wqflask.show_trait.show_trait.locate_ignore_error") def test_get_scales_from_genofile_found(self, mock_ingore_location): -- cgit v1.2.3 From 7078bd8a2d9d3a2ab8f31e102deeece8bc0e5fcf Mon Sep 17 00:00:00 2001 From: Alexanderlacuna Date: Fri, 6 Nov 2020 00:45:50 +0300 Subject: fix typo --- wqflask/tests/wqflask/show_trait/test_show_trait.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wqflask/tests/wqflask/show_trait/test_show_trait.py b/wqflask/tests/wqflask/show_trait/test_show_trait.py index 600baefb..8c866874 100644 --- a/wqflask/tests/wqflask/show_trait/test_show_trait.py +++ b/wqflask/tests/wqflask/show_trait/test_show_trait.py @@ -244,9 +244,9 @@ class TestTraits(unittest.TestCase): @mock.patch("wqflask.show_trait.show_trait.locate_ignore_error") - def test_get_scales_from_genofile_found(self, mock_ingore_location): + def test_get_scales_from_genofile_found(self, mock_ignore_location): """"add test for get scales from genofile where file is found""" - mock_ingore_location.return_value = True + mock_ignore_location.return_value = True geno_file = """ #sample line with no @scales:other\n #sample line @scales and :separated by semicolon\n -- cgit v1.2.3 From 6b23bf4a0698339a1c7672b8d84dfef9b9066a79 Mon Sep 17 00:00:00 2001 From: Alexanderlacuna Date: Fri, 6 Nov 2020 10:59:12 +0300 Subject: modify tests to for changes in the natural sort function --- wqflask/tests/wqflask/show_trait/testSampleList.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/wqflask/tests/wqflask/show_trait/testSampleList.py b/wqflask/tests/wqflask/show_trait/testSampleList.py index 4cc8c4da..34c51e3e 100644 --- a/wqflask/tests/wqflask/show_trait/testSampleList.py +++ b/wqflask/tests/wqflask/show_trait/testSampleList.py @@ -10,9 +10,7 @@ class TestSampleList(unittest.TestCase): characters_list = ["z", "f", "q", "s", "t", "a", "g"] names_list = ["temp1", "publish", "Sample", "Dataset"] - - natural_sort(characters_list) - natural_sort(names_list) - - self.assertEqual(characters_list, ["a", "f", "g", "q", "s", "t", "z"]) - self.assertEqual(names_list, ["Dataset", "Sample", "publish", "temp1"]) + sorted_list_a=natural_sort(characters_list) + sorted_list_b=natural_sort(names_list) + self.assertEqual(sorted_list_a, ["a", "f", "g", "q", "s", "t", "z"]) + self.assertEqual(sorted_list_b,["Dataset", "Sample", "publish", "temp1"]) -- cgit v1.2.3 From fd402e9248f32bd79c4527d7d2cec518d1079586 Mon Sep 17 00:00:00 2001 From: Alexanderlacuna Date: Fri, 6 Nov 2020 11:02:05 +0300 Subject: modify natural_sort and return list to avoid having side effect --- wqflask/wqflask/show_trait/SampleList.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/wqflask/wqflask/show_trait/SampleList.py b/wqflask/wqflask/show_trait/SampleList.py index 37c1d6d5..6a056144 100644 --- a/wqflask/wqflask/show_trait/SampleList.py +++ b/wqflask/wqflask/show_trait/SampleList.py @@ -162,7 +162,7 @@ class SampleList(object): return first_attr_col -def natural_sort(list, key=lambda s: s): +def natural_sort(a_list, key=lambda s: s): """ Sort the list into natural alphanumeric order. """ @@ -170,4 +170,8 @@ def natural_sort(list, key=lambda s: s): def convert(text): return int(text) if text.isdigit() else text return lambda s: [convert(c) for c in re.split('([0-9]+)', key(s))] sort_key = get_alphanum_key_func(key) - list.sort(key=sort_key) \ No newline at end of file + + sorted_list=sorted(a_list,key=sort_key) + + return sorted_list + \ No newline at end of file -- cgit v1.2.3 From 55aa5bf7ec00fb5b5d28a1847e7f8682a62aa547 Mon Sep 17 00:00:00 2001 From: Alexanderlacuna Date: Fri, 6 Nov 2020 11:09:11 +0300 Subject: remove whitespace --- wqflask/wqflask/show_trait/SampleList.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/wqflask/wqflask/show_trait/SampleList.py b/wqflask/wqflask/show_trait/SampleList.py index 6a056144..191c29bd 100644 --- a/wqflask/wqflask/show_trait/SampleList.py +++ b/wqflask/wqflask/show_trait/SampleList.py @@ -8,6 +8,7 @@ from pprint import pformat as pf from utility import Plot from utility import Bunch + class SampleList(object): def __init__(self, dataset, @@ -67,7 +68,8 @@ class SampleList(object): self.sample_list.append(sample) self.se_exists = any(sample.variance for sample in self.sample_list) - self.num_cases_exists = any(sample.num_cases for sample in self.sample_list) + self.num_cases_exists = any( + sample.num_cases for sample in self.sample_list) first_attr_col = self.get_first_attr_col() for sample in self.sample_list: @@ -162,6 +164,7 @@ class SampleList(object): return first_attr_col + def natural_sort(a_list, key=lambda s: s): """ Sort the list into natural alphanumeric order. @@ -170,8 +173,5 @@ def natural_sort(a_list, key=lambda s: s): def convert(text): return int(text) if text.isdigit() else text return lambda s: [convert(c) for c in re.split('([0-9]+)', key(s))] sort_key = get_alphanum_key_func(key) - - sorted_list=sorted(a_list,key=sort_key) - + sorted_list = sorted(a_list, key=sort_key) return sorted_list - \ No newline at end of file -- cgit v1.2.3 From 61d5c7710d044c507d69e600e04a40067cf7320f Mon Sep 17 00:00:00 2001 From: Alexanderlacuna Date: Mon, 16 Nov 2020 20:23:21 +0300 Subject: update refactored natural_sort function in show_trait/SampleList.py --- wqflask/wqflask/show_trait/SampleList.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wqflask/wqflask/show_trait/SampleList.py b/wqflask/wqflask/show_trait/SampleList.py index 191c29bd..349f6b65 100644 --- a/wqflask/wqflask/show_trait/SampleList.py +++ b/wqflask/wqflask/show_trait/SampleList.py @@ -112,7 +112,7 @@ class SampleList(object): self.attributes[key].name = name self.attributes[key].distinct_values = [ item.Value for item in values] - natural_sort(self.attributes[key].distinct_values) + self.attributes[key].distinct_values=natural_sort(self.attributes[key].distinct_values) all_numbers = True for value in self.attributes[key].distinct_values: try: -- cgit v1.2.3