Browse Source

Add partition service tests

tumao 3 years ago
parent
commit
f642ff3e62

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

@@ -2,17 +2,23 @@ import {
   AlterAliasReq,
   CreateAliasReq,
   CreateCollectionReq,
+  CreatePartitionReq,
   DescribeCollectionReq,
   DropAliasReq,
   DropCollectionReq,
+  DropPartitionReq,
   FlushReq,
   GetCollectionStatisticsReq,
   GetIndexStateReq,
+  GetPartitionStatisticsReq,
   InsertReq,
   LoadCollectionReq,
+  LoadPartitionsReq,
   ReleaseLoadCollectionReq,
+  ReleasePartitionsReq,
   SearchReq,
   ShowCollectionsReq,
+  ShowPartitionsReq,
 } from '@zilliz/milvus2-sdk-node/dist/milvus/types';
 import { QueryDto } from '../../collections/dto';
 import {
@@ -25,6 +31,7 @@ import {
   mockCollections,
   mockIndexState,
   mockLoadedCollections,
+  mockPartition,
 } from '../../__tests__/utils/constants';
 import { mockStatusInfo } from '../../__tests__/utils/mock.util';
 
@@ -151,7 +158,62 @@ const mockMilvusClient = jest.fn().mockImplementation((address: string) => {
         };
       },
     },
