-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuilder_tests.py
222 lines (170 loc) · 8.85 KB
/
builder_tests.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
# Generated by CodiumAI
# Dependencies:
# pip install pytest-mock
import pytest
from r2k.ebook.epub_builder import EPUBArticle, EPUB
from r2k.feeds import Article
class TestRenderArticles:
# Tests that the method correctly renders the content for a single article
def test_render_single_article(self, mocker):
# Create a mock parser class
class MockParser:
def __init__(self):
pass
def parse(self, url):
# Return a mock parsed article
return {
"content": "<p>This is the content of the article</p>",
"lead_image_url": None,
}
# Create a mock article
mock_article = EPUBArticle(Article(link="https://example.com/article1", title="Article 1"), "epub_path")
# Mock the _get_parser_class method to return the mock parser class
mocker.patch.object(EPUB, "_get_parser_class", return_value=MockParser)
# Call the render_articles method
self.render_articles()
# Assert that the article's content has been correctly rendered
assert mock_article.content == "<p>This is the content of the article</p>"
# Tests that the method correctly renders the content for multiple articles
def test_render_multiple_articles(self, mocker):
# Create a mock parser class
class MockParser:
def __init__(self):
pass
def parse(self, url):
# Return a mock parsed article
return {
"content": "<p>This is the content of the article</p>",
"lead_image_url": None,
}
# Create mock articles
mock_article1 = EPUBArticle(Article(link="https://example.com/article1", title="Article 1"), "epub_path")
mock_article2 = EPUBArticle(Article(link="https://example.com/article2", title="Article 2"), "epub_path")
# Mock the _get_parser_class method to return the mock parser class
mocker.patch.object(EPUB, "_get_parser_class", return_value=MockParser)
# Call the render_articles method
self.render_articles()
# Assert that the articles' content has been correctly rendered
assert mock_article1.content == "<p>This is the content of the article</p>"
assert mock_article2.content == "<p>This is the content of the article</p>"
# Tests that the method correctly renders the content for an article with no images
def test_render_article_with_no_images(self, mocker):
# Create a mock parser class
class MockParser:
def __init__(self):
pass
def parse(self, url):
# Return a mock parsed article with no lead image URL
return {
"content": "<p>This is the content of the article</p>",
"lead_image_url": None,
}
# Create a mock article
mock_article = EPUBArticle(Article(link="https://example.com/article1", title="Article 1"), "epub_path")
# Mock the _get_parser_class method to return the mock parser class
mocker.patch.object(EPUB, "_get_parser_class", return_value=MockParser)
# Call the render_articles method
self.render_articles()
# Assert that the article's content has been correctly rendered
assert mock_article.content == "<p>This is the content of the article</p>"
# Tests that the method correctly renders the content for an article with images
def test_render_article_with_images(self, mocker):
# Create a mock parser class
class MockParser:
def __init__(self):
pass
def parse(self, url):
# Return a mock parsed article with a lead image URL
return {
"content": "<p>This is the content of the article</p>",
"lead_image_url": "https://example.com/image.jpg",
}
# Create a mock article
mock_article = EPUBArticle(Article(link="https://example.com/article1", title="Article 1"), "epub_path")
# Mock the _get_parser_class method to return the mock parser class
mocker.patch.object(EPUB, "_get_parser_class", return_value=MockParser)
# Mock the download_image method to return a mock image name
mocker.patch.object(EPUBArticle, "download_image", return_value="image.jpg")
# Call the render_articles method
self.render_articles()
# Assert that the article's content has been correctly rendered
assert mock_article.content == '<p><img src="../images/image.jpg"/>This is the content of the article</p>'
# Tests that the method correctly renders the content for an article with an empty author field
def test_render_article_with_empty_author(self, mocker):
# Create a mock parser class
class MockParser:
def __init__(self):
pass
def parse(self, url):
# Return a mock parsed article with an empty author field
return {
"content": "<p>This is the content of the article</p>",
"lead_image_url": None,
"author": "",
}
# Create a mock article
mock_article = EPUBArticle(Article(link="https://example.com/article1", title="Article 1"), "epub_path")
# Mock the _get
# Tests that the method correctly renders the content for an article with an empty date field
def test_render_article_with_empty_date(self, mocker):
# Create a mock parser class
class MockParser:
def __init__(self):
pass
def parse(self, url):
# Return a mock parsed article with empty date field
return {
"content": "<p>This is the content of the article</p>",
"lead_image_url": None,
"date": ""
}
# Create a mock article
mock_article = EPUBArticle(Article(link="https://example.com/article1", title="Article 1"), "epub_path")
# Mock the _get_parser_class method to return the mock parser class
mocker.patch.object(EPUB, "_get_parser_class", return_value=MockParser)
# Call the render_articles method
self.render_articles()
# Assert that the article's content has been correctly rendered
assert mock_article.content == "<p>This is the content of the article</p>"
# Tests that the method correctly renders the content for an article with an empty title field
def test_render_article_with_empty_title(self, mocker):
# Create a mock parser class
class MockParser:
def __init__(self):
pass
def parse(self, url):
# Return a mock parsed article with empty title field
return {
"content": "<p>This is the content of the article</p>",
"lead_image_url": None,
"title": ""
}
# Create a mock article
mock_article = EPUBArticle(Article(link="https://example.com/article1", title="Article 1"), "epub_path")
# Mock the _get_parser_class method to return the mock parser class
mocker.patch.object(EPUB, "_get_parser_class", return_value=MockParser)
# Call the render_articles method
self.render_articles()
# Assert that the article's content has been correctly rendered
assert mock_article.content == "<p>This is the content of the article</p>"
# Tests that the method correctly renders the content for an article with special characters in the title and content fields
def test_render_article_with_special_characters(self, mocker):
# Create a mock parser class
class MockParser:
def __init__(self):
pass
def parse(self, url):
# Return a mock parsed article with special characters in title and content fields
return {
"content": "<p>This is the content of the article with special characters: & < ></p>",
"lead_image_url": None,
"title": "Article with special characters: & < >"
}
# Create a mock article
mock_article = EPUBArticle(Article(link="https://example.com/article1", title="Article 1"), "epub_path")
# Mock the _get_parser_class method to return the mock parser class
mocker.patch.object(EPUB, "_get_parser_class", return_value=MockParser)
# Call the render_articles method
self.render_articles()
# Assert that the article's content has been correctly rendered
assert mock_article.content == "<p>This is the content of the article with special characters: & < ></p>"