-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathtest_sitemap.py
More file actions
513 lines (455 loc) ยท 14.8 KB
/
Copy pathtest_sitemap.py
File metadata and controls
513 lines (455 loc) ยท 14.8 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
"""Unit tests for the sitemap plugin."""
import datetime
from unittest.mock import MagicMock, patch
import reflex as rx
from reflex.app import UnevaluatedPage
from reflex.plugins.sitemap import SitemapLink, generate_links_for_sitemap, generate_xml
def test_generate_xml_empty_links():
"""Test generate_xml with an empty list of links."""
xml_output = generate_xml([])
expected = """<?xml version='1.0' encoding='utf-8'?>
<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9" />"""
assert xml_output == expected
def test_generate_xml_single_link_loc_only():
"""Test generate_xml with a single link having only loc."""
links: list[SitemapLink] = [{"loc": "https://example.com"}]
xml_output = generate_xml(links)
expected = """<?xml version='1.0' encoding='utf-8'?>
<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://example.com</loc>
</url>
</urlset>"""
assert xml_output == expected
def test_generate_xml_multiple_links_all_fields():
"""Test generate_xml with multiple links having all fields."""
now = datetime.datetime(2023, 6, 13, 12, 0, 0)
links: list[SitemapLink] = [
{
"loc": "https://example.com/page1",
"lastmod": now,
"changefreq": "daily",
"priority": 0.8,
},
{
"loc": "https://example.com/page2",
"lastmod": datetime.datetime(2023, 1, 1, 0, 0, 0),
"changefreq": "weekly",
"priority": 0.5,
},
]
xml_output = generate_xml(links)
expected = """<?xml version='1.0' encoding='utf-8'?>
<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://example.com/page1</loc>
<changefreq>daily</changefreq>
<lastmod>2023-06-13T12:00:00</lastmod>
<priority>0.8</priority>
</url>
<url>
<loc>https://example.com/page2</loc>
<changefreq>weekly</changefreq>
<lastmod>2023-01-01T00:00:00</lastmod>
<priority>0.5</priority>
</url>
</urlset>"""
assert xml_output == expected
@patch("reflex.config.get_config")
@patch("reflex.utils.console.warn")
def test_generate_links_for_sitemap_static_routes(
mock_warn: MagicMock, mock_get_config: MagicMock
):
"""Test generate_links_for_sitemap with static routes.
Args:
mock_warn: Mock for the console.warn function.
mock_get_config: Mock for the get_config function.
"""
mock_get_config.return_value.deploy_url = "https://example.com"
def mock_component():
return rx.text("Test")
pages = [
UnevaluatedPage(
component=mock_component,
route="index",
title=None,
description=None,
image="favicon.ico",
on_load=None,
meta=[],
context={},
),
UnevaluatedPage(
component=mock_component,
route="about",
title=None,
description=None,
image="favicon.ico",
on_load=None,
meta=[],
context={},
),
UnevaluatedPage(
component=mock_component,
route="contact",
title=None,
description=None,
image="favicon.ico",
on_load=None,
meta=[],
context={"sitemap": {"priority": 0.7, "changefreq": "monthly"}},
),
]
links = generate_links_for_sitemap(pages)
assert len(links) == 3
assert {"loc": "https://example.com/"} in links
assert {"loc": "https://example.com/about"} in links
assert {
"loc": "https://example.com/contact",
"priority": 0.7,
"changefreq": "monthly",
} in links
mock_warn.assert_not_called()
@patch("reflex.config.get_config")
@patch("reflex.utils.console.warn")
def test_generate_links_for_sitemap_dynamic_routes(
mock_warn: MagicMock, mock_get_config: MagicMock
):
"""Test generate_links_for_sitemap with dynamic routes.
Args:
mock_warn: Mock for the console.warn function.
mock_get_config: Mock for the get_config function.
"""
mock_get_config.return_value.deploy_url = "https://sub.example.org"
now = datetime.datetime(2023, 6, 13, 12, 0, 0)
def mock_component():
return rx.text("Test")
pages = [
UnevaluatedPage(
component=mock_component,
route="blog/[id]",
title=None,
description=None,
image="favicon.ico",
on_load=None,
meta=[],
context={
"sitemap": {
"loc": "/custom-blog-path",
"lastmod": now,
"priority": 0.9,
}
},
),
UnevaluatedPage(
component=mock_component,
route="products/[name]",
title=None,
description=None,
image="favicon.ico",
on_load=None,
meta=[],
context={},
), # No sitemap config
UnevaluatedPage(
component=mock_component,
route="user/[user_id]/profile",
title=None,
description=None,
image="favicon.ico",
on_load=None,
meta=[],
context={"sitemap": {"changefreq": "yearly"}},
), # Has sitemap config but no loc
]
links = generate_links_for_sitemap(pages)
assert len(links) == 1
expected_link = {
"loc": "https://sub.example.org/custom-blog-path",
"lastmod": now,
"priority": 0.9,
}
assert expected_link in links
assert mock_warn.call_count == 1
mock_warn.assert_any_call(
"Dynamic route 'user/[user_id]/profile' does not have a 'loc' in sitemap configuration. Skipping."
)
@patch("reflex.config.get_config")
@patch("reflex.utils.console.warn")
def test_generate_links_for_sitemap_404_route(
mock_warn: MagicMock, mock_get_config: MagicMock
):
"""Test generate_links_for_sitemap with the 404 route.
Args:
mock_warn: Mock for the console.warn function.
mock_get_config: Mock for the get_config function.
"""
mock_get_config.return_value.deploy_url = None # No deploy URL
def mock_component():
return rx.text("404")
pages = [
UnevaluatedPage(
component=mock_component,
route="404",
title=None,
description=None,
image="favicon.ico",
on_load=None,
meta=[],
context={"sitemap": {"loc": "/custom-404", "priority": 0.1}},
),
UnevaluatedPage(
component=mock_component,
route="404",
title=None,
description=None,
image="favicon.ico",
on_load=None,
meta=[],
context={"sitemap": {"priority": 0.2}},
), # Has sitemap config but no loc
]
links = generate_links_for_sitemap(pages)
assert len(links) == 1
assert {"loc": "/custom-404", "priority": 0.1} in links
mock_warn.assert_called_once_with(
"Route 404 '404' does not have a 'loc' in sitemap configuration. Skipping."
)
@patch("reflex.config.get_config")
def test_generate_links_for_sitemap_opt_out(mock_get_config: MagicMock):
"""Test generate_links_for_sitemap with sitemap set to None.
Args:
mock_get_config: Mock for the get_config function.
"""
mock_get_config.return_value.deploy_url = None # No deploy URL
def mock_component():
return rx.text("Unlisted")
pages = [
UnevaluatedPage(
component=mock_component,
route="unlisted",
title=None,
description=None,
image="favicon.ico",
on_load=None,
meta=[],
context={"sitemap": None},
),
UnevaluatedPage(
component=mock_component,
route="listed",
title=None,
description=None,
image="favicon.ico",
on_load=None,
meta=[],
context={},
),
]
links = generate_links_for_sitemap(pages)
assert len(links) == 1
assert {"loc": "/listed"} in links
@patch("reflex.config.get_config")
def test_generate_links_for_sitemap_loc_override(mock_get_config: MagicMock):
"""Test generate_links_for_sitemap with loc override in sitemap config.
Args:
mock_get_config: Mock for the get_config function.
"""
mock_get_config.return_value.deploy_url = "http://localhost:3000"
def mock_component():
return rx.text("Test")
pages = [
UnevaluatedPage(
component=mock_component,
route="features",
title=None,
description=None,
image="favicon.ico",
on_load=None,
meta=[],
context={"sitemap": {"loc": "https://override.com/features_page"}},
),
UnevaluatedPage(
component=mock_component,
route="pricing",
title=None,
description=None,
image="favicon.ico",
on_load=None,
meta=[],
context={"sitemap": {"loc": "/custom_pricing"}},
),
]
links = generate_links_for_sitemap(pages)
assert len(links) == 2
assert {"loc": "https://override.com/features_page"} in links
assert {"loc": "http://localhost:3000/custom_pricing"} in links
@patch("reflex.config.get_config")
def test_generate_links_for_sitemap_priority_clamping(mock_get_config: MagicMock):
"""Test that priority is clamped between 0.0 and 1.0.
Args:
mock_get_config: Mock for the get_config function.
"""
mock_get_config.return_value.deploy_url = "https://example.com"
def mock_component():
return rx.text("Test")
pages = [
UnevaluatedPage(
component=mock_component,
route="high_prio",
title=None,
description=None,
image="favicon.ico",
on_load=None,
meta=[],
context={"sitemap": {"priority": 1.5}},
),
UnevaluatedPage(
component=mock_component,
route="low_prio",
title=None,
description=None,
image="favicon.ico",
on_load=None,
meta=[],
context={"sitemap": {"priority": -0.5}},
),
UnevaluatedPage(
component=mock_component,
route="valid_prio",
title=None,
description=None,
image="favicon.ico",
on_load=None,
meta=[],
context={"sitemap": {"priority": 0.5}},
),
]
links = generate_links_for_sitemap(pages)
expected_links = [
{"loc": "https://example.com/high_prio", "priority": 1.0},
{"loc": "https://example.com/low_prio", "priority": 0.0},
{"loc": "https://example.com/valid_prio", "priority": 0.5},
]
for expected_link in expected_links:
assert expected_link in links
@patch("reflex.config.get_config")
def test_generate_links_for_sitemap_no_deploy_url(mock_get_config: MagicMock):
"""Test generate_links_for_sitemap when deploy_url is not set.
Args:
mock_get_config: Mock for the get_config function.
"""
mock_get_config.return_value.deploy_url = None
def mock_component():
return rx.text("Test")
pages = [
UnevaluatedPage(
component=mock_component,
route="home",
title=None,
description=None,
image="favicon.ico",
on_load=None,
meta=[],
context={"sitemap": {"loc": "/home"}},
),
UnevaluatedPage(
component=mock_component,
route="about",
title=None,
description=None,
image="favicon.ico",
on_load=None,
meta=[],
context={},
), # No loc, should use route
UnevaluatedPage(
component=mock_component,
route="index",
title=None,
description=None,
image="favicon.ico",
on_load=None,
meta=[],
context={},
), # Special case for index
]
links = generate_links_for_sitemap(pages)
assert len(links) == 3
expected_links = [{"loc": "/home"}, {"loc": "/about"}, {"loc": "/"}]
for expected_link in expected_links:
assert expected_link in links
@patch("reflex.config.get_config")
def test_generate_links_for_sitemap_deploy_url_trailing_slash(
mock_get_config: MagicMock,
):
"""Test generate_links_for_sitemap with deploy_url having a trailing slash.
Args:
mock_get_config: Mock for the get_config function.
"""
mock_get_config.return_value.deploy_url = "https://example.com/"
def mock_component():
return rx.text("Test")
pages = [
UnevaluatedPage(
component=mock_component,
route="testpage",
title=None,
description=None,
image="favicon.ico",
on_load=None,
meta=[],
context={},
),
]
links = generate_links_for_sitemap(pages)
assert len(links) == 1
assert {"loc": "https://example.com/testpage"} in links
@patch("reflex.config.get_config")
def test_generate_links_for_sitemap_loc_leading_slash(mock_get_config: MagicMock):
"""Test generate_links_for_sitemap with loc having a leading slash.
Args:
mock_get_config: Mock for the get_config function.
"""
mock_get_config.return_value.deploy_url = "https://example.com"
def mock_component():
return rx.text("Test")
pages = [
UnevaluatedPage(
component=mock_component,
route="another",
title=None,
description=None,
image="favicon.ico",
on_load=None,
meta=[],
context={"sitemap": {"loc": "/another"}},
),
]
links = generate_links_for_sitemap(pages)
assert len(links) == 1
assert {"loc": "https://example.com/another"} in links
@patch("reflex.config.get_config")
def test_generate_links_for_sitemap_loc_full_url(mock_get_config: MagicMock):
"""Test generate_links_for_sitemap with loc being a full URL.
Args:
mock_get_config: Mock for the get_config function.
"""
mock_get_config.return_value.deploy_url = "https://example.com"
def mock_component():
return rx.text("Test")
pages = [
UnevaluatedPage(
component=mock_component,
route="external",
title=None,
description=None,
image="favicon.ico",
on_load=None,
meta=[],
context={"sitemap": {"loc": "http://othersite.com/page"}},
),
]
links = generate_links_for_sitemap(pages)
assert len(links) == 1
assert {"loc": "http://othersite.com/page"} in links