annotate tests/test_columns.py @ 18:56596902e9ae default tip

add some setup + tests
author Jeff Hammel <k0scist@gmail.com>
date Sun, 10 Dec 2017 17:57:03 -0800
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 import csv
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 import os
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 import shutil
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 import tempfile
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 import unittest
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 from collections import OrderedDict
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 from orion.columns import read_columns
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 from orion.transpose import transpose
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 from StringIO import StringIO
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 class TestColumns(unittest.TestCase):
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 # fake data
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 header = ["Animal", "Baby", "Group"]
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 data = [("Fox", "Kit", "Leash"),
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 ("Whale", "Calf", "Pod"),
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 ("Cuttlefish", "Hatchling", "Bob"),
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 ("Crow", "Hatchling", "Murder")]
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 def create_data(self, fp):
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 """write simple data to a test file and return its path"""
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 writer = csv.writer(fp)
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 writer.writerow(self.header)
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 writer.writerows(self.data)
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 fp.flush()
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 def fake_data_assertions(self, columns):
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 """test the columns created from our fake data"""
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 assert len(columns) == len(self.header)
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 assert set(columns.keys()) == set(self.header)
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 transposed = transpose(self.data)
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 assert set([len(row) for row in transposed]) == set([len(self.data)])
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 for key, column in zip(self.header, transposed):
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 assert len(columns[key]) == len(self.data)
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 assert columns[key] == column
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 def test_string(self):
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 """basic test of columns interface"""
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 # write fake data
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 fileobj = StringIO()
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47 self.create_data(fileobj)
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48 fileobj.seek(0)
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 # read into columns
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51 columns = read_columns(fileobj)
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53 # test what we have
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
54 self.fake_data_assertions(columns)
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
56 def test_file(self):
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
57 """write to a real file and use that"""
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
58
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
59 tmpdir = tempfile.mkdtemp()
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
60 try:
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
61 # write the data
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
62 path = os.path.join(tmpdir, 'tmp.csv')
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
63 with open(path, 'w') as f:
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
64 self.create_data(f)
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
65 assert os.path.exists(path)
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
66
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
67 # read into columns
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
68 columns = read_columns(path)
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
69
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
70 # test what we have
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
71 self.fake_data_assertions(columns)
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
72 finally:
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
73 shutil.rmtree(tmpdir, ignore_errors=True)
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
74
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
75 def test_ordered_dict(self):
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
76 """ensure we can make use of an OrderedDict"""
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
77
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
78 # write fake data
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
79 fileobj = StringIO()
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
80 self.create_data(fileobj)
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
81 fileobj.seek(0)
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
82
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
83 # read into columns
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
84 columns = read_columns(fileobj, type=OrderedDict)
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
85
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
86 # test what we have
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
87 self.fake_data_assertions(columns)
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
88
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
89
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
90 if __name__ == '__main__':
56596902e9ae add some setup + tests
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
91 unittest.main()