schema.service.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import mockMilvusClient from '../__mocks__/milvus/milvusClient';
  2. import { MilvusService } from '../../milvus/milvus.service';
  3. import { CodeEnum, ERR_NO_COLLECTION } from '../utils/constants';
  4. import { SchemaService } from '../../schema/schema.service';
  5. import { insightCacheForTest, mockAddress } from '../__mocks__/consts';
  6. import { MilvusClient } from '@zilliz/milvus2-sdk-node/dist/milvus';
  7. // mock Milvus client
  8. jest.mock('@zilliz/milvus2-sdk-node', () => {
  9. return {
  10. MilvusClient: mockMilvusClient,
  11. };
  12. });
  13. describe('Test schema service', () => {
  14. let milvusService: any;
  15. let service: any;
  16. beforeAll(async () => {
  17. // setup Milvus service and connect to mock Milvus client
  18. milvusService = new MilvusService();
  19. MilvusService.activeAddress = mockAddress;
  20. MilvusService.activeMilvusClient = new MilvusClient(mockAddress);
  21. await milvusService.connectMilvus(mockAddress, insightCacheForTest);
  22. service = new SchemaService(milvusService);
  23. });
  24. afterAll(() => {
  25. milvusService = null;
  26. service = null;
  27. });
  28. test('test manager after connected to Milvus', () => {
  29. expect(service.indexManager).toBeDefined();
  30. });
  31. test('test createIndex method', async () => {
  32. const mockParam = {
  33. collection_name: 'c1',
  34. field_name: 'vector_field',
  35. extra_params: {
  36. index_type: 'BIN_FLAT',
  37. metric_type: 'HAMMING',
  38. params: JSON.stringify({ nlist: 1024 }),
  39. },
  40. };
  41. const res = await service.createIndex(mockParam);
  42. expect(res.data).toEqual(mockParam);
  43. try {
  44. await service.createIndex({ ...mockParam, collection_name: '' });
  45. } catch (err) {
  46. expect(err).toBe(ERR_NO_COLLECTION);
  47. }
  48. });
  49. test('test describeIndex method', async () => {
  50. const res = await service.describeIndex({
  51. collection_name: 'c1',
  52. field_name: 'f1',
  53. });
  54. expect(res.data).toEqual({ collection_name: 'c1', field_name: 'f1' });
  55. const noExistRes = await service.describeIndex({ collection_name: 'c1' });
  56. expect(noExistRes.status.error_code).toBe(CodeEnum.indexNoExist);
  57. try {
  58. await service.describeIndex({ collection_name: '' });
  59. } catch (err) {
  60. expect(err).toBe(ERR_NO_COLLECTION);
  61. }
  62. });
  63. test('test dropIndex method', async () => {
  64. const res = await service.dropIndex({
  65. collection_name: 'c1',
  66. });
  67. expect(res.data).toEqual({ collection_name: 'c1' });
  68. try {
  69. await service.dropIndex({ collection_name: '' });
  70. } catch (err) {
  71. expect(err).toBe(ERR_NO_COLLECTION);
  72. }
  73. });
  74. test('test getIndexState method', async () => {
  75. const res = await service.getIndexState({ collection_name: 'c1' });
  76. const { status, ...data } = res;
  77. expect(data).toEqual({ collection_name: 'c1', state: 3 });
  78. try {
  79. await service.getIndexState({ collection_name: '' });
  80. } catch (err) {
  81. expect(err).toBe(ERR_NO_COLLECTION);
  82. }
  83. });
  84. test('test getIndexBuildProgress method', async () => {
  85. const mockParam = {
  86. collection_name: 'c1',
  87. field_name: 'f1',
  88. index_name: 'i1',
  89. };
  90. const res = await service.getIndexBuildProgress(mockParam);
  91. expect(res.data).toEqual(mockParam);
  92. try {
  93. await service.getIndexBuildProgress({
  94. ...mockParam,
  95. collection_name: '',
  96. });
  97. } catch (err) {
  98. expect(err).toBe(ERR_NO_COLLECTION);
  99. }
  100. });
  101. });