Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/python/pyste/tests/SmartFileUT.py @ 45

Last change on this file since 45 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 2.4 KB
Line 
1# Copyright Bruno da Silva de Oliveira 2003. Use, modification and
2# distribution is subject to the Boost Software License, Version 1.0.
3# (See accompanying file LICENSE_1_0.txt or copy at
4# http:#www.boost.org/LICENSE_1_0.txt)
5import sys
6sys.path.append('../src')
7from SmartFile import *
8import unittest
9import tempfile
10import os
11import time
12
13
14class SmartFileTest(unittest.TestCase):
15
16    FILENAME = tempfile.mktemp()
17   
18    def setUp(self):
19        self._Clean()
20       
21    def tearDown(self):
22        self._Clean()
23
24    def _Clean(self):
25        try:
26            os.remove(self.FILENAME)
27        except OSError: pass
28
29       
30    def testNonExistant(self):
31        "Must override the file, as there's no file in the disk yet"
32        self.assert_(not os.path.isfile(self.FILENAME))
33        f = SmartFile(self.FILENAME, 'w')
34        f.write('Testing 123\nTesting again.')
35        f.close()
36        self.assert_(os.path.isfile(self.FILENAME))
37
38
39    def testOverride(self):
40        "Must override the file, because the contents are different"
41        contents = 'Contents!\nContents!'
42        # create the file normally first
43        f = file(self.FILENAME, 'w')
44        f.write(contents)
45        f.close()
46        file_time = os.path.getmtime(self.FILENAME)
47        self.assert_(os.path.isfile(self.FILENAME))
48        time.sleep(2)
49        f = SmartFile(self.FILENAME, 'w')
50        f.write(contents + '_')
51        f.close()
52        new_file_time = os.path.getmtime(self.FILENAME)
53        self.assert_(new_file_time != file_time)
54
55
56    def testNoOverride(self):
57        "Must not override the file, because the contents are the same"
58        contents = 'Contents!\nContents!'
59        # create the file normally first
60        f = file(self.FILENAME, 'w')
61        f.write(contents)
62        f.close()
63        file_time = os.path.getmtime(self.FILENAME)
64        self.assert_(os.path.isfile(self.FILENAME))
65        time.sleep(2)
66        f = SmartFile(self.FILENAME, 'w')
67        f.write(contents)
68        f.close()
69        new_file_time = os.path.getmtime(self.FILENAME)
70        self.assert_(new_file_time == file_time) 
71
72
73    def testAutoClose(self):
74        "Must be closed when garbage-collected"
75        def foo():
76            f = SmartFile(self.FILENAME)
77            f.write('testing') 
78            self.assert_(not os.path.isfile(self.FILENAME))
79        foo()
80        self.assert_(os.path.isfile(self.FILENAME))
81       
82
83if __name__ == '__main__':
84    unittest.main()
Note: See TracBrowser for help on using the repository browser.