source: test/test_stat_cache.py @ 1663:40280d912474

Last change on this file since 1663:40280d912474 was 1663:40280d912474, checked in by Stefan Schwarzer <sschwarzer@…>, 3 years ago
Remove `unittest` dependency - Remove `unittest` imports - Let test case classes inherit from `object` instead of `unittest.TestCase` - Use `setup_method` and `teardown_method` instead of `unittest`'s `setUp` and `tearDown` - Use `pytest.mark.skipif` instead of `unittest.skipIf` - Use `pytest.mark.slow_test` instead of decorator in `test/__init__.py`. Exclude slow tests with py.test -m "not slow_test" test - Replace some leftover `assert*` calls with `assert` statements - Replace `assert False, ...` with `pytest.fail` ticket: 98
File size: 3.3 KB
Line 
1# Copyright (C) 2006-2016, Stefan Schwarzer <sschwarzer@sschwarzer.net>
2# and ftputil contributors (see `doc/contributors.txt`)
3# See the file LICENSE for licensing terms.
4
5from __future__ import unicode_literals
6
7import time
8
9import pytest
10
11import ftputil.error
12import ftputil.stat_cache
13
14from test import test_base
15
16
17class TestStatCache(object):
18
19    def setup_method(self, method):
20        self.cache = ftputil.stat_cache.StatCache()
21
22    def test_get_set(self):
23        with pytest.raises(ftputil.error.CacheMissError):
24            self.cache.__getitem__("/path")
25        self.cache["/path"] = "test"
26        assert self.cache["/path"] == "test"
27
28    def test_invalidate(self):
29        # Don't raise a `CacheMissError` for missing paths
30        self.cache.invalidate("/path")
31        self.cache["/path"] = "test"
32        self.cache.invalidate("/path")
33        assert len(self.cache) == 0
34
35    def test_clear(self):
36        self.cache["/path1"] = "test1"
37        self.cache["/path2"] = "test2"
38        self.cache.clear()
39        assert len(self.cache) == 0
40
41    def test_contains(self):
42        self.cache["/path1"] = "test1"
43        assert "/path1" in self.cache
44        assert "/path2" not in self.cache
45
46    def test_len(self):
47        assert len(self.cache) == 0
48        self.cache["/path1"] = "test1"
49        self.cache["/path2"] = "test2"
50        assert len(self.cache) == 2
51
52    def test_resize(self):
53        self.cache.resize(100)
54        # Don't grow the cache beyond it's set size.
55        for i in range(150):
56            self.cache["/{0:d}".format(i)] = i
57        assert len(self.cache) == 100
58
59    def test_max_age1(self):
60        """Set expiration after setting a cache item."""
61        self.cache["/path1"] = "test1"
62        # Expire after one second
63        self.cache.max_age = 1
64        time.sleep(0.5)
65        # Should still be present
66        assert self.cache["/path1"] == "test1"
67        time.sleep(0.6)
68        # Should have expired (_setting_ the cache counts)
69        with pytest.raises(ftputil.error.CacheMissError):
70            self.cache.__getitem__("/path1")
71
72    def test_max_age2(self):
73        """Set expiration before setting a cache item."""
74        # Expire after one second
75        self.cache.max_age = 1
76        self.cache["/path1"] = "test1"
77        time.sleep(0.5)
78        # Should still be present
79        assert self.cache["/path1"] == "test1"
80        time.sleep(0.6)
81        # Should have expired (_setting_ the cache counts)
82        with pytest.raises(ftputil.error.CacheMissError):
83            self.cache.__getitem__("/path1")
84
85    def test_disabled(self):
86        self.cache["/path1"] = "test1"
87        self.cache.disable()
88        self.cache["/path2"] = "test2"
89        with pytest.raises(ftputil.error.CacheMissError):
90            self.cache.__getitem__("/path1")
91        with pytest.raises(ftputil.error.CacheMissError):
92            self.cache.__getitem__("/path2")
93        assert len(self.cache) == 1
94        # Don't raise a `CacheMissError` for missing paths.
95        self.cache.invalidate("/path2")
96
97    def test_cache_size_zero(self):
98        host = test_base.ftp_host_factory()
99        with pytest.raises(ValueError):
100            host.stat_cache.resize(0)
101        # If bug #38 was present, this raised an `IndexError`.
102        items = host.listdir(host.curdir)
103        assert items[:3] == ["chemeng", "download", "image"]
Note: See TracBrowser for help on using the repository browser.