aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/test_data_helpers.py
blob: 39aea45c8e2afd473e1d3d4877c17ef50326a75c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
Test functions in gn3.data_helpers
"""

from unittest import TestCase

from gn3.data_helpers import 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)