forked from nephila/django-meta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_metadata_mixin.py
288 lines (222 loc) · 9.24 KB
/
test_metadata_mixin.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
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
283
284
285
286
287
288
import warnings
from copy import copy
from django.test import TestCase
from meta import settings
from meta.views import Meta, MetadataMixin
class MetadataMixinTestCase(TestCase):
def test_get_meta_class(self):
m = MetadataMixin()
self.assertEqual(m.get_meta_class(), Meta)
def test_get_meta_title(self):
m = MetadataMixin()
self.assertEqual(m.get_meta_title(), None)
m.title = "Foo"
self.assertEqual(m.get_meta_title(), "Foo")
def test_get_meta_og_title(self):
m = MetadataMixin()
self.assertEqual(m.get_meta_og_title(), None)
m.og_title = "Foo"
self.assertEqual(m.get_meta_og_title(), "Foo")
def test_get_meta_twitter_title(self):
m = MetadataMixin()
self.assertEqual(m.get_meta_twitter_title(), None)
m.twitter_title = "Foo"
self.assertEqual(m.get_meta_twitter_title(), "Foo")
def test_get_meta_schemaorg_title(self):
m = MetadataMixin()
self.assertEqual(m.get_meta_schemaorg_title(), None)
m.schemaorg_title = "Foo"
self.assertEqual(m.get_meta_schemaorg_title(), "Foo")
def test_get_meta_description(self):
m = MetadataMixin()
self.assertEqual(m.get_meta_description(), None)
m.description = "Foo"
self.assertEqual(m.get_meta_description(), "Foo")
def test_get_meta_url(self):
m = MetadataMixin()
self.assertEqual(m.get_meta_url(), None)
m.url = "/foo/bar"
self.assertEqual(m.get_meta_url(), "/foo/bar")
def test_get_meta_image(self):
m = MetadataMixin()
self.assertEqual(m.get_meta_image(), None)
m.image = "img/foo.gif"
self.assertEqual(m.get_meta_image(), "img/foo.gif")
def test_get_meta_image_object(self):
m = MetadataMixin()
self.assertEqual(m.get_meta_image(), None)
media = {
"url": "http://meta.example.com/image.gif",
"secure_url": "https://meta.example.com/custom.gif",
"type": "some/mime",
"width": 100,
"height": 100,
"alt": "a media",
}
m.image_object = media
self.assertEqual(m.get_meta_image_object(), media)
self.assertEqual(m.get_meta_image(), media["url"])
def test_get_meta_object_tye(self):
m = MetadataMixin()
self.assertEqual(m.get_meta_object_type(), None)
m.object_type = "bar"
self.assertEqual(m.get_meta_object_type(), "bar")
def test_get_domain(self):
domain = settings.SITE_DOMAIN
settings.SITE_DOMAIN = "somedomain.com"
m = MetadataMixin()
self.assertEqual(m.get_domain(), "somedomain.com")
settings.SITE_DOMAIN = domain
def test_get_domain_http(self):
protocol = settings.SITE_PROTOCOL
settings.SITE_PROTOCOL = "http"
m = MetadataMixin()
self.assertEqual(m.get_protocol(), "http")
settings.SITE_PROTOCOL = protocol
def test_get_meta_object_type_with_setting(self):
m = MetadataMixin()
settings.SITE_TYPE = "foo"
self.assertEqual(m.get_meta_object_type(), "foo")
def test_get_meta_site_name(self):
m = MetadataMixin()
self.assertEqual(m.get_meta_site_name(), None)
m.site_name = "Foo"
self.assertEqual(m.get_meta_site_name(), "Foo")
def test_get_meta_site_name_with_setting(self):
m = MetadataMixin()
settings.SITE_NAME = "Foo"
self.assertEqual(m.get_meta_site_name(), "Foo")
def test_get_meta_extra_props(self):
m = MetadataMixin()
self.assertEqual(m.get_meta_extra_props(), None)
m.extra_props = {"app_name": "Foo", "app_id": "Bar"}
self.assertEqual(m.get_meta_extra_props(), {"app_name": "Foo", "app_id": "Bar"})
def test_get_meta_extra_custom_props(self):
m = MetadataMixin()
self.assertEqual(m.get_meta_extra_custom_props(), None)
m.extra_custom_props = [
("property", "app_name", "Foo"),
("property", "app_id", "Bar"),
]
self.assertEqual(
m.get_meta_extra_custom_props(), [("property", "app_name", "Foo"), ("property", "app_id", "Bar")]
)
def test_get_meta_custom_namespace(self):
m = MetadataMixin()
self.assertEqual(m.get_meta_custom_namespace(), None)
m.custom_namespace = "my-website"
self.assertEqual(m.get_meta_custom_namespace(), "my-website")
settings.OG_NAMESPACES = ["foo", "bar"]
m = MetadataMixin()
self.assertEqual(m.get_meta_custom_namespace(), ["foo", "bar"])
settings.OG_NAMESPACES = None
def test_get_meta_twitter_site(self):
m = MetadataMixin()
self.assertEqual(m.get_meta_twitter_site(), None)
m.twitter_site = "@foo"
self.assertEqual(m.get_meta_twitter_site(), "@foo")
def test_get_meta_twitter_creator(self):
m = MetadataMixin()
self.assertEqual(m.get_meta_twitter_creator(), None)
m.twitter_creator = "@foo"
self.assertEqual(m.get_meta_twitter_creator(), "@foo")
def test_get_meta_twitter_card(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
m = MetadataMixin()
self.assertEqual(m.get_meta_twitter_card(), None)
assert len(w) == 1
assert issubclass(w[-1].category, PendingDeprecationWarning)
m.twitter_card = "summary"
assert len(w) == 2
assert issubclass(w[-1].category, PendingDeprecationWarning)
self.assertEqual(m.twitter_card, "summary")
assert len(w) == 3
assert issubclass(w[-1].category, PendingDeprecationWarning)
self.assertEqual(m.get_meta_twitter_card(), "summary")
assert len(w) == 4
assert issubclass(w[-1].category, PendingDeprecationWarning)
def test_get_meta_twitter_type(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
m = MetadataMixin()
self.assertEqual(m.get_meta_twitter_type(), None)
assert len(w) == 0
m.twitter_type = "summary"
self.assertEqual(m.get_meta_twitter_type(), "summary")
assert len(w) == 0
def test_get_meta_facebook_app_id(self):
m = MetadataMixin()
self.assertEqual(m.get_meta_facebook_app_id(), None)
m.facebook_app_id = "12345"
self.assertEqual(m.get_meta_facebook_app_id(), "12345")
def test_get_meta_locale(self):
m = MetadataMixin()
self.assertEqual(m.get_meta_locale(), None)
m.locale = "en_US"
self.assertEqual(m.get_meta_locale(), "en_US")
def test_get_meta(self):
settings.SITE_PROTOCOL = "http"
settings.SITE_DOMAIN = "foo.com"
settings.USE_SITES = False
settings.FB_PAGES = "fbpages"
settings.FB_APPID = "appid"
media = {
"url": "images/foo.gif",
"type": "some/mime",
"width": 100,
"height": 100,
"alt": "a media",
}
full_url_media = copy(media)
full_url_media["url"] = "http://foo.com/static/images/foo.gif"
m = MetadataMixin()
m.title = "title"
m.description = "description"
m.keywords = ["foo", "bar"]
m.url = "some/path"
m.image_object = media
meta_object = m.get_meta()
self.assertTrue(type(meta_object), Meta)
self.assertEqual(meta_object.title, "title")
self.assertEqual(meta_object.description, "description")
self.assertEqual(meta_object.url, "http://foo.com/some/path")
self.assertEqual(meta_object.keywords, ["foo", "bar"])
self.assertEqual(meta_object.image, "http://foo.com/static/images/foo.gif")
self.assertEqual(meta_object.image_object, media)
settings.SITE_DOMAIN = "example.com"
def test_get_context(self):
media = {
"url": "images/foo.gif",
"type": "some/mime",
"width": 100,
"height": 100,
"alt": "a media",
}
full_url_media = copy(media)
full_url_media["url"] = "https://foo.com/static/images/foo.gif"
full_url_media["secure_url"] = "https://foo.com/static/images/foo.gif"
class Super:
def get_context_data(self):
return {}
class View(MetadataMixin, Super):
title = "title"
description = "description"
keywords = ["foo", "bar"]
url = "some/path"
image_object = media
settings.SITE_PROTOCOL = "https"
settings.SITE_DOMAIN = "foo.com"
settings.USE_SITES = False
v = View()
context = v.get_context_data()
self.assertTrue("meta" in context)
self.assertTrue(type(context["meta"]), Meta)
self.assertEqual(context["meta"].url, "https://foo.com/some/path")
self.assertEqual(context["meta"].keywords, ["foo", "bar"])
self.assertEqual(context["meta"].image, "https://foo.com/static/images/foo.gif")
self.assertEqual(context["meta"].image_object, full_url_media)
settings.SITE_PROTOCOL = "http"
settings.SITE_DOMAIN = "example.com"
settings.FB_PAGES = ""
settings.FB_APPID = ""