summaryrefslogtreecommitdiff
path: root/tests/test_cachecontrol.py
blob: dd7c28346172ef3b30744041ce1b3f4e7423f741 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import pytest


def test_cache_control_object_max_age_None():
    from webob.cachecontrol import CacheControl

    cc = CacheControl({}, "a")
    cc.properties["max-age"] = None
    assert cc.max_age == -1


class TestUpdateDict(object):
    def setup_method(self, method):
        self.call_queue = []

        def callback(args):
            self.call_queue.append("Called with: %s" % repr(args))

        self.callback = callback

    def make_one(self, callback):
        from webob.cachecontrol import UpdateDict

        ud = UpdateDict()
        ud.updated = callback
        return ud

    def test_clear(self):
        newone = self.make_one(self.callback)
        newone["first"] = 1
        assert len(newone) == 1
        newone.clear()
        assert len(newone) == 0

    def test_update(self):
        newone = self.make_one(self.callback)
        d = {"one": 1}
        newone.update(d)
        assert newone == d

    def test_set_delete(self):
        newone = self.make_one(self.callback)
        newone["first"] = 1
        assert len(self.call_queue) == 1
        assert self.call_queue[-1] == "Called with: {'first': 1}"

        del newone["first"]
        assert len(self.call_queue) == 2
        assert self.call_queue[-1] == "Called with: {}"

    def test_setdefault(self):
        newone = self.make_one(self.callback)
        assert newone.setdefault("haters", "gonna-hate") == "gonna-hate"
        assert len(self.call_queue) == 1
        assert self.call_queue[-1] == "Called with: {'haters': 'gonna-hate'}"

        # no effect if failobj is not set
        assert newone.setdefault("haters", "gonna-love") == "gonna-hate"
        assert len(self.call_queue) == 1

    def test_pop(self):
        newone = self.make_one(self.callback)
        newone["first"] = 1
        newone.pop("first")
        assert len(self.call_queue) == 2
        assert self.call_queue[-1] == "Called with: {}", self.call_queue[-1]

    def test_popitem(self):
        newone = self.make_one(self.callback)
        newone["first"] = 1
        assert newone.popitem() == ("first", 1)
        assert len(self.call_queue) == 2
        assert self.call_queue[-1] == "Called with: {}", self.call_queue[-1]


class TestExistProp(object):
    """
    Test webob.cachecontrol.exists_property
    """

    def setUp(self):
        pass

    def make_one(self):
        from webob.cachecontrol import exists_property

        class Dummy(object):
            properties = dict(prop=1)
            type = "dummy"
            prop = exists_property("prop", "dummy")
            badprop = exists_property("badprop", "big_dummy")

        return Dummy

    def test_get_on_class(self):
        from webob.cachecontrol import exists_property

        Dummy = self.make_one()
        assert isinstance(Dummy.prop, exists_property), Dummy.prop

    def test_get_on_instance(self):
        obj = self.make_one()()
        assert obj.prop is True

    def test_type_mismatch_raise(self):
        with pytest.raises(AttributeError):
            obj = self.make_one()()
            obj.badprop = True

    def test_set_w_value(self):
        obj = self.make_one()()
        obj.prop = True
        assert obj.prop is True
        assert obj.properties["prop"] is None

    def test_del_value(self):
        obj = self.make_one()()
        del obj.prop
        assert "prop" not in obj.properties


