| 1 |
import unittest |
| 2 |
from unittest import mock |
| 3 |
|
| 4 |
from django.utils.lorem_ipsum import paragraph, paragraphs, sentence, words |
| 5 |
|
| 6 |
|
| 7 |
class LoremIpsumTests(unittest.TestCase): |
| 8 |
def test_negative_words(self): |
| 9 |
"""words(n) returns n + 19 words, even if n is negative.""" |
| 10 |
self.assertEqual( |
| 11 |
words(-5), |
| 12 |
"lorem ipsum dolor sit amet consectetur adipisicing elit sed do " |
| 13 |
"eiusmod tempor incididunt ut", |
| 14 |
) |
| 15 |
|
| 16 |
def test_same_or_less_common_words(self): |
| 17 |
"""words(n) for n < 19.""" |
| 18 |
self.assertEqual(words(7), "lorem ipsum dolor sit amet consectetur adipisicing") |
| 19 |
|
| 20 |
def test_common_words_in_string(self): |
| 21 |
"""words(n) starts with the 19 standard lorem ipsum words for n > 19.""" |
| 22 |
self.assertTrue( |
| 23 |
words(25).startswith( |
| 24 |
"lorem ipsum dolor sit amet consectetur adipisicing elit sed " |
| 25 |
"do eiusmod tempor incididunt ut labore et dolore magna aliqua" |
| 26 |
) |
| 27 |
) |
| 28 |
|
| 29 |
def test_more_words_than_common(self): |
| 30 |
"""words(n) returns n words for n > 19.""" |
| 31 |
self.assertEqual(len(words(25).split()), 25) |
| 32 |
|
| 33 |
def test_common_large_number_of_words(self): |
| 34 |
"""words(n) has n words when n is greater than len(WORDS).""" |
| 35 |
self.assertEqual(len(words(500).split()), 500) |
| 36 |
|
| 37 |
@mock.patch("django.utils.lorem_ipsum.random.sample") |
| 38 |
def test_not_common_words(self, mock_sample): |
| 39 |
"""words(n, common=False) returns random words.""" |
| 40 |
mock_sample.return_value = ["exercitationem", "perferendis"] |
| 41 |
self.assertEqual(words(2, common=False), "exercitationem perferendis") |
| 42 |
|
| 43 |
def test_sentence_starts_with_capital(self): |
| 44 |
"""A sentence starts with a capital letter.""" |
| 45 |
self.assertTrue(sentence()[0].isupper()) |
| 46 |
|
| 47 |
@mock.patch("django.utils.lorem_ipsum.random.sample") |
| 48 |
@mock.patch("django.utils.lorem_ipsum.random.choice") |
| 49 |
@mock.patch("django.utils.lorem_ipsum.random.randint") |
| 50 |
def test_sentence(self, mock_randint, mock_choice, mock_sample): |
| 51 |
""" |
| 52 |
Sentences are built using some number of phrases and a set of words. |
| 53 |
""" |
| 54 |
mock_randint.return_value = 2 # Use two phrases. |
| 55 |
mock_sample.return_value = ["exercitationem", "perferendis"] |
| 56 |
mock_choice.return_value = "?" |
| 57 |
value = sentence() |
| 58 |
self.assertEqual(mock_randint.call_count, 3) |
| 59 |
self.assertEqual(mock_sample.call_count, 2) |
| 60 |
self.assertEqual(mock_choice.call_count, 1) |
| 61 |
self.assertEqual( |
| 62 |
value, "Exercitationem perferendis, exercitationem perferendis?" |
| 63 |
) |
| 64 |
|
| 65 |
@mock.patch("django.utils.lorem_ipsum.random.choice") |
| 66 |
def test_sentence_ending(self, mock_choice): |
| 67 |
"""Sentences end with a question mark or a period.""" |
| 68 |
mock_choice.return_value = "?" |
| 69 |
self.assertIn(sentence()[-1], "?") |
| 70 |
mock_choice.return_value = "." |
| 71 |
self.assertIn(sentence()[-1], ".") |
| 72 |
|
| 73 |
@mock.patch("django.utils.lorem_ipsum.random.sample") |
| 74 |
@mock.patch("django.utils.lorem_ipsum.random.choice") |
| 75 |
@mock.patch("django.utils.lorem_ipsum.random.randint") |
| 76 |
def test_paragraph(self, mock_paragraph_randint, mock_choice, mock_sample): |
| 77 |
"""paragraph() generates a single paragraph.""" |
| 78 |
# Make creating 2 sentences use 2 phrases. |
| 79 |
mock_paragraph_randint.return_value = 2 |
| 80 |
mock_sample.return_value = ["exercitationem", "perferendis"] |
| 81 |
mock_choice.return_value = "." |
| 82 |
value = paragraph() |
| 83 |
self.assertEqual(mock_paragraph_randint.call_count, 7) |
| 84 |
self.assertEqual( |
| 85 |
value, |
| 86 |
( |
| 87 |
"Exercitationem perferendis, exercitationem perferendis. " |
| 88 |
"Exercitationem perferendis, exercitationem perferendis." |
| 89 |
), |
| 90 |
) |
| 91 |
|
| 92 |
@mock.patch("django.utils.lorem_ipsum.random.sample") |
| 93 |
@mock.patch("django.utils.lorem_ipsum.random.choice") |
| 94 |
@mock.patch("django.utils.lorem_ipsum.random.randint") |
| 95 |
def test_paragraphs_not_common(self, mock_randint, mock_choice, mock_sample): |
| 96 |
""" |
| 97 |
paragraphs(1, common=False) generating one paragraph that's not the |
| 98 |
COMMON_P paragraph. |
| 99 |
""" |
| 100 |
# Make creating 2 sentences use 2 phrases. |
| 101 |
mock_randint.return_value = 2 |
| 102 |
mock_sample.return_value = ["exercitationem", "perferendis"] |
| 103 |
mock_choice.return_value = "." |
| 104 |
self.assertEqual( |
| 105 |
paragraphs(1, common=False), |
| 106 |
[ |
| 107 |
"Exercitationem perferendis, exercitationem perferendis. " |
| 108 |
"Exercitationem perferendis, exercitationem perferendis." |
| 109 |
], |
| 110 |
) |
| 111 |
self.assertEqual(mock_randint.call_count, 7) |
| 112 |
|
| 113 |
def test_paragraphs(self): |
| 114 |
"""paragraphs(1) uses the COMMON_P paragraph.""" |
| 115 |
self.assertEqual( |
| 116 |
paragraphs(1), |
| 117 |
[ |
| 118 |
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, " |
| 119 |
"sed do eiusmod tempor incididunt ut labore et dolore magna " |
| 120 |
"aliqua. Ut enim ad minim veniam, quis nostrud exercitation " |
| 121 |
"ullamco laboris nisi ut aliquip ex ea commodo consequat. " |
| 122 |
"Duis aute irure dolor in reprehenderit in voluptate velit " |
| 123 |
"esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " |
| 124 |
"occaecat cupidatat non proident, sunt in culpa qui officia " |
| 125 |
"deserunt mollit anim id est laborum." |
| 126 |
], |
| 127 |
) |