aboutsummaryrefslogtreecommitdiff
path: root/wqflask/tests/unit/utility
diff options
context:
space:
mode:
Diffstat (limited to 'wqflask/tests/unit/utility')
-rw-r--r--wqflask/tests/unit/utility/__init__.py0
-rw-r--r--wqflask/tests/unit/utility/test_authentication_tools.py189
-rw-r--r--wqflask/tests/unit/utility/test_chunks.py19
-rw-r--r--wqflask/tests/unit/utility/test_corestats.py55
-rw-r--r--wqflask/tests/unit/utility/test_corr_result_helpers.py32
-rw-r--r--wqflask/tests/unit/utility/test_formatting.py33
-rw-r--r--wqflask/tests/unit/utility/test_hmac.py51
-rw-r--r--wqflask/tests/unit/utility/test_type_checking.py54
8 files changed, 433 insertions, 0 deletions
diff --git a/wqflask/tests/unit/utility/__init__.py b/wqflask/tests/unit/utility/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/wqflask/tests/unit/utility/__init__.py
diff --git a/wqflask/tests/unit/utility/test_authentication_tools.py b/wqflask/tests/unit/utility/test_authentication_tools.py
new file mode 100644
index 00000000..42dcae88
--- /dev/null
+++ b/wqflask/tests/unit/utility/test_authentication_tools.py
@@ -0,0 +1,189 @@
+"""Tests for authentication tools"""
+import unittest
+from unittest import mock
+
+from utility.authentication_tools import check_resource_availability
+from utility.authentication_tools import add_new_resource
+
+class TestResponse:
+ """Mock Test Response after a request"""
+ @property
+ def content(self):
+ """Mock the content from Requests.get(params).content"""
+ return '["foo"]'
+
+
+class TestUser:
+ """Mock user"""
+ @property
+ def user_id(self):
+ """Mockes user id. Used in Flask.g.user_session.user_id"""
+ return b"Jane"
+
+
+class TestUserSession:
+ """Mock user session"""
+ @property
+ def user_session(self):
+ """Mock user session. Mocks Flask.g.user_session object"""
+ return TestUser()
+
+
+def mock_add_resource(resource_ob, update=False):
+ return resource_ob
+
+
+class TestCheckResourceAvailability(unittest.TestCase):
+ """Test methods related to checking the resource availability"""
+ @mock.patch('utility.authentication_tools.add_new_resource')
+ @mock.patch('utility.authentication_tools.Redis')
+ @mock.patch('utility.authentication_tools.g', TestUserSession())
+ @mock.patch('utility.authentication_tools.get_resource_id')
+ def test_check_resource_availability_default_mask(
+ self,
+ resource_id_mock,
+ redis_mock,
+ add_new_resource_mock):
+ """Test the resource availability with default mask"""
+
+ resource_id_mock.return_value = 1
+ redis_mock.smembers.return_value = []
+ test_dataset = mock.MagicMock()
+ type(test_dataset).type = mock.PropertyMock(return_value="Test")
+ add_new_resource_mock.return_value = {"default_mask": 2}
+ self.assertEqual(check_resource_availability(test_dataset), 2)
+
+ @mock.patch('utility.authentication_tools.requests.get')
+ @mock.patch('utility.authentication_tools.add_new_resource')
+ @mock.patch('utility.authentication_tools.Redis')
+ @mock.patch('utility.authentication_tools.g', TestUserSession())
+ @mock.patch('utility.authentication_tools.get_resource_id')
+ def test_check_resource_availability_non_default_mask(
+ self,
+ resource_id_mock,
+ redis_mock,
+ add_new_resource_mock,
+ requests_mock):
+ """Test the resource availability with non-default mask"""
+ resource_id_mock.return_value = 1
+ redis_mock.smembers.return_value = []
+ add_new_resource_mock.return_value = {"default_mask": 2}
+ requests_mock.return_value = TestResponse()
+ test_dataset = mock.MagicMock()
+ type(test_dataset).type = mock.PropertyMock(return_value="Test")
+ self.assertEqual(check_resource_availability(test_dataset),
+ ['foo'])
+
+ @mock.patch('utility.authentication_tools.webqtlConfig.SUPER_PRIVILEGES',
+ "SUPERUSER")
+ @mock.patch('utility.authentication_tools.requests.get')
+ @mock.patch('utility.authentication_tools.add_new_resource')
+ @mock.patch('utility.authentication_tools.Redis')
+ @mock.patch('utility.authentication_tools.g', TestUserSession())
+ @mock.patch('utility.authentication_tools.get_resource_id')
+ def test_check_resource_availability_of_super_user(
+ self,
+ resource_id_mock,
+ redis_mock,
+ add_new_resource_mock,
+ requests_mock):
+ """Test the resource availability if the user is the super user"""
+ resource_id_mock.return_value = 1
+ redis_mock.smembers.return_value = [b"Jane"]
+ add_new_resource_mock.return_value = {"default_mask": 2}
+ requests_mock.return_value = TestResponse()
+ test_dataset = mock.MagicMock()
+ type(test_dataset).type = mock.PropertyMock(return_value="Test")
+ self.assertEqual(check_resource_availability(test_dataset),
+ "SUPERUSER")
+
+ @mock.patch('utility.authentication_tools.webqtlConfig.DEFAULT_PRIVILEGES',
+ "John Doe")
+ def test_check_resource_availability_string_dataset(self):
+ """Test the resource availability if the dataset is a string"""
+ self.assertEqual(check_resource_availability("Test"),
+ "John Doe")
+
+ @mock.patch('utility.authentication_tools.webqtlConfig.DEFAULT_PRIVILEGES',
+ "John Doe")
+ def test_check_resource_availability_temp(self):
+ """Test the resource availability if the dataset is a string"""
+ test_dataset = mock.MagicMock()
+ type(test_dataset).type = mock.PropertyMock(return_value="Temp")
+ self.assertEqual(check_resource_availability(test_dataset),
+ "John Doe")
+
+
+class TestAddNewResource(unittest.TestCase):
+ """Test cases for add_new_resource method"""
+ @mock.patch('utility.authentication_tools.webqtlConfig.DEFAULT_PRIVILEGES',
+ "John Doe")
+ @mock.patch('utility.authentication_tools.add_resource', mock_add_resource)
+ @mock.patch('utility.authentication_tools.get_group_code')
+ def test_add_new_resource_if_publish_datatype(self, group_code_mock):
+ """Test add_new_resource if dataset type is 'publish'"""
+ group_code_mock.return_value = "Test"
+ test_dataset = mock.MagicMock()
+ type(test_dataset).type = mock.PropertyMock(return_value="Publish")
+ type(test_dataset).id = mock.PropertyMock(return_value=10)
+ expected_value = {
+ "owner_id": "none",
+ "default_mask": "John Doe",
+ "group_masks": {},
+ "name": "Test_None",
+ "data": {
+ "dataset": 10,
+ "trait": None
+ },
+ "type": "dataset-publish"
+ }
+ self.assertEqual(add_new_resource(test_dataset),
+ expected_value)
+
+ @mock.patch('utility.authentication_tools.webqtlConfig.DEFAULT_PRIVILEGES',
+ "John Doe")
+ @mock.patch('utility.authentication_tools.add_resource', mock_add_resource)
+ @mock.patch('utility.authentication_tools.get_group_code')
+ def test_add_new_resource_if_geno_datatype(self, group_code_mock):
+ """Test add_new_resource if dataset type is 'geno'"""
+ group_code_mock.return_value = "Test"
+ test_dataset = mock.MagicMock()
+ type(test_dataset).name = mock.PropertyMock(return_value="Geno")
+ type(test_dataset).type = mock.PropertyMock(return_value="Geno")
+ type(test_dataset).id = mock.PropertyMock(return_value=20)
+ expected_value = {
+ "owner_id": "none",
+ "default_mask": "John Doe",
+ "group_masks": {},
+ "name": "Geno",
+ "data": {
+ "dataset": 20,
+ },
+ "type": "dataset-geno"
+ }
+ self.assertEqual(add_new_resource(test_dataset),
+ expected_value)
+
+ @mock.patch('utility.authentication_tools.webqtlConfig.DEFAULT_PRIVILEGES',
+ "John Doe")
+ @mock.patch('utility.authentication_tools.add_resource', mock_add_resource)
+ @mock.patch('utility.authentication_tools.get_group_code')
+ def test_add_new_resource_if_other_datatype(self, group_code_mock):
+ """Test add_new_resource if dataset type is not 'geno' or 'publish'"""
+ group_code_mock.return_value = "Test"
+ test_dataset = mock.MagicMock()
+ type(test_dataset).name = mock.PropertyMock(return_value="Geno")
+ type(test_dataset).type = mock.PropertyMock(return_value="other")
+ type(test_dataset).id = mock.PropertyMock(return_value=20)
+ expected_value = {
+ "owner_id": "none",
+ "default_mask": "John Doe",
+ "group_masks": {},
+ "name": "Geno",
+ "data": {
+ "dataset": 20,
+ },
+ "type": "dataset-probeset"
+ }
+ self.assertEqual(add_new_resource(test_dataset),
+ expected_value)
diff --git a/wqflask/tests/unit/utility/test_chunks.py b/wqflask/tests/unit/utility/test_chunks.py
new file mode 100644
index 00000000..8d90a1ec
--- /dev/null
+++ b/wqflask/tests/unit/utility/test_chunks.py
@@ -0,0 +1,19 @@
+"""Test chunking"""
+
+import unittest
+
+from utility.chunks import divide_into_chunks
+
+
+class TestChunks(unittest.TestCase):
+ "Test Utility method for chunking"
+ def test_divide_into_chunks(self):
+ "Check that a list is chunked correctly"
+ self.assertEqual(divide_into_chunks([1, 2, 7, 3, 22, 8, 5, 22, 333], 3),
+ [[1, 2, 7], [3, 22, 8], [5, 22, 333]])
+ self.assertEqual(divide_into_chunks([1, 2, 7, 3, 22, 8, 5, 22, 333], 4),
+ [[1, 2, 7], [3, 22, 8], [5, 22, 333]])
+ self.assertEqual(divide_into_chunks([1, 2, 7, 3, 22, 8, 5, 22, 333], 5),
+ [[1, 2], [7, 3], [22, 8], [5, 22], [333]])
+ self.assertEqual(divide_into_chunks([], 5),
+ [[]])
diff --git a/wqflask/tests/unit/utility/test_corestats.py b/wqflask/tests/unit/utility/test_corestats.py
new file mode 100644
index 00000000..cf91a248
--- /dev/null
+++ b/wqflask/tests/unit/utility/test_corestats.py
@@ -0,0 +1,55 @@
+"""Test Core Stats"""
+
+import unittest
+
+from utility.corestats import Stats
+
+
+class TestChunks(unittest.TestCase):
+ "Test Utility method for chunking"
+
+ def setUp(self):
+ self.stat_test = Stats((x for x in range(1, 11)))
+
+ def test_stats_sum(self):
+ """ Test sequence sum """
+ self.assertEqual(self.stat_test.sum(), 55)
+ self.stat_test = Stats([])
+ self.assertEqual(self.stat_test.sum(), None)
+
+ def test_stats_count(self):
+ """ Test sequence count """
+ self.assertEqual(self.stat_test.count(), 10)
+ self.stat_test = Stats([])
+ self.assertEqual(self.stat_test.count(), 0)
+
+ def test_stats_min(self):
+ """ Test min value in sequence"""
+ self.assertEqual(self.stat_test.min(), 1)
+ self.stat_test = Stats([])
+ self.assertEqual(self.stat_test.min(), None)
+
+ def test_stats_max(self):
+ """ Test max value in sequence """
+ self.assertEqual(self.stat_test.max(), 10)
+ self.stat_test = Stats([])
+ self.assertEqual(self.stat_test.max(), None)
+
+ def test_stats_avg(self):
+ """ Test avg of sequence """
+ self.assertEqual(self.stat_test.avg(), 5.5)
+ self.stat_test = Stats([])
+ self.assertEqual(self.stat_test.avg(), None)
+
+ def test_stats_stdev(self):
+ """ Test standard deviation of sequence """
+ self.assertEqual(self.stat_test.stdev(), 3.0276503540974917)
+ self.stat_test = Stats([])
+ self.assertEqual(self.stat_test.stdev(), None)
+
+ def test_stats_percentile(self):
+ """ Test percentile of sequence """
+ self.assertEqual(self.stat_test.percentile(20), 3.0)
+ self.assertEqual(self.stat_test.percentile(101), None)
+ self.stat_test = Stats([])
+ self.assertEqual(self.stat_test.percentile(20), None)
diff --git a/wqflask/tests/unit/utility/test_corr_result_helpers.py b/wqflask/tests/unit/utility/test_corr_result_helpers.py
new file mode 100644
index 00000000..e196fbdf
--- /dev/null
+++ b/wqflask/tests/unit/utility/test_corr_result_helpers.py
@@ -0,0 +1,32 @@
+""" Test correlation helper methods """
+
+import unittest
+from utility.corr_result_helpers import normalize_values, common_keys, normalize_values_with_samples
+
+
+class TestCorrelationHelpers(unittest.TestCase):
+ """Test methods for normalising lists"""
+
+ def test_normalize_values(self):
+ """Test that a list is normalised correctly"""
+ self.assertEqual(
+ normalize_values([2.3, None, None, 3.2, 4.1, 5], [
+ 3.4, 7.2, 1.3, None, 6.2, 4.1]),
+ ([2.3, 4.1, 5], [3.4, 6.2, 4.1], 3)
+ )
+
+ def test_common_keys(self):
+ """Test that common keys are returned as a list"""
+ a = dict(BXD1=9.113, BXD2=9.825, BXD14=8.985, BXD15=9.300)
+ b = dict(BXD1=9.723, BXD3=9.825, BXD14=9.124, BXD16=9.300)
+ self.assertEqual(sorted(common_keys(a, b)), ['BXD1', 'BXD14'])
+
+ def test_normalize_values_with_samples(self):
+ """Test that a sample(dict) is normalised correctly"""
+ self.assertEqual(
+ normalize_values_with_samples(
+ dict(BXD1=9.113, BXD2=9.825, BXD14=8.985,
+ BXD15=9.300, BXD20=9.300),
+ dict(BXD1=9.723, BXD3=9.825, BXD14=9.124, BXD16=9.300)),
+ (({'BXD1': 9.113, 'BXD14': 8.985}, {'BXD1': 9.723, 'BXD14': 9.124}, 2))
+ )
diff --git a/wqflask/tests/unit/utility/test_formatting.py b/wqflask/tests/unit/utility/test_formatting.py
new file mode 100644
index 00000000..9d3033d1
--- /dev/null
+++ b/wqflask/tests/unit/utility/test_formatting.py
@@ -0,0 +1,33 @@
+import unittest
+from utility.formatting import numify, commify
+
+
+class TestFormatting(unittest.TestCase):
+ """Test formatting numbers by numifying or commifying"""
+
+ def test_numify(self):
+ "Test that a number is correctly converted to a English readable string"
+ self.assertEqual(numify(1, 'item', 'items'),
+ 'one item')
+ self.assertEqual(numify(2, 'book'), 'two')
+ self.assertEqual(numify(2, 'book', 'books'), 'two books')
+ self.assertEqual(numify(0, 'book', 'books'), 'zero books')
+ self.assertEqual(numify(0), 'zero')
+ self.assertEqual(numify(5), 'five')
+ self.assertEqual(numify(14, 'book', 'books'), '14 books')
+ self.assertEqual(numify(999, 'book', 'books'), '999 books')
+ self.assertEqual(numify(1000000, 'book', 'books'), '1,000,000 books')
+ self.assertEqual(numify(1956), '1956')
+
+ def test_commify(self):
+ "Test that commas are added correctly"
+ self.assertEqual(commify(1), '1')
+ self.assertEqual(commify(123), '123')
+ self.assertEqual(commify(1234), '1234')
+ self.assertEqual(commify(12345), '12,345')
+ self.assertEqual(commify(1234567890), '1,234,567,890')
+ self.assertEqual(commify(123.0), '123.0')
+ self.assertEqual(commify(1234.5), '1234.5')
+ self.assertEqual(commify(1234.56789), '1234.56789')
+ self.assertEqual(commify(123456.789), '123,456.789')
+ self.assertEqual(commify(None), None)
diff --git a/wqflask/tests/unit/utility/test_hmac.py b/wqflask/tests/unit/utility/test_hmac.py
new file mode 100644
index 00000000..13d6261d
--- /dev/null
+++ b/wqflask/tests/unit/utility/test_hmac.py
@@ -0,0 +1,51 @@
+"""Test hmac utility functions"""
+
+import unittest
+from unittest import mock
+
+from utility.hmac import data_hmac
+from utility.hmac import url_for_hmac
+from utility.hmac import hmac_creation
+
+
+class TestHmacUtil(unittest.TestCase):
+ """Test Utility method for hmac creation"""
+
+ @mock.patch("utility.hmac.app.config", {'SECRET_HMAC_CODE': "secret"})
+ def test_hmac_creation(self):
+ """Test hmac creation with a utf-8 string"""
+ self.assertEqual(hmac_creation("ファイ"), "7410466338cfe109e946")
+
+ @mock.patch("utility.hmac.app.config",
+ {'SECRET_HMAC_CODE': ('\x08\xdf\xfa\x93N\x80'
+ '\xd9\\H@\\\x9f`\x98d^'
+ '\xb4a;\xc6OM\x946a\xbc'
+ '\xfc\x80:*\xebc')})
+ def test_hmac_creation_with_cookie(self):
+ """Test hmac creation with a cookie"""
+ cookie = "3f4c1dbf-5b56-4260-87d6-f35445bda37e:af4fcf5eace9e7c864ce"
+ uuid_, _, signature = cookie.partition(":")
+ self.assertEqual(
+ hmac_creation(uuid_),
+ "af4fcf5eace9e7c864ce")
+
+ @mock.patch("utility.hmac.app.config", {'SECRET_HMAC_CODE': "secret"})
+ def test_data_hmac(self):
+ """Test data_hmac fn with a utf-8 string"""
+ self.assertEqual(data_hmac("ファイ"), "ファイ:7410466338cfe109e946")
+
+ @mock.patch("utility.hmac.app.config", {'SECRET_HMAC_CODE': "secret"})
+ @mock.patch("utility.hmac.url_for")
+ def test_url_for_hmac_with_plain_url(self, mock_url):
+ """Test url_for_hmac without params"""
+ mock_url.return_value = "https://mock_url.com/ファイ/"
+ self.assertEqual(url_for_hmac("ファイ"),
+ "https://mock_url.com/ファイ/?hm=05bc39e659b1948f41e7")
+
+ @mock.patch("utility.hmac.app.config", {'SECRET_HMAC_CODE': "secret"})
+ @mock.patch("utility.hmac.url_for")
+ def test_url_for_hmac_with_param_in_url(self, mock_url):
+ """Test url_for_hmac with params"""
+ mock_url.return_value = "https://mock_url.com/?ファイ=1"
+ self.assertEqual(url_for_hmac("ファイ"),
+ "https://mock_url.com/?ファイ=1&hm=4709c1708270644aed79")
diff --git a/wqflask/tests/unit/utility/test_type_checking.py b/wqflask/tests/unit/utility/test_type_checking.py
new file mode 100644
index 00000000..48d110c7
--- /dev/null
+++ b/wqflask/tests/unit/utility/test_type_checking.py
@@ -0,0 +1,54 @@
+import unittest
+from utility.type_checking import is_float
+from utility.type_checking import is_int
+from utility.type_checking import is_str
+from utility.type_checking import get_float
+from utility.type_checking import get_int
+from utility.type_checking import get_string
+
+
+class TestTypeChecking(unittest.TestCase):
+ def test_is_float(self):
+ floats = [2, 1.2, '3.1']
+ not_floats = ["String", None, [], ()]
+ for flt in floats:
+ results = is_float(flt)
+ self.assertTrue(results)
+ for nflt in not_floats:
+ results = is_float(nflt)
+ self.assertFalse(results)
+
+ def test_is_int(self):
+ int_values = [1, 1.1]
+ not_int_values = ["string", None, [], "1.1"]
+ for int_val in int_values:
+ results = is_int(int_val)
+ self.assertTrue(results)
+ for not_int in not_int_values:
+ results = is_int(not_int)
+ self.assertFalse(results)
+
+ def test_is_str(self):
+ string_values = [1, False, [], {}, "string_value"]
+ falsey_values = [None]
+ for string_val in string_values:
+ results = is_str(string_val)
+ self.assertTrue(results)
+ for non_string in falsey_values:
+ results = is_str(non_string)
+ self.assertFalse(results)
+
+ def test_get_float(self):
+ vars_object = {"min_value": "12"}
+ results = get_float(vars_object, "min_value")
+ self.assertEqual(results, 12.0)
+
+ def test_get_int(self):
+ vars_object = {"lx_value": "1"}
+ results = get_int(vars_object, "lx_value")
+ self.assertEqual(results, 1)
+
+ def test_get_string(self):
+ string_object = {"mx_value": 1}
+ results = get_string(string_object, "mx_value")
+ self.assertEqual(results, "1") \ No newline at end of file