class TestValueProp(object):
    """
    Test webob.cachecontrol.exists_property
    """

    def setUp(self):
        pass

    def make_one(self):
        from webob.cachecontrol import value_property

        class Dummy(object):
            properties = dict(prop=1)
            type = "dummy"
            prop = value_property("prop", "dummy")
            badprop = value_property("badprop", "big_dummy")

        return Dummy

    def test_get_on_class(self):
        from webob.cachecontrol import value_property

        Dummy = self.make_one()
        assert isinstance(Dummy.prop, value_property), Dummy.prop

    def test_get_on_instance(self):
        dummy = self.make_one()()
        assert dummy.prop, dummy.prop

    def test_set_on_instance(self):
        dummy = self.make_one()()
        dummy.prop = "new"
        assert dummy.prop == "new", dummy.prop
        assert dummy.properties["prop"] == "new", dict(dummy.properties)

    def test_set_on_instance_bad_attribute(self):
        dummy = self.make_one()()
        dummy.prop = "new"
        assert dummy.prop == "new", dummy.prop
        assert dummy.properties["prop"] == "new", dict(dummy.properties)

    def test_set_wrong_type(self):
        from webob.cachecontrol import value_property

        class Dummy(object):
            properties = dict(prop=1, type="fail")
            type = "dummy"
            prop = value_property("prop", "dummy", type="failingtype")

        dummy = Dummy()

        def assign():
            dummy.prop = "foo"

        with pytest.raises(AttributeError):
            assign()

    def test_set_type_true(self):
        dummy = self.make_one()()
        dummy.prop = True
        assert dummy.prop is None

    def test_set_on_instance_w_default(self):
        dummy = self.make_one()()
        dummy.prop = "dummy"
        assert dummy.prop == "dummy"
        # TODO: this probably needs more tests

    def test_del(self):
        dummy = self.make_one()()
        dummy.prop = "Ian Bicking likes to skip"
        del dummy.prop
        assert dummy.prop == "dummy"


def test_copy_cc():
    from webob.cachecontrol import CacheControl

    cc = CacheControl({"header": "%", "msg": "arewerichyet?"}, "request")
    cc2 = cc.copy()
    assert cc.properties is not cc2.properties
    assert cc.type is cc2.type


def test_serialize_cache_control_emptydict():
    from webob.cachecontrol import serialize_cache_control

    result = serialize_cache_control(dict())
    assert result == ""


def test_serialize_cache_control_cache_control_object():
    from webob.cachecontrol import serialize_cache_control, CacheControl

    result = serialize_cache_control(CacheControl({}, "request"))
    assert result == ""


def test_serialize_cache_control_object_with_headers():
    from webob.cachecontrol import serialize_cache_control, CacheControl

    result = serialize_cache_control(CacheControl({"header": "a"}, "request"))
    assert result == "header=a"


def test_serialize_cache_control_value_is_None():
    from webob.cachecontrol import serialize_cache_control, CacheControl

    result = serialize_cache_control(CacheControl({"header": None}, "request"))
    assert result == "header"


def test_serialize_cache_control_value_needs_quote():
    from webob.cachecontrol import serialize_cache_control, CacheControl

    result = serialize_cache_control(CacheControl({"header": '""'}, "request"))
    assert result == 'header=""""'


class TestCacheControl(object):
    def make_one(self, props, typ):
        from webob.cachecontrol import CacheControl

        return CacheControl(props, typ)

    def test_ctor(self):
        cc = self.make_one({"a": 1}, "typ")
        assert cc.properties == {"a": 1}
        assert cc.type == "typ"

    def test_parse(self):
        from webob.cachecontrol import CacheControl

        cc = CacheControl.parse("public, max-age=315360000")
        assert type(cc) == CacheControl
        assert cc.max_age == 315360000
        assert cc.public is True

    def test_parse_updates_to(self):
        from webob.cachecontrol import CacheControl

        def foo(arg):
            return {"a": 1}

        cc = CacheControl.parse("public, max-age=315360000", updates_to=foo)
        assert type(cc) == CacheControl
        assert cc.max_age == 315360000

    def test_parse_valueerror_int(self):
        from webob.cachecontrol import CacheControl

        def foo(arg):
            return {"a": 1}

        cc = CacheControl.parse("public, max-age=abc")
        assert type(cc) == CacheControl
        assert cc.max_age == "abc"

    def test_repr(self):
        cc = self.make_one({"a": "1"}, "typ")
        assert repr(cc) == "<CacheControl 'a=1'>"