2 _baseDataPath = os.path.dirname(os.path.abspath(__file__))
3 _baseDataPath +=
"/data/test_Utilities/"
5 def test_openPossiblyCompressedFile():
12 prefix = _baseDataPath +
"openPossiblyCompressedFile/"
13 return openPossiblyCompressedFile(prefix + suffix)
15 def compare(suffix, expected):
16 assert oPCF(suffix).read() == expected
18 def isBZ2File(suffix):
21 if sys.version_info[0] < 3:
22 BZ2FileType = bz2.BZ2File
25 BZ2FileType = _io.TextIOWrapper
27 assert isinstance(oPCF(suffix), BZ2FileType)
29 def isPlainFile(suffix):
31 if sys.version_info[0] < 3:
35 PlainFileType = _io.TextIOWrapper
37 assert isinstance(oPCF(suffix), PlainFileType)
41 for x
in [
"nonexistent.bz2",
"dirPath2.bz2"]:
42 with pytest.raises(IOError):
45 for x
in [
"nonexistent",
"dirPath",
"dirPath/foo"]:
46 with pytest.raises(ValueError):
49 isBZ2File(
"bz2file.bz2")
50 compare(
"bz2file.bz2",
"I am bz2file.bz2!\n")
53 compare(
"bz2file.bz2",
"I am bz2file.bz2!\n")
55 _path = _baseDataPath +
"openPossiblyCompressedFile/bz2file"
56 assert open(_path,
"r").read() ==
"I am bz2file!\n"
58 isPlainFile(
"plainfile")
59 compare(
"plainfile",
"I am plainfile!\n")
61 isPlainFile(
"dirPath/plainfile")
62 compare(
"dirPath/plainfile",
"I am dirPath/plainfile!\n")
64 isBZ2File(
"dirPath2.bz2/bz2file.bz2")
65 compare(
"dirPath2.bz2/bz2file.bz2",
"I am dirPath2.bz2/bz2file.bz2!\n")
67 isBZ2File(
"dirPath2.bz2/bz2file")
68 compare(
"dirPath2.bz2/bz2file.bz2",
"I am dirPath2.bz2/bz2file.bz2!\n")
72 def test_readValuePairsFromFile():
78 def kvp(suffix, minXValue = None):
79 prefix = _baseDataPath +
"readValuePairsFromFile/"
81 return readValuePairsFromFile(prefix + suffix)
83 return readValuePairsFromFile(prefix + suffix, minXValue)
86 for x
in [
"nonexistent.bz2",
"dirPath2.bz2"]:
87 with pytest.raises(IOError):
90 for x
in [
"nonexistent",
"dirPath",
"dirPath/foo"]:
91 with pytest.raises(ValueError):
95 with pytest.raises(ValueError):
100 assert isinstance(d, collections.OrderedDict)
101 expected = collections.OrderedDict()
102 expected[-1.5] = 3.1415
104 expected[900.0] = 0.1
107 d = kvp(
"plain.txt", -1.0)
108 assert isinstance(d, collections.OrderedDict)
109 expected = collections.OrderedDict()
111 expected[900.0] = 0.1
115 assert isinstance(d, collections.OrderedDict)
116 expected = collections.OrderedDict()
117 expected[-1.5] = 3.1415
119 expected[800.0] = 0.1
125 def test_getConfigValueAndCheckConstistency():
129 def getConfig(suffix):
130 prefix = _baseDataPath +
"getConfigValueAndCheckConstistency/"
134 return Configuration(prefix + suffix)
137 with pytest.raises(TypeError):
138 gcc(
"x",
"foo",
None)
140 with pytest.raises(TypeError):
141 gcc(
"x",
"foo",
"bar")
144 c1 = getConfig(
"c1.txt")
146 with pytest.raises(TypeError):
149 with pytest.raises(TypeError):
154 assert gcc(c1,
"foo",
None) ==
"bar"
155 assert gcc(c1,
"foo",
"bar") ==
"bar"
156 assert gcc(c1,
"baz",
None) == 1.0
157 assert gcc(c1,
"baz", 1.0) == 1.0
158 assert gcc(c1,
"grp.asdf",
None) == 10
159 assert gcc(c1,
"grp.asdf", 10) == 10
161 with pytest.raises(ValueError):
162 gcc(c1,
"foo",
"foo")
164 with pytest.raises(ValueError):
167 with pytest.raises(KeyError):
168 gcc(c1,
"asdf",
None)
170 with pytest.raises(ValueError):
173 with pytest.raises(ValueError):
174 gcc(c1,
"baz",
"1234")
176 with pytest.raises(ValueError):
177 gcc(c1,
"grp.asdf", -10)
181 def test_getConsistentConfigValue():
188 _baseDataPath +
"getConsistentConfigValue/" + x
189 for x
in [
"compatible1",
"compatible2"]
193 _baseDataPath +
"getConsistentConfigValue/" + x
194 for x
in [
"compatible1",
"compatible2",
"other"]
198 with pytest.raises(TypeError):
201 with pytest.raises(TypeError):
202 gcc((x
for x
in compatible),
"foo")
205 assert gcc(compatible,
"foo") ==
"bar"
206 assert gcc(compatible,
"baz") == 1.0
207 assert gcc(compatible,
"grp.asdf") == 10
209 assert gcc(compatible,
"same") ==
"hello"
210 assert gcc(incompatible,
"same") ==
"hello"
212 with pytest.raises(ValueError):
213 gcc(compatible,
"nonexistent")
215 with pytest.raises(ValueError):
216 gcc(compatible,
"unique")
218 with pytest.raises(ValueError):
219 gcc(incompatible,
"foo")
221 with pytest.raises(ValueError):
222 gcc(incompatible,
"baz")
224 with pytest.raises(ValueError):
225 gcc(incompatible,
"grp.asdf")
228 def test_getNumberOfArgumentsFromCallable():
238 def twoArgsWithDefault(arg1, arg2 = 1):
241 def threeArgs(arg1, arg2, arg3):
244 fourArgs =
lambda x, y, z, w: 0
247 assert gna(zeroArgs) == 0
248 assert gna(oneArg) == 1
249 assert gna(twoArgsWithDefault) == 2
250 assert gna(threeArgs) == 3
251 assert gna(fourArgs) == 4
252 assert gna(
lambda a, b, c, d, e: a) == 5