Exemplo n.º 1
0
    def test_index_and_unindex(self):
        instance = self._create_test_data()
        search_index = search_api.Index(name='test_index')

        search.index_entity(instance, index='test_index')

        # test indexing with custom converters
        converters = {
            ndb.StringProperty: mock.Mock(side_effect=lambda n, v: search_api.TextField(name=n, value=v))
        }
        search.index_entity(instance, index='test_index', extra_converters=converters)

        assert converters[ndb.StringProperty].called

        # test indexing with callback
        callback_mock = mock.Mock()
        search.index_entity(instance, index='text_index', callback=callback_mock)
        assert callback_mock.called

        # test error handler
        with mock.patch('ferris3.search.search_api.Index.put', side_effect=ValueError()):
            self.assertRaises(ValueError, search.index_entity, instance, index='test_index')

        # Test responses and translation
        response = search_index.get_range()
        assert len(response.results) == 1

        entities = search.to_entities(response)
        assert len(entities) == 1

        # Test unindexing
        search.unindex_entity(instance, index='test_index')

        response = search_index.get_range()
        assert len(response.results) == 0
Exemplo n.º 2
0
    def test_search(self):
        instance = self._create_test_data()
        search.index_entity(instance, index='test_index')

        result = search.search('test_index', '')

        assert not result.error
        assert len(result.items) == 1

        results, error, next_page_token = search.to_entities(result)
        assert len(results) == 1

        # with sorting
        result = search.search('test_index', '', sort='string')

        assert not result.error
        assert len(result.items) == 1

        result = search.search('test_index', '', sort='-string')

        assert not result.error
        assert len(result.items) == 1

        result = search.search('test_index', '', sort=search_api.SortExpression('string'))

        assert not result.error
        assert len(result.items) == 1

        # test error handling
        with mock.patch('ferris3.search.search_api.Index.search', side_effect=search_api.Error(":(")):
            result = search.search('test_index', '', sort='-string')
            assert result.error
Exemplo n.º 3
0
    def test_search(self):
        instance = self._create_test_data()
        search.index_entity(instance, index='test_index')

        result = search.search('test_index', '')

        assert not result.error
        assert len(result.items) == 1

        results, error, next_page_token = search.to_entities(result)
        assert len(results) == 1

        # with sorting
        result = search.search('test_index', '', sort='string')

        assert not result.error
        assert len(result.items) == 1

        result = search.search('test_index', '', sort='-string')

        assert not result.error
        assert len(result.items) == 1

        result = search.search('test_index',
                               '',
                               sort=search_api.SortExpression('string'))

        assert not result.error
        assert len(result.items) == 1

        # test error handling
        with mock.patch('ferris3.search.search_api.Index.search',
                        side_effect=search_api.Error(":(")):
            result = search.search('test_index', '', sort='-string')
            assert result.error
Exemplo n.º 4
0
    def test_index_and_unindex(self):
        instance = self._create_test_data()
        search_index = search_api.Index(name='test_index')

        search.index_entity(instance, index='test_index')

        # test indexing with custom converters
        converters = {
            ndb.StringProperty:
            mock.Mock(
                side_effect=lambda n, v: search_api.TextField(name=n, value=v))
        }
        search.index_entity(instance,
                            index='test_index',
                            extra_converters=converters)

        assert converters[ndb.StringProperty].called

        # test indexing with callback
        callback_mock = mock.Mock()
        search.index_entity(instance,
                            index='text_index',
                            callback=callback_mock)
        assert callback_mock.called

        # test error handler
        with mock.patch('ferris3.search.search_api.Index.put',
                        side_effect=ValueError()):
            self.assertRaises(ValueError,
                              search.index_entity,
                              instance,
                              index='test_index')

        # Test responses and translation
        response = search_index.get_range()
        assert len(response.results) == 1

        entities = search.to_entities(response)
        assert len(entities) == 1

        # Test unindexing
        search.unindex_entity(instance, index='test_index')

        response = search_index.get_range()
        assert len(response.results) == 0