2 Test suite for util.py. Uses unittest module.
4 2011, Razvan Deaconescu, razvan.deaconescu@cs.pub.ro.
16 class LiveLogStatusTest(unittest.TestCase):
17 """Test suite for LiveLogStatus class in util.py."""
20 """Class constructor works properly."""
21 status = util.LiveLogStatus(12345)
23 self.assertEqual(status.position, 12345)
25 class LiveLogFileTest(unittest.TestCase):
26 """Test suite for LiveLogFile class in util.py."""
27 ok_log_filename = "ok.log"
28 ok_status_filename = "ok.log.status"
29 empty_log_filename = "empty.log"
30 empty_status_filename = "empty.log.status"
31 incomplete_log_filename = "incomplete.log"
32 incomplete_status_filename = "incomplete.log.status"
33 no_log_filename = "no.log"
34 first_line = "line 1\n"
35 second_line = "line 1\n"
36 third_line = "line 1\n"
38 def remove_status_files(self):
40 os.remove(self.ok_status_filename)
45 os.remove(self.empty_status_filename)
50 os.remove(self.incomplete_status_filename)
55 """Create test log files."""
56 f = open(self.ok_log_filename, "wt")
57 f.write(self.first_line)
58 f.write(self.second_line)
59 f.write(self.third_line)
62 f = open(self.empty_log_filename, "wt")
65 f = open(self.incomplete_log_filename, "wt")
69 # Remove "no log" file and status files in case they exist.
71 os.remove(self.no_log_filename)
74 self.remove_status_files()
77 """Remove test log files."""
78 os.remove(self.ok_log_filename)
79 os.remove(self.empty_log_filename)
80 os.remove(self.incomplete_log_filename)
82 # Remove "no log" file and status files in case they exist.
84 os.remove(self.no_log_filename)
87 self.remove_status_files()
89 def read_position_in_status_file(self, status_filename):
90 """Read first line in status file as integer."""
91 f = open(status_filename, "rt")
95 def test_no_log_file(self):
96 """No log file is present."""
97 exception_raised = False
99 llf = util.LiveLogFile(self.no_log_filename)
101 exception_raised = True
103 self.assertEqual(exception_raised, True)
105 def test_no_status_file(self):
106 """No status file is present."""
108 # Status file should be created by constructor.
109 llf = util.LiveLogFile(self.ok_log_filename)
110 self.assertEqual(os.access(self.ok_status_filename, os.W_OK), True)
112 # Log file cursor should be 0 and the first line should be read.
113 line = llf.readline()
115 self.assertEqual(line, self.first_line)
117 # Check whether position is updated in status file.
118 pos = self.read_position_in_status_file(self.ok_status_filename)
119 self.assertEqual(pos, len(self.first_line))
121 def test_invalid_content_in_status_file(self):
122 """Status file contains invalid content (no string)."""
124 f = open(self.ok_status_filename, "wt")
125 f.write("ana-are-mere\n")
128 llf = util.LiveLogFile(self.ok_log_filename)
130 # Log file cursor should be 0 and the first line should be read.
131 line = llf.readline()
133 self.assertEqual(line, self.first_line)
135 # Check whether position is updated in status file.
136 pos = self.read_position_in_status_file(self.ok_status_filename)
137 self.assertEqual(pos, len(self.first_line))
139 def test_read_from_beggining(self):
140 """Read from beginning of the log file.
141 Status file is present.
143 f = open(self.ok_status_filename, "wt")
147 llf = util.LiveLogFile(self.ok_log_filename)
149 # Log file cursor should be 0 and the first line should be read.
150 line = llf.readline()
152 self.assertEqual(line, self.first_line)
154 # Check whether position is updated in status file.
155 pos = self.read_position_in_status_file(self.ok_status_filename)
156 self.assertEqual(pos, len(self.first_line))
158 def test_read_from_middle(self):
159 """Read the third line of the log file.
160 Status file content is 14 (the log file cursor position for
163 f = open(self.ok_status_filename, "wt")
164 f.write(str(len(self.first_line) + len(self.second_line)) + "\n")
167 llf = util.LiveLogFile(self.ok_log_filename)
169 # Log file cursor should be 14 and the third line should be read.
170 line = llf.readline()
172 self.assertEqual(line, self.third_line)
174 # Check whether position is updated in status file.
175 pos = self.read_position_in_status_file(self.ok_status_filename)
176 self.assertEqual(pos, len(self.first_line) +
177 len(self.second_line) + len(self.third_line))
179 def test_no_newline_at_end_of_status_file(self):
180 """Status file contains invalid content (no string)."""
181 f = open(self.ok_status_filename, "wt")
185 llf = util.LiveLogFile(self.ok_log_filename)
187 # Log file cursor should be 0 and the first line should be read.
188 line = llf.readline()
190 self.assertEqual(line, self.first_line)
192 # Check whether position is updated in status file.
193 pos = self.read_position_in_status_file(self.ok_status_filename)
194 self.assertEqual(pos, len(self.first_line))
196 def test_read_two_lines_from_log_file(self):
197 """Read two lines from the log file.
198 Status information should be updated accordingly.
200 f = open(self.ok_status_filename, "wt")
204 llf = util.LiveLogFile(self.ok_log_filename)
206 # Log file cursor should be 0 and the first two lines should be read.
207 line1 = llf.readline()
208 line2 = llf.readline()
210 self.assertEqual(line1, self.first_line)
211 self.assertEqual(line2, self.second_line)
213 # Check whether position is updated in status file.
214 pos = self.read_position_in_status_file(self.ok_status_filename)
215 self.assertEqual(pos, len(self.first_line) + len(self.second_line))
217 def test_status_position_is_greater_than_log_file_length(self):
218 """Information in status file is a too greater position.
219 No data may be read. Information stays the same in status file.
221 f = open(self.ok_status_filename, "wt")
225 llf = util.LiveLogFile(self.ok_log_filename)
227 # Log file cursor should be 0 and the first line should be read.
228 line = llf.readline()
230 self.assertEqual(line, "")
232 # Position should still be 200.
233 pos = self.read_position_in_status_file(self.ok_status_filename)
234 self.assertEqual(pos, 200)
236 def test_empty_log_file(self):
237 """Log file is empty (zero length)."""
239 f = open(self.empty_status_filename, "wt")
243 llf = util.LiveLogFile(self.empty_log_filename)
245 # Log file cursor should be 0 and a blank line should be returned.
246 line = llf.readline()
248 self.assertEqual(line, "")
250 # Position should still be 0.
251 pos = self.read_position_in_status_file(self.empty_status_filename)
252 self.assertEqual(pos, 0)
254 def test_incomplete_log_file(self):
255 """Log file's last doesn't end in a new line."""
257 f = open(self.incomplete_status_filename, "wt")
261 llf = util.LiveLogFile(self.incomplete_log_filename)
263 # Log file cursor should be 0 and a blank line should be returned.
264 line = llf.readline()
266 self.assertEqual(line, "")
268 # Position should still be 0.
269 pos = self.read_position_in_status_file(self.incomplete_status_filename)
270 self.assertEqual(pos, 0)
272 def test_readline_fails_after_close(self):
273 """Use readline() after close(). Should fail."""
275 f = open(self.ok_status_filename, "wt")
279 llf = util.LiveLogFile(self.ok_log_filename)
281 # Log file cursor should be 0 and the first line should be read.
282 line = llf.readline()
284 self.assertEqual(line, self.first_line)
286 exception_raised = False
289 except (IOError, ValueError), e:
290 exception_raised = True
292 self.assertEqual(exception_raised, True)
294 if __name__ == "__main__":