root/_test_ftp_stat_cache.py

Revision 869:808476873684, 3.3 kB (checked in by Stefan Schwarzer <sschwarzer@…>, 3 months ago)
Replaced licenses in each file with reference to common `LICENSE` file (suggested by Steve Steiner). I removed the copyright notice for Roger Demetrescu from `_test_ftputil.py` because I remembered only after the last commit that the tests for the `with` statement had gone into their own file, `_test_with_statement.py`. I added Roger's name there, and also in `ftp_file.py` which had also gotten `with` support.
Line 
1# Copyright (C) 2006, Stefan Schwarzer <sschwarzer@sschwarzer.net>
2# See the file LICENSE for licensing terms.
3
4import time
5import unittest
6
7import ftp_stat_cache
8import _test_base
9
10
11class TestStatCache(unittest.TestCase):
12    def setUp(self):
13        self.cache = ftp_stat_cache.StatCache()
14
15    def test_get_set(self):
16        self.assertRaises(ftp_stat_cache.CacheMissError,
17                          self.cache.__getitem__, "path")
18        self.cache["path"] = "test"
19        self.assertEqual(self.cache["path"], "test")
20
21    def test_invalidate(self):
22        # don't raise a `CacheMissError` for missing paths
23        self.cache.invalidate("/path")
24        self.cache["/path"] = "test"
25        self.cache.invalidate("/path")
26        self.assertEqual(len(self.cache), 0)
27
28    def test_clear(self):
29        self.cache["path1"] = "test1"
30        self.cache["path2"] = "test2"
31        self.cache.clear()
32        self.assertEqual(len(self.cache), 0)
33
34    def test_contains(self):
35        self.cache["path1"] = "test1"
36        self.assertEqual("path1" in self.cache, True)
37        self.assertEqual("path2" in self.cache, False)
38
39    def test_len(self):
40        self.assertEqual(len(self.cache), 0)
41        self.cache["path1"] = "test1"
42        self.cache["path2"] = "test2"
43        self.assertEqual(len(self.cache), 2)
44
45    def test_resize(self):
46        self.cache.resize(100)
47        for i in xrange(150):
48            self.cache["/%d" % i] = i
49        self.assertEqual(len(self.cache), 100)
50
51    def test_max_age1(self):
52        """Set expiration after setting a cache item."""
53        self.cache["/path1"] = "test1"
54        # expire after one second
55        self.cache.max_age = 1
56        time.sleep(0.5)
57        # should still be present
58        self.assertEqual(self.cache["/path1"], "test1")
59        time.sleep(0.6)
60        # should have expired (_setting_ the cache counts)
61        self.assertRaises(ftp_stat_cache.CacheMissError,
62                          self.cache.__getitem__, "/path1")
63
64    def test_max_age2(self):
65        """Set expiration before setting a cache item."""
66        # expire after one second
67        self.cache.max_age = 1
68        self.cache["/path1"] = "test1"
69        time.sleep(0.5)
70        # should still be present
71        self.assertEqual(self.cache["/path1"], "test1")
72        time.sleep(0.6)
73        # should have expired (_setting_ the cache counts)
74        self.assertRaises(ftp_stat_cache.CacheMissError,
75                          self.cache.__getitem__, "/path1")
76
77    def test_disabled(self):
78        self.cache["/path1"] = "test1"
79        self.cache.disable()
80        self.cache["/path2"] = "test2"
81        self.assertRaises(ftp_stat_cache.CacheMissError,
82                          self.cache.__getitem__, "/path1")
83        self.assertRaises(ftp_stat_cache.CacheMissError,
84                          self.cache.__getitem__, "/path2")
85        self.assertEqual(len(self.cache), 1)
86        # don't raise a `CacheMissError` for missing paths
87        self.cache.invalidate("/path2")
88
89    def test_cache_size_zero(self):
90        host = _test_base.ftp_host_factory()
91        host.stat_cache.resize(0)
92        # if bug #38 is present, this raises an `IndexError`
93        items = host.listdir(host.curdir)
94        self.assertEqual(items[:3], ['chemeng', 'download', 'image'])
95
96
97if __name__ == '__main__':
98    unittest.main()
Note: See TracBrowser for help on using the browser.