source: test/test_stat_cache.py @ 1660:93ea351f922b

Last change on this file since 1660:93ea351f922b was 1660:93ea351f922b, checked in by Stefan Schwarzer <sschwarzer@…>, 3 years ago
Remove `unittest.main` calls Since we now use pytest as test runner and not a shell loop in the makefile, there's no longer a need for explicit `unittest.main` calls. 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
8import unittest
9
10import pytest
11
12import ftputil.error
13import ftputil.stat_cache
14
15from test import test_base
16
17
18class TestStatCache(unittest.TestCase):
19
20    def setUp(self):
21        self.cache = ftputil.stat_cache.StatCache()
22
23    def test_get_set(self):
24        with pytest.raises(ftputil.error.CacheMissError):
25            self.cache.__getitem__("/path")
26        self.cache["/path"] = "test"
27        assert self.cache["/path"] == "test"
28
29    def test_invalidate(self):
30        # Don't raise a `CacheMissError` for missing paths
31        self.cache.invalidate("/path")
32        self.cache["/path"] = "test"
33        self.cache.invalidate("/path")
34        assert len(self.cache) == 0
35
36    def test_clear(self):
37        self.cache["/path1"] = "test1"
38        self.cache["/path2"] = "test2"
39        self.cache.clear()
40        assert len(self.cache) == 0
41
42    def test_contains(self):
43        self.cache["/path1"] = "test1"
44        assert "/path1" in self.cache
45        assert "/path2" not in self.cache
46
47    def test_len(self):
48        self.assertEqual(len(self.cache), 0)
49        self.cache["/path1"] = "test1"
50        self.cache["/path2"] = "test2"
51        assert len(self.cache) == 2
52
53    def test_resize(self):
54        self.cache.resize(100)
55        # Don't grow the cache beyond it's set size.
56        for i in range(150):
57            self.cache["/{0:d}".format(i)] = i
58        assert len(self.cache) == 100
59
60    def test_max_age1(self):
61        """Set expiration after setting a cache item."""
62        self.cache["/path1"] = "test1"
63        # Expire after one second
64        self.cache.max_age = 1
65        time.sleep(0.5)
66        # Should still be present
67        assert self.cache["/path1"] == "test1"
68        time.sleep(0.6)
69        # Should have expired (_setting_ the cache counts)
70        with pytest.raises(ftputil.error.CacheMissError):
71            self.cache.__getitem__("/path1")
72
73    def test_max_age2(self):
74        """Set expiration before setting a cache item."""
75        # Expire after one second
76        self.cache.max_age = 1
77        self.cache["/path1"] = "test1"
78        time.sleep(0.5)
79        # Should still be present
80        assert self.cache["/path1"] == "test1"
81        time.sleep(0.6)
82        # Should have expired (_setting_ the cache counts)
83        with pytest.raises(ftputil.error.CacheMissError):
84            self.cache.__getitem__("/path1")
85
86    def test_disabled(self):
87        self.cache["/path1"] = "test1"
88        self.cache.disable()
89        self.cache["/path2"] = "test2"
90        with pytest.raises(ftputil.error.CacheMissError):
91            self.cache.__getitem__("/path1")
92        with pytest.raises(ftputil.error.CacheMissError):
93            self.cache.__getitem__("/path2")
94        assert len(self.cache) == 1
95        # Don't raise a `CacheMissError` for missing paths.
96        self.cache.invalidate("/path2")
97
98    def test_cache_size_zero(self):
99        host = test_base.ftp_host_factory()
100        with pytest.raises(ValueError):
101            host.stat_cache.resize(0)
102        # If bug #38 was present, this raised an `IndexError`.
103        items = host.listdir(host.curdir)
104        assert items[:3] == ["chemeng", "download", "image"]
Note: See TracBrowser for help on using the repository browser.