aboutsummaryrefslogtreecommitdiff
path: root/wqflask/tests/base
diff options
context:
space:
mode:
Diffstat (limited to 'wqflask/tests/base')
-rw-r--r--wqflask/tests/base/test_data_set.py10
-rw-r--r--wqflask/tests/base/test_trait.py101
2 files changed, 105 insertions, 6 deletions
diff --git a/wqflask/tests/base/test_data_set.py b/wqflask/tests/base/test_data_set.py
index 94780a5d..dd7f5051 100644
--- a/wqflask/tests/base/test_data_set.py
+++ b/wqflask/tests/base/test_data_set.py
@@ -66,7 +66,7 @@ class TestDataSetTypes(unittest.TestCase):
@mock.patch('base.data_set.g')
def test_set_dataset_key_mrna(self, db_mock):
with app.app_context():
- db_mock.db.execute.return_value = [1, 2, 3]
+ db_mock.db.execute.return_value.fetchone.return_value = [1, 2, 3]
redis_mock = mock.Mock()
redis_mock.get.return_value = self.test_dataset
data_set = DatasetType(redis_mock)
@@ -84,7 +84,7 @@ class TestDataSetTypes(unittest.TestCase):
@mock.patch('base.data_set.g')
def test_set_dataset_key_pheno(self, db_mock):
with app.app_context():
- db_mock.db.execute.return_value = [1, 2, 3]
+ db_mock.db.execute.return_value.fetchone.return_value = [1, 2, 3]
redis_mock = mock.Mock()
redis_mock.get.return_value = self.test_dataset
data_set = DatasetType(redis_mock)
@@ -93,7 +93,6 @@ class TestDataSetTypes(unittest.TestCase):
redis_mock.set.assert_called_once_with(
"dataset_structure",
'{"Aging-Brain-UCIPublish": "Publish", "AKXDGeno": "Geno", "B139_K_1206_M": "ProbeSet", "AD-cases-controls-MyersGeno": "Geno", "AD-cases-controls-MyersPublish": "Publish", "All Phenotypes": "Publish", "Test": "Publish", "AXBXAPublish": "Publish", "B139_K_1206_R": "ProbeSet", "AXBXAGeno": "Geno"}')
- expected_db_call = """"""
db_mock.db.execute.assert_called_with(
("SELECT InfoFiles.GN_AccesionId " +
"FROM InfoFiles, PublishFreeze, InbredSet " +
@@ -105,7 +104,7 @@ class TestDataSetTypes(unittest.TestCase):
@mock.patch('base.data_set.g')
def test_set_dataset_other_pheno(self, db_mock):
with app.app_context():
- db_mock.db.execute.return_value = [1, 2, 3]
+ db_mock.db.execute.return_value.fetchone.return_value = [1, 2, 3]
redis_mock = mock.Mock()
redis_mock.get.return_value = self.test_dataset
data_set = DatasetType(redis_mock)
@@ -114,7 +113,6 @@ class TestDataSetTypes(unittest.TestCase):
redis_mock.set.assert_called_once_with(
"dataset_structure",
'{"Aging-Brain-UCIPublish": "Publish", "AKXDGeno": "Geno", "B139_K_1206_M": "ProbeSet", "AD-cases-controls-MyersGeno": "Geno", "AD-cases-controls-MyersPublish": "Publish", "All Phenotypes": "Publish", "Test": "Publish", "AXBXAPublish": "Publish", "B139_K_1206_R": "ProbeSet", "AXBXAGeno": "Geno"}')
- expected_db_call = """"""
db_mock.db.execute.assert_called_with(
("SELECT PublishFreeze.Name " +
"FROM PublishFreeze, InbredSet " +
@@ -125,7 +123,7 @@ class TestDataSetTypes(unittest.TestCase):
@mock.patch('base.data_set.g')
def test_set_dataset_geno(self, db_mock):
with app.app_context():
- db_mock.db.execute.return_value = [1, 2, 3]
+ db_mock.db.execute.return_value.fetchone.return_value = [1, 2, 3]
redis_mock = mock.Mock()
redis_mock.get.return_value = self.test_dataset
data_set = DatasetType(redis_mock)
diff --git a/wqflask/tests/base/test_trait.py b/wqflask/tests/base/test_trait.py
new file mode 100644
index 00000000..53b0d440
--- /dev/null
+++ b/wqflask/tests/base/test_trait.py
@@ -0,0 +1,101 @@
+# -*- coding: utf-8 -*-
+"""Tests wqflask/base/trait.py"""
+import unittest
+import mock
+
+from base.trait import GeneralTrait
+from base.trait import retrieve_trait_info
+
+
+class TestResponse:
+ """Mock Test Response after a request"""
+ @property
+ def content(self):
+ """Mock the content from Requests.get(params).content"""
+ return "[1, 2, 3, 4]"
+
+
+class TestNilResponse:
+ """Mock Test Response after a request"""
+ @property
+ def content(self):
+ """Mock the content from Requests.get(params).content"""
+ return "{}"
+
+
+class MockTrait(GeneralTrait):
+ @property
+ def wikidata_alias_fmt(self):
+ return "Mock alias"
+
+
+class TestRetrieveTraitInfo(unittest.TestCase):
+ """Tests for 'retrieve_trait_info'"""
+ def test_retrieve_trait_info_with_empty_dataset(self):
+ """Test that an exception is raised when dataset is empty"""
+ with self.assertRaises(AssertionError):
+ retrieve_trait_info(trait=mock.MagicMock(),
+ dataset={})
+
+ @mock.patch('base.trait.requests.get')
+ @mock.patch('base.trait.g')
+ def test_retrieve_trait_info_with_empty_trait_info(self,
+ g_mock,
+ requests_mock):
+ """Empty trait info"""
+ requests_mock.return_value = TestNilResponse()
+ with self.assertRaises(KeyError):
+ retrieve_trait_info(trait=mock.MagicMock(),
+ dataset=mock.MagicMock())
+
+ @mock.patch('base.trait.requests.get')
+ @mock.patch('base.trait.g')
+ def test_retrieve_trait_info_with_non_empty_trait_info(self,
+ g_mock,
+ requests_mock):
+ """Test that attributes are set"""
+ mock_dataset = mock.MagicMock()
+ requests_mock.return_value = TestResponse()
+ type(mock_dataset).display_fields = mock.PropertyMock(
+ return_value=["a", "b", "c", "d"])
+ test_trait = retrieve_trait_info(trait=MockTrait(dataset=mock_dataset),
+ dataset=mock_dataset)
+ self.assertEqual(test_trait.a, 1)
+ self.assertEqual(test_trait.b, 2)
+ self.assertEqual(test_trait.c, 3)
+ self.assertEqual(test_trait.d, 4)
+
+ @mock.patch('base.trait.requests.get')
+ @mock.patch('base.trait.g')
+ def test_retrieve_trait_info_utf8_parsing(self,
+ g_mock,
+ requests_mock):
+ """Test that utf-8 strings are parsed correctly"""
+ utf_8_string = "test_string"
+ mock_dataset = mock.MagicMock()
+ requests_mock.return_value = TestResponse()
+ type(mock_dataset).display_fields = mock.PropertyMock(
+ return_value=["a", "b", "c", "d"])
+ type(mock_dataset).type = 'Publish'
+
+ mock_trait = MockTrait(
+ dataset=mock_dataset,
+ pre_publication_description=utf_8_string
+ )
+ trait_attrs = {
+ "group_code": "test_code",
+ "pre_publication_description": "test_pre_pub",
+ "pre_publication_abbreviation": "ファイルを画面毎に見て行くには、次のコマンドを使います。",
+ "post_publication_description": None,
+ "pubmed_id": None,
+ 'year': "2020",
+ "authors": "Jane Doe かいと",
+ }
+ for key, val in list(trait_attrs.items()):
+ setattr(mock_trait, key, val)
+ test_trait = retrieve_trait_info(trait=mock_trait,
+ dataset=mock_dataset)
+ self.assertEqual(test_trait.abbreviation,
+ "ファイルを画面毎に見て行くには、次のコマンドを使います。")
+ self.assertEqual(test_trait.authors,
+ "Jane Doe かいと")