Skip to content

Commit

Permalink
tests: add basic tests and fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts committed Jan 12, 2024
1 parent 4a08a8b commit e1468a6
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 26 deletions.
4 changes: 2 additions & 2 deletions mkdocs_rss_plugin/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
MKDOCS_LOGGER_NAME = "[RSS-plugin]"
OUTPUT_RSS_FEED_CREATED = "feed_rss_created.xml"
OUTPUT_RSS_FEED_UPDATED = "feed_rss_updated.xml"
OUTPUT_JSON_FEED_CREATED = "feed_created.json"
OUTPUT_JSON_FEED_UPDATED = "feed_updated.json"
OUTPUT_JSON_FEED_CREATED = "feed_json_created.json"
OUTPUT_JSON_FEED_UPDATED = "feed_json_updated.json"
REMOTE_REQUEST_HEADERS = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"User-Agent": f"{__about__.__title__}/{__about__.__version__}",
Expand Down
12 changes: 12 additions & 0 deletions tests/fixtures/mkdocs_jsonfeed_enabled_not_rss.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
site_name: Test RSS Plugin
site_description: Test RSS Plugin
site_url: https://guts.github.io/mkdocs-rss-plugin

plugins:
- rss:
enabled: true
rss_feed_enabled: false
json_feed_enabled: true

theme:
name: mkdocs
12 changes: 12 additions & 0 deletions tests/fixtures/mkdocs_rss_enabled_not_jsonfeed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
site_name: Test RSS Plugin
site_description: Test RSS Plugin
site_url: https://guts.github.io/mkdocs-rss-plugin

plugins:
- rss:
enabled: true
rss_feed_enabled: true
json_feed_enabled: false

theme:
name: mkdocs
123 changes: 99 additions & 24 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
# 3rd party
import feedparser

# project
from mkdocs_rss_plugin.constants import (
OUTPUT_JSON_FEED_CREATED,
OUTPUT_JSON_FEED_UPDATED,
OUTPUT_RSS_FEED_CREATED,
OUTPUT_RSS_FEED_UPDATED,
)

# test suite
from tests.base import BaseTest

Expand Down Expand Up @@ -100,7 +108,7 @@ def test_simple_build_minimal(self):
self.assertIsNone(cli_result.exception)

# created items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
feed_parsed = feedparser.parse(Path(tmpdirname) / OUTPUT_RSS_FEED_CREATED)
for feed_item in feed_parsed.entries:
# mandatory properties
self.assertTrue("description" in feed_item)
Expand Down Expand Up @@ -134,7 +142,7 @@ def test_simple_build_complete(self):
self.assertIsNone(cli_result.exception)

# created items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
feed_parsed = feedparser.parse(Path(tmpdirname) / OUTPUT_RSS_FEED_CREATED)
for feed_item in feed_parsed.entries:
# mandatory properties
self.assertTrue("description" in feed_item)
Expand Down Expand Up @@ -172,6 +180,73 @@ def test_simple_build_disabled(self):
self.assertEqual(cli_result.exit_code, 0)
self.assertIsNone(cli_result.exception)

self.assertFalse(
Path(tmpdirname).joinpath(OUTPUT_RSS_FEED_CREATED).exists()
)
self.assertFalse(
Path(tmpdirname).joinpath(OUTPUT_RSS_FEED_UPDATED).exists()
)
self.assertFalse(
Path(tmpdirname).joinpath(OUTPUT_JSON_FEED_CREATED).exists()
)
self.assertFalse(
Path(tmpdirname).joinpath(OUTPUT_JSON_FEED_UPDATED).exists()
)

def test_simple_build_rss_enabled_not_jsonfeed(self):
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path(
"tests/fixtures/mkdocs_rss_enabled_not_jsonfeed.yml"
),
output_path=tmpdirname,
)
if cli_result.exception is not None:
e = cli_result.exception
logger.debug(format_exception(type(e), e, e.__traceback__))

self.assertEqual(cli_result.exit_code, 0)
self.assertIsNone(cli_result.exception)

self.assertTrue(Path(tmpdirname).joinpath(OUTPUT_RSS_FEED_CREATED).exists())
self.assertTrue(Path(tmpdirname).joinpath(OUTPUT_RSS_FEED_UPDATED).exists())
self.assertFalse(
Path(tmpdirname).joinpath(OUTPUT_JSON_FEED_CREATED).exists()
)
self.assertFalse(
Path(tmpdirname).joinpath(OUTPUT_JSON_FEED_UPDATED).exists()
)

