OpenMPCD
test_Utilities.py
1 import os
2 _baseDataPath = os.path.dirname(os.path.abspath(__file__))
3 _baseDataPath += "/data/test_Utilities/"
4 
5 def test_openPossiblyCompressedFile():
6  from MPCDAnalysis.Utilities import openPossiblyCompressedFile
7  import pytest
8 
9  import bz2
10 
11  def oPCF(suffix):
12  prefix = _baseDataPath + "openPossiblyCompressedFile/"
13  return openPossiblyCompressedFile(prefix + suffix)
14 
15  def compare(suffix, expected):
16  assert oPCF(suffix).read() == expected
17 
18  def isBZ2File(suffix):
19  import bz2
20  import sys
21  if sys.version_info[0] < 3:
22  BZ2FileType = bz2.BZ2File
23  else:
24  import _io
25  BZ2FileType = _io.TextIOWrapper
26 
27  assert isinstance(oPCF(suffix), BZ2FileType)
28 
29  def isPlainFile(suffix):
30  import sys
31  if sys.version_info[0] < 3:
32  PlainFileType = file
33  else:
34  import _io
35  PlainFileType = _io.TextIOWrapper
36 
37  assert isinstance(oPCF(suffix), PlainFileType)
38 
39 
40 
41  for x in ["nonexistent.bz2", "dirPath2.bz2"]:
42  with pytest.raises(IOError):
43  oPCF(x)
44 
45  for x in ["nonexistent", "dirPath", "dirPath/foo"]:
46  with pytest.raises(ValueError):
47  oPCF(x)
48 
49  isBZ2File("bz2file.bz2")
50  compare("bz2file.bz2", "I am bz2file.bz2!\n")
51 
52  isBZ2File("bz2file")
53  compare("bz2file.bz2", "I am bz2file.bz2!\n")
54 
55  _path = _baseDataPath + "openPossiblyCompressedFile/bz2file"
56  assert open(_path, "r").read() == "I am bz2file!\n"
57 
58  isPlainFile("plainfile")
59  compare("plainfile", "I am plainfile!\n")
60 
61  isPlainFile("dirPath/plainfile")
62  compare("dirPath/plainfile", "I am dirPath/plainfile!\n")
63 
64  isBZ2File("dirPath2.bz2/bz2file.bz2")
65  compare("dirPath2.bz2/bz2file.bz2", "I am dirPath2.bz2/bz2file.bz2!\n")
66 
67  isBZ2File("dirPath2.bz2/bz2file")
68  compare("dirPath2.bz2/bz2file.bz2", "I am dirPath2.bz2/bz2file.bz2!\n")
69 
70 
71 
72 def test_readValuePairsFromFile():
73  from MPCDAnalysis.Utilities import readValuePairsFromFile
74  import pytest
75 
76  import collections
77 
78  def kvp(suffix, minXValue = None):
79  prefix = _baseDataPath + "readValuePairsFromFile/"
80  if minXValue is None:
81  return readValuePairsFromFile(prefix + suffix)
82  else:
83  return readValuePairsFromFile(prefix + suffix, minXValue)
84 
85 
86  for x in ["nonexistent.bz2", "dirPath2.bz2"]:
87  with pytest.raises(IOError):
88  kvp(x)
89 
90  for x in ["nonexistent", "dirPath", "dirPath/foo"]:
91  with pytest.raises(ValueError):
92  kvp(x)
93 
94 
95  with pytest.raises(ValueError):
96  kvp("malformed.txt")
97 
98 
99  d = kvp("plain.txt")
100  assert isinstance(d, collections.OrderedDict)
101  expected = collections.OrderedDict()
102  expected[-1.5] = 3.1415
103  expected[0.0] = 1.0
104  expected[900.0] = 0.1
105  assert expected == d
106 
107  d = kvp("plain.txt", -1.0)
108  assert isinstance(d, collections.OrderedDict)
109  expected = collections.OrderedDict()
110  expected[0.0] = 1.0
111  expected[900.0] = 0.1
112  assert expected == d
113 
114  d = kvp("bz2.txt")
115  assert isinstance(d, collections.OrderedDict)
116  expected = collections.OrderedDict()
117  expected[-1.5] = 3.1415
118  expected[0.0] = 1.0
119  expected[800.0] = 0.1
120  assert expected == d
121 
122 
123 
124 
125 def test_getConfigValueAndCheckConstistency():
126  from MPCDAnalysis.Utilities import getConfigValueAndCheckConstistency as gcc
127  import pytest
128 
129  def getConfig(suffix):
130  prefix = _baseDataPath + "getConfigValueAndCheckConstistency/"
131 
132  from MPCDAnalysis.Configuration import Configuration
133 
134  return Configuration(prefix + suffix)
135 
136 
137  with pytest.raises(TypeError):
138  gcc("x", "foo", None)
139 
140  with pytest.raises(TypeError):
141  gcc("x", "foo", "bar")
142 
143 
144  c1 = getConfig("c1.txt")
145 
146  with pytest.raises(TypeError):
147  gcc(c1, 1, "bar")
148 
149  with pytest.raises(TypeError):
150  gcc(c1, 1, None)
151 
152 
153 
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
160 
161  with pytest.raises(ValueError):
162  gcc(c1, "foo", "foo")
163 
164  with pytest.raises(ValueError):
165  gcc(c1, "foo", 1)
166 
167  with pytest.raises(KeyError):
168  gcc(c1, "asdf", None)
169 
170  with pytest.raises(ValueError):
171  gcc(c1, "baz", 1234)
172 
173  with pytest.raises(ValueError):
174  gcc(c1, "baz", "1234")
175 
176  with pytest.raises(ValueError):
177  gcc(c1, "grp.asdf", -10)
178 
179 
180 
181 def test_getConsistentConfigValue():
182  from MPCDAnalysis.Utilities import getConsistentConfigValue as gcc
183  import pytest
184 
185 
186  compatible = \
187  [
188  _baseDataPath + "getConsistentConfigValue/" + x
189  for x in ["compatible1", "compatible2"]
190  ]
191  incompatible = \
192  [
193  _baseDataPath + "getConsistentConfigValue/" + x
194  for x in ["compatible1", "compatible2", "other"]
195  ]
196 
197 
198  with pytest.raises(TypeError):
199  gcc(compatible, 1)
200 
201  with pytest.raises(TypeError):
202  gcc((x for x in compatible), "foo")
203 
204 
205  assert gcc(compatible, "foo") == "bar"
206  assert gcc(compatible, "baz") == 1.0
207  assert gcc(compatible, "grp.asdf") == 10
208 
209  assert gcc(compatible, "same") == "hello"
210  assert gcc(incompatible, "same") == "hello"
211 
212  with pytest.raises(ValueError):
213  gcc(compatible, "nonexistent")
214 
215  with pytest.raises(ValueError):
216  gcc(compatible, "unique")
217 
218  with pytest.raises(ValueError):
219  gcc(incompatible, "foo")
220 
221  with pytest.raises(ValueError):
222  gcc(incompatible, "baz")
223 
224  with pytest.raises(ValueError):
225  gcc(incompatible, "grp.asdf")
226 
227 
228 def test_getNumberOfArgumentsFromCallable():
229  from MPCDAnalysis.Utilities import getNumberOfArgumentsFromCallable as gna
230  import pytest
231 
232  def zeroArgs():
233  pass
234 
235  def oneArg(arg1):
236  pass
237 
238  def twoArgsWithDefault(arg1, arg2 = 1):
239  pass
240 
241  def threeArgs(arg1, arg2, arg3):
242  pass
243 
244  fourArgs = lambda x, y, z, w: 0
245 
246 
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
MPCDAnalysis.Utilities
Definition: Utilities.py:1
MPCDAnalysis.Configuration
Definition: Configuration.py:1