Browse Source

Add schema service tests

tumao 3 years ago
parent
commit
67546c1589

+ 58 - 1
express/src/__mocks__/milvus/milvusClient.ts

@@ -2,13 +2,17 @@ import {
   AlterAliasReq,
   CreateAliasReq,
   CreateCollectionReq,
+  CreateIndexReq,
   CreatePartitionReq,
   DescribeCollectionReq,
+  DescribeIndexReq,
   DropAliasReq,
   DropCollectionReq,
+  DropIndexReq,
   DropPartitionReq,
   FlushReq,
   GetCollectionStatisticsReq,
+  GetIndexBuildProgressReq,
   GetIndexStateReq,
   GetPartitionStatisticsReq,
   InsertReq,
@@ -26,6 +30,7 @@ import {
   ERR_NO_ADDRESS,
   ERR_NO_ALIAS,
   ERR_NO_COLLECTION,
+  ERR_NO_INDEX,
   ERR_NO_PARAM,
   mockCollectionNames,
   mockCollections,
@@ -217,14 +222,66 @@ const mockMilvusClient = jest.fn().mockImplementation((address: string) => {
     indexManager: {
       getIndexState: (param: GetIndexStateReq) => {
         const { collection_name } = param;
+        if (!collection_name) {
+          return { status: mockStatusInfo(CodeEnum.error, ERR_NO_COLLECTION) };
+        }
         const data =
           mockIndexState.find((i) => i.collection_name === collection_name) ||
           {};
         return {
-          ...mockStatusInfo(CodeEnum.success),
+          status: mockStatusInfo(CodeEnum.success),
           ...data,
         };
       },
+      createIndex: (param: CreateIndexReq) => {
+        const { collection_name } = param;
+        if (!collection_name) {
+          return mockStatusInfo(CodeEnum.error, ERR_NO_COLLECTION);
+        }
+
+        return {
+          ...mockStatusInfo(CodeEnum.success),
+          data: param,
+        };
+      },
+      describeIndex: (param: DescribeIndexReq) => {
+        const { collection_name, field_name } = param;
+        if (!collection_name) {
+          return { status: mockStatusInfo(CodeEnum.error, ERR_NO_COLLECTION) };
+        }
+        if (!field_name) {
+          return {
+            status: mockStatusInfo(CodeEnum.indexNoExist, ERR_NO_INDEX),
+          };
+        }
+
+        return {
+          status: mockStatusInfo(CodeEnum.success),
+          data: param,
+        };
+      },
+      dropIndex: (param: DropIndexReq) => {
+        const { collection_name } = param;
+        if (!collection_name) {
+          return mockStatusInfo(CodeEnum.error, ERR_NO_COLLECTION);
+        }
+
+        return {
+          ...mockStatusInfo(CodeEnum.success),
+          data: param,
+        };
+      },
+      getIndexBuildProgress: (param: GetIndexBuildProgressReq) => {
+        const { collection_name } = param;
+        if (!collection_name) {
+          return { status: mockStatusInfo(CodeEnum.error, ERR_NO_COLLECTION) };
+        }
+
+        return {
+          status: mockStatusInfo(CodeEnum.success),
+          data: param,
+        };
+      },
     },
     dataManager: {
       flush: (data: FlushReq) => ({

+ 1 - 1
express/src/__tests__/collections/collections.service.test.ts

@@ -244,7 +244,7 @@ describe('Test collections service', () => {
 
   test('test getIndexStatus method', async () => {
     const res = await service.getIndexStatus({ collection_name: 'c1' });
-    const { error_code, reason, ...data } = res;
+    const { status, ...data } = res;
     expect(data).toEqual({ collection_name: 'c1', state: 3 });
   });
 

+ 113 - 0
express/src/__tests__/schema/schema.service.test.ts

@@ -0,0 +1,113 @@
+import mockMilvusClient from '../../__mocks__/milvus/milvusClient';
+import { MilvusService } from '../../milvus/milvus.service';
+import { CodeEnum, ERR_NO_COLLECTION, mockAddress } from '../utils/constants';
+import { SchemaService } from '../../schema/schema.service';
+
+// mock Milvus client
+jest.mock('@zilliz/milvus2-sdk-node', () => {
+  return {
+    MilvusClient: mockMilvusClient,
+  };
+});
+
+describe('Test schema service', () => {
+  let milvusService: any;
+  let service: any;
+
+  beforeAll(async () => {
+    // setup Milvus service and connect to mock Milvus client
+    milvusService = new MilvusService();
+    await milvusService.connectMilvus(mockAddress);
+    service = new SchemaService(milvusService);
+  });
+
+  afterAll(() => {
+    milvusService = null;
+    service = null;
+  });
+
+  test('test manager after connected to Milvus', () => {
+    expect(service.indexManager).toBeDefined();
+  });
+
+  test('test createIndex method', async () => {
+    const mockParam = {
+      collection_name: 'c1',
+      field_name: 'vector_field',
+      extra_params: {
+        index_type: 'BIN_FLAT',
+        metric_type: 'HAMMING',
+        params: JSON.stringify({ nlist: 1024 }),
+      },
+    };
+    const res = await service.createIndex(mockParam);
+    expect(res.data).toEqual(mockParam);
+
+    try {
+      await service.createIndex({ ...mockParam, collection_name: '' });
+    } catch (err) {
+      expect(err).toBe(ERR_NO_COLLECTION);
+    }
+  });
+
+  test('test describeIndex method', async () => {
+    const res = await service.describeIndex({
+      collection_name: 'c1',
+      field_name: 'f1',
+    });
+    expect(res.data).toEqual({ collection_name: 'c1', field_name: 'f1' });
+
+    const noExistRes = await service.describeIndex({ collection_name: 'c1' });
+    expect(noExistRes.status.error_code).toBe(CodeEnum.indexNoExist);
+
+    try {
+      await service.describeIndex({ collection_name: '' });
+    } catch (err) {
+      expect(err).toBe(ERR_NO_COLLECTION);
+    }
+  });
+
+  test('test dropIndex method', async () => {
+    const res = await service.dropIndex({
+      collection_name: 'c1',
+    });
+    expect(res.data).toEqual({ collection_name: 'c1' });
+
+    try {
+      await service.dropIndex({ collection_name: '' });
+    } catch (err) {
+      expect(err).toBe(ERR_NO_COLLECTION);
+    }
+  });
+
+  test('test getIndexState method', async () => {
+    const res = await service.getIndexState({ collection_name: 'c1' });
+    const { status, ...data } = res;
+    expect(data).toEqual({ collection_name: 'c1', state: 3 });
+
+    try {
+      await service.getIndexState({ collection_name: '' });
+    } catch (err) {
+      expect(err).toBe(ERR_NO_COLLECTION);
+    }
+  });
+
+  test('test getIndexBuildProgress method', async () => {
+    const mockParam = {
+      collection_name: 'c1',
+      field_name: 'f1',
+      index_name: 'i1',
+    };
+    const res = await service.getIndexBuildProgress(mockParam);
+    expect(res.data).toEqual(mockParam);
+
+    try {
+      await service.getIndexBuildProgress({
+        ...mockParam,
+        collection_name: '',
+      });
+    } catch (err) {
+      expect(err).toBe(ERR_NO_COLLECTION);
+    }
+  });
+});

+ 2 - 0
express/src/__tests__/utils/constants.ts

@@ -1,6 +1,7 @@
 export enum CodeEnum {
   success = 'Success',
   error = 'Error',
+  indexNoExist = 'IndexNotExist',
 }
 
 export type CodeStatus = {
@@ -13,6 +14,7 @@ export const ERR_NO_COLLECTION = 'collection name is invalid';
 export const ERR_NO_ADDRESS = 'no address';
 export const ERR_NO_PARAM = 'no valid param';
 export const ERR_NO_ALIAS = 'no valid alias';
+export const ERR_NO_INDEX = 'index not exist';
 
 // mock data
 export const mockAddress = '127.0.0.1';