-
Notifications
You must be signed in to change notification settings - Fork 608
/
test_cli.py
42 lines (32 loc) · 2.34 KB
/
test_cli.py
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
#!/usr/bin/env python
import unittest
from csvkit.cli import match_column_identifier, parse_column_identifiers
class TestCli(unittest.TestCase):
def setUp(self):
self.headers = ['id', 'name', 'i_work_here', '1', 'more-header-values', 'stuff', 'blueberry']
def test_match_column_identifier_string(self):
self.assertEqual(2, match_column_identifier(self.headers, 'i_work_here'))
self.assertEqual(2, match_column_identifier(self.headers, 'i_work_here', zero_based=True))
def test_match_column_identifier_numeric(self):
self.assertEqual(2, match_column_identifier(self.headers, 3))
self.assertEqual(3, match_column_identifier(self.headers, 3, zero_based=True))
def test_match_column_which_could_be_integer_name_is_treated_as_positional_id(self):
self.assertEqual(0, match_column_identifier(self.headers, '1'))
self.assertEqual(1, match_column_identifier(self.headers, '1', zero_based=True))
def test_parse_column_identifiers(self):
self.assertEqual([2, 0, 1], parse_column_identifiers(' i_work_here, 1,name ', self.headers))
self.assertEqual([2, 1, 1], parse_column_identifiers(' i_work_here, 1,name ', self.headers, zero_based=True))
def test_range_notation(self):
self.assertEqual([0,1,2], parse_column_identifiers('1:3', self.headers))
self.assertEqual([1,2,3], parse_column_identifiers('1:3', self.headers, zero_based=True))
self.assertEqual([1,2,3], parse_column_identifiers('2-4', self.headers))
self.assertEqual([2,3,4], parse_column_identifiers('2-4', self.headers, zero_based=True))
self.assertEqual([0,1,2,3], parse_column_identifiers('1,2:4', self.headers))
self.assertEqual([1,2,3,4], parse_column_identifiers('1,2:4', self.headers, zero_based=True))
self.assertEqual([4,2,5], parse_column_identifiers('more-header-values,3,stuff', self.headers))
self.assertEqual([4,3,5], parse_column_identifiers('more-header-values,3,stuff', self.headers,zero_based=True))
def test_range_notation_open_ended(self):
self.assertEqual([0,1,2], parse_column_identifiers(':3', self.headers))
target = range(3,len(self.headers) - 1) # protect against devs adding to self.headers
target.insert(0,0)
self.assertEqual(target, parse_column_identifiers('1,4:', self.headers))