-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathtest_python.py
More file actions
168 lines (154 loc) · 4.59 KB
/
test_python.py
File metadata and controls
168 lines (154 loc) · 4.59 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
##
# copyright 2009, James William Pye
# http://python.projects.postgresql.org
##
import unittest
import socket
import errno
from itertools import chain
from operator import methodcaller
from contextlib import contextmanager
from ..python.contextlib import *
from ..python import functools
from ..python import itertools
from ..python.socket import find_available_port
from ..python import element
class Ele(element.Element):
_e_label = property(
lambda x: getattr(x, 'label', 'ELEMENT')
)
_e_factors = ('ancestor', 'secondary')
secondary = None
def __init__(self, s = None):
self.ancestor = s
def __str__(self):
return 'STRDATA'
def _e_metas(self):
yield ('first', getattr(self, 'first', 'firstv'))
yield ('second', getattr(self, 'second', 'secondv'))
class test_element(unittest.TestCase):
def test_primary_factor(self):
x = Ele()
# no factors
self.failUnlessEqual(element.prime_factor(object()), None)
self.failUnlessEqual(element.prime_factor(x), ('ancestor', None))
y = Ele(x)
self.failUnlessEqual(element.prime_factor(y), ('ancestor', x))
def test_primary_factors(self):
x = Ele()
x.ancestor = x
self.failUnlessRaises(
element.RecursiveFactor, list, element.prime_factors(x)
)
y = Ele(x)
x.ancestor = y
self.failUnlessRaises(
element.RecursiveFactor, list, element.prime_factors(y)
)
self.failUnlessRaises(
element.RecursiveFactor, list, element.prime_factors(x)
)
x.ancestor = None
z = Ele(y)
self.failUnlessEqual(list(element.prime_factors(z)), [
('ancestor', y),
('ancestor', x),
('ancestor', None),
])
def test_format_element(self):
# Considering that this is subject to change, frequently,
# I/O equality tests are inappropriate.
# Rather, a hierarchy will be defined, and the existence
# of certain pieces of information in the string will be validated.
x = Ele()
y = Ele()
z = Ele()
alt1 = Ele()
alt2 = Ele()
alt1.first = 'alt1-first'
alt1.second = 'alt1-second'
alt2.first = 'alt2-first'
alt2.second = 'alt2-second'
altprime = Ele()
altprime.first = 'alt2-ancestor'
alt2.ancestor = altprime
z.ancestor = y
y.ancestor = x
z.secondary = alt1
y.secondary = alt2
x.first = 'unique1'
y.first = 'unique2'
x.second = 'unique3'
z.second = 'unique4'
y.label = 'DIFF'
data = element.format_element(z)
self.failUnless(x.first in data)
self.failUnless(y.first in data)
self.failUnless(x.second in data)
self.failUnless(z.second in data)
self.failUnless('DIFF' in data)
self.failUnless('alt1-first' in data)
self.failUnless('alt2-first' in data)
self.failUnless('alt1-second' in data)
self.failUnless('alt2-second' in data)
self.failUnless('alt2-ancestor' in data)
x.ancestor = z
self.failUnlessRaises(element.RecursiveFactor, element.format_element, z)
class test_itertools(unittest.TestCase):
def testInterlace(self):
i1 = range(0, 100, 4)
i2 = range(1, 100, 4)
i3 = range(2, 100, 4)
i4 = range(3, 100, 4)
self.failUnlessEqual(
list(itertools.interlace(i1, i2, i3, i4)),
list(range(100))
)
class test_functools(unittest.TestCase):
def testComposition(self):
compose = functools.Composition
simple = compose((int, str))
self.failUnlessEqual("100", simple("100"))
timesfour_fourtimes = compose((methodcaller('__mul__', 4),)*4)
self.failUnlessEqual(4*(4*4*4*4), timesfour_fourtimes(4))
nothing = compose(())
self.failUnlessEqual(nothing("100"), "100")
self.failUnlessEqual(nothing(100), 100)
self.failUnlessEqual(nothing(None), None)
def testRSetAttr(self):
class anob(object):
pass
ob = anob()
self.failUnlessRaises(AttributeError, getattr, ob, 'foo')
rob = functools.rsetattr('foo', 'bar', ob)
self.failUnless(rob is ob)
self.failUnless(rob.foo is ob.foo)
self.failUnless(rob.foo == 'bar')
class test_socket(unittest.TestCase):
def testFindAvailable(self):
# the port is randomly generated, so make a few trials before
# determining success.
for i in range(100):
portnum = find_available_port()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect(('localhost', portnum))
except socket.error as err:
self.failUnlessEqual(err.errno, errno.ECONNREFUSED)
else:
self.fail("got a connection to an available port: " + str(portnum))
class test_contextlib(unittest.TestCase):
def testNoCM(self):
with NoCM as foo:
pass
self.failUnlessEqual(foo, None)
self.failUnlessEqual(NoCM(), NoCM)
# has no state, may be used repeatedly
with NoCM as foo:
pass
self.failUnlessEqual(foo, None)
if __name__ == '__main__':
from types import ModuleType
this = ModuleType("this")
this.__dict__.update(globals())
unittest.main(this)