Projet

Général

Profil

0001-requests_wrapper-mind-query-params-when-caching-4917.patch

Valentin Deniaud, 08 décembre 2020 14:25

Télécharger (2,49 ko)

Voir les différences:

Subject: [PATCH 1/3] requests_wrapper: mind query params when caching (#49175)

 combo/utils/requests_wrapper.py |  3 ++-
 tests/test_requests.py          | 10 ++++++++--
 2 files changed, 10 insertions(+), 3 deletions(-)
combo/utils/requests_wrapper.py
117 117

  
118 118
        if method == 'GET' and cache_duration:
119 119
            # handle cache
120
            cache_key = hashlib.md5(smart_bytes(url)).hexdigest()
120
            params = urlencode(kwargs.get('params', {}))
121
            cache_key = hashlib.md5(smart_bytes(url + params)).hexdigest()
121 122
            cache_content = cache.get(cache_key)
122 123
            if cache_content and not invalidate_cache:
123 124
                response = Response()
tests/test_requests.py
130 130
        # now there's something in cache
131 131
        assert requests.get('http://cache.example.org/').content == b'hello world'
132 132
        assert requests_get.call_count == 1
133
        # passing parameters triggers new request
134
        assert requests.get('http://cache.example.org/', params={'test': 'test'}).content == b'hello world'
135
        assert requests_get.call_count == 2
136
        # if parameters are the same, cache is used
137
        assert requests.get('http://cache.example.org/', params={'test': 'test'}).content == b'hello world'
138
        assert requests_get.call_count == 2
133 139
        # value changed
134 140
        requests_get.return_value = mock.Mock(content=b'hello second world', status_code=200)
135 141
        assert requests.get('http://cache.example.org/').content == b'hello world'
136
        assert requests_get.call_count == 1
142
        assert requests_get.call_count == 2
137 143
        # force cache invalidation
138 144
        assert requests.get('http://cache.example.org/', invalidate_cache=True).content == b'hello second world'
139
        assert requests_get.call_count == 2
145
        assert requests_get.call_count == 3
140 146
        # check raise_if_not_cached
141 147
        with pytest.raises(NothingInCacheException):
142 148
            requests.get('http://cache.example.org/other', raise_if_not_cached=True)
143
-