def test_simple_build_jsonfeed_enabled_not_rss(self):
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path(
"tests/fixtures/mkdocs_jsonfeed_enabled_not_rss.yml"
),
output_path=tmpdirname,
)
if cli_result.exception is not None:
e = cli_result.exception
logger.debug(format_exception(type(e), e, e.__traceback__))

self.assertEqual(cli_result.exit_code, 0)
self.assertIsNone(cli_result.exception)

self.assertFalse(
Path(tmpdirname).joinpath(OUTPUT_RSS_FEED_CREATED).exists()
)
self.assertFalse(
Path(tmpdirname).joinpath(OUTPUT_RSS_FEED_UPDATED).exists()
)
self.assertTrue(
Path(tmpdirname).joinpath(OUTPUT_JSON_FEED_CREATED).exists()
)
self.assertTrue(
Path(tmpdirname).joinpath(OUTPUT_JSON_FEED_UPDATED).exists()
)

def test_simple_build_item_dates(self):
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
Expand Down Expand Up @@ -203,11 +278,11 @@ def test_simple_build_feed_length(self):
self.assertIsNone(cli_result.exception)

# created items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
feed_parsed = feedparser.parse(Path(tmpdirname) / OUTPUT_RSS_FEED_CREATED)
self.assertEqual(len(feed_parsed.entries), 3)

# updated items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_updated.xml")
feed_parsed = feedparser.parse(Path(tmpdirname) / OUTPUT_RSS_FEED_UPDATED)
self.assertEqual(len(feed_parsed.entries), 3)

def test_simple_build_feed_ttl(self):
Expand All @@ -225,12 +300,12 @@ def test_simple_build_feed_ttl(self):
self.assertIsNone(cli_result.exception)

# created items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
feed_parsed = feedparser.parse(Path(tmpdirname) / OUTPUT_RSS_FEED_CREATED)
self.assertNotEqual(feed_parsed.feed.ttl, "1440")
self.assertEqual(feed_parsed.feed.ttl, "90")

# updated items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_updated.xml")
feed_parsed = feedparser.parse(Path(tmpdirname) / OUTPUT_RSS_FEED_UPDATED)
self.assertNotEqual(feed_parsed.feed.ttl, "1440")
self.assertEqual(feed_parsed.feed.ttl, "90")

Expand All @@ -250,7 +325,7 @@ def test_simple_build_item_categories_enabled(self):
self.assertIsNone(cli_result.exception)

# created items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
feed_parsed = feedparser.parse(Path(tmpdirname) / OUTPUT_RSS_FEED_CREATED)

for feed_item in feed_parsed.entries:
if feed_item.title in ("Test page with meta",):
Expand All @@ -272,7 +347,7 @@ def test_simple_build_item_comments_enabled(self):
self.assertIsNone(cli_result.exception)

# created items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
feed_parsed = feedparser.parse(Path(tmpdirname) / OUTPUT_RSS_FEED_CREATED)
self.assertEqual(feed_parsed.bozo, 0)

for feed_item in feed_parsed.entries:
Expand All @@ -294,7 +369,7 @@ def test_simple_build_item_comments_disabled(self):
self.assertIsNone(cli_result.exception)

# created items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
feed_parsed = feedparser.parse(Path(tmpdirname) / OUTPUT_RSS_FEED_CREATED)
self.assertEqual(feed_parsed.bozo, 0)

for feed_item in feed_parsed.entries:
Expand All @@ -318,7 +393,7 @@ def test_simple_build_item_length_unlimited(self):
self.assertIsNone(cli_result.exception)

# created items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
feed_parsed = feedparser.parse(Path(tmpdirname) / OUTPUT_RSS_FEED_CREATED)
self.assertEqual(feed_parsed.bozo, 0)

for feed_item in feed_parsed.entries:
Expand Down Expand Up @@ -349,11 +424,11 @@ def test_simple_build_locale_with_territory(self):
self.assertIsNone(cli_result.exception)

# created items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
feed_parsed = feedparser.parse(Path(tmpdirname) / OUTPUT_RSS_FEED_CREATED)
self.assertEqual(feed_parsed.feed.get("language"), "en-US")

