<commit_before>from django.conf import settings
from django.test import TestCase

from algoliasearch_django import AlgoliaIndex
from algoliasearch_django import algolia_engine
from algoliasearch_django.models import AlgoliaIndexError

from .models import User, Website, Example


class IndexTestCase(TestCase):
    def setUp(self):
        self.client = algolia_engine.client
        self.user = User(name='Algolia', username="algolia",
                         bio='Milliseconds matter', followers_count=42001,
                         following_count=42, _lat=123, _lng=-42.24,
                         _permissions='read,write,admin')
        self.example = Example(uid=4,
                               name='SuperK',
                               address='Finland',
                               lat=63.3,
                               lng=-32.0,
                               is_admin=True)
        self.example.category = ['Shop', 'Grocery']
        self.example.locations = [
            {'lat': 10.3, 'lng': -20.0},
            {'lat': 22.3, 'lng': 10.0},
        ]

    def test_default_index_name(self):
        index = AlgoliaIndex(Website, self.client, settings.ALGOLIA)
        regex = r'^test_Website_django(_travis-\d+.\d+)?$'
        try:
            self.assertRegex(index.index_name, regex)
        except AttributeError:
            self.assertRegexpMatches(index.index_name, regex)

    def test_custom_index_name(self):
        class WebsiteIndex(AlgoliaIndex):
            index_name = 'customName'

        index = WebsiteIndex(Website, self.client, settings.ALGOLIA)
        regex = r'^test_customName_django(_travis-\d+.\d+)?$'
        try:
            self.assertRegex(index.index_name, regex)
        except AttributeError:
            self.assertRegexpMatches(index.index_name, regex)

    def test_index_name_settings(self):
        algolia_settings = dict(settings.ALGOLIA)
        del algolia_settings['INDEX_PREFIX']
        del algolia_settings['INDEX_SUFFIX']

        with self.settings(ALGOLIA=algolia_settings):
            index = AlgoliaIndex(Website, self.client, settings.ALGOLIA)
            regex = r'^Website$'
            try:
                self.assertRegex(index.index_name, regex)
            except AttributeError:
                self.assertRegexpMatches(index.index_name, regex)

    def test_reindex_with_replicas(self):
        class WebsiteIndex(AlgoliaIndex):
            settings = {
                'replicas': [
                    'django_contact_name_asc',
                    'django_contact_name_desc'
                ]
            }

        index = WebsiteIndex(Website, self.client, settings.ALGOLIA)
        index.reindex_all()

    def test_custom_objectID(self):
        class UserIndex(AlgoliaIndex):
            custom_objectID = 'username'

        index = UserIndex(User, self.client, settings.ALGOLIA)
        obj = index.get_raw_record(self.user)
        self.assertEqual(obj['objectID'], 'algolia')

    def test_invalid_custom_objectID(self):
        class UserIndex(AlgoliaIndex):
            custom_objectID = 'uid'

        with self.assertRaises(AlgoliaIndexError):
            UserIndex(User, self.client, settings.ALGOLIA)

    def test_geo_fields(self):
        class UserIndex(AlgoliaIndex):
            geo_field = 'location'
<commit_msg>fix(tests): Unique replica index name<commit_after>from django.conf import settings
from django.test import TestCase

from algoliasearch_django import AlgoliaIndex
from algoliasearch_django import algolia_engine
from algoliasearch_django.models import AlgoliaIndexError

from .models import User, Website, Example


class IndexTestCase(TestCase):
    def setUp(self):
        self.client = algolia_engine.client
        self.user = User(name='Algolia', username="algolia",
                         bio='Milliseconds matter', followers_count=42001,
                         following_count=42, _lat=123, _lng=-42.24,
                         _permissions='read,write,admin')
        self.example = Example(uid=4,
                               name='SuperK',
                               address='Finland',
                               lat=63.3,
                               lng=-32.0,
                               is_admin=True)
        self.example.category = ['Shop', 'Grocery']
        self.example.locations = [
            {'lat': 10.3, 'lng': -20.0},
            {'lat': 22.3, 'lng': 10.0},
        ]

    def test_default_index_name(self):
        index = AlgoliaIndex(Website, self.client, settings.ALGOLIA)
        regex = r'^test_Website_django(_travis-\d+.\d+)?$'
        try:
            self.assertRegex(index.index_name, regex)
        except AttributeError:
            self.assertRegexpMatches(index.index_name, regex)

    def test_custom_index_name(self):
        class WebsiteIndex(AlgoliaIndex):
            index_name = 'customName'

        index = WebsiteIndex(Website, self.client, settings.ALGOLIA)
        regex = r'^test_customName_django(_travis-\d+.\d+)?$'
        try:
            self.assertRegex(index.index_name, regex)
        except AttributeError:
            self.assertRegexpMatches(index.index_name, regex)

    def test_index_name_settings(self):
        algolia_settings = dict(settings.ALGOLIA)
        del algolia_settings['INDEX_PREFIX']
        del algolia_settings['INDEX_SUFFIX']

        with self.settings(ALGOLIA=algolia_settings):
            index = AlgoliaIndex(Website, self.client, settings.ALGOLIA)
            regex = r'^Website$'
            try:
                self.assertRegex(index.index_name, regex)
            except AttributeError:
                self.assertRegexpMatches(index.index_name, regex)

    def test_reindex_with_replicas(self):
        index = AlgoliaIndex(Website, self.client, settings.ALGOLIA)
        class WebsiteIndex(AlgoliaIndex):
            settings = {
                'replicas': [
                    index.index_name + '_name_asc',
                    index.index_name + '_name_desc'
                ]
            }

        index = WebsiteIndex(Website, self.client, settings.ALGOLIA)
        index.reindex_all()

    def test_custom_objectID(self):
        class UserIndex(AlgoliaIndex):
            custom_objectID = 'username'

        index = UserIndex(User, self.client, settings.ALGOLIA)
        obj = index.get_raw_record(self.user)
        self.assertEqual(obj['objectID'], 'algolia')

    def test_invalid_custom_objectID(self):
        class UserIndex(AlgoliaIndex):
            custom_objectID = 'uid'

        with self.assertRaises(AlgoliaIndexError):
            UserIndex(User, self.client, settings.ALGOLIA)

    def test_geo_fields(self):
        class UserIndex(AlgoliaIndex):
            geo_field = 'location'

{'fraction_non_alphanumeric': 0.09702706700192279, 'fraction_numerical': 0.009761869545925159, 'mean_word_length': 2.1305555555555555, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7179062', 'n_tokens_mistral': 2015, 'n_tokens_neox': 1911, 'n_words': 334}