-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestutils.py
126 lines (104 loc) · 4.2 KB
/
testutils.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import unittest
from StringIO import StringIO
import sys
from numpy.testing import assert_allclose
from datetime import datetime
import common, subsample, cut
"""
To run, from the test/ directory, type:
$ python -m unittest test_utils
OR for verbose output
$ python -m unittest -v test_utils
OR to run only the methods in TestCut
$ python -m unittest test_utils.TestCut
OR to run only the TestCut.test_cut_file_keepname method
$ python -m unittest test_utils.TestCut.test_cut_file_keepname
"""
class TestCut(unittest.TestCase):
"""
Tests the implementation (but not the interface) of cut.py
"""
def setUp(self):
self.outfile = StringIO()
commastring = \
"name,age,weight\r\nian,1,11\r\ndaniel,2,22\r\nchang,3,33"
self.commafile = StringIO(commastring)
pipestring = \
"name|age|weight\r\nian|1|11\r\ndaniel|2|22\r\nchang|3|33"
self.pipefile = StringIO(pipestring)
def test_cut_file_keepname(self):
cut.cut_file(self.commafile, self.outfile, keep_list=['name'])
result = self.outfile.getvalue()
self.assertEqual('name\r\nian\r\ndaniel\r\nchang\r\n', result)
def test_cut_file_keepnameage(self):
cut.cut_file(self.commafile, self.outfile, keep_list=['name', 'age'])
result = self.outfile.getvalue()
self.assertEqual('name,age\r\nian,1\r\ndaniel,2\r\nchang,3\r\n', result)
def test_cut_file_keepagename(self):
cut.cut_file(self.commafile, self.outfile, keep_list=['age', 'name'])
result = self.outfile.getvalue()
self.assertEqual('age,name\r\n1,ian\r\n2,daniel\r\n3,chang\r\n', result)
def test_cut_file_keepagename_pipe(self):
cut.cut_file(
self.pipefile, self.outfile, keep_list=['age', 'name'],
delimiter='|')
result = self.outfile.getvalue()
self.assertEqual('age|name\r\n1|ian\r\n2|daniel\r\n3|chang\r\n', result)
def test_cut_file_keepempty(self):
"""
Test keeping no columns
"""
cut.cut_file(self.commafile, self.outfile, keep_list=[])
result = self.outfile.getvalue()
self.assertEqual('\r\n\r\n\r\n\r\n', result)
def test_cut_file_keepNone(self):
"""
Test keeping no columns
"""
cut.cut_file(self.commafile, self.outfile, keep_list=None)
result = self.outfile.getvalue()
self.assertEqual('\r\n\r\n\r\n\r\n', result)
def tearDown(self):
self.outfile.close()
class TestSubsample(unittest.TestCase):
"""
Tests the subsampler
"""
def setUp(self):
self.outfile = StringIO()
self.commafile = StringIO(
'name,age,weight\nian,1,11\ndaniel,2,22\nchang,3,33')
self.pipefile = StringIO(
'name|age|weight\nian|1|11\ndaniel|2|22\nchang|3|33')
self.longfile = StringIO(
'name,age,weight\nian,1,11\ndaniel,2,22\nian,1b,11b\nchang,3,33'
'\ndaniel,2b,22b\nchang,3b,33b')
self.seed = 1234
def test_r0p0_comma(self):
subsample.subsample(
self.commafile, self.outfile, subsample_rate=0.0, seed=self.seed)
result = self.outfile.getvalue()
benchmark = 'name,age,weight\r\n'
self.assertEqual(result, benchmark)
def test_r0p5_comma(self):
subsample.subsample(
self.commafile, self.outfile, subsample_rate=0.5, seed=self.seed)
result = self.outfile.getvalue()
benchmark = 'name,age,weight\r\nian,1,11\r\nchang,3,33\r\n'
self.assertEqual(result, benchmark)
def test_r0p5_pipe(self):
subsample.subsample(
self.pipefile, self.outfile, subsample_rate=0.5, seed=self.seed)
result = self.outfile.getvalue()
benchmark = 'name|age|weight\r\nian|1|11\r\nchang|3|33\r\n'
self.assertEqual(result, benchmark)
def test_r0p5_keyname_comma(self):
subsample.subsample(
self.longfile, self.outfile, subsample_rate=0.5,
key_column='name', seed=self.seed)
result = self.outfile.getvalue()
benchmark = 'name,age,weight\r\nian,1,11\r\nian,1b,11b\r\nchang,3,33'\
'\r\nchang,3b,33b\r\n'
self.assertEqual(result, benchmark)
def tearDown(self):
self.outfile.close()