def test_optimize_index(self):
        stub = IndexStub(self.channel)

        # read index_config.yaml
        with open(self.example_dir + '/index_config.yaml',
                  'r',
                  encoding='utf-8') as file_obj:
            index_config_dict = yaml.safe_load(file_obj.read())

        # create index
        request = CreateIndexRequest()
        request.index_name = 'test_index'
        request.index_config = pickle.dumps(index_config_dict)
        request.sync = True
        response = stub.CreateIndex(request)
        self.assertEqual(True, response.status.success)

        # get index
        request = GetIndexRequest()
        request.index_name = 'test_index'
        response = stub.GetIndex(request)
        self.assertEqual(True, response.status.success)
        self.assertEqual('test_index', response.index_stats.name)

        # optimize index
        request = OptimizeIndexRequest()
        request.index_name = 'test_index'
        request.sync = True
        response = stub.CloseIndex(request)
        self.assertEqual(True, response.status.success)
    def test_get_document(self):
        stub = IndexStub(self.channel)

        # read index_config.yaml
        with open(self.example_dir + '/index_config.yaml',
                  'r',
                  encoding='utf-8') as file_obj:
            index_config_dict = yaml.safe_load(file_obj.read())

        # create index
        request = CreateIndexRequest()
        request.index_name = 'test_index'
        request.index_config = pickle.dumps(index_config_dict)
        request.sync = True
        response = stub.CreateIndex(request)
        self.assertEqual(True, response.status.success)

        # get index
        request = GetIndexRequest()
        request.index_name = 'test_index'
        response = stub.GetIndex(request)
        self.assertEqual(True, response.status.success)
        self.assertEqual('test_index', response.index_stats.name)

        # read doc1.yaml
        with open(self.example_dir + '/doc1.yaml', 'r',
                  encoding='utf-8') as file_obj:
            fields_dict = yaml.safe_load(file_obj.read())

        # put document
        request = PutDocumentRequest()
        request.index_name = 'test_index'
        request.doc_id = '1'
        request.fields = pickle.dumps(fields_dict)
        request.sync = True
        response = stub.PutDocument(request)
        self.assertEqual(1, response.count)
        self.assertEqual(True, response.status.success)

        # commit
        request = CommitIndexRequest()
        request.index_name = 'test_index'
        request.sync = True
        response = stub.CommitIndex(request)
        self.assertEqual(True, response.status.success)

        # get document
        request = GetDocumentRequest()
        request.index_name = 'test_index'
        request.doc_id = '1'
        response = stub.GetDocument(request)

        self.assertEqual(True, response.status.success)
        self.assertEqual('1', pickle.loads(response.fields)['id'])
        self.assertEqual('Search engine (computing)',
                         pickle.loads(response.fields)['title'])
    def test_search_documents(self):
        stub = IndexStub(self.channel)

        # read index_config.yaml
        with open(self.example_dir + '/index_config.yaml',
                  'r',
                  encoding='utf-8') as file_obj:
            index_config_dict = yaml.safe_load(file_obj.read())

        # create index
        request = CreateIndexRequest()
        request.index_name = 'test_index'
        request.index_config = pickle.dumps(index_config_dict)
        request.sync = True
        response = stub.CreateIndex(request)
        self.assertEqual(True, response.status.success)

        # get index
        request = GetIndexRequest()
        request.index_name = 'test_index'
        response = stub.GetIndex(request)
        self.assertEqual(True, response.status.success)
        self.assertEqual('test_index', response.index_stats.name)

        # read bulk_put.yaml
        with open(self.example_dir + '/bulk_put.yaml', 'r',
                  encoding='utf-8') as file_obj:
            docs_dict = yaml.safe_load(file_obj.read())

        # put documents
        request = PutDocumentsRequest()
        request.index_name = 'test_index'
        request.docs = pickle.dumps(docs_dict)
        request.sync = True
        response = stub.PutDocuments(request)
        self.assertEqual(5, response.count)
        self.assertEqual(True, response.status.success)

        # commit
        request = CommitIndexRequest()
        request.index_name = 'test_index'
        request.sync = True
        response = stub.CommitIndex(request)
        self.assertEqual(True, response.status.success)

        # read weighting.yaml
        with open(self.example_dir + '/weighting.yaml', 'r',
                  encoding='utf-8') as file_obj:
            weighting_dict = yaml.safe_load(file_obj.read())

        # search documents
        request = SearchDocumentsRequest()
        request.index_name = 'test_index'
        request.query = 'search'
        request.search_field = 'text'
        request.page_num = 1
        request.page_len = 10
        request.weighting = pickle.dumps(weighting_dict)
        response = stub.SearchDocuments(request)
        self.assertEqual(5, pickle.loads(response.results)['total'])
        self.assertEqual(True, response.status.success)