Пример #1
0
    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)
Пример #2
0
    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')