def test_delete_queryset(self, mock_delete): """ delete_queryset() should override Django Admin's delete_queryset action to use model's delete method and update elasticsearch index """ modeladmin = LetterAdmin(Letter, site) # First an empty queryset queryset = Letter.objects.all() modeladmin.delete_queryset(RequestFactory(), queryset) self.assertEqual( mock_delete.call_count, 0, "delete_selected() shouldn't delete anything if queryset empty") # Now a queryset with objects LetterFactory() LetterFactory() queryset = Letter.objects.all() modeladmin.delete_queryset(RequestFactory(), queryset) self.assertEqual( mock_delete.call_count, 2, 'delete_queryset() should delete all objects in queryset')
def setUp(self): self.request = RequestFactory().get('/') self.request.user = AnonymousUser() self.modeladmin = DocumentAdmin(Letter, site) self.sept_1862_letter = LetterFactory(date='1862-09-00') self.may_1863_letter = LetterFactory(date='1863-05-00')
def setUp(self): self.request = RequestFactory().get('/') self.request.user = AnonymousUser() self.modeladmin = LetterAdmin(Letter, site) self.letter_writer1 = CorrespondentFactory() self.letter1 = LetterFactory(writer=self.letter_writer1) self.letter_writer2 = CorrespondentFactory() self.letter2 = LetterFactory(writer=self.letter_writer2) EnvelopeFactory(writer=CorrespondentFactory())
def test_get_initial_filter_values(self, mock_get_sentiment_list): mock_get_sentiment_list.return_value = ['Hipster', 'OMG Ponies!!!'] # If there are no letters, start_date and end_date should be empty strings with patch.object(Letter, 'index_date', autospec=True, return_value=''): result = get_initial_filter_values() self.assertEqual( result['start_date'], '', 'If there are no letters, start_date should be empty string') self.assertEqual( result['end_date'], '', 'Ifthere are no letters, end_date should be empty string') mock_get_sentiment_list.reset_mock() doc_source1 = DocumentSourceFactory(name='Document source 1') doc_source2 = DocumentSourceFactory(name='Document source 2') correspondent1 = CorrespondentFactory() correspondent2 = CorrespondentFactory() LetterFactory(source=doc_source1, writer=correspondent1, date='1857-6-15') LetterFactory(source=doc_source2, writer=correspondent2, date='1863-1-1') result = get_initial_filter_values() self.assertEqual( mock_get_sentiment_list.call_count, 1, 'get_initial_filter_values() should call get_sentiment_list()') # If the letters are dated, start_date and end_date should be filled self.assertEqual(result['start_date'], '1857-06-15', 'If letters are dated, start_date should be filled') self.assertEqual(result['end_date'], '1863-01-01', 'If letters are dated, end_date should be filled') self.assertEqual( result['sources'], [doc_source1, doc_source2], 'get_initial_filter_values() should return list of letter sources') self.assertEqual( result['writers'], sorted([correspondent1, correspondent2]), 'get_initial_filter_values() should return list of letter writers') self.assertEqual( result['words'], DEFAULT_STATS_SEARCH_WORDS, 'get_initial_filter_values() should return default stats search words' ) self.assertEqual( result['sentiments'], mock_get_sentiment_list.return_value, 'get_initial_filter_values() should return list of sentiments')
def test_contents(self, mock_body_as_text): """ Letter.contents() should contain letter heading, greeting, body_as_text(), closing, signature, and ps """ mock_body_as_text.return_value = 'As this is the beginin of a new year I thought as I was a lone to night I ' \ 'would write you a few lines to let you know that we are not all ded yet.' letter = Letter(heading='Januery the 1st / 62', greeting='Miss Evey', closing='your friend as every', signature='F.P. Black', ps='p.s. remember me to enquirin friends') contents = letter.contents() self.assertIn(letter.heading, contents, 'Letter.contents() should include Letter.heading') self.assertIn(letter.greeting, contents, 'Letter.contents() should include Letter.greeting') self.assertIn(mock_body_as_text.return_value, contents, 'Letter.contents() should include Letter.body_as_text()') self.assertIn(letter.closing, contents, 'Letter.contents() should include Letter.closing') self.assertIn(letter.signature, contents, 'Letter.contents() should include Letter.signature') self.assertIn(letter.ps, contents, 'Letter.contents() should include Letter.ps') # If letter is completely blank, Letter.contents() should return empty string mock_body_as_text.return_value = '' self.assertEqual(LetterFactory().contents(), '', 'If letter is completely blank, Letter.contents() should return empty string')
def test_get_envelope_preview(self, mock_image_preview): mock_image_preview.return_value = 'image preview' letter = LetterFactory() number_of_envelopes = 2 for _ in range(number_of_envelopes): letter.envelopes.add(EnvelopeFactory()) envelope_preview = get_envelope_preview(letter) # get_envelope_preview() should call Envelope.image_preview() <number_of_envelopes> times self.assertEqual( mock_image_preview.call_count, number_of_envelopes, 'get_envelope_preview() should call Envelope.image_preview() <number_of_envelopes> times' ) self.assertEqual( envelope_preview.count(mock_image_preview.return_value), number_of_envelopes, 'get_envelope_preview() should contain image_preview() for each envelope' ) # Envelope preview should contain number_of_envelopes - 1 ' ' # because the list is -separated self.assertEqual( envelope_preview.count(' '), number_of_envelopes - 1, 'get_envelope_preview() should return -separated list of image previews' )
def test_get_image_preview(self, mock_image_tag): mock_image_tag.return_value = 'image tag' letter = LetterFactory() number_of_images = 2 for _ in range(number_of_images): letter.images.add(DocumentImageFactory()) image_preview = get_image_preview(letter) # get_image_preview() should call DocumentImage.image_tag() <number_of_images> times self.assertEqual( mock_image_tag.call_count, number_of_images, 'get_image_preview() should call DocumentImage.image_tag() <number_of_images> times' ) self.assertEqual( image_preview.count(mock_image_tag.return_value), number_of_images, 'get_image_preview() should contain image_tag() for each image') # Image preview should contain number_of_images - 1 ' ' # because the list is -separated self.assertEqual( image_preview.count(' '), number_of_images - 1, 'get_image_preview() should return -separated list of image previews' )
def test_template_content(self): template = 'snippets/search_list.html' letter = LetterFactory( date=ApproximateDate(1862, 1, 1), writer=CorrespondentFactory(first_names='Francis P.', last_name='Black'), recipient=CorrespondentFactory(first_names='Eveline', last_name='Johnston'), place=PlaceFactory(name='Manassas Junction', state='Virginia')) Sentiment = namedtuple('Sentiment', ['id', 'name']) sentiments = [Sentiment(id=123, name='Positive/negative')] search_results = [(letter, 'sentiment highlight', sentiments, 'sentiment score')] rendered = render_to_string(template, context={'search_results': search_results}) for heading in ['Date', 'Writer', 'Recipient', 'Place']: self.assertIn(heading, rendered, "Heading '{}' should appear in HTML".format(heading)) self.assertIn(letter.list_date(), rendered, 'Letter list_date() should appear in HTML') self.assertIn(str(letter.writer), rendered, 'Letter writer should appear in HTML') self.assertIn(str(letter.recipient), rendered, 'Letter recipient should appear in HTML') self.assertIn(str(letter.place), rendered, 'Letter place should appear in HTML') self.assertIn('123', rendered, 'Sentiment ID should appear in HTML') self.assertIn('Positive/negative', rendered, 'Sentiment name should appear in HTML') self.assertIn('sentiment highlight', rendered, 'Sentiment highlight should appear in HTML') self.assertIn('sentiment score', rendered, 'Sentiment score should appear in HTML')
def test_get_correspondents_of_source(self): misc_doc_and_letter_source = DocumentSourceFactory() misc_doc_writer = CorrespondentFactory() MiscDocumentFactory(writer=misc_doc_writer, source=misc_doc_and_letter_source) letter_writer = CorrespondentFactory() letter_recipient = CorrespondentFactory() LetterFactory(writer=letter_writer, recipient=letter_recipient, source=misc_doc_and_letter_source) envelope_source = DocumentSourceFactory() envelope_writer = CorrespondentFactory() envelope_recipient = CorrespondentFactory() EnvelopeFactory(writer=envelope_writer, recipient=envelope_recipient, source=envelope_source) # Correspondent not associated with either DocumentSource CorrespondentFactory() # When DocumentSource is misc_doc_and_letter_source, ids for misc_doc_writer, letter_writer, # and letter_recipient should be returned self.assertSetEqual(set(get_correspondents_of_source(misc_doc_and_letter_source.id)), set([misc_doc_writer.id, letter_writer.id, letter_recipient.id]), 'get_correspondents_of_source() should return unique set of ids of Corresponents for DocumentSource') # When DocumentSource is envelope_source, ids for envelope_writer and envelope_recipient # should be returned self.assertSetEqual(set(get_correspondents_of_source(envelope_source.id)), set([envelope_writer.id, envelope_recipient.id]), 'get_correspondents_of_source() should return unique set of ids of Corresponents for DocumentSource')
def test_get_es_writer(self): """ Letter.get_es_writer() should return Letter.writer.id """ letter = LetterFactory() self.assertEqual(letter.get_es_writer(), letter.writer.id, 'Letter.get_es_writer() should return Letter.writer.id')
def test_get_es_source(self): """ Letter.get_es_source() should return Letter.source.id """ letter = LetterFactory() self.assertEqual(letter.get_es_source(), letter.source.id, 'Letter.get_es_source() should return Letter.source.id')
def setUp(self): self.command = Command() self.letter = LetterFactory( date=ApproximateDate(1862, 1, 1), body= 'As this is the beginin of a new year I thought as I was a lone to night I ' 'would write you a few lines to let you know that we are not all ded yet.' )
def test_get_es_date(self, mock_index_date): """ Letter.get_es_date() should return Letter.index_date() """ mock_index_date.return_value = 'index date' self.assertEqual(LetterFactory().get_es_date(), mock_index_date.return_value, 'Letter.get_es_date() should return Letter.index_date()')
def test_get_es_contents(self, mock_contents): """ Letter.get_es_contents() should return Letter.contents() """ mock_contents.return_value = 'Contents' self.assertEqual(LetterFactory().get_es_contents(), mock_contents.return_value, 'Letter.get_es_contents() should return Letter.contents()')
def test_delete_from_elasticsearch(self, mock_delete): """ Letter.delete_from_elasticsearch() should call ES_CLIENT.delete() """ letter = LetterFactory() letter.delete_from_elasticsearch(letter.pk) args, kwargs = mock_delete.call_args self.assertEqual(kwargs['id'], letter.pk, 'Letter.delete_from_elasticsearch() should call ES_CLIENT.delete()')
def test__lt__(self): """ Letter.__lt__() should return True if self.to_string() comes before other letter.to_string() in alphabetic sorting, otherwise it should return False """ # Letters should be ordered by display string: list_date, writer, recipient writer = CorrespondentFactory() recipient = CorrespondentFactory() letter_from_1862 = LetterFactory(date=ApproximateDate(1862), writer=writer, recipient=recipient) letter_from_1861 = LetterFactory(date=ApproximateDate(1861), writer=writer, recipient=recipient) other_letter_from_1861 = LetterFactory(date=ApproximateDate(1861), writer=writer, recipient=recipient) self.assertTrue(letter_from_1861 < letter_from_1862, '{} should be < {}'.format(letter_from_1861, letter_from_1862)) self.assertFalse(letter_from_1861 < other_letter_from_1861, "{} shouldn't be be < {}".format(letter_from_1861, other_letter_from_1861)) self.assertFalse(letter_from_1862 < letter_from_1861, "{} shouldn't be be < {}".format(letter_from_1862, letter_from_1861))
def test_queryset(self, mock_get_correspondents_of_source): """ queryset() should return all Correspondents associated with a particular DocumentSource by finding out which Correspondents were writers or recipients of Letters, Envelopes, and MiscDocuments with this DocumentSource """ misc_doc_and_letter_source = DocumentSourceFactory() misc_doc_writer = CorrespondentFactory() MiscDocumentFactory(writer=misc_doc_writer, source=misc_doc_and_letter_source) letter_writer = CorrespondentFactory() letter_recipient = CorrespondentFactory() LetterFactory(writer=letter_writer, recipient=letter_recipient, source=misc_doc_and_letter_source) envelope_source = DocumentSourceFactory() envelope_writer = CorrespondentFactory() envelope_recipient = CorrespondentFactory() EnvelopeFactory(writer=envelope_writer, recipient=envelope_recipient, source=envelope_source) parameter = 'source' # When no DocumentSource specified, all Correspondents should be returned filter = CorrespondentSourceFilter(self.request, params={}, model=DocumentSource, model_admin=self.modeladmin) queryset = filter.queryset(self.request, Correspondent.objects.all()) self.assertEqual(mock_get_correspondents_of_source.call_count, 0, "get_correspondents_of_source() shouldn't be called if no DocumentSource specified") self.assertSetEqual(set(queryset), set(Correspondent.objects.all()), 'CorrespondentSourceFilter.queryset() should return all Corresponents if no DocumentSource specified') # When DocumentSource is misc_doc_and_letter_source, misc_doc_writer, letter_writer, # and letter_recipient should be returned mock_get_correspondents_of_source.return_value = set([misc_doc_writer.id, letter_writer.id, letter_recipient.id]) filter = CorrespondentSourceFilter(self.request, params={parameter: misc_doc_and_letter_source.id}, model=DocumentSource, model_admin=self.modeladmin) queryset = filter.queryset(self.request, Correspondent.objects.all()) self.assertEqual(mock_get_correspondents_of_source.call_count, 1, 'get_correspondents_of_source() should be called if DocumentSource specified') mock_get_correspondents_of_source.reset_mock() self.assertSetEqual(set(queryset), set([misc_doc_writer, letter_writer, letter_recipient]), 'CorrespondentSourceFilter.queryset() should return Corresponents for DocumentSource') # When DocumentSource is envelope_source, envelope_writer and envelope_recipient should be returned mock_get_correspondents_of_source.return_value = set([envelope_writer.id, envelope_recipient.id]) filter = CorrespondentSourceFilter(self.request, params={parameter: envelope_source.id}, model=DocumentSource, model_admin=self.modeladmin) queryset = filter.queryset(self.request, Correspondent.objects.all()) self.assertEqual(mock_get_correspondents_of_source.call_count, 1, 'get_correspondents_of_source() should be called if DocumentSource specified') mock_get_correspondents_of_source.reset_mock() self.assertSetEqual(set(queryset), set([envelope_writer, envelope_recipient]), 'CorrespondentSourceFilter.queryset() should return Corresponents for DocumentSource')
def test_es_repr(self, mock_field_es_repr): """ Letter.es_repr() should return a dict with fields in es_mapping mapped to field_es_repr() for each field """ letter = LetterFactory() data = letter.es_repr() self.assertEqual(data['_id'], letter.pk, "Letter.es_repr() should return a dict with '_id' = letter.pk") self.assertGreater(mock_field_es_repr.call_count, 0, 'Letter.es_repr() should call Letter.field_es_repr() at least once')
def test_field_es_repr(self, mock_get_es_source, mock_get_es_writer, mock_get_es_date, mock_get_es_contents): """ Letter.field_es_repr() should return serialize letter fields for Elasticsearch indexing by doing the following: Get field description from mapping If there's a method named get_es_{field name} – use it to get field's value If it's an object, populate a dictionary directly from attributes of the related object If it's not an object, and there's no special method with special name, get an attribute from the model See https://qbox.io/blog/elasticsearch-and-django-bulk-index/ """ place = PlaceFactory(name='Manassas Junction') letter = LetterFactory(heading='Januery the 1st / 62', place=place) # If there's a method named get_es_{field name}, it should be used to get field's value letter.field_es_repr('contents') self.assertEqual(mock_get_es_contents.call_count, 1, 'Letter.field_es_repr() should call get_es_contents() for contents()') letter.field_es_repr('date') self.assertEqual(mock_get_es_contents.call_count, 1, 'Letter.field_es_repr() should call get_es_date() for date field') letter.field_es_repr('writer') self.assertEqual(mock_get_es_writer.call_count, 1, 'Letter.field_es_repr() should call get_es_writer() for writer field') letter.field_es_repr('source') self.assertEqual(mock_get_es_source.call_count, 1, 'Letter.field_es_repr() should call get_es_source() for source field') # If it's an object, a dictionary should be populated directly from attributes of the related object # There aren't currently any fields like this in the mapping, so add one here to test it Letter._meta.es_mapping['properties']['place'] = {'type': 'object', 'properties': {'name': 'name'}} result = letter.field_es_repr('place') self.assertEqual(result['_id'], place.id, "If field is an object, Letter.field_es_repr() should return a dictionary with object's attributes") self.assertEqual(result['name'], place.name, "If field is an object, Letter.field_es_repr() should return a dictionary with object's attributes") # If it's not an object, and there's no special method with special name, # field_es_repr() should get an attribute from the model # There aren't currently any fields like this in the mapping, so add one here to test it Letter._meta.es_mapping['properties']['heading'] = {'type': 'something else'} result = letter.field_es_repr('heading') self.assertEqual(result, letter.heading, "Letter.field_es_repr() should return the field's value if it doesn't have its own method") # Set mapping back to a valid type Letter._meta.es_mapping['properties']['heading'] = {'type': 'text'}
def test_body_as_text(self, mock_html_to_text): """ Letter.body_as_text() should return html_to_text() """ mock_html_to_text.return_value = 'html to text' letter = LetterFactory(body='Wish you were here') body_as_text = letter.body_as_text() args, kwargs = mock_html_to_text.call_args self.assertEqual(args[0], letter.body, 'Letter.body_as_text() should call html_to_text(letter.body)') self.assertEqual(body_as_text, mock_html_to_text.return_value, 'Letter.body_as_text() should return value of html_to_text(letter)')
def test_envelope_preview(self, mock_get_envelope_preview): """ Letter.envelope_preview() should return get_envelope_preview() """ mock_get_envelope_preview.return_value = 'envelope preview' letter = LetterFactory() envelope_preview = letter.envelope_preview() args, kwargs = mock_get_envelope_preview.call_args self.assertEqual(args[0], letter, 'Letter.envelope_preview() should call get_envelope_preview(letter)') self.assertEqual(envelope_preview, mock_get_envelope_preview.return_value, 'Letter.envelope_preview() should return value of get_envelope_preview(letter)')
def create_test_letter(): letter = LetterFactory( date=ApproximateDate(1862, 1, 1), writer=CorrespondentFactory(first_names='Francis P.', last_name='Black'), recipient=CorrespondentFactory(first_names='Eveline', last_name='Johnston'), heading='Januery the 1st / 62', greeting='Miss Evey', body= 'As this is the beginin of a new year I thought as I was a lone to night I would write you ' 'a few lines to let you know that we are not all ded yet.', closing='your friend as every', signature='F.P. Black', ps='p.s. remember me to all') return letter
def test_sentiment(self, mock_get_sentiment, mock_contents): """ Letter.sentiment() should return get_sentiment(letter.contents()) """ mock_contents.return_value = 'contents' mock_get_sentiment.return_value = 'sentiment' letter = LetterFactory() sentiment = letter.sentiment() args, kwargs = mock_get_sentiment.call_args self.assertEqual(args[0], mock_contents.return_value, 'Letter.sentiment() should call get_sentiment(letter.contents())') self.assertEqual(sentiment, mock_get_sentiment.return_value, 'Letter.sentiment() should return value of get_sentiment(letter.contents())')
def test_get_form(self): """ get_form() should return a form with custom styling """ modeladmin = LetterAdmin(Letter, site) # Set up superuser to log in to admin and be able to create new Employees user = User.objects.create_superuser('admin', '*****@*****.**', 'Password123') request = RequestFactory() request.user = user form = modeladmin.get_form(request=request, obj=LetterFactory()) # Make sure form fields have height set for field in ['heading', 'greeting', 'closing', 'signature', 'ps']: self.assertIn( 'height', form.base_fields[field].widget.attrs['style'], "LetterAdmin form should have height set for field'{}'")
def test_get_display_string(self, mock_list_date): """ Letter.get_display_string() should return formatted string containing lost_date, writer, and recipient """ mock_list_date.return_value = 'list date' writer = CorrespondentFactory() recipient = CorrespondentFactory() letter = LetterFactory(writer=writer, recipient=recipient) display_string = letter.get_display_string() self.assertIn(str(writer), display_string, 'Letter.get_display_string() should return string containing writer') self.assertIn(str(recipient), display_string, 'Letter.get_display_string() should return string containing recipient') self.assertIn(mock_list_date.return_value, display_string, 'Letter.get_display_string() should return string containing list_date')
def test_calculate_custom_sentiment_without_mocks(self): """ Haven't yet figured out why Elasticsearch doesn't deliver the same score in test as in prod, so for now just make sure it doesn't cause an error """ sentiment = CustomSentimentFactory(name='OMG Ponies!', max_weight=2) TermFactory(text='pony', weight=2, custom_sentiment=sentiment) TermFactory(text='horse', weight=1, custom_sentiment=sentiment) letter = LetterFactory(date=ApproximateDate(1970, 1, 1), body='Look at the horse. Look at the pony.') # Make sure there's not already an indexed document with the same Id # because it might not have gotten cleaned up properly after a previous test if es_settings.ES_CLIENT.exists(index=[Letter._meta.es_index_name], id=letter.pk): letter.delete_from_elasticsearch(pk=letter.pk) letter.create_or_update_in_elasticsearch(is_new=None) calculate_custom_sentiment(letter_id=letter.id, sentiment_id=sentiment.id) letter.delete_from_elasticsearch(pk=letter.pk)
def test_create_or_update_in_elasticsearch(self, mock_update, mock_create, mock_es_repr): """ If this is a newly-created Letter that hasn't been assigned a pk yet, it should get indexed in Elasticsearch If it's an existing Letter, Elasticsearch index should get updated """ letter = LetterFactory() letter.create_or_update_in_elasticsearch(is_new=None) self.assertEqual(mock_es_repr.call_count, 1, 'Letter.create_or_update_in_elasticsearch() should call Letter.es_repr()') # When create_or_update_in_elasticsearch() called with is_new = None, ES_CLIENT.create() should get called self.assertEqual(mock_create.call_count, 1, 'When create_or_update_in_elasticsearch(is_new=None) called, ES_CLIENT.create() should get called') # When create_or_update_in_elasticsearch() called with is_new not None, ES_CLIENT.update() should get called letter.create_or_update_in_elasticsearch(is_new=letter.pk) self.assertEqual(mock_update.call_count, 1, 'When create_or_update_in_elasticsearch(is_new=not None) called, ES_CLIENT.mock_update() should get called')
def test_template_content(self): template = 'snippets/letter_contents.html' letter = LetterFactory( heading='Januery the 1st / 62', greeting='Miss Evey', body= 'As this is the beginin of a new year I thought as I was a lone to night I would write you ' 'a few lines to let you know that we are not all ded yet.', closing='your friend as every', signature='F.P. Black', ps='p.s. remember me to all') rendered = render_to_string(template, context={'letter': letter}) self.assertIn(letter.heading, rendered, 'HTML should contain heading') self.assertIn(letter.greeting, rendered, 'HTML should contain greeting') self.assertIn(letter.body, rendered, 'HTML should contain body') self.assertIn(letter.closing, rendered, 'HTML should contain closing') self.assertIn(letter.signature, rendered, 'HTML should contain signature') self.assertIn(letter.ps, rendered, 'HTML should contain ps')
def test_get_objects_with_date(self): dated_letter = LetterFactory(date='1890-04-01') LetterFactory() self.assertSetEqual(set(get_objects_with_date(Letter)), set([dated_letter]))
def test_queryset(self, mock_get_correspondents_of_source): """ queryset() should return all DocumentImages associated with a particular DocumentSource by finding out which DocumentImages were images of Letters, Envelopes, and MiscDocuments with this DocumentSource """ letter_source = DocumentSourceFactory() letter_writer = CorrespondentFactory() letter_recipient = CorrespondentFactory() letter = LetterFactory(writer=letter_writer, recipient=letter_recipient, source=letter_source) letter_image = DocumentImageFactory() letter.images.add(letter_image) envelope_source = DocumentSourceFactory() envelope_writer = CorrespondentFactory() envelope_recipient = CorrespondentFactory() envelope = EnvelopeFactory(writer=envelope_writer, recipient=envelope_recipient, source=envelope_source) envelope_image = DocumentImageFactory() envelope.images.add(envelope_image) correspondent_with_image_source = DocumentSourceFactory() correspondent_with_image = CorrespondentFactory() LetterFactory(writer=correspondent_with_image, source=correspondent_with_image_source) correspondent_image = DocumentImageFactory() correspondent_with_image.images.add(correspondent_image) parameter = 'source' # When no DocumentSource specified, all DocumentImages should be returned filter = ImageSourceFilter(self.request, params={}, model=DocumentImage, model_admin=self.modeladmin) queryset = filter.queryset(self.request, DocumentImage.objects.all()) self.assertEqual(mock_get_correspondents_of_source.call_count, 0, "get_correspondents_of_source() shouldn't be called if no DocumentSource specified") self.assertSetEqual(set(queryset), set(DocumentImage.objects.all()), 'ImageSourceFilter.queryset() should return all DocumentImages if no DocumentSource specified') # When DocumentSource is letter_source, letter_image should be returned mock_get_correspondents_of_source.return_value = set([letter_writer.id, letter_recipient.id]) filter = ImageSourceFilter(self.request, params={parameter: letter_source.id}, model=DocumentImage, model_admin=self.modeladmin) queryset = filter.queryset(self.request, DocumentImage.objects.all()) self.assertEqual(mock_get_correspondents_of_source.call_count, 1, 'get_correspondents_of_source() should be called if DocumentSource specified') mock_get_correspondents_of_source.reset_mock() self.assertSetEqual(set(queryset), set([letter_image]), 'ImageSourceFilter.queryset() should return DocumentImages for DocumentSource') # When DocumentSource is envelope_source, envelope_image should be returned mock_get_correspondents_of_source.return_value = set([envelope_writer.id, envelope_recipient.id]) filter = ImageSourceFilter(self.request, params={parameter: envelope_source.id}, model=DocumentImage, model_admin=self.modeladmin) queryset = filter.queryset(self.request, DocumentImage.objects.all()) self.assertEqual(mock_get_correspondents_of_source.call_count, 1, 'get_correspondents_of_source() should be called if DocumentSource specified') mock_get_correspondents_of_source.reset_mock() self.assertSetEqual(set(queryset), set([envelope_image]), 'ImageSourceFilter.queryset() should return DocumentImages for DocumentSource') # When DocumentSource is correspondent_with_image_source, correspondent_image should be returned mock_get_correspondents_of_source.return_value = set([correspondent_with_image.id]) filter = ImageSourceFilter(self.request, params={parameter: correspondent_with_image.id}, model=DocumentImage, model_admin=self.modeladmin) queryset = filter.queryset(self.request, DocumentImage.objects.all()) self.assertEqual(mock_get_correspondents_of_source.call_count, 1, 'get_correspondents_of_source() should be called if DocumentSource specified') mock_get_correspondents_of_source.reset_mock() self.assertSetEqual(set(queryset), set([correspondent_image]), 'ImageSourceFilter.queryset() should return DocumentImages for DocumentSource')