|
| 1 | +# ================================================================= |
| 2 | +# |
| 3 | +# Authors: Benjamin Webb <bwebb@lincolninst.edu> |
| 4 | +# |
| 5 | +# Copyright (c) 2023 Benjamin Webb |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person |
| 8 | +# obtaining a copy of this software and associated documentation |
| 9 | +# files (the "Software"), to deal in the Software without |
| 10 | +# restriction, including without limitation the rights to use, |
| 11 | +# copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +# copies of the Software, and to permit persons to whom the |
| 13 | +# Software is furnished to do so, subject to the following |
| 14 | +# conditions: |
| 15 | +# |
| 16 | +# The above copyright notice and this permission notice shall be |
| 17 | +# included in all copies or substantial portions of the Software. |
| 18 | +# |
| 19 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 21 | +# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 22 | +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 23 | +# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 24 | +# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 25 | +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 26 | +# OTHER DEALINGS IN THE SOFTWARE. |
| 27 | +# |
| 28 | +# ================================================================= |
| 29 | + |
| 30 | +from datetime import datetime |
| 31 | +from pathlib import Path |
| 32 | +import xml.etree.ElementTree as ET |
| 33 | + |
| 34 | +from sitemap_generator.handler import Handler, SITEMAP_DIR |
| 35 | +from sitemap_generator.util import walk_path, url_join |
| 36 | + |
| 37 | +THIS_DIR = Path(__file__).parent.resolve() |
| 38 | +NAMESPACE = THIS_DIR / 'data' / 'namespaces' |
| 39 | + |
| 40 | +URI_STEM = 'https://geoconnex.us' |
| 41 | +HANDLER = Handler(str(NAMESPACE), URI_STEM) |
| 42 | + |
| 43 | + |
| 44 | +def test_handler(): |
| 45 | + HANDLER.handle() |
| 46 | + glob = list(walk_path(SITEMAP_DIR, r'.*')) |
| 47 | + glob1 = list(walk_path(SITEMAP_DIR, r'.*xml')) |
| 48 | + assert len(glob) == len(glob1) |
| 49 | + |
| 50 | + |
| 51 | +def test_sitemapindex(): |
| 52 | + [sitemapindex] = list(walk_path(SITEMAP_DIR, r'.*_sitemap.xml')) |
| 53 | + assert sitemapindex.name == '_sitemap.xml' |
| 54 | + assert HANDLER._get_rel_path(sitemapindex) == '.' |
| 55 | + |
| 56 | + _ = HANDLER._get_filetime(sitemapindex) |
| 57 | + file_time = datetime.strptime(_, '%Y-%m-%dT%H:%M:%SZ') |
| 58 | + today = datetime.utcnow().strftime('%Y-%m-%d') |
| 59 | + assert file_time.strftime('%Y-%m-%d') == today |
| 60 | + |
| 61 | + tree = ET.parse(sitemapindex) |
| 62 | + root = tree.getroot() |
| 63 | + |
| 64 | + assert all(child.tag == 'sitemap' for child in root) |
| 65 | + assert all(URI_STEM in child.find('loc').text for child in root) |
| 66 | + |
| 67 | + links = root.find('sitemap') |
| 68 | + _ = links.find('lastmod').text |
| 69 | + lastmod = datetime.strptime(_, '%Y-%m-%dT%H:%M:%SZ') |
| 70 | + assert lastmod.strftime('%Y-%m-%d') != today |
| 71 | + |
| 72 | + |
| 73 | +def test_urlset(): |
| 74 | + [urlset] = list(walk_path(SITEMAP_DIR, r'.*links__0.xml')) |
| 75 | + assert urlset.name == 'links__0.xml' |
| 76 | + assert HANDLER._get_rel_path(urlset) == 'iow' |
| 77 | + |
| 78 | + _ = HANDLER._get_filetime(urlset) |
| 79 | + file_time = datetime.strptime(_, '%Y-%m-%dT%H:%M:%SZ') |
| 80 | + today = datetime.utcnow().strftime('%Y-%m-%d') |
| 81 | + assert file_time.strftime('%Y-%m-%d') != today |
| 82 | + |
| 83 | + namespace = url_join(URI_STEM, HANDLER._get_rel_path(urlset)) |
| 84 | + assert namespace == 'https://geoconnex.us/iow' |
| 85 | + |
| 86 | + [urlset] = list(walk_path(SITEMAP_DIR, r'.*autotest1__0.xml')) |
| 87 | + file_time = HANDLER._get_filetime(urlset) |
| 88 | + tree = ET.parse(urlset) |
| 89 | + root = tree.getroot() |
| 90 | + |
| 91 | + assert all(child.tag == 'url' for child in root) |
| 92 | + for child in root: |
| 93 | + assert file_time == child.find('lastmod').text |
| 94 | + print(child.find('lastmod').text, child.find('loc').text) |
| 95 | + assert all(file_time == child.find('lastmod').text for child in root) |
| 96 | + |
| 97 | + url = root.find('url') |
| 98 | + lastmod = url.find('lastmod').text |
| 99 | + assert lastmod == '2023-06-23T22:38:13Z' |
0 commit comments