forked from materialsvirtuallab/monty
/
test_io.py
164 lines (139 loc) · 5.91 KB
/
test_io.py
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
import os
import unittest
try:
from pathlib import Path
except ImportError:
Path = None # type: ignore
from monty.io import (
FileLock,
FileLockException,
reverse_readfile,
reverse_readline,
zopen,
)
test_dir = os.path.join(os.path.dirname(__file__), "test_files")
class ReverseReadlineTest(unittest.TestCase):
NUMLINES = 3000
def test_reverse_readline(self):
"""
We are making sure a file containing line numbers is read in reverse
order, i.e. the first line that is read corresponds to the last line.
number
"""
with open(os.path.join(test_dir, "3000_lines.txt")) as f:
for idx, line in enumerate(reverse_readline(f)):
self.assertEqual(
int(line),
self.NUMLINES - idx,
"read_backwards read {} whereas it should " "have read {}".format(int(line), self.NUMLINES - idx),
)
def test_reverse_readline_fake_big(self):
"""
Make sure that large textfiles are read properly
"""
with open(os.path.join(test_dir, "3000_lines.txt")) as f:
for idx, line in enumerate(reverse_readline(f, max_mem=0)):
self.assertEqual(
int(line),
self.NUMLINES - idx,
"read_backwards read {} whereas it should " "have read {}".format(int(line), self.NUMLINES - idx),
)
def test_reverse_readline_bz2(self):
"""
We are making sure a file containing line numbers is read in reverse
order, i.e. the first line that is read corresponds to the last line.
number
"""
lines = []
with zopen(os.path.join(test_dir, "myfile_bz2.bz2"), "rb") as f:
for line in reverse_readline(f):
lines.append(line.strip())
self.assertIn(lines[-1].strip(), ["HelloWorld.", b"HelloWorld."])
def test_empty_file(self):
"""
make sure an empty file does not throw an error when reverse_readline
is called this was a problem with an earlier implementation
"""
with open(os.path.join(test_dir, "empty_file.txt")) as f:
for idx, line in enumerate(reverse_readline(f)):
raise ValueError("an empty file is being read!")
class ReverseReadfileTest(unittest.TestCase):
NUMLINES = 3000
def test_reverse_readfile(self):
"""
We are making sure a file containing line numbers is read in reverse
order, i.e. the first line that is read corresponds to the last line.
number
"""
fname = os.path.join(test_dir, "3000_lines.txt")
for idx, line in enumerate(reverse_readfile(fname)):
self.assertEqual(
int(line),
self.NUMLINES - idx,
"read_backwards read {} whereas it should " "have read {}".format(int(line), self.NUMLINES - idx),
)
def test_reverse_readfile_gz(self):
"""
We are making sure a file containing line numbers is read in reverse
order, i.e. the first line that is read corresponds to the last line.
number
"""
fname = os.path.join(test_dir, "3000_lines.txt.gz")
for idx, line in enumerate(reverse_readfile(fname)):
self.assertEqual(
int(line),
self.NUMLINES - idx,
"read_backwards read {} whereas it should " "have read {}".format(int(line), self.NUMLINES - idx),
)
def test_reverse_readfile_bz2(self):
"""
We are making sure a file containing line numbers is read in reverse
order, i.e. the first line that is read corresponds to the last line.
number
"""
fname = os.path.join(test_dir, "3000_lines.txt.bz2")
for idx, line in enumerate(reverse_readfile(fname)):
self.assertEqual(
int(line),
self.NUMLINES - idx,
"read_backwards read {} whereas it should " "have read {}".format(int(line), self.NUMLINES - idx),
)
def test_empty_file(self):
"""
make sure an empty file does not throw an error when reverse_readline
is called this was a problem with an earlier implementation
"""
for idx, line in enumerate(reverse_readfile(os.path.join(test_dir, "empty_file.txt"))):
raise ValueError("an empty file is being read!")
class ZopenTest(unittest.TestCase):
def test_zopen(self):
with zopen(os.path.join(test_dir, "myfile_gz.gz"), mode="rt") as f:
self.assertEqual(f.read(), "HelloWorld.\n\n")
with zopen(os.path.join(test_dir, "myfile_bz2.bz2"), mode="rt") as f:
self.assertEqual(f.read(), "HelloWorld.\n\n")
with zopen(os.path.join(test_dir, "myfile_bz2.bz2"), "rt") as f:
self.assertEqual(f.read(), "HelloWorld.\n\n")
with zopen(os.path.join(test_dir, "myfile_xz.xz"), "rt") as f:
self.assertEqual(f.read(), "HelloWorld.\n\n")
with zopen(os.path.join(test_dir, "myfile_lzma.lzma"), "rt") as f:
self.assertEqual(f.read(), "HelloWorld.\n\n")
with zopen(os.path.join(test_dir, "myfile"), mode="rt") as f:
self.assertEqual(f.read(), "HelloWorld.\n\n")
@unittest.skipIf(Path is None, "Not Py3k")
def test_Path_objects(self):
p = Path(test_dir) / "myfile_gz.gz"
with zopen(p, mode="rt") as f:
self.assertEqual(f.read(), "HelloWorld.\n\n")
class FileLockTest(unittest.TestCase):
def setUp(self):
self.file_name = "__lock__"
self.lock = FileLock(self.file_name, timeout=1)
self.lock.acquire()
def test_raise(self):
with self.assertRaises(FileLockException):
new_lock = FileLock(self.file_name, timeout=1)
new_lock.acquire()
def tearDown(self):
self.lock.release()
if __name__ == "__main__":
unittest.main()