Вопрос

I have two different tests, and both are failing when run with other tests. I'm going to display one of them here. This test is for testing that synonyms are working. I've got the following synonyms in my synonym.txt file:

knife, machete
bayonet, dagger, sword

the unit test looks like this:

def test_synonyms(self):
    """
    Test that synonyms are working
    """
    user = UserFactory()
    SubscriberFactory.create(user=user)
    descriptions = [
        'bayonet',
        'dagger',
        'sword',
        'knife',
        'machete'
    ]
    for desc in descriptions:
        ListingFactory.create(user=user,
                              description="Great {0} for all of your undertakings".format(desc))

    call_command('update_index', settings.LISTING_INDEX, using=[settings.LISTING_INDEX])

    self.sqs = SearchQuerySet().using(settings.LISTING_INDEX)

    self.assertEqual(self.sqs.count(), 5)
    # 3 of the 5 are in one group, 2 in the other
    self.assertEqual(self.sqs.auto_query('bayonet').count(), 3)
    self.assertEqual(self.sqs.auto_query('dagger').count(), 3)
    self.assertEqual(self.sqs.auto_query('sword').count(), 3)
    # 2 of the 5 in this group
    self.assertEqual(self.sqs.auto_query('knife').count(), 2)
    self.assertEqual(self.sqs.auto_query('machete').count(), 2)

The problem is that when I run the test in isolation with the command ./manage.py test AnalyzersTestCase.test_synonyms it works fine. But if I run it along with other tests, it fails, returning 1 result where it should return 3. If I run a raw elasticsearch query at that point, elasticsearch returns 1 result. So it must be something in the setup of the index... but I'm deleting the index in the setup() method, so I don't see how it can be in a different state when run in isolation from when it's run alongside other tests.

Any help you can give would be great.

Это было полезно?

Решение

Figured it out...

Haystack's connections singleton needed to be cleared between tests, so:

import haystack
for key, opts in haystack.connections.connections_info.items():
    haystack.connections.reload(key)
call_command('clear_index', interactive=False, verbosity=0)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top