diff --git a/abschnitte/tests.py b/abschnitte/tests.py index 7ce503c..93a0813 100644 --- a/abschnitte/tests.py +++ b/abschnitte/tests.py @@ -1,3 +1,377 @@ -from django.test import TestCase +from unittest.mock import Mock, patch, MagicMock +from django.test import TestCase, override_settings +from abschnitte.models import AbschnittTyp, Textabschnitt +from abschnitte.utils import render_textabschnitte, md_table_to_html -# Create your tests here. + +class MockTextabschnitt: + """Mock object for Textabschnitt (since it's abstract).""" + def __init__(self, abschnitttyp, inhalt): + self.abschnitttyp = abschnitttyp + self.inhalt = inhalt + + +class RenderTextabschnitteTestCase(TestCase): + """Test cases for render_textabschnitte function.""" + + def setUp(self): + """Set up test fixtures.""" + # Create mock AbschnittTyp objects + self.typ_text = Mock() + self.typ_text.abschnitttyp = "text" + + self.typ_liste_ungeordnet = Mock() + self.typ_liste_ungeordnet.abschnitttyp = "liste ungeordnet" + + self.typ_liste_geordnet = Mock() + self.typ_liste_geordnet.abschnitttyp = "liste geordnet" + + self.typ_tabelle = Mock() + self.typ_tabelle.abschnitttyp = "tabelle" + + self.typ_diagramm = Mock() + self.typ_diagramm.abschnitttyp = "diagramm" + + self.typ_code = Mock() + self.typ_code.abschnitttyp = "code" + + def test_render_basic_text(self): + """Test rendering basic text content.""" + abschnitt = MockTextabschnitt( + abschnitttyp=self.typ_text, + inhalt="This is **bold** text with *italic*." + ) + + result = render_textabschnitte([abschnitt]) + + self.assertEqual(len(result), 1) + typ, html = result[0] + self.assertEqual(typ, "text") + self.assertIn("bold", html) + self.assertIn("italic", html) + + def test_render_unordered_list(self): + """Test rendering unordered list.""" + abschnitt = MockTextabschnitt( + abschnitttyp=self.typ_liste_ungeordnet, + inhalt="Item 1\nItem 2\nItem 3" + ) + + result = render_textabschnitte([abschnitt]) + + self.assertEqual(len(result), 1) + typ, html = result[0] + self.assertEqual(typ, "liste ungeordnet") + self.assertIn("
| Name | ', html) + self.assertIn('Age | ', html) + self.assertIn('Alice | ', html) + self.assertIn('30 | ', html) + + def test_table_with_spaces(self): + """Test table with various spacing.""" + md = """| Name | Age | +|--------|-------| +| Alice | 30 | +| Bob | 25 |""" + + html = md_table_to_html(md) + + # Spaces should be trimmed + self.assertIn('Name | ', html) + self.assertIn('Age | ', html) + self.assertIn('Alice | ', html) + + def test_table_no_outer_pipes(self): + """Test table without outer pipe characters.""" + md = """Name | Age +-----|----- +Alice | 30 +Bob | 25""" + + html = md_table_to_html(md) + + self.assertIn('Name | ', html) + self.assertIn('Alice | ', html) + + def test_table_insufficient_rows(self): + """Test error handling for malformed table.""" + md = """| Name | +|------|""" + + with self.assertRaises(ValueError) as context: + md_table_to_html(md) + + self.assertIn("at least header + separator", str(context.exception)) + + def test_table_with_empty_cells(self): + """Test table with empty cells.""" + md = """| Name | Age | +|------|-----| +| Alice | | +| | 25 |""" + + html = md_table_to_html(md) + + self.assertIn('Alice | ', html) + self.assertIn('', html) + self.assertIn(' | 25 | ', html) + + def test_table_three_columns(self): + """Test table with more columns.""" + md = """| First | Middle | Last | +|-------|--------|------| +| John | Q | Doe |""" + + html = md_table_to_html(md) + + self.assertIn('First | ', html) + self.assertIn('Middle | ', html) + self.assertIn('Last | ', html) + self.assertIn('John | ', html) + self.assertIn('Q | ', html) + self.assertIn('Doe | ', html) + + +class RenderIntegrationTestCase(TestCase): + """Integration tests for rendering pipeline.""" + + @patch('abschnitte.utils.get_cached_diagram') + def test_mixed_content_rendering(self, mock_get_cached): + """Test rendering a mix of different content types.""" + mock_get_cached.return_value = 'diagram_cache/test.svg' + + # Create various types of content + typ_text = Mock(abschnitttyp="text") + typ_liste = Mock(abschnitttyp="liste ungeordnet") + typ_diagram = Mock(abschnitttyp="diagramm") + + abschnitte = [ + MockTextabschnitt(typ_text, "Introduction paragraph"), + MockTextabschnitt(typ_liste, "Point 1\nPoint 2\nPoint 3"), + MockTextabschnitt(typ_diagram, "plantuml\n@startuml\nA -> B\n@enduml"), + MockTextabschnitt(typ_text, "Conclusion"), + ] + + result = render_textabschnitte(abschnitte) + + # Should have 4 results + self.assertEqual(len(result), 4) + + # Verify each type was rendered correctly + self.assertEqual(result[0][0], "text") + self.assertIn("Introduction", result[0][1]) + + self.assertEqual(result[1][0], "liste ungeordnet") + self.assertIn("
|---|