Fixed #28306 -- Completed test coverage for django.utils.lorem_ipsum.

Thanks Idan Melamed for the original patch.
This commit is contained in:
LBerrocal 2017-08-17 13:29:05 -07:00 committed by Tim Graham
parent 473ab4610e
commit 54f7aa04a7
2 changed files with 103 additions and 3 deletions

View File

@ -325,6 +325,7 @@ answer newbie questions, and generally made Django that much better:
Ian Lee <IanLee1521@gmail.com> Ian Lee <IanLee1521@gmail.com>
Ibon <ibonso@gmail.com> Ibon <ibonso@gmail.com>
Idan Gazit <idan@gazit.me> Idan Gazit <idan@gazit.me>
Idan Melamed
Ifedapo Olarewaju <ifedapoolarewaju@gmail.com> Ifedapo Olarewaju <ifedapoolarewaju@gmail.com>
Igor Kolar <ike@email.si> Igor Kolar <ike@email.si>
Illia Volochii <illia.volochii@gmail.com> Illia Volochii <illia.volochii@gmail.com>
@ -478,6 +479,7 @@ answer newbie questions, and generally made Django that much better:
Luan Pablo <luanpab@gmail.com> Luan Pablo <luanpab@gmail.com>
Luciano Ramalho Luciano Ramalho
Ludvig Ericson <ludvig.ericson@gmail.com> Ludvig Ericson <ludvig.ericson@gmail.com>
Luis C. Berrocal <luis.berrocal.1942@gmail.com>
Łukasz Langa <lukasz@langa.pl> Łukasz Langa <lukasz@langa.pl>
Łukasz Rekucki <lrekucki@gmail.com> Łukasz Rekucki <lrekucki@gmail.com>
Luke Granger-Brown <django@lukegb.com> Luke Granger-Brown <django@lukegb.com>

View File

@ -1,14 +1,112 @@
import unittest import unittest
from unittest import mock
from django.utils.lorem_ipsum import paragraphs, words from django.utils.lorem_ipsum import paragraph, paragraphs, sentence, words
class WebdesignTest(unittest.TestCase): class LoremIpsumTests(unittest.TestCase):
def test_negative_words(self):
"""words(n) returns n + 19 words, even if n is negative."""
self.assertEqual(
words(-5),
'lorem ipsum dolor sit amet consectetur adipisicing elit sed do '
'eiusmod tempor incididunt ut'
)
def test_words(self): def test_same_or_less_common_words(self):
"""words(n) for n < 19."""
self.assertEqual(words(7), 'lorem ipsum dolor sit amet consectetur adipisicing') self.assertEqual(words(7), 'lorem ipsum dolor sit amet consectetur adipisicing')
def test_common_words_in_string(self):
"""words(n) starts with the 19 standard lorem ipsum words for n > 19."""
self.assertTrue(
words(25).startswith(
'lorem ipsum dolor sit amet consectetur adipisicing elit sed '
'do eiusmod tempor incididunt ut labore et dolore magna aliqua'
)
)
def test_more_words_than_common(self):
"""words(n) returns n words for n > 19."""
self.assertEqual(len(words(25).split()), 25)
def test_common_large_number_of_words(self):
"""words(n) has n words when n is greater than len(WORDS)."""
self.assertEqual(len(words(500).split()), 500)
@mock.patch('django.utils.lorem_ipsum.random.sample')
def test_not_common_words(self, mock_sample):
"""words(n, common=False) returns random words."""
mock_sample.return_value = ['exercitationem', 'perferendis']
self.assertEqual(words(2, common=False), 'exercitationem perferendis')
def test_sentence_starts_with_capital(self):
"""A sentence starts with a capital letter."""
self.assertTrue(sentence()[0].isupper())
@mock.patch('django.utils.lorem_ipsum.random.sample')
@mock.patch('django.utils.lorem_ipsum.random.choice')
@mock.patch('django.utils.lorem_ipsum.random.randint')
def test_sentence(self, mock_randint, mock_choice, mock_sample):
"""
Sentences are built using some number of phrases and a set of words.
"""
mock_randint.return_value = 2 # Use two phrases.
mock_sample.return_value = ['exercitationem', 'perferendis']
mock_choice.return_value = '?'
value = sentence()
self.assertEqual(mock_randint.call_count, 3)
self.assertEqual(mock_sample.call_count, 2)
self.assertEqual(mock_choice.call_count, 1)
self.assertEqual(value, 'Exercitationem perferendis, exercitationem perferendis?')
@mock.patch('django.utils.lorem_ipsum.random.choice')
def test_sentence_ending(self, mock_choice):
"""Sentences end with a question mark or a period."""
mock_choice.return_value = '?'
self.assertIn(sentence()[-1], '?')
mock_choice.return_value = '.'
self.assertIn(sentence()[-1], '.')
@mock.patch('django.utils.lorem_ipsum.random.sample')
@mock.patch('django.utils.lorem_ipsum.random.choice')
@mock.patch('django.utils.lorem_ipsum.random.randint')
def test_paragraph(self, mock_paragraph_randint, mock_choice, mock_sample):
"""paragraph() generates a single paragraph."""
# Make creating 2 sentences use 2 phrases.
mock_paragraph_randint.return_value = 2
mock_sample.return_value = ['exercitationem', 'perferendis']
mock_choice.return_value = '.'
value = paragraph()
self.assertEqual(mock_paragraph_randint.call_count, 7)
self.assertEqual(value, (
'Exercitationem perferendis, exercitationem perferendis. '
'Exercitationem perferendis, exercitationem perferendis.'
))
@mock.patch('django.utils.lorem_ipsum.random.sample')
@mock.patch('django.utils.lorem_ipsum.random.choice')
@mock.patch('django.utils.lorem_ipsum.random.randint')
def test_paragraphs_not_common(self, mock_randint, mock_choice, mock_sample):
"""
paragraphs(1, common=False) generating one paragraph that's not the
COMMON_P paragraph.
"""
# Make creating 2 sentences use 2 phrases.
mock_randint.return_value = 2
mock_sample.return_value = ['exercitationem', 'perferendis']
mock_choice.return_value = '.'
self.assertEqual(
paragraphs(1, common=False),
[
'Exercitationem perferendis, exercitationem perferendis. '
'Exercitationem perferendis, exercitationem perferendis.'
]
)
self.assertEqual(mock_randint.call_count, 7)
def test_paragraphs(self): def test_paragraphs(self):
"""paragraphs(1) uses the COMMON_P paragraph."""
self.assertEqual( self.assertEqual(
paragraphs(1), [ paragraphs(1), [
'Lorem ipsum dolor sit amet, consectetur adipisicing elit, ' 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, '