-    partitionManager: {},
+    partitionManager: {
+      createPartition: (param: CreatePartitionReq) => {
+        const { collection_name, partition_name } = param;
+        if (!collection_name) {
+          return mockStatusInfo(CodeEnum.error, ERR_NO_COLLECTION);
+        }
+        return { ...mockStatusInfo(CodeEnum.success), data: partition_name };
+      },
+      dropPartition: (param: DropPartitionReq) => {
+        const { collection_name } = param;
+        if (!collection_name) {
+          return mockStatusInfo(CodeEnum.error, ERR_NO_COLLECTION);
+        }
+        return { ...mockStatusInfo(CodeEnum.success), data: param };
+      },
+      loadPartitions: (param: LoadPartitionsReq) => {
+        const { collection_name } = param;
+        if (!collection_name) {
+          return mockStatusInfo(CodeEnum.error, ERR_NO_COLLECTION);
+        }
+        return { ...mockStatusInfo(CodeEnum.success), data: param };
+      },
+      releasePartitions: (param: ReleasePartitionsReq) => {
+        const { collection_name } = param;
+        if (!collection_name) {
+          return mockStatusInfo(CodeEnum.error, ERR_NO_COLLECTION);
+        }
+        return { ...mockStatusInfo(CodeEnum.success), data: param };
+      },
+      showPartitions: (param: ShowPartitionsReq) => {
+        const { collection_name } = param;
+        if (!collection_name) {
+          return { status: mockStatusInfo(CodeEnum.error, ERR_NO_COLLECTION) };
+        }
+        return {
+          status: mockStatusInfo(CodeEnum.success),
+          ...mockPartition,
+        };
+      },
+      getPartitionStatistics: (param: GetPartitionStatisticsReq) => {
+        const { collection_name, partition_name } = param;
+        if (!collection_name) {
+          return { status: mockStatusInfo(CodeEnum.error, ERR_NO_COLLECTION) };
+        }
+
+        const data = {
+          name: partition_name,
+          stats: [{ key: 'row_count', value: 7 }],
+        };
+
+        return {
+          status: mockStatusInfo(CodeEnum.success),
+          ...data,
+        };
+      },
+    },
     indexManager: {
       getIndexState: (param: GetIndexStateReq) => {
         const { collection_name } = param;

+ 3 - 4
express/src/__tests__/collections/collections.service.test.ts

@@ -9,7 +9,6 @@ import {
   mockCollectionNames,
   mockCollections,
   mockGetAllCollectionsData,
-  mockIndexState,
   mockLoadedCollections,
   mockLoadedCollectionsData,
 } from '../utils/constants';
@@ -25,14 +24,14 @@ describe('Test collections service', () => {
   let milvusService: any;
   let service: any;
 
-  beforeEach(async () => {
+  beforeAll(async () => {
     // setup Milvus service and connect to mock Milvus client
     milvusService = new MilvusService();
     await milvusService.connectMilvus(mockAddress);
     service = new CollectionsService(milvusService);
   });
 
-  afterEach(() => {
+  afterAll(() => {
     milvusService = null;
     service = null;
   });
@@ -245,7 +244,7 @@ describe('Test collections service', () => {
 
   test('test getIndexStatus method', async () => {
     const res = await service.getIndexStatus({ collection_name: 'c1' });
-    const { error_code, ...data } = res;
+    const { error_code, reason, ...data } = res;
     expect(data).toEqual({ collection_name: 'c1', state: 3 });
   });
 

+ 144 - 0
express/src/__tests__/partitions/partitions.service.test.ts

@@ -0,0 +1,144 @@
+import mockMilvusClient from '../../__mocks__/milvus/milvusClient';
+import { MilvusService } from '../../milvus/milvus.service';
+import {
+  ERR_NO_COLLECTION,
+  mockAddress,
+  mockGetPartitionsInfoData,
+  mockPartition,
+} from '../utils/constants';
+import { PartitionsService } from '../../partitions/partitions.service';
+
+// mock Milvus client
+jest.mock('@zilliz/milvus2-sdk-node', () => {
+  return {
+    MilvusClient: mockMilvusClient,
+  };
+});
+
+describe('Test partitions 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 PartitionsService(milvusService);
+  });
+
+  afterAll(() => {
+    milvusService = null;
+    service = null;
+  });
+
+  test('test manager after connected to Milvus', () => {
+    expect(service.partitionManager).toBeDefined();
+  });
+
+  test('test createPartition method', async () => {
+    const res = await service.createPartition({
+      collection_name: 'c1',
+      partition_name: 'p1',
+    });
+    expect(res.data).toBe('p1');
+
+    try {
+      await service.createPartition({
+        collection_name: '',
+        partition_name: 'p1',
+      });
+    } catch (err) {
+      expect(err).toBe(ERR_NO_COLLECTION);
+    }
+  });
+
+  test('test deletePartition method', async () => {
+    const mockParam = {
+      collection_name: 'c1',
+      partition_name: 'p1',
+    };
+    const res = await service.deletePartition(mockParam);
+    expect(res.data).toEqual(mockParam);
+
+    try {
+      await service.deletePartition({
+        collection_name: '',
+        partition_name: '',
+      });
+    } catch (err) {
+      expect(err).toBe(ERR_NO_COLLECTION);
+    }
+  });
+
+  test('test loadPartitions method', async () => {
+    const mockParam = {
+      collection_name: 'c1',
+      partition_names: ['p1', 'p2'],
+    };
+    const res = await service.loadPartitions(mockParam);
+    expect(res.data).toEqual(mockParam);
+
+    try {
+      await service.loadPartitions({
+        collection_name: '',
+        partition_names: [],
+      });
+    } catch (err) {
+      expect(err).toBe(ERR_NO_COLLECTION);
+    }
+  });
+
+  test('test releasePartitions method', async () => {
+    const mockParam = {
+      collection_name: 'c1',
+      partition_names: ['p1', 'p2'],
+    };
+    const res = await service.releasePartitions(mockParam);
+    expect(res.data).toEqual(mockParam);
+
+    try {
+      await service.releasePartitions({
+        collection_name: '',
+        partition_names: [],
+      });
+    } catch (err) {
+      expect(err).toBe(ERR_NO_COLLECTION);
+    }
+  });
+
+  test('test getPartitions method', async () => {
+    const res = await service.getPartitions({ collection_name: 'c1' });
+    const { status, ...data } = res;
+    expect(data).toEqual(mockPartition);
+
+    try {
+      await service.getPartitions({ collection_name: '' });
+    } catch (err) {
+      expect(err).toBe(ERR_NO_COLLECTION);
+    }
+  });
+
+  test('test getPartitionStatistics method', async () => {
+    const res = await service.getPartitionStatistics({
+      collection_name: 'c1',
+      partition_name: 'p1',
+    });
+    const { status, ...data } = res;
+    expect(data.name).toBe('p1');
+    expect(data.stats.length).toBe(1);
+
+    try {
+      await service.getPartitionStatistics({
+        collection_name: '',
+        partition_name: '',
+      });
+    } catch (err) {
+      expect(err).toBe(ERR_NO_COLLECTION);
+    }
+  });
+
+  test('test getPartitionsInfo method', async () => {
+    const res = await service.getPartitionsInfo({ collection_name: 'c1' });
+    expect(res).toEqual(mockGetPartitionsInfoData);
+  });
+});

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

@@ -84,6 +84,13 @@ export const mockIndexState = [
   { collection_name: 'c2', state: 2 },
 ];
 
+export const mockPartition = {
+  partition_names: ['p1', 'p2'],
+  partitionIDs: [1, 2],
+  created_timestamps: ['12345', '12354'],
+  created_utc_timestamps: ['12345', '12354'],
+};
+
 // mock results
 export const mockGetAllCollectionsData = [
   {
@@ -157,3 +164,18 @@ export const mockLoadedCollectionsData = [
     rowCount: 7,
   },
 ];
+
+export const mockGetPartitionsInfoData = [
+  {
+    name: 'p1',
+    id: 1,
+    createdTime: '12345',
+    rowCount: 7,
+  },
+  {
+    name: 'p2',
+    id: 2,
+    createdTime: '12354',
+    rowCount: 7,
+  },
+];

+ 1 - 0
express/src/__tests__/utils/mock.util.ts

@@ -4,6 +4,7 @@ export const mockStatusInfo = (type: CodeEnum, msg?: string): CodeStatus => {
   if (type === CodeEnum.success) {
     return {
       error_code: type,
+      reason: 'success',
     };
   }
   return {

+ 16 - 16
express/src/partitions/partitions.controller.ts

@@ -1,13 +1,13 @@
-import { NextFunction, Request, Response, Router } from "express";
-import { dtoValidationMiddleware } from "../middlewares/validation";
-import { PartitionsService } from "./partitions.service";
-import { milvusService } from "../milvus";
+import { NextFunction, Request, Response, Router } from 'express';
+import { dtoValidationMiddleware } from '../middlewares/validation';
+import { PartitionsService } from './partitions.service';
+import { milvusService } from '../milvus';
 
 import {
   GetPartitionsInfoDto,
   ManagePartitionDto,
   LoadPartitionsDto,
-} from "./dto";
+} from './dto';
 
 export class PartitionController {
   private router: Router;
@@ -20,25 +20,25 @@ export class PartitionController {
 
   generateRoutes() {
     this.router.get(
-      "/",
+      '/',
       dtoValidationMiddleware(GetPartitionsInfoDto),
-      this.getPatitionsInfo.bind(this)
+      this.getPartitionsInfo.bind(this)
     );
 
     this.router.post(
-      "/",
+      '/',
       dtoValidationMiddleware(ManagePartitionDto),
       this.managePartition.bind(this)
     );
 
     this.router.post(
-      "/load",
+      '/load',
       dtoValidationMiddleware(LoadPartitionsDto),
       this.loadPartition.bind(this)
     );
 
     this.router.post(
-      "/release",
+      '/release',
       dtoValidationMiddleware(LoadPartitionsDto),
       this.releasePartition.bind(this)
     );
@@ -46,10 +46,10 @@ export class PartitionController {
     return this.router;
   }
 
-  async getPatitionsInfo(req: Request, res: Response, next: NextFunction) {
-    const collectionName = "" + req.query?.collection_name;
+  async getPartitionsInfo(req: Request, res: Response, next: NextFunction) {
+    const collectionName = '' + req.query?.collection_name;
     try {
-      const result = await this.partitionsService.getPatitionsInfo({
+      const result = await this.partitionsService.getPartitionsInfo({
         collection_name: collectionName,
       });
       res.send(result);
@@ -62,9 +62,9 @@ export class PartitionController {
     const { type, ...params } = req.body;
     try {
       const result =
-        type.toLocaleLowerCase() === "create"
-          ? await this.partitionsService.createParition(params)
-          : await this.partitionsService.deleteParition(params);
+        type.toLocaleLowerCase() === 'create'
+          ? await this.partitionsService.createPartition(params)
+          : await this.partitionsService.deletePartition(params);
       res.send(result);
     } catch (error) {
       next(error);

+ 4 - 4
express/src/partitions/partitions.service.ts

@@ -18,7 +18,7 @@ export class PartitionsService {
     return this.milvusService.partitionManager;
   }
 
-  async getPatitionsInfo(data: ShowPartitionsReq) {
+  async getPartitionsInfo(data: ShowPartitionsReq) {
     const result = [];
     const res = await this.getPartitions(data);
     if (res.partition_names && res.partition_names.length) {
@@ -44,13 +44,13 @@ export class PartitionsService {
     return res;
   }
 
-  async createParition(data: CreatePartitionReq) {
+  async createPartition(data: CreatePartitionReq) {
     const res = await this.partitionManager.createPartition(data);
     throwErrorFromSDK(res);
     return res;
   }
 
-  async deleteParition(data: DropPartitionReq) {
+  async deletePartition(data: DropPartitionReq) {
     const res = await this.partitionManager.dropPartition(data);
     throwErrorFromSDK(res);
     return res;
@@ -73,4 +73,4 @@ export class PartitionsService {
     throwErrorFromSDK(res);
     return res;
   }
-}
+}

+ 4 - 4
server/src/partitions/partitions.controller.ts

@@ -20,12 +20,12 @@ import { PartitionsService } from './partitions.service';
 @ApiTags('partitions')
 @Controller('partitions')
 export class PartitionsController {
-  constructor(private partitionsService: PartitionsService) { }
+  constructor(private partitionsService: PartitionsService) {}
 
   @Get()
   @UsePipes(new ValidationPipe())
   async getPartitions(@Query() query: GetPartitionsInfo) {
-    return await this.partitionsService.getPatitionsInfo(query);
+    return await this.partitionsService.getPartitionsInfo(query);
   }
 
   @Post()
@@ -34,8 +34,8 @@ export class PartitionsController {
     const { type, ...params } = body;
 
     return type.toLocaleLowerCase() === ManageType.CREATE
-      ? await this.partitionsService.createParition(params)
-      : await this.partitionsService.deleteParition(params);
+      ? await this.partitionsService.createPartition(params)
+      : await this.partitionsService.deletePartition(params);
   }
 
   @Put('load')

+ 3 - 3
server/src/partitions/partitions.service.ts

@@ -20,7 +20,7 @@ export class PartitionsService {
     return this.milvusService.partitionManager;
   }
 
-  async getPatitionsInfo(data: ShowPartitionsReq) {
+  async getPartitionsInfo(data: ShowPartitionsReq) {
     const result = [];
     const res = await this.getPartitions(data);
     if (res.partition_names && res.partition_names.length) {
@@ -46,13 +46,13 @@ export class PartitionsService {
     return res;
   }
 
-  async createParition(data: CreatePartitionReq) {
+  async createPartition(data: CreatePartitionReq) {
     const res = await this.partitionManager.createPartition(data);
     throwErrorFromSDK(res);
     return res;
   }
 
-  async deleteParition(data: DropPartitionReq) {
+  async deletePartition(data: DropPartitionReq) {
     const res = await this.partitionManager.dropPartition(data);
     throwErrorFromSDK(res);
     return res;