from django.test import SimpleTestCase from .embed import YouTubeLiteEmbedFinder from .models import BasePage from .utils import count_words, extract_text, get_page_models, get_table_of_contents class BasePageTestCase(SimpleTestCase): def test_unique_body_classes(self) -> None: body_classes = [page.body_class for page in get_page_models()] self.assertEqual(len(body_classes), len(set(body_classes))) def test_pages_inherit_base_page(self) -> None: for page_model in get_page_models(): self.assertTrue( issubclass(page_model, BasePage), f"{page_model} does not inherit from {BasePage}.", ) class YouTubeLiteEmbedFinderTestCase(SimpleTestCase): def test_finds_video_id(self) -> None: self.assertEqual( YouTubeLiteEmbedFinder._get_video_id( '' ), "dQw4w9WgXcQ", ) with self.assertRaises(ValueError): YouTubeLiteEmbedFinder._get_video_id("something-else") class TableOfContentsTestCase(SimpleTestCase): def test_creates_table_of_contents(self) -> None: toc = get_table_of_contents( """
I'm no heading
") self.assertEqual(toc, []) def test_no_content(self) -> None: toc = get_table_of_contents("") self.assertEqual(toc, []) def test_non_sequential_headings(self) -> None: toc = get_table_of_contents( """2 content
2.2 content
Hello there!
"), "Hello there!") self.assertEqual( extract_text("Paragraph 1
\nParagraph 2
"), "Paragraph 1 Paragraph 2", ) def test_plain_text(self) -> None: self.assertEqual(extract_text("Hello there!"), "Hello there!") class CountWordsTestCase(SimpleTestCase): def test_counts_words(self) -> None: self.assertEqual(count_words("a b c"), 3) self.assertEqual(count_words("Correct Horse Battery Staple"), 4) self.assertEqual(count_words("Hello there! How are you?"), 5)