Browse Source

Add constants

tumao 3 years ago
parent
commit
bde794e899

+ 69 - 2
express/src/__mocks__/milvus/milvusClient.ts

@@ -1,4 +1,12 @@
-import { FlushReq } from '@zilliz/milvus2-sdk-node/dist/milvus/types';
+import {
+  CreateCollectionReq,
+  DescribeCollectionReq,
+  DropCollectionReq,
+  FlushReq,
+  LoadCollectionReq,
+  ShowCollectionsReq
+} from '@zilliz/milvus2-sdk-node/dist/milvus/types';
+import { CodeEnum, ERR_NO_ADDRESS, ERR_NO_COLLECTION, ERR_NO_PARAM } from '../../__tests__/utils/constants';
 
 const mockMilvusClient = jest.fn().mockImplementation((address: string) => {
   return {
@@ -6,10 +14,69 @@ const mockMilvusClient = jest.fn().mockImplementation((address: string) => {
       hasCollection: (param: { collection_name: string }) => {
         const { collection_name } = param;
         if (address === '') {
-          throw new Error('no address');
+          throw new Error(ERR_NO_ADDRESS);
         }
         return collection_name;
       },
+      showCollections: (param?: ShowCollectionsReq) => {
+        if (!param) {
+          return {
+            status: {
+              error_code: CodeEnum.error,
+              reason: ERR_NO_PARAM,
+            },
+          };
+        }
+        const { collection_names } = param;
+        return {
+          status: { error_code: CodeEnum.success },
+          data: collection_names,
+        };
+      },
+      createCollection: (param: CreateCollectionReq) => {
+        const { collection_name, fields } = param;
+        if (!collection_name) {
+          return {
+            error_code: CodeEnum.error,
+            reason: ERR_NO_COLLECTION,
+          };
+        }
+        return { error_code: CodeEnum.success, data: fields };
+      },
+      describeCollection: (param: DescribeCollectionReq) => {
+        const { collection_name } = param;
+        if (!collection_name) {
+          return {
+            status: {
+              error_code: CodeEnum.error,
+              reason: ERR_NO_COLLECTION,
+            },
+          };
+        }
+        return {
+          status: { error_code: CodeEnum.success },
+          data: collection_name,
+        };
+      },
+      dropCollection: (param: DropCollectionReq) => {
+        const { collection_name } = param;
+        if (!collection_name) {
+          return {
+            error_code: CodeEnum.error,
+            reason: ERR_NO_COLLECTION,
+          };
+        }
+        return { error_code: CodeEnum.success, data: collection_name };
+      },
+      loadCollection: (param: LoadCollectionReq) => {
+        const {collection_name} = param
+        if (!collection_name) {
+          return {
+            error_code: CodeEnum.error,
+            reason: ERR_NO_COLLECTION
+          }
+        }
+      }
     },
     partitionManager: {},
     indexManager: {},

+ 92 - 0
express/src/__tests__/collections/collections.service.test.ts

@@ -0,0 +1,92 @@
+
+
+import mockMilvusClient from '../../__mocks__/milvus/milvusClient';
+import { CollectionsService } from '../../collections/collections.service';
+import { MilvusService } from '../../milvus/milvus.service';
+import { ERR_NO_COLLECTION, ERR_NO_PARAM, mockAddress } from '../utils/constants';
+
+
+// mock Milvus client
+jest.mock('@zilliz/milvus2-sdk-node', () => {
+  return {
+    MilvusClient: mockMilvusClient,
+  };
+});
+
+describe('Test collections service', () => {
+  let milvusService: any;
+  let service: any;
+
+  beforeEach(async () => {
+    // setup Milvus service and connect to mock Milvus client
+    milvusService = new MilvusService();
+    await milvusService.connectMilvus(mockAddress);
+    service = new CollectionsService(milvusService);
+  });
+
+  afterEach(() => {
+    milvusService = null;
+    service = null;
+  });
+
+  test('test managers after connected to Milvus', () => {
+    expect(service.collectionManager).toBeDefined();
+    expect(service.dataManager).toBeDefined();
+    expect(service.indexManager).toBeDefined();
+  });
+
+  test('test getCollections method', async () => {
+    const res = await service.getCollections({
+      collection_names: ['c1', 'c2'],
+    });
+    expect(res.data.length).toBe(2);
+
+    try {
+      await service.getCollections();
+    } catch (err) {
+      expect(err).toBe(ERR_NO_PARAM);
+    }
+  });
+
+  test('test createCollection method', async () => {
+    const res = await service.createCollection({
+      collection_name: 'c1',
+      fields: [],
+    });
+    expect(res.data.length).toBe(0);
+
+    try {
+      await service.createCollection({ collection_name: '', fields: [] });
+    } catch (err) {
+      expect(err).toBe(ERR_NO_COLLECTION);
+    }
+  });
+
+  test('test describeCollection method', async () => {
+    const res = await service.describeCollection({
+      collection_name: 'c1',
+    });
+    expect(res.data).toBe('c1');
+
+    try {
+      await service.describeCollection({ collection_name: '' });
+    } catch (err) {
+      expect(err).toBe(ERR_NO_COLLECTION);
+    }
+  });
+
+  test('test dropCollection method', async () => {
+    const res = await service.dropCollection({ collection_name: 'c1' });
+    expect(res.data).toBe('c1');
+
+    try {
+      await service.dropCollection({ collection_name: '' });
+    } catch (err) {
+      expect(err).toBe(ERR_NO_COLLECTION);
+    }
+  });
+
+  test('test loadCollection method', async () => {
+    
+  })
+});

+ 1 - 2
express/src/__tests__/milvus/milvus.service.test.ts

@@ -1,8 +1,7 @@
 import mockMilvusClient from '../../__mocks__/milvus/milvusClient';
 import { MilvusService } from '../../milvus/milvus.service';
+import { mockAddress } from '../utils/constants';
 
-// variables
-const mockAddress = '127.0.0.1';
 
 // mock Milvus client
 jest.mock('@zilliz/milvus2-sdk-node', () => {

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

@@ -0,0 +1,12 @@
+export enum CodeEnum {
+  success = 'Success',
+  error = 'Error'
+}
+
+// error msgs
+export const ERR_NO_COLLECTION = 'collection name is invalid'
+export const ERR_NO_ADDRESS = 'no address'
+export const ERR_NO_PARAM = 'no valid param'
+
+// mock data
+export const mockAddress = '127.0.0.1'

+ 7 - 7
express/src/collections/collections.service.ts

@@ -1,4 +1,4 @@
-import { MilvusService } from "../milvus/milvus.service";
+import { MilvusService } from '../milvus/milvus.service';
 import {
   CreateCollectionReq,
   DescribeCollectionReq,
@@ -9,17 +9,17 @@ import {
   LoadCollectionReq,
   ReleaseLoadCollectionReq,
   SearchReq,
-} from "@zilliz/milvus2-sdk-node/dist/milvus/types";
-import { throwErrorFromSDK } from "../utils/Error";
-import { findKeyValue } from "../utils/Helper";
-import { ROW_COUNT } from "../utils/Const";
+} from '@zilliz/milvus2-sdk-node/dist/milvus/types';
+import { throwErrorFromSDK } from '../utils/Error';
+import { findKeyValue } from '../utils/Helper';
+import { ROW_COUNT } from '../utils/Const';
 import {
   AlterAliasReq,
   CreateAliasReq,
   DropAliasReq,
   ShowCollectionsReq,
   ShowCollectionsType,
-} from "@zilliz/milvus2-sdk-node/dist/milvus/types/Collection";
+} from '@zilliz/milvus2-sdk-node/dist/milvus/types/Collection';
 
 export class CollectionsService {
   constructor(private milvusService: MilvusService) {}
@@ -155,7 +155,7 @@ export class CollectionsService {
         );
 
         const loadedPercentage = !loadCollection
-          ? "-1"
+          ? '-1'
           : loadCollection.loadedPercentage;
 
         data.push({