# updated items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_updated.xml")
feed_parsed = feedparser.parse(Path(tmpdirname) / OUTPUT_RSS_FEED_UPDATED)
self.assertEqual(feed_parsed.feed.get("language"), "en-US")

def test_simple_build_locale_without_territory(self):
Expand All @@ -375,11 +450,11 @@ def test_simple_build_locale_without_territory(self):
self.assertIsNone(cli_result.exception)

# created items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
feed_parsed = feedparser.parse(Path(tmpdirname) / OUTPUT_RSS_FEED_CREATED)
self.assertEqual(feed_parsed.feed.get("language"), "fr")

# updated items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_updated.xml")
feed_parsed = feedparser.parse(Path(tmpdirname) / OUTPUT_RSS_FEED_UPDATED)
self.assertEqual(feed_parsed.feed.get("language"), "fr")

def test_simple_build_language_specific_material(self):
Expand All @@ -401,11 +476,11 @@ def test_simple_build_language_specific_material(self):
self.assertIsNone(cli_result.exception)

# created items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
feed_parsed = feedparser.parse(Path(tmpdirname) / OUTPUT_RSS_FEED_CREATED)
self.assertEqual(feed_parsed.feed.get("language"), "fr")

# updated items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_updated.xml")
feed_parsed = feedparser.parse(Path(tmpdirname) / OUTPUT_RSS_FEED_UPDATED)
self.assertEqual(feed_parsed.feed.get("language"), "fr")

def test_simple_build_pretty_print_enabled(self):
Expand All @@ -425,11 +500,11 @@ def test_simple_build_pretty_print_enabled(self):
self.assertIsNone(cli_result.exception)

# created items
with Path(Path(tmpdirname) / "feed_rss_created.xml").open("r") as f:
with Path(Path(tmpdirname) / OUTPUT_RSS_FEED_CREATED).open("r") as f:
self.assertGreater(len(f.readlines()), 0)

# updated items
with Path(Path(tmpdirname) / "feed_rss_updated.xml").open("r") as f:
with Path(Path(tmpdirname) / OUTPUT_RSS_FEED_UPDATED).open("r") as f:
self.assertGreater(len(f.readlines()), 0)

def test_simple_build_pretty_print_disabled(self):
Expand All @@ -449,11 +524,11 @@ def test_simple_build_pretty_print_disabled(self):
self.assertIsNone(cli_result.exception)

# created items
with Path(Path(tmpdirname) / "feed_rss_created.xml").open("r") as f:
with Path(Path(tmpdirname) / OUTPUT_RSS_FEED_CREATED).open("r") as f:
self.assertEqual(len(f.readlines()), 1)

# updated items
with Path(Path(tmpdirname) / "feed_rss_updated.xml").open("r") as f:
with Path(Path(tmpdirname) / OUTPUT_RSS_FEED_UPDATED).open("r") as f:
self.assertEqual(len(f.readlines()), 1)

def test_rss_feed_validation(self):
Expand All @@ -472,11 +547,11 @@ def test_rss_feed_validation(self):
self.assertIsNone(cli_result.exception)

# created items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_created.xml")
feed_parsed = feedparser.parse(Path(tmpdirname) / OUTPUT_RSS_FEED_CREATED)
self.assertEqual(feed_parsed.bozo, 0)

# updated items
feed_parsed = feedparser.parse(Path(tmpdirname) / "feed_rss_updated.xml")
feed_parsed = feedparser.parse(Path(tmpdirname) / OUTPUT_RSS_FEED_UPDATED)
self.assertEqual(feed_parsed.bozo, 0)

# some feed characteristics
Expand Down Expand Up @@ -524,15 +599,15 @@ def test_date(self):
self.assertIsNone(cli_result.exception)

feed_rss_created = feedparser.parse(
Path(tmpdirname) / "feed_rss_created.xml"
Path(tmpdirname) / OUTPUT_RSS_FEED_CREATED
)
for page in feed_rss_created.entries:
if page.title == "Page with meta date in dot key":
self.assertEqual(page.published, "Sat, 07 Oct 2023 10:20:00 +0000")
break

feed_rss_updated = feedparser.parse(
Path(tmpdirname) / "feed_rss_updated.xml"
Path(tmpdirname) / OUTPUT_RSS_FEED_UPDATED
)
for page in feed_rss_updated.entries:
if page.title == "Page with meta date in dot key":
Expand Down

0 comments on commit e1468a6

Please sign in to comment.