File: test_csvstack.py

package info (click to toggle)
csvkit 2.1.0-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 40,660 kB
  • sloc: python: 4,921; perl: 1,000; makefile: 131; sql: 4
file content (190 lines) | stat: -rw-r--r-- 6,283 bytes parent folder | download
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import os
import sys
import unittest
from unittest.mock import patch

from csvkit.utilities.csvstack import CSVStack, launch_new_instance
from tests.utils import CSVKitTestCase, EmptyFileTests, stdin_as_string


class TestCSVStack(CSVKitTestCase, EmptyFileTests):
    Utility = CSVStack
    default_args = ['-']

    def test_launch_new_instance(self):
        with patch.object(sys, 'argv', [self.Utility.__name__.lower(), 'examples/dummy.csv']):
            launch_new_instance()

    def test_options(self):
        self.assertError(
            launch_new_instance,
            ['-g', '1,2'],
            'The number of grouping values must be equal to the number of CSV files being stacked.',
        )

    @unittest.skipIf(os.name != 'nt', 'Windows only')
    def test_glob(self):
        self.assertRows(['examples/dummy*.csv'], [
            ['a', 'b', 'c', 'd'],
            ['1', '2', '3', ''],
            ['1', '2', '3', ''],
            ['1', '2', '3', ''],
            ['1', '4', '5', ''],
            ['1', '2', '3', ''],
            ['1', '2', '3', '4'],
        ])

    def test_skip_lines(self):
        self.assertRows(['--skip-lines', '3', 'examples/test_skip_lines.csv', 'examples/test_skip_lines.csv'], [
            ['a', 'b', 'c'],
            ['1', '2', '3'],
            ['1', '2', '3'],
        ])

    def test_skip_lines_stdin(self):
        with open('examples/test_skip_lines.csv', 'rb') as f, stdin_as_string(f):
            self.assertRows(['--skip-lines', '3', '-', 'examples/test_skip_lines.csv'], [
                ['a', 'b', 'c'],
                ['1', '2', '3'],
                ['1', '2', '3'],
            ])

    def test_single_file_stack(self):
        self.assertRows(['examples/dummy.csv'], [
            ['a', 'b', 'c'],
            ['1', '2', '3'],
        ])

    def test_multiple_file_stack(self):
        self.assertRows(['examples/dummy.csv', 'examples/dummy2.csv'], [
            ['a', 'b', 'c'],
            ['1', '2', '3'],
            ['1', '2', '3'],
        ])

    def test_multiple_file_stack_col(self):
        self.assertRows(['examples/dummy.csv', 'examples/dummy_col_shuffled.csv'], [
            ['a', 'b', 'c'],
            ['1', '2', '3'],
            ['1', '2', '3'],
        ])

        self.assertRows(['examples/dummy_col_shuffled.csv', 'examples/dummy.csv'], [
            ['b', 'c', 'a'],
            ['2', '3', '1'],
            ['2', '3', '1'],
        ])

    def test_multiple_file_stack_col_ragged(self):
        self.assertRows(['examples/dummy.csv', 'examples/dummy_col_shuffled_ragged.csv'], [
            ['a', 'b', 'c', 'd'],
            ['1', '2', '3', ''],
            ['1', '2', '3', '4'],
        ])

    def test_multiple_file_stack_col_ragged_stdin(self):
        with open('examples/dummy.csv', 'rb') as f, stdin_as_string(f):
            self.assertRows(['-', 'examples/dummy_col_shuffled_ragged.csv'], [
                ['a', 'b', 'c', 'd'],
                ['1', '2', '3', ''],
                ['1', '2', '3', '4'],
            ])

        with open('examples/dummy.csv', 'rb') as f, stdin_as_string(f):
            self.assertRows(['examples/dummy_col_shuffled_ragged.csv', '-'], [
                ['b', 'c', 'a', 'd'],
                ['2', '3', '1', '4'],
                ['2', '3', '1', ''],
            ])

    def test_explicit_grouping(self):
        self.assertRows(['--groups', 'asd,sdf', '-n', 'foo', 'examples/dummy.csv', 'examples/dummy2.csv'], [
            ['foo', 'a', 'b', 'c'],
            ['asd', '1', '2', '3'],
            ['sdf', '1', '2', '3'],
        ])

    def test_filenames_grouping(self):
        self.assertRows(['--filenames', '-n', 'path', 'examples/dummy.csv', 'examples/dummy2.csv'], [
            ['path', 'a', 'b', 'c'],
            ['dummy.csv', '1', '2', '3'],
            ['dummy2.csv', '1', '2', '3'],
        ])


class TestNoHeaderRow(TestCSVStack):

    def test_no_header_row_basic(self):
        self.assertRows(['--no-header-row', 'examples/no_header_row.csv', 'examples/no_header_row2.csv'], [
            ['a', 'b', 'c'],
            ['1', '2', '3'],
            ['4', '5', '6'],
        ])

    def test_no_header_row_basic_stdin(self):
        with open('examples/no_header_row.csv', 'rb') as f, stdin_as_string(f):
            self.assertRows(['--no-header-row', '-', 'examples/no_header_row2.csv'], [
                ['a', 'b', 'c'],
                ['1', '2', '3'],
                ['4', '5', '6'],
            ])

        with open('examples/no_header_row.csv', 'rb') as f, stdin_as_string(f):
            self.assertRows(['--no-header-row', 'examples/no_header_row2.csv', '-'], [
                ['a', 'b', 'c'],
                ['4', '5', '6'],
                ['1', '2', '3'],
            ])

    def test_grouped_manual_and_named_column(self):
        self.assertRows(
            [
                "--no-header-row",
                "--groups",
                "foo,bar",
                "-n",
                "hey",
                "examples/dummy.csv",
                "examples/dummy3.csv",
            ],
            [
                ["hey", "a", "b", "c"],
                ["foo", "a", "b", "c"],
                ["foo", "1", "2", "3"],
                ["bar", "a", "b", "c"],
                ["bar", "1", "2", "3"],
                ["bar", "1", "4", "5"],
            ],
        )

    def test_grouped_filenames(self):
        self.assertRows(
            [
                "-H",
                "--filenames",
                "examples/no_header_row.csv",
                "examples/no_header_row2.csv",
            ],
            [
                ["group",                "a", "b", "c"],
                ["no_header_row.csv",    "1", "2", "3"],
                ["no_header_row2.csv",   "4", "5", "6"],
            ],
        )

    def test_grouped_filenames_and_named_column(self):
        self.assertRows(
            [
                "-H",
                "--filenames",
                "-n",
                "hello",
                "examples/no_header_row.csv",
                "examples/no_header_row2.csv",
            ],
            [
                ["hello", "a", "b", "c"],
                ["no_header_row.csv", "1", "2", "3"],
                ["no_header_row2.csv", "4", "5", "6"],
            ],
        )