collections.service.test.ts 8.4 KB

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