about summary refs log tree commit diff
path: root/tests/unit/test_data_helpers.py
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2021-12-06 14:04:59 +0300
committerFrederick Muriuki Muriithi2021-12-06 14:04:59 +0300
commit66406115f41594ba40e3fbbc6f69aace2d11800f (patch)
tree0f3de09b74a3f47918dd4a192665c8a06c508144 /tests/unit/test_data_helpers.py
parent77099cac68e8f4792bf54d8e1f7ce6f315bedfa7 (diff)
parent5d2248f1dabbc7dd04f48aafcc9f327817a9c92c (diff)
downloadgenenetwork3-66406115f41594ba40e3fbbc6f69aace2d11800f.tar.gz
Merge branch 'partial-correlations'
Diffstat (limited to 'tests/unit/test_data_helpers.py')
-rw-r--r--tests/unit/test_data_helpers.py89
1 files changed, 89 insertions, 0 deletions
diff --git a/tests/unit/test_data_helpers.py b/tests/unit/test_data_helpers.py
new file mode 100644
index 0000000..88ea469
--- /dev/null
+++ b/tests/unit/test_data_helpers.py
@@ -0,0 +1,89 @@
+"""
+Test functions in gn3.data_helpers
+"""
+
+from unittest import TestCase
+
+from gn3.data_helpers import partition_by, partition_all, parse_csv_line
+
+class TestDataHelpers(TestCase):
+    """
+    Test functions in gn3.data_helpers
+    """
+
+    def test_partition_all(self):
+        """
+        Test that `gn3.data_helpers.partition_all` partitions sequences as expected.
+
+        Given:
+            - `num`: The number of items per partition
+            - `items`: A sequence of items
+        When:
+            - The arguments above are passed to the `gn3.data_helpers.partition_all`
+        Then:
+            - Return a new sequence with partitions, each of which has `num`
+              items in the same order as those in `items`, save for the last
+              partition which might have fewer items than `num`.
+        """
+        for count, items, expected in (
+                (1, [0, 1, 2, 3], ((0,), (1,), (2,), (3,))),
+                (3, (0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
+                 ((0, 1, 2), (3, 4, 5), (6, 7, 8), (9, ))),
+                (4, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
+                 ((0, 1, 2, 3), (4, 5, 6, 7), (8, 9))),
+                (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)
+
+    def test_parse_csv_line(self):
+        """
+        Test parsing a single line from a CSV file
+
+        Given:
+            - `line`: a line read from a csv file
+            - `delimiter`: the expected delimiter in the csv file
+            - `quoting`: the quoting enclosing each column in the csv file
+        When:
+            - `line` is parsed with the `parse_csv_file` with the given
+               parameters
+        Then:
+            - return a tuple of the columns in the CSV file, without the
+              delimiter and quoting
+        """
+        for line, delimiter, quoting, expected in (
+                ('"this","is","a","test"', ",", '"', ("this", "is", "a", "test")),
+                ('"this","is","a","test"', ",", None, ('"this"', '"is"', '"a"', '"test"'))):
+            with self.subTest(line=line, delimiter=delimiter, quoting=quoting):
+                self.assertEqual(
+                    parse_csv_line(
+                        line=line, delimiter=delimiter, quoting=quoting),
+                    expected)
+
+    def test_partition_by(self):
+        """
+        Test that `partition_by` groups the data using the given predicate
+
+        Given:
+          - `part_fn`: a predicate funtion that return boolean True/False
+          - `items`: a sequence of items
+        When:
+          - the partitioning predicate function and the sequence of items are
+            passed to the `partition_by` function
+        Then:
+          - the result is a tuple, with sub-tuples containing the data in the
+            original sequence. Each sub-tuple is a partition, ending as soon as
+            the next value in the sequence, when passed to `part_fn`, returns
+            boolean `True`.
+        """
+        for part_fn, items, expected in (
+                (lambda s: s.startswith("----"),
+                 ("------", "a", "b", "-----", "c", "----", "d", "e", "---",
+                  "f"),
+                 (("------", "a", "b"), ("-----", "c"),
+                  ("----", "d", "e", "---", "f"))),
+                (lambda x: (x % 2) == 0,
+                 (0, 1, 3, 2, 4, 5, 7, 6, 9, 1),
+                 ((0, 1, 3), (2,), (4, 5, 7), (6, 9, 1))),):
+            with self.subTest(items=items):
+                self.assertEqual(partition_by(part_fn, items), expected)