Ejemplo n.º 1
0
    def replace_all_objects(self, objects, request_options=None):
        # type: (Union[List[dict], Iterator[dict]], Optional[Union[dict, RequestOptions]]) -> MultipleResponse # noqa: E501

        safe = False
        if isinstance(request_options, dict) and "safe" in request_options:
            safe = request_options.pop("safe")

        tmp_index_name = self._create_temporary_name()
        responses = MultipleResponse()
        responses.push(
            self.copy_to(tmp_index_name, {"scope": ["settings", "synonyms", "rules"]})
        )

        if safe:
            responses.wait()

        try:
            from algoliasearch.search_client import SearchClient
        except ImportError:  # Already imported.
            pass

        tmp_client = SearchClient(self._transporter, self._config)
        tmp_index = tmp_client.init_index(tmp_index_name)

        responses.push(tmp_index.save_objects(objects, request_options))

        if safe:
            responses.wait()

        responses.push(tmp_index.move_to(self._name))

        if safe:
            responses.wait()

        return responses
class TestSearchClient(unittest.TestCase):
    def setUp(self):
        self.config = SearchConfig('foo', 'bar')
        self.transporter = Transporter(Requester(), self.config)
        self.transporter.read = mock.Mock(name="read")
        self.transporter.read.return_value = {}
        self.transporter.write = mock.Mock(name="write")
        self.transporter.write.return_value = {}

        self.client = SearchClient(self.transporter, self.config)

    def test_create(self):
        self.assertIsInstance(self.client, SearchClient)
        with self.assertRaises(AssertionError) as _:
            SearchClient.create('', '')

    def test_create_with_config(self):
        config = SearchConfig('foo', 'bar')

        self.assertIsInstance(SearchClient.create_with_config(config),
                              SearchClient)

    def test_init_index(self):
        index = self.client.init_index('foo')

        self.assertIsInstance(index, SearchIndex)

    def test_app_id_getter(self):
        client = SearchClient.create('foo', 'bar')

        self.assertEqual(client.app_id, 'foo')
class TestSearchClient(unittest.TestCase):
    def setUp(self):
        self.config = SearchConfig('foo', 'bar')
        self.transporter = Transporter(Requester(), self.config)
        self.transporter.read = mock.Mock(name="read")
        self.transporter.read.return_value = {}
        self.transporter.write = mock.Mock(name="write")
        self.transporter.write.return_value = {}

        self.client = SearchClient(self.transporter, self.config)

    def test_create(self):
        self.assertIsInstance(self.client, SearchClient)
        with self.assertRaises(AssertionError) as _:
            SearchClient.create('', '')

    def test_create_with_config(self):
        config = SearchConfig('foo', 'bar')

        self.assertIsInstance(
            SearchClient.create_with_config(config),
            SearchClient
        )

    def test_init_index(self):
        index = self.client.init_index('foo')

        self.assertIsInstance(index, SearchIndex)

    def test_app_id_getter(self):
        client = SearchClient.create('foo', 'bar')

        self.assertEqual(client.app_id, 'foo')

    def test_set_personalization_strategy(self):
        strategy = {
            'eventsScoring': {
                'Add to cart': {'score': 50, 'type': 'conversion'},
                'Purchase': {'score': 100, 'type': 'conversion'}
            },
            'facetsScoring': {
                'brand': {'score': 100},
                'categories': {'score': 10}
            }
        }

        self.client.set_personalization_strategy(strategy)

        self.transporter.write.assert_called_once_with(
            'POST',
            '1/recommendation/personalization/strategy',
            strategy,
            None,
        )
Ejemplo n.º 4
0
    def replace_all_objects_async(  # type: ignore
            self,
            objects,
            request_options=None,
    ):
        # type: (Union[List[dict], Iterator[dict]], Optional[Union[dict, RequestOptions]]) -> MultipleResponse # noqa: E501

        safe = False
        if isinstance(request_options, dict) and "safe" in request_options:
            safe = request_options.pop("safe")

        tmp_index_name = self._create_temporary_name()
        responses = MultipleResponse()
        response = yield from self.copy_to_async(  # type: ignore
            tmp_index_name, {"scope": ["settings", "synonyms", "rules"]})
        responses.push(response)

        if safe:
            responses.wait()

        tmp_client = SearchClient(self._transporter, self._config)
        tmp_index = SearchIndexAsync(
            tmp_client.init_index(tmp_index_name),
            self._transporter_async,
            self._config,
            tmp_index_name,
        )

        response = yield from tmp_index.save_objects_async(  # type: ignore
            objects, request_options)
        responses.push(response)

        if safe:
            responses.wait()

        response = yield from tmp_index.move_to_async(  # type: ignore
            self._name)
        responses.push(response)

        if safe:
            responses.wait()

        return responses
class TestSearchClient(unittest.TestCase):
    def setUp(self):
        self.config = SearchConfig('foo', 'bar')
        self.transporter = Transporter(Requester(), self.config)
        self.transporter.read = mock.Mock(name="read")
        self.transporter.read.return_value = {}
        self.transporter.write = mock.Mock(name="write")
        self.transporter.write.return_value = {}

        self.client = SearchClient(self.transporter, self.config)

    def test_create(self):
        self.assertIsInstance(self.client, SearchClient)
        with self.assertRaises(AssertionError) as _:
            SearchClient.create('', '')

    def test_create_with_config(self):
        config = SearchConfig('foo', 'bar')

        self.assertIsInstance(SearchClient.create_with_config(config),
                              SearchClient)

    def test_init_index(self):
        index = self.client.init_index('foo')

        self.assertIsInstance(index, SearchIndex)

    def test_app_id_getter(self):
        client = SearchClient.create('foo', 'bar')

        self.assertEqual(client.app_id, 'foo')

    def test_set_personalization_strategy(self):
        strategy = {
            'eventsScoring': {
                'Add to cart': {
                    'score': 50,
                    'type': 'conversion'
                },
                'Purchase': {
                    'score': 100,
                    'type': 'conversion'
                }
            },
            'facetsScoring': {
                'brand': {
                    'score': 100
                },
                'categories': {
                    'score': 10
                }
            }
        }

        self.client.set_personalization_strategy(strategy)

        self.transporter.write.assert_called_once_with(
            'POST',
            '1/recommendation/personalization/strategy',
            strategy,
            None,
        )