1 | import unittest |
---|
2 | import os |
---|
3 | import order_boundary as ob |
---|
4 | |
---|
5 | |
---|
6 | class Test_order_boundary(unittest.TestCase): |
---|
7 | def setUp(self): |
---|
8 | pass |
---|
9 | |
---|
10 | def tearDown(self): |
---|
11 | pass |
---|
12 | |
---|
13 | def test_Simple(self): |
---|
14 | import os |
---|
15 | import csv |
---|
16 | |
---|
17 | # filenames |
---|
18 | Test_input_file_path = 'test.in.csv' |
---|
19 | Test_output_file_path = 'test.out.csv' |
---|
20 | |
---|
21 | # input data |
---|
22 | Data = [('longitude','latitude','index'), |
---|
23 | ( 1.0, 1.0, 'alpha', 'extra'), |
---|
24 | ( 2.3, 2.0, 'bravo'), |
---|
25 | ( 3.9, 3.0, 'charlie'), |
---|
26 | ( 9.0, 9.9, 'delta'), |
---|
27 | (10.0, 10.4, 'echo'), |
---|
28 | (11.0, 11.0, 'foxtrot'), |
---|
29 | (15.9, 16.0, 'golf'), |
---|
30 | (17.0, 17.1, 'hotel'), |
---|
31 | (17.9, 18.0, 'india'), |
---|
32 | (12.2, 12.0, 'juliet'), |
---|
33 | ( 4.7, 4.0, 'kilo'), |
---|
34 | ( 5.2, 5.0, 'lima'), |
---|
35 | ( 6.0, 6.0, 'mike'), |
---|
36 | ( 7.0, 7.3, 'november', 'extra', 'extra', 'extra'), |
---|
37 | ( 8.0, 8.7, 'oscar'), |
---|
38 | (13.6, 13.0, 'papa'), |
---|
39 | (14.9, 14.0, 'quebec'), |
---|
40 | (15.8, 15.0, 'romeo'), |
---|
41 | (16.0, 16.2, 'sierra'), |
---|
42 | (17.1, 17.1, 'tango'), |
---|
43 | (18.0, 18.7, 'uniform'), |
---|
44 | (19.0, 19.9, 'victor'), |
---|
45 | (20.0, 20.0, 'whisky')] |
---|
46 | |
---|
47 | # expected output data |
---|
48 | Expected = [('longitude','latitude','index'), |
---|
49 | ( 1.0, 1.0, 'alpha', 'extra'), |
---|
50 | ( 2.3, 2.0, 'bravo'), |
---|
51 | ( 3.9, 3.0, 'charlie'), |
---|
52 | ( 4.7, 4.0, 'kilo'), |
---|
53 | ( 5.2, 5.0, 'lima'), |
---|
54 | ( 6.0, 6.0, 'mike'), |
---|
55 | ( 7.0, 7.3, 'november', 'extra', 'extra', 'extra'), |
---|
56 | ( 8.0, 8.7, 'oscar'), |
---|
57 | ( 9.0, 9.9, 'delta'), |
---|
58 | (10.0, 10.4, 'echo'), |
---|
59 | (11.0, 11.0, 'foxtrot'), |
---|
60 | (12.2, 12.0, 'juliet'), |
---|
61 | (13.6, 13.0, 'papa'), |
---|
62 | (14.9, 14.0, 'quebec'), |
---|
63 | (15.8, 15.0, 'romeo'), |
---|
64 | (15.9, 16.0, 'golf'), |
---|
65 | (16.0, 16.2, 'sierra'), |
---|
66 | (17.0, 17.1, 'hotel'), |
---|
67 | (17.1, 17.1, 'tango'), |
---|
68 | (17.9, 18.0, 'india'), |
---|
69 | (18.0, 18.7, 'uniform'), |
---|
70 | (19.0, 19.9, 'victor'), |
---|
71 | (20.0, 20.0, 'whisky')] |
---|
72 | |
---|
73 | # put test data into a file |
---|
74 | fd = open(Test_input_file_path, 'wb') |
---|
75 | w = csv.writer(fd) |
---|
76 | for d in Data: |
---|
77 | w.writerow(d) |
---|
78 | fd.close() |
---|
79 | |
---|
80 | # call routine, put sorted points into output file |
---|
81 | ob.order_boundary(Test_input_file_path, Test_output_file_path) |
---|
82 | |
---|
83 | # get sorted data into memory |
---|
84 | fd = open(Test_output_file_path, 'r') |
---|
85 | data_list = [] |
---|
86 | for data in csv.reader(fd): |
---|
87 | try: |
---|
88 | data[0] = float(data[0]) |
---|
89 | except: |
---|
90 | pass |
---|
91 | try: |
---|
92 | data[1] = float(data[1]) |
---|
93 | except: |
---|
94 | pass |
---|
95 | data_list.append(tuple(data)) |
---|
96 | fd.close() |
---|
97 | |
---|
98 | # check same as Expected |
---|
99 | self.failUnless(data_list == Expected) |
---|
100 | |
---|
101 | # clean up |
---|
102 | try: |
---|
103 | os.remove(Test_input_file_path) |
---|
104 | except: |
---|
105 | pass |
---|
106 | try: |
---|
107 | os.remove(Test_output_file_path) |
---|
108 | except: |
---|
109 | pass |
---|
110 | |
---|
111 | def test_OK(self): |
---|
112 | infile = 'test.csv' |
---|
113 | outfile = 'test.out.csv' |
---|
114 | |
---|
115 | ob.order_boundary(infile, outfile) |
---|
116 | |
---|
117 | os.remove(outfile) |
---|
118 | |
---|
119 | |
---|
120 | #------------------------------------------------------------- |
---|
121 | if __name__ == "__main__": |
---|
122 | suite = unittest.makeSuite(Test_order_boundary, 'test') |
---|
123 | runner = unittest.TextTestRunner() |
---|
124 | runner.run(suite) |
---|
125 | |
---|