-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_extended_meta.py
190 lines (123 loc) · 7.31 KB
/
test_extended_meta.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
import unittest
import extended_meta
from extended_meta import ExtendedMeta
class FakeGenerator(object):
def __init__(self):
self.settings = {
}
self.articles = []
for i in range(10):
article = FakeArticle()
article.meta_description = 'should-be-description'
self.articles.append(article)
class FakeArticle(object):
def __init__(self):
self.title = 'should-be-title'
self.summary = 'should-be-summary'
self.content = """<p>Lorem ipsum</p>
<p><span><img src="test.jpg"></span></p>
<p>Dolor sit amet</p>"""
self.tags = [FakeTag('tag1'), FakeTag('tag2'), FakeTag('tag3')]
self.url = 'should-be-the-article-url.html'
class FakeTag(object):
def __init__(self, slug):
self.slug = slug
class ExtendedMetaTestCase(unittest.TestCase):
def setUp(self):
ExtendedMeta.settings['SITEURL'] = 'http://localhost'
ExtendedMeta.settings['DEFAULT_OG_IMAGE'] = 'should-be-default-og-image.jpg'
self.article = FakeArticle()
class ExtendedMetaFilledMetaTestCase(ExtendedMetaTestCase):
def test_should_create_meta_attribute_on_article_when_it_has_description(self):
self.article.meta_description = 'should-be-meta-description'
ExtendedMeta.create_meta_attribute(self.article)
self.assertEqual(self.article.meta['description'], 'should-be-meta-description')
def test_should_create_meta_attribute_on_article_when_it_has_keywords(self):
self.article.meta_keywords = 'should-be-meta-keywords'
ExtendedMeta.create_meta_attribute(self.article)
self.assertEqual(self.article.meta['keywords'], 'should-be-meta-keywords')
def test_should_create_meta_robots_on_article_when_it_has_robots(self):
self.article.meta_robots = 'index,follow'
ExtendedMeta.create_meta_attribute(self.article)
self.assertEqual(self.article.meta['robots'], 'index,follow')
def test_should_create_meta_canonical(self):
ExtendedMeta.create_meta_attribute(self.article)
self.assertEqual(self.article.meta['canonical'],
'http://localhost/should-be-the-article-url.html')
def test_should_create_meta_ogtitle_on_article_when_it_has_og_title(self):
self.article.meta_og_title = 'should-be-meta-og-title'
ExtendedMeta.create_meta_attribute(self.article)
self.assertEqual(self.article.meta['og_title'], 'should-be-meta-og-title')
def test_should_create_meta_ogdescription_on_article_when_it_has_og_description(self):
self.article.meta_og_description = 'should-be-meta-og-description'
ExtendedMeta.create_meta_attribute(self.article)
self.assertEqual(self.article.meta['og_description'], 'should-be-meta-og-description')
def test_should_create_meta_ogurl_on_article_when_it_has_og_url(self):
self.article.meta_og_url = 'http://mycustomdomain/should-be-article-url.html'
ExtendedMeta.create_meta_attribute(self.article)
self.assertEqual(self.article.meta['og_url'],
'http://mycustomdomain/should-be-article-url.html')
def test_should_create_meta_ogimage_on_article_when_it_has_og_image(self):
self.article.meta_og_image = 'http://mycustomdomain/og.jpg'
ExtendedMeta.create_meta_attribute(self.article)
self.assertEqual(self.article.meta['og_image'],
'http://mycustomdomain/og.jpg')
class ExtendedMetaUnfilledMetaTestCase(ExtendedMetaTestCase):
def setUp(self):
super(ExtendedMetaUnfilledMetaTestCase, self).setUp()
ExtendedMeta.create_meta_attribute(self.article)
def test_should_use_the_summary_as_description_when_it_hasnt_meta_description(self):
self.assertEqual(self.article.meta['description'], 'should-be-summary')
def test_should_use_tags_as_keywords_when_it_hasnt_meta_keywords(self):
self.assertEqual(self.article.meta['keywords'], 'tag1, tag2, tag3')
def test_should_use_index_follow_as_default_robots_data(self):
self.assertEqual(self.article.meta['robots'], 'index,follow')
def test_should_format_the_summary_to_use_as_description(self):
self.article.summary = '<p>This is a "test"! <strong>Just a test!</strong></p>'
ExtendedMeta.create_meta_attribute(self.article)
self.assertEqual(self.article.meta['description'], 'This is a "test"! Just a test!')
def test_should_limit_the_summary_content_when_using_it_as_description(self):
self.article.summary = """<p>Do you see any "Teletubbies" in here? Do you
see a slender plastic tag clipped to my shirt with my name printed on
it? Do you see a little Asian child with a blank expression on his
face sitting outside on a mechanical helicopter that shakes when you
put quarters in it? No? Well, that's what you see at a toy store.
And you must think you're in a toy store, because you're here shopping
for an infant named Jeb.<p>"""
ExtendedMeta.create_meta_attribute(self.article)
self.assertEqual(self.article.meta['description'],
'Do you see any "Teletubbies" in here? Do you see a slender plastic tag clipped to my shirt with my name printed on it? Do you see a little Asian child with...')
class ExtendedMetaUnfilledOGMetaTestCase(ExtendedMetaTestCase):
def setUp(self):
super(ExtendedMetaUnfilledOGMetaTestCase, self).setUp()
ExtendedMeta.create_meta_attribute(self.article)
def test_should_use_the_article_title_as_ogtitle(self):
self.assertEqual(self.article.meta['og_title'], 'should-be-title')
def test_should_use_the_article_url_as_ogurl(self):
self.assertEqual(self.article.meta['og_url'],
'http://localhost/should-be-the-article-url.html')
def test_should_use_the_article_description_as_ogdescription(self):
self.assertEqual(self.article.meta['og_description'],
'should-be-summary')
def test_should_use_the_article_image_as_ogimage(self):
self.assertEqual(self.article.meta['og_image'], 'http://localhost/test.jpg')
class ExtendedMetaUnfilledOGMetaAlternativeImageTestCase(ExtendedMetaTestCase):
def test_should_use_the_first_image_of_the_article(self):
self.article.content = """<h1>hey!</h1><p><div><img src="http://myhost/1.jpg"></div>
</p><br><img src="2.jpg">"""
ExtendedMeta.create_meta_attribute(self.article)
self.assertEqual(self.article.meta['og_image'], 'http://myhost/1.jpg')
def test_should_use_the_default_image_as_ogimage_when_article_hasnt_one(self):
self.article.content = '<p>Lorem ipsum</p>'
ExtendedMeta.create_meta_attribute(self.article)
self.assertEqual(self.article.meta['og_image'],
'should-be-default-og-image.jpg')
class ExtendedMetaPelicanIntegrationTestCase(unittest.TestCase):
def setUp(self):
self.generator = FakeGenerator()
def test_module_should_have_register_method(self):
self.assertTrue(hasattr(extended_meta, 'register'))
def test_should_create_meta_attribute_with_data_on_articles(self):
ExtendedMeta.add_meta_to_articles(self.generator)
for article in self.generator.articles:
self.assertEqual(article.meta['description'], 'should-be-description')