<commit_before># -*- coding: utf-8 -*-

import unittest
import os
import time
import hashlib
import hmac

from algoliasearch import algoliasearch


def safe_index_name(name):
    if 'TRAVIS' not in os.environ:
        return name
    job = os.environ['TRAVIS_JOB_NUMBER']
    return '%s_travis-%s' % (name, job)


class ClientTest(unittest.TestCase):
    def setUp(self):
        try:
            self.name = unichr(224) + 'lgol?' + unichr(224) + '-python'
            self.name2 = unichr(224) + 'lgol?' + unichr(224) + '2-python'
            self.name_obj = unichr(224) + '/go/?' + unichr(224) + '2-python'
        except Exception:
            self.name = 'àlgol?à-python'
            self.name2 = 'àlgol?à2-python'
            self.name_obj = 'à/go/?à2-python'

        self.client = algoliasearch.Client(
            os.environ['ALGOLIA_APPLICATION_ID'],
            os.environ['ALGOLIA_API_KEY'])
        index_name = safe_index_name(self.name)
        try:
            self.client.delete_index(index_name)
        except algoliasearch.AlgoliaException:
            pass
        self.index = self.client.init_index(index_name)

    def tearDown(self):
        index_name = safe_index_name(self.name)
        try:
            self.client.delete_index(index_name)
        except algoliasearch.AlgoliaException:
            pass
        index_name2 = safe_index_name(self.name2)
        try:
            self.client.delete_index(index_name2)
        except algoliasearch.AlgoliaException:
            pass

    def test_secured_keys(self):
        self.assertEquals(
            '1fd74b206c64fb49fdcd7a5f3004356cd3bdc9d9aba8733656443e64daafc417',
            hmac.new('my_api_key'.encode('utf-8'), '(public,user1)'.encode(
                'utf-8'), hashlib.sha256).hexdigest())
        key = self.client.generate_secured_api_key('my_api_key',
                                                   '(public,user1)')
        self.assertEquals(key, hmac.new('my_api_key'.encode('utf-8'),
                                        '(public,user1)'.encode('utf-8'),
                                        hashlib.sha256).hexdigest())
        key = self.client.generate_secured_api_key('my_api_key',
                                                   '(public,user1)', 42)
        self.assertEquals(key, hmac.new('my_api_key'.encode('utf-8'),
                                        '(public,user1)42'.encode('utf-8'),
                                        hashlib.sha256).hexdigest())
        key = self.client.generate_secured_api_key('my_api_key', ['public'])
        self.assertEquals(key, hmac.new('my_api_key'.encode(
            'utf-8'), 'public'.encode('utf-8'), hashlib.sha256).hexdigest())
        key = self.client.generate_secured_api_key(
            'my_api_key', ['public', ['premium', 'vip']])
        self.assertEquals(key, hmac.new('my_api_key'.encode('utf-8'),
                                        'public,(premium,vip)'.encode('utf-8'),
                                        hashlib.sha256).hexdigest())

    def test_disjunctive_faceting(self):
        self.index.set_settings(
            {'attributesForFacetting': ['city', 'stars', 'facilities']})
        task = self.index.add_objects([{
            'name': 'Hotel A',<commit_msg>Add test for network errors<commit_after># -*- coding: utf-8 -*-

import unittest
import os
import time
import hashlib
import hmac

from algoliasearch import algoliasearch


def safe_index_name(name):
    if 'TRAVIS' not in os.environ:
        return name
    job = os.environ['TRAVIS_JOB_NUMBER']
    return '%s_travis-%s' % (name, job)


class ClientTest(unittest.TestCase):
    def setUp(self):
        try:
            self.name = unichr(224) + 'lgol?' + unichr(224) + '-python'
            self.name2 = unichr(224) + 'lgol?' + unichr(224) + '2-python'
            self.name_obj = unichr(224) + '/go/?' + unichr(224) + '2-python'
        except Exception:
            self.name = 'àlgol?à-python'
            self.name2 = 'àlgol?à2-python'
            self.name_obj = 'à/go/?à2-python'

        self.client = algoliasearch.Client(
            os.environ['ALGOLIA_APPLICATION_ID'],
            os.environ['ALGOLIA_API_KEY'])
        index_name = safe_index_name(self.name)
        try:
            self.client.delete_index(index_name)
        except algoliasearch.AlgoliaException:
            pass
        self.index = self.client.init_index(index_name)

    def tearDown(self):
        index_name = safe_index_name(self.name)
        try:
            self.client.delete_index(index_name)
        except algoliasearch.AlgoliaException:
            pass
        index_name2 = safe_index_name(self.name2)
        try:
            self.client.delete_index(index_name2)
        except algoliasearch.AlgoliaException:
            pass

    def test_wrong_app_id(self):
        client = algoliasearch.Client("fakeappID", "blabla")
        try:
            client.listIndexes()
            self.assertTrue(False)
        except algoliasearch.AlgoliaException as e:
            pass

    def test_secured_keys(self):
        self.assertEquals(
            '1fd74b206c64fb49fdcd7a5f3004356cd3bdc9d9aba8733656443e64daafc417',
            hmac.new('my_api_key'.encode('utf-8'), '(public,user1)'.encode(
                'utf-8'), hashlib.sha256).hexdigest())
        key = self.client.generate_secured_api_key('my_api_key',
                                                   '(public,user1)')
        self.assertEquals(key, hmac.new('my_api_key'.encode('utf-8'),
                                        '(public,user1)'.encode('utf-8'),
                                        hashlib.sha256).hexdigest())
        key = self.client.generate_secured_api_key('my_api_key',
                                                   '(public,user1)', 42)
        self.assertEquals(key, hmac.new('my_api_key'.encode('utf-8'),
                                        '(public,user1)42'.encode('utf-8'),
                                        hashlib.sha256).hexdigest())
        key = self.client.generate_secured_api_key('my_api_key', ['public'])
        self.assertEquals(key, hmac.new('my_api_key'.encode(
            'utf-8'), 'public'.encode('utf-8'), hashlib.sha256).hexdigest())
        key = self.client.generate_secured_api_key(
            'my_api_key', ['public', ['premium', 'vip']])
        self.assertEquals(key, hmac.new('my_api_key'.encode('utf-8'),
                                        'public,(premium,vip)'.encode('utf-8'),
                                        hashlib.sha256).hexdigest())

    def test_disjunctive_faceting(self):
        self.index.set_settings(
            {'attributesForFacetting': ['city', 'stars', 'facilities']})
        task = self.index.add_objects([{
            'name': 'Hotel A',
{'fraction_non_alphanumeric': 0.12837937656965578, 'fraction_numerical': 0.029250997193086128, 'mean_word_length': 1.9537521815008727, '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': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1410585', 'n_tokens_mistral': 2200, 'n_tokens_neox': 2077, 'n_words': 300}