Skip to content
Myr

test_structures.py

This file

Path

tests/test_structures.py

148 lines · 4.7 KiB · Python

Last changed by

Merge branch 'master' of github.com:kennethreitz/requestsKenneth Reitzbefore adoptionb60c5a5

History of this file

Take it

1# -*- coding: utf-8 -*-23import pytest45from requests.structures import CaseInsensitiveDict, LookupDict, TimedCache, TimedCacheManaged678class TestCaseInsensitiveDict:910    @pytest.fixture(autouse=True)11    def setup(self):12        """CaseInsensitiveDict instance with "Accept" header."""13        self.case_insensitive_dict = CaseInsensitiveDict()14        self.case_insensitive_dict['Accept'] = 'application/json'1516    def test_list(self):17        assert list(self.case_insensitive_dict) == ['Accept']1819    possible_keys = pytest.mark.parametrize('key', ('accept', 'ACCEPT', 'aCcEpT', 'Accept'))2021    @possible_keys22    def test_getitem(self, key):23        assert self.case_insensitive_dict[key] == 'application/json'2425    @possible_keys26    def test_delitem(self, key):27        del self.case_insensitive_dict[key]28        assert key not in self.case_insensitive_dict2930    def test_lower_items(self):31        assert list(self.case_insensitive_dict.lower_items()) == [('accept', 'application/json')]3233    def test_repr(self):34        assert repr(self.case_insensitive_dict) == "{'Accept': 'application/json'}"3536    def test_copy(self):37        copy = self.case_insensitive_dict.copy()38        assert copy is not self.case_insensitive_dict39        assert copy == self.case_insensitive_dict4041    @pytest.mark.parametrize(42        'other, result', (43            ({'AccePT': 'application/json'}, True),44            ({}, False),45            (None, False)46        )47    )48    def test_instance_equality(self, other, result):49        assert (self.case_insensitive_dict == other) is result505152class TestLookupDict:5354    @pytest.fixture(autouse=True)55    def setup(self):56        """LookupDict instance with "bad_gateway" attribute."""57        self.lookup_dict = LookupDict('test')58        self.lookup_dict.bad_gateway = 5025960    def test_repr(self):61        assert repr(self.lookup_dict) == "<lookup 'test'>"6263    get_item_parameters = pytest.mark.parametrize(64        'key, value', (65            ('bad_gateway', 502),66            ('not_a_key', None)67        )68    )6970    @get_item_parameters71    def test_getitem(self, key, value):72        assert self.lookup_dict[key] == value7374    @get_item_parameters75    def test_get(self, key, value):76        assert self.lookup_dict.get(key) == value777879class TestTimedCache(object):80    @pytest.fixture(autouse=True)81    def setup(self):82        self.any_value = 'some value'83        self.expiration_secs = 6084        self.cache = TimedCache(expiration_secs=self.expiration_secs)85        yield86        self.cache.clear()8788    def test_get(self):89        self.cache['a'] = self.any_value90        assert self.cache['a'] is self.any_value9192    def test_repr(self):93        repr = str(self.cache)94        assert repr == '<TimedCache maxlen:32 len:0 expiration_secs:60>'9596    def test_get_expired_item(self, mocker):97        self.cache = TimedCache(maxlen=1, expiration_secs=self.expiration_secs)9899        mocker.patch('requests.structures.current_time', lambda: 0)100        self.cache['a'] = self.any_value101        mocker.patch('requests.structures.current_time', lambda: self.expiration_secs + 1)102        assert self.cache.get('a') is None103104    def test_evict_first_entry_when_full(self, mocker):105        self.cache = TimedCache(maxlen=2, expiration_secs=2)106        mocker.patch('requests.structures.current_time', lambda: 0)107        self.cache['a'] = self.any_value108        mocker.patch('requests.structures.current_time', lambda: 1)109        self.cache['b'] = self.any_value110        mocker.patch('requests.structures.current_time', lambda: 3)111        self.cache['c'] = self.any_value112        assert len(self.cache) is 2113        with pytest.raises(KeyError, message='Expected key not found'):114            self.cache['a']115        assert self.cache['b'] is self.any_value116        assert self.cache['c'] is self.any_value117118    def test_delete_item_removes_item(self):119        self.cache['a'] = self.any_value120        del self.cache['a']121        with pytest.raises(KeyError, message='Expected key not found'):122            self.cache['a']123124    def test_iterating_hides_timestamps(self):125        self.cache['a'] = 1126        self.cache['b'] = 2127        expected = [('a', 1), ('b', 2)]128        actual = [(key, val) for key, val in self.cache]129        assert expected == actual130131132class TestTimedCacheManagedDecorator(object):133    def test_caches_repeated_calls(self, mocker):134        mocker.patch('requests.structures.current_time', lambda: 0)135136        nonlocals = {'value': 0}137138        @TimedCacheManaged139        def some_method(x):140            nonlocals['value'] = nonlocals['value'] + x141            return nonlocals['value']142143        first_result = some_method(1)144        assert first_result is 1145        second_result = some_method(1)146        assert second_result is 1147        third_result = some_method(2)148        assert third_result is 3