collections.service.test.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. import mockMilvusClient from '../__mocks__/milvus/milvusClient';
  2. import { CollectionsService } from '../../collections/collections.service';
  3. import { MilvusService } from '../../milvus/milvus.service';
  4. import {
  5. ERR_NO_ALIAS,
  6. ERR_NO_COLLECTION,
  7. ERR_NO_PARAM,
  8. } from '../utils/constants';
  9. import {
  10. insightCacheForTest,
  11. mockAddress,
  12. mockCollectionNames,
  13. mockCollections,
  14. mockGetAllCollectionsData,
  15. mockLoadedCollections,
  16. mockLoadedCollectionsData,
  17. mockGetReplicasData,
  18. } from '../__mocks__/consts';
  19. import { MilvusClient } from '@zilliz/milvus2-sdk-node/dist/milvus';
  20. // mock Milvus client
  21. jest.mock('@zilliz/milvus2-sdk-node', () => {
  22. return {
  23. MilvusClient: mockMilvusClient,
  24. };
  25. });
  26. describe('Test collections service', () => {
  27. let milvusService: any;
  28. let service: any;
  29. beforeAll(async () => {
  30. // setup Milvus service and connect to mock Milvus client
  31. milvusService = new MilvusService();
  32. MilvusService.activeAddress = mockAddress;
  33. MilvusService.activeMilvusClient = new MilvusClient(mockAddress);
  34. await milvusService.connectMilvus(
  35. { address: mockAddress },
  36. insightCacheForTest
  37. );
  38. service = new CollectionsService(milvusService);
  39. // mock getReplicas
  40. service.getReplicas = async () => {
  41. return new Promise<any>(resolve => {
  42. resolve(mockGetReplicasData);
  43. });
  44. };
  45. });
  46. afterAll(() => {
  47. milvusService = null;
  48. service = null;
  49. });
  50. test('test managers after connected to Milvus', () => {
  51. expect(service.collectionManager).toBeDefined();
  52. expect(service.dataManager).toBeDefined();
  53. expect(service.indexManager).toBeDefined();
  54. });
  55. test('test getCollections method', async () => {
  56. const res = await service.getCollections({
  57. collection_names: ['c1', 'c2'],
  58. });
  59. expect(res.data.length).toBe(2);
  60. const defaultRes = await service.getCollections();
  61. expect(defaultRes.data).toEqual(mockCollectionNames);
  62. const loadedRes = await service.getCollections({ type: 1 });
  63. expect(loadedRes.data).toEqual(mockLoadedCollections);
  64. try {
  65. await service.getCollections({ collection_names: [] });
  66. } catch (err) {
  67. expect(err).toBe(ERR_NO_PARAM);
  68. }
  69. });
  70. test('test createCollection method', async () => {
  71. const res = await service.createCollection({
  72. collection_name: 'c1',
  73. fields: [],
  74. });
  75. expect(res.data.length).toBe(0);
  76. try {
  77. await service.createCollection({ collection_name: '', fields: [] });
  78. } catch (err) {
  79. expect(err).toBe(ERR_NO_COLLECTION);
  80. }
  81. });
  82. test('test describeCollection method', async () => {
  83. const res = await service.describeCollection({
  84. collection_name: 'c1',
  85. });
  86. const { status, ...result } = res;
  87. const [mockRes] = mockCollections;
  88. expect(result).toEqual(mockRes);
  89. try {
  90. await service.describeCollection({ collection_name: '' });
  91. } catch (err) {
  92. expect(err).toBe(ERR_NO_COLLECTION);
  93. }
  94. });
  95. test('test dropCollection method', async () => {
  96. const res = await service.dropCollection({ collection_name: 'c1' });
  97. expect(res.data).toBe('c1');
  98. try {
  99. await service.dropCollection({ collection_name: '' });
  100. } catch (err) {
  101. expect(err).toBe(ERR_NO_COLLECTION);
  102. }
  103. });
  104. test('test loadCollection method', async () => {
  105. const res = await service.loadCollection({ collection_name: 'c1' });
  106. expect(res.data).toBe('c1');
  107. try {
  108. await service.loadCollection({ collection_name: '' });
  109. } catch (err) {
  110. expect(err).toBe(ERR_NO_COLLECTION);
  111. }
  112. });
  113. test('test releaseCollection method', async () => {
  114. const res = await service.releaseCollection({ collection_name: 'c1' });
  115. expect(res.data).toBe('c1');
  116. try {
  117. await service.releaseCollection({ collection_name: '' });
  118. } catch (err) {
  119. expect(err).toBe(ERR_NO_COLLECTION);
  120. }
  121. });
  122. test('test getCollectionStatistics method', async () => {
  123. const res = await service.getCollectionStatistics({
  124. collection_name: 'c1',
  125. });
  126. const { status, ...data } = res;
  127. expect(data.name).toBe('c1');
  128. expect(data.stats.length).toBe(1);
  129. try {
  130. await service.getCollectionStatistics({ collection_name: '' });
  131. } catch (err) {
  132. expect(err).toBe(ERR_NO_COLLECTION);
  133. }
  134. });
  135. test('test insert method', async () => {
  136. const mockParam = {
  137. collection_name: 'c1',
  138. fields_data: {
  139. vector_field: [1, 2, 3, 4],
  140. age: 7,
  141. },
  142. };
  143. const res = await service.insert(mockParam);
  144. expect(res.data).toEqual(mockParam);
  145. try {
  146. await service.insert({
  147. collection_name: '',
  148. fields_data: {
  149. vector_field: [1, 2, 3, 4],
  150. },
  151. });
  152. } catch (err) {
  153. expect(err).toBe(ERR_NO_COLLECTION);
  154. }
  155. });
  156. test('test importSample method', async () => {
  157. const mockParam = {
  158. collection_name: 'c1',
  159. size: 2,
  160. };
  161. const res = await service.importSample(mockParam);
  162. expect(res.data.fields_data.length).toEqual(2);
  163. try {
  164. await service.importSample({
  165. collection_name: '',
  166. size: 20,
  167. });
  168. } catch (err) {
  169. expect(err).toBe(ERR_NO_COLLECTION);
  170. }
  171. });
  172. test('test vectorSearch method', async () => {
  173. const mockParam = {
  174. collection_name: 'c1',
  175. search_params: {
  176. anns_field: 'float_vector',
  177. topk: '10',
  178. metric_type: 'L2',
  179. params: JSON.stringify({ nprobe: 1024 }),
  180. },
  181. vectors: [[1, 2, 3, 4]],
  182. vector_type: 101,
  183. };
  184. const res = await service.vectorSearch(mockParam);
  185. expect(res.data).toEqual(mockParam);
  186. try {
  187. await service.vectorSearch({ ...mockParam, collection_name: '' });
  188. } catch (err) {
  189. expect(err).toBe(ERR_NO_COLLECTION);
  190. }
  191. });
  192. test('test createAlias method', async () => {
  193. const mockParam = {
  194. collection_name: 'c1',
  195. alias: 'alias',
  196. };
  197. const res = await service.createAlias(mockParam);
  198. expect(res.data).toEqual(mockParam);
  199. try {
  200. await service.createAlias({ collection_name: '', alias: '' });
  201. } catch (err) {
  202. expect(err).toBe(ERR_NO_COLLECTION);
  203. }
  204. });
  205. test('test alterAlias method', async () => {
  206. const mockParam = {
  207. collection_name: 'c1',
  208. alias: 'alias',
  209. };
  210. const res = await service.alterAlias(mockParam);
  211. expect(res.data).toEqual(mockParam);
  212. try {
  213. await service.alterAlias({ collection_name: '', alias: '' });
  214. } catch (err) {
  215. expect(err).toBe(ERR_NO_COLLECTION);
  216. }
  217. });
  218. test('test dropAlias method', async () => {
  219. const res = await service.dropAlias({ alias: 'alias' });
  220. expect(res.data).toBe('alias');
  221. try {
  222. await service.dropAlias({ alias: '' });
  223. } catch (err) {
  224. expect(err).toBe(ERR_NO_ALIAS);
  225. }
  226. });
  227. test('test query method', async () => {
  228. const mockParam = {
  229. collection_name: 'c1',
  230. expr: 'age > 7',
  231. };
  232. const res = await service.query(mockParam);
  233. expect(res.data).toEqual(mockParam);
  234. try {
  235. await service.query({ collection_name: '', expr: '' });
  236. } catch (err) {
  237. expect(err).toBe(ERR_NO_COLLECTION);
  238. }
  239. });
  240. test('test getIndexStatus method', async () => {
  241. const res = await service.getIndexStatus({ collection_name: 'c1' });
  242. const { status, ...data } = res;
  243. expect(data).toEqual({ collection_name: 'c1', state: 3 });
  244. });
  245. test('test getAllCollections method', async () => {
  246. const res = await service.getAllCollections();
  247. expect(res).toEqual(mockGetAllCollectionsData);
  248. });
  249. test('test getLoadedCollections method', async () => {
  250. const res = await service.getLoadedColletions();
  251. expect(res).toEqual(mockLoadedCollectionsData);
  252. });
  253. test('test getStatistics method', async () => {
  254. const res = await service.getStatistics();
  255. expect(res).toEqual({
  256. // 2 collections
  257. collectionCount: 2,
  258. // each collection 7 row counts
  259. totalData: 14,
  260. });
  261. });
  262. test('test getCollectionIndexStatus method', async () => {
  263. const res = await service.getCollectionsIndexStatus();
  264. expect(res).toEqual([
  265. {
  266. collection_name: 'c1',
  267. index_status: 3,
  268. },
  269. {
  270. collection_name: 'c2',
  271. index_status: 2,
  272. },
  273. ]);
  274. });
  275. test('test deleteEntities method', async () => {
  276. const mockParam = {
  277. collection_name: 'c1',
  278. expr: 'age > 7',
  279. };
  280. const res = await service.deleteEntities(mockParam);
  281. expect(res.data).toEqual(mockParam);
  282. try {
  283. await service.deleteEntities({ collection_name: '', expr: '' });
  284. } catch (err) {
  285. expect(err).toBe(ERR_NO_COLLECTION);
  286. }
  287. });
  288. });