jestClient增删改查

import com.alibaba.fastjson.JSON;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.JestResult;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Bulk;
import io.searchbox.core.BulkResult;
import io.searchbox.core.Delete;
import io.searchbox.core.Index;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import net.aihelp.development.platform.util.UUIDUtils;

import java.io.IOException;
import java.io.Serializable;

@Slf4j
public class Test {

    private static JestClient jestClient=null;
    private static String TABLE_NAME="test_user";
    private static String TYPE="_doc";

    static {
        JestClientFactory factory=new JestClientFactory();
        factory.setHttpClientConfig(new HttpClientConfig.Builder("http://127.0.0.1:9200")
                .multiThreaded(true)
                .build());
        jestClient=factory.getObject();
    }

    /**
     * insert && update
     */
    public static void insert(){
        User user=new User(UUIDUtils.getUUID(),"北京",11);
        Index index = new Index.Builder(user).index(TABLE_NAME).type(TYPE).id(UUIDUtils.getUUID()).refresh(true).build();
        try {
            JestResult result = jestClient.execute(index);
            if(!result.isSucceeded()){
                String errorMessage = result.getErrorMessage();
                log.error("TranHistory save errorMessage:" + errorMessage);
            }
        } catch (Exception e) {
            log.error("TranHistory save error" + e);
        }
    }

    /**
     * batch insert && batch update
     * @throws IOException
     */
    public static void batchUpdate() throws IOException {
        Bulk.Builder bulk = new Bulk.Builder().defaultIndex(TABLE_NAME).defaultType(TYPE).refresh(true);

        String id1="9be9c6d6672f4f888675da56e072c5e4";
        User user1=new User(id1,"小放",15);
        Index index1 = new Index.Builder(JSON.toJSON(user1)).index(TABLE_NAME).type(TYPE).id(user1.getId()).refresh(true).build();
        bulk.addAction(index1);

        String id2="8b7ab9c4274841ee8d369f2b8914da6c";
        User user2=new User(id2,"小放",15);
        Index index2 = new Index.Builder(user1).index(TABLE_NAME).type(TYPE).id(user2.getId()).refresh(true).build();
        bulk.addAction(index2);

        BulkResult br = jestClient.execute(bulk.build());
        boolean succeeded = br.isSucceeded();
    }

    /**
     * delete
     * @throws IOException
     */
    public static void del() throws IOException {
        Bulk.Builder bulk = new Bulk.Builder().defaultIndex(TABLE_NAME).defaultType(TYPE).refresh(true);
        Delete dr = new Delete.Builder("9be9c6d6672f4f888675da56e072c5e4").index(TABLE_NAME).type(TYPE).id("9be9c6d6672f4f888675da56e072c5e4").refresh(true).build();
        bulk.addAction(dr);
        BulkResult br = jestClient.execute(bulk.build());
        boolean succeeded = br.isSucceeded();
    }

    @AllArgsConstructor
    @Data
    public static class User implements Serializable{

        private String id;

        private String name;

        private Integer age;
        
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,你需要安装 `@vue/cli-plugin-unit-jest` 插件,可以使用以下命令进行安装: ``` vue add unit-jest ``` 然后,在你的测试文件中引入要测试的组件或方法,以及封装好的 api 请求和配置文件。例如: ```js import { shallowMount } from '@vue/test-utils' import MyComponent from '@/components/MyComponent.vue' import { api } from '@/api' import { config } from '@/config' ``` 接下来,你可以编写增删改查的测试用例。例如: ```js describe('MyComponent', () => { it('should add a new item', async () => { const wrapper = shallowMount(MyComponent) const newItem = { name: 'New Item', description: 'This is a new item.' } await wrapper.vm.addItem(newItem) expect(wrapper.vm.items).toContainEqual(newItem) }) it('should delete an item', async () => { const wrapper = shallowMount(MyComponent) const itemToDelete = wrapper.vm.items[0] await wrapper.vm.deleteItem(itemToDelete.id) expect(wrapper.vm.items).not.toContainEqual(itemToDelete) }) it('should update an item', async () => { const wrapper = shallowMount(MyComponent) const itemToUpdate = wrapper.vm.items[0] const updatedItem = { ...itemToUpdate, name: 'Updated Item' } await wrapper.vm.updateItem(updatedItem) expect(wrapper.vm.items).toContainEqual(updatedItem) }) it('should fetch items from API', async () => { api.setConfig(config) const itemsFromApi = [{ id: 1, name: 'Item 1', description: 'This is item 1.' }] api.get = jest.fn().mockResolvedValue(itemsFromApi) const wrapper = shallowMount(MyComponent) await wrapper.vm.fetchItems() expect(wrapper.vm.items).toEqual(itemsFromApi) }) }) ``` 在这个例子中,我们编写了四个测试用例,分别测试了增加、删除、更新和从 API 获取数据的功能。我们使用了 `shallowMount` 方法来创建一个组件实例,并且调用了组件实例中的相应方法来测试各种功能。我们还在测试用例中使用了 `api` 和 `config` 对象,这些对象在其他文件中进行了封装,可以通过 `import` 引入。 最后,你可以使用以下命令运行测试: ``` npm run test:unit ``` 如果所有测试用例都通过,你就可以放心地将代码部署到生产环境中了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值