-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_context.py
100 lines (74 loc) · 2.35 KB
/
test_context.py
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
from itertools import repeat
import pytest
from django.conf import settings
from django.test import RequestFactory
from django.utils.functional import cached_property
from django.views.generic import TemplateView
import django_context_decorator
context = django_context_decorator.context
settings.configure()
class ViewMixin:
@context
def mixin(self):
return "mixin"
@context
@property
def property_mixin(self):
return "mixin property"
@context
@cached_property
def cached_property_mixin(self):
return "mixin cached property"
class View(ViewMixin, TemplateView):
template_name = ".html"
def __init__(self, request, **kwargs):
self.request = request
super().__init__(**kwargs)
@context
def data(self):
return "data"
@context
@property
def property_data(self):
return "data property"
@context
@cached_property
def cached_property_data(self):
return "data cached property"
class OtherView(ViewMixin, TemplateView):
template_name = ".html"
def __init__(self, request, **kwargs):
self.request = request
super().__init__(**kwargs)
@context
def other_data(self):
return "data"
DATA = {
"data": "data",
"property_data": "data property",
"cached_property_data": "data cached property",
"mixin": "mixin",
"property_mixin": "mixin property",
"cached_property_mixin": "mixin cached property",
}
@pytest.mark.parametrize("key,value", ((key, value) for key, value in DATA.items()))
def test_context_decorator_view_attributes(key, value):
view = View(RequestFactory().get(""))
for _ in repeat(None, 2):
attr = getattr(view, key)
if callable(attr):
assert attr() == value
else:
assert attr == value
@pytest.mark.parametrize("key,value", ((key, value) for key, value in DATA.items()))
def test_context_decorator_context_content(key, value):
view = View(RequestFactory().get(""))
for _ in repeat(None, 2):
assert view.get_context_data()[key] == value
def test_view_inheritance():
view = OtherView(RequestFactory().get(""))
context = view.get_context_data()
assert "other_data" in context
assert "data" not in context
assert "property_data" not in context
assert "cached_property_data" not in context