-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_tables_ops.py
105 lines (93 loc) · 3.23 KB
/
test_tables_ops.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
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
import unittest
import subprocess
import os
import csv
class TestTableOps(unittest.TestCase):
TEST_DATA_DIR = "test-data"
def _run_command(self, command, input_data=None):
process = subprocess.Popen(
command,
stdin=subprocess.PIPE if input_data else None,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True, # Important for handling text I/O
)
stdout, stderr = process.communicate(input_data)
return process.returncode, stdout, stderr
def _compare_tsv(self, expected_file, actual_output):
with open(
os.path.join(self.TEST_DATA_DIR, expected_file), "r", encoding="utf-8"
) as f:
expected_lines = list(csv.reader(f, delimiter="\t"))
actual_lines = list(csv.reader(actual_output.splitlines(), delimiter="\t"))
self.assertEqual(expected_lines, actual_lines)
def test_table_union_union(self):
returncode, stdout, stderr = self._run_command(
[
"table-union",
os.path.join(self.TEST_DATA_DIR, "dingbat.tsv"),
os.path.join(self.TEST_DATA_DIR, "loki.tsv"),
]
)
self.assertEqual(returncode, 0)
self._compare_tsv("combined.tsv", stdout)
self.assertEqual(stderr, "")
def test_table_union_join(self):
returncode, stdout, stderr = self._run_command(
[
"table-union",
"--no-union",
os.path.join(self.TEST_DATA_DIR, "users.tsv"),
os.path.join(self.TEST_DATA_DIR, "orders.tsv"),
]
)
self.assertEqual(returncode, 0)
self._compare_tsv("merged_expected.tsv", stdout)
self.assertEqual(stderr, "")
def test_table_summarize(self):
returncode, stdout, stderr = self._run_command(
["table-summarize", os.path.join(self.TEST_DATA_DIR, "data_summarize.tsv")]
)
self.assertEqual(returncode, 0)
expected_summary = """Summary:
Category:
\t - A: 3 of 6
\t - B: 2 of 6
\t - C: 1 of 6
Value:
\t - 10: 1 of 6
\t - 12: 1 of 6
\t - 15: 1 of 6
\t - 20: 1 of 6
\t - 25: 1 of 6
\t - 30: 1 of 6
"""
self.assertEqual(stdout.strip(), expected_summary.strip())
self.assertEqual(stderr, "")
def test_table_sort(self):
returncode, stdout, stderr = self._run_command(
[
"table-sort",
"-k",
"Age",
"-k",
"Name",
os.path.join(self.TEST_DATA_DIR, "data_sort.tsv"),
]
)
self.assertEqual(returncode, 0)
self._compare_tsv("sorted_data_expected.tsv", stdout)
self.assertEqual(stderr, "")
def test_table_sort_pipe(self):
with open(
os.path.join(self.TEST_DATA_DIR, "data_sort.tsv"), "r", encoding="utf-8"
) as infile:
input_data = infile.read()
returncode, stdout, stderr = self._run_command(
["table-sort", "-k", "Age", "-k", "Name"], input_data
)
self.assertEqual(returncode, 0)
self._compare_tsv("sorted_data_expected.tsv", stdout)
self.assertEqual(stderr, "")
if __name__ == "__main__":
unittest.main()