Browse Source

Fix milvus service tests

Signed-off-by: tumao <yan.wang@zilliz.com>
tumao 3 years ago
parent
commit
a1182ae3ad

+ 8 - 0
express/src/__tests__/__mocks__/consts.ts

@@ -1,3 +1,11 @@
+import LRUCache from 'lru-cache';
+import { EXPIRED_TIME } from '../../utils/Const';
+
+export const insightCacheForTest = new LRUCache({
+  maxAge: EXPIRED_TIME,
+  updateAgeOnGet: true,
+});
+
 // mock data
 export const mockAddress = '127.0.0.1';
 export const mockCollectionNames = [{ name: 'c1' }, { name: 'c2' }];

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

@@ -7,6 +7,7 @@ import {
   ERR_NO_PARAM,
 } from '../utils/constants';
 import {
+  insightCacheForTest,
   mockAddress,
   mockCollectionNames,
   mockCollections,
@@ -14,6 +15,7 @@ import {
   mockLoadedCollections,
   mockLoadedCollectionsData,
 } from '../__mocks__/consts';
+import { MilvusClient } from '@zilliz/milvus2-sdk-node/dist/milvus';
 
 // mock Milvus client
 jest.mock('@zilliz/milvus2-sdk-node', () => {
@@ -29,7 +31,10 @@ describe('Test collections service', () => {
   beforeAll(async () => {
     // setup Milvus service and connect to mock Milvus client
     milvusService = new MilvusService();
-    await milvusService.connectMilvus(mockAddress);
+    MilvusService.activeAddress = mockAddress;
+    MilvusService.activeMilvusClient = new MilvusClient(mockAddress);
+
+    await milvusService.connectMilvus(mockAddress, insightCacheForTest);
     service = new CollectionsService(milvusService);
   });
 

+ 7 - 2
express/src/__tests__/crons/crons.service.test.ts

@@ -4,7 +4,8 @@ import { CollectionsService } from '../../collections/collections.service';
 import { CronsService, SchedulerRegistry } from '../../crons/crons.service';
 import { MilvusService } from '../../milvus/milvus.service';
 import { WS_EVENTS, WS_EVENTS_TYPE } from '../../utils/Const';
-import { mockAddress } from '../__mocks__/consts';
+import { insightCacheForTest, mockAddress } from '../__mocks__/consts';
+import { MilvusClient } from '@zilliz/milvus2-sdk-node/dist/milvus';
 
 // mock Milvus client
 jest.mock('@zilliz/milvus2-sdk-node', () => {
@@ -41,7 +42,11 @@ describe('test crons service', () => {
 
   const setup = async () => {
     milvusService = new MilvusService();
-    await milvusService.connectMilvus(mockAddress);
+    MilvusService.activeAddress = mockAddress;
+    MilvusService.activeMilvusClient = new MilvusClient(mockAddress);
+
+    await milvusService.connectMilvus(mockAddress, insightCacheForTest);
+
     collectionService = new CollectionsService(milvusService);
 
     schedulerRegistry = new SchedulerRegistry([]);

+ 12 - 9
express/src/__tests__/milvus/milvus.service.test.ts

@@ -1,6 +1,7 @@
 import mockMilvusClient from '../__mocks__/milvus/milvusClient';
 import { MilvusService } from '../../milvus/milvus.service';
-import { mockAddress } from '../__mocks__/consts';
+import { insightCacheForTest, mockAddress } from '../__mocks__/consts';
+import { MilvusClient } from '@zilliz/milvus2-sdk-node/dist/milvus';
 
 // mock Milvus client
 jest.mock('@zilliz/milvus2-sdk-node', () => {
@@ -15,6 +16,8 @@ describe('Test Milvus service', () => {
   // init service
   beforeEach(() => {
     service = new MilvusService();
+    MilvusService.activeAddress = mockAddress;
+    MilvusService.activeMilvusClient = new MilvusClient(mockAddress);
   });
 
   afterEach(() => {
@@ -22,13 +25,13 @@ describe('Test Milvus service', () => {
   });
 
   test('test connectMilvus method', async () => {
-    const res = await service.connectMilvus(mockAddress);
+    const res = await service.connectMilvus(mockAddress, insightCacheForTest);
     expect(res.address).toBe(mockAddress);
   });
 
   test('test connectMilvus method error', async () => {
     try {
-      await service.connectMilvus('');
+      await service.connectMilvus('', insightCacheForTest);
     } catch (err) {
       expect(err.message).toBe(
         'Connect milvus failed, check your milvus address.'
@@ -46,16 +49,16 @@ describe('Test Milvus service', () => {
 
   test('test checkConnect method', async () => {
     // mock connect first
-    await service.connectMilvus(mockAddress);
+    await service.connectMilvus(mockAddress, insightCacheForTest);
     // different address
-    const errorRes = await service.checkConnect('123');
+    const errorRes = await service.checkConnect('123', insightCacheForTest);
     expect(errorRes.connected).toBeFalsy();
-    const res = await service.checkConnect(mockAddress);
+    const res = await service.checkConnect(mockAddress, insightCacheForTest);
     expect(res.connected).toBeTruthy();
   });
 
   test('test managers after connected', async () => {
-    await service.connectMilvus(mockAddress);
+    await service.connectMilvus(mockAddress, insightCacheForTest);
     expect(service.collectionManager).toBeDefined();
     expect(service.partitionManager).toBeDefined();
     expect(service.indexManager).toBeDefined();
@@ -63,14 +66,14 @@ describe('Test Milvus service', () => {
   });
 
   test('test flush method', async () => {
-    await service.connectMilvus(mockAddress);
+    await service.connectMilvus(mockAddress, insightCacheForTest);
     const res = await service.flush({ collection_names: ['c1', 'c2'] });
     const data = res.data.collection_names;
     expect(data.length).toBe(2);
   });
 
   test('test getMetrics method', async () => {
-    await service.connectMilvus(mockAddress);
+    await service.connectMilvus(mockAddress, insightCacheForTest);
     const res = await service.getMetrics();
     expect(res.type).toBe('system_info');
   });

+ 6 - 1
express/src/__tests__/partitions/partitions.service.test.ts

@@ -3,10 +3,12 @@ import { MilvusService } from '../../milvus/milvus.service';
 import { ERR_NO_COLLECTION } from '../utils/constants';
 import { PartitionsService } from '../../partitions/partitions.service';
 import {
+  insightCacheForTest,
   mockAddress,
   mockGetPartitionsInfoData,
   mockPartition,
 } from '../__mocks__/consts';
+import { MilvusClient } from '@zilliz/milvus2-sdk-node/dist/milvus';
 
 // mock Milvus client
 jest.mock('@zilliz/milvus2-sdk-node', () => {
@@ -22,7 +24,10 @@ describe('Test partitions service', () => {
   beforeAll(async () => {
     // setup Milvus service and connect to mock Milvus client
     milvusService = new MilvusService();
-    await milvusService.connectMilvus(mockAddress);
+    MilvusService.activeAddress = mockAddress;
+    MilvusService.activeMilvusClient = new MilvusClient(mockAddress);
+
+    await milvusService.connectMilvus(mockAddress, insightCacheForTest);
     service = new PartitionsService(milvusService);
   });
 

+ 6 - 2
express/src/__tests__/schema/schema.service.test.ts

@@ -2,7 +2,8 @@ import mockMilvusClient from '../__mocks__/milvus/milvusClient';
 import { MilvusService } from '../../milvus/milvus.service';
 import { CodeEnum, ERR_NO_COLLECTION } from '../utils/constants';
 import { SchemaService } from '../../schema/schema.service';
-import { mockAddress } from '../__mocks__/consts';
+import { insightCacheForTest, mockAddress } from '../__mocks__/consts';
+import { MilvusClient } from '@zilliz/milvus2-sdk-node/dist/milvus';
 
 // mock Milvus client
 jest.mock('@zilliz/milvus2-sdk-node', () => {
@@ -18,7 +19,10 @@ describe('Test schema service', () => {
   beforeAll(async () => {
     // setup Milvus service and connect to mock Milvus client
     milvusService = new MilvusService();
-    await milvusService.connectMilvus(mockAddress);
+    MilvusService.activeAddress = mockAddress;
+    MilvusService.activeMilvusClient = new MilvusClient(mockAddress);
+
+    await milvusService.connectMilvus(mockAddress, insightCacheForTest);
     service = new SchemaService(milvusService);
   });