UNPKG

js-node-arango

Version:
516 lines 21.7 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const __1 = __importStar(require("..")); const doc = { _key: 'key', _id: 'id', _rev: 'rev', }; const query = { _source: function () { throw new Error('Function not implemented.'); }, query: '', bindVars: {}, }; const queryResult = { all: () => ['result'], }; const view = { updateProperties: jest.fn(), }; const mockExists = jest.fn(); const mockClose = jest.fn(); const mockDropDatabase = jest.fn(); const mockCreateDatabase = jest.fn(); const mockCreateCollection = jest.fn(); const mockCreateEdgeCollection = jest.fn(); const mockCreateView = jest.fn().mockResolvedValue(view); const mockSave = jest.fn().mockResolvedValue(doc); const mockQuery = jest.fn().mockResolvedValue(queryResult); const mockDocument = jest.fn().mockResolvedValue(doc); const mockTruncate = jest.fn(); jest.mock('arangojs', () => { const aql = function () { return query; }; aql.join = jest.fn(); return { Database: function () { return { exists: mockExists, close: mockClose, dropDatabase: mockDropDatabase, createDatabase: mockCreateDatabase, createCollection: mockCreateCollection, createEdgeCollection: mockCreateEdgeCollection, createView: mockCreateView, collection: () => { return { save: mockSave, document: mockDocument, truncate: mockTruncate, }; }, view: () => { return {}; }, query: mockQuery, }; }, aql: aql, }; }); describe('arangodb', () => { let testObject; const username = 'user'; const password = 'password'; const viewName = 'collection'; const collectionName = 'collection'; const id = 'id'; const edgeViewName = 'edge_wiew'; const edgeCollectionName = 'edge_collection'; const relatedCollectionName = 'related_collection'; const entityName = 'entity'; beforeEach(() => { const config = { dbName: 'database', uri: 'uri', }; const loggerConfig = { appName: 'test-app-name', moduleName: 'arangodb-test', logLevel: 'info', logStyle: 'cli', }; testObject = new __1.default(config, loggerConfig); mockClose.mockClear(); mockQuery.mockClear(); }); it('should construct the arangodb with arango config and logger config option', () => { // assert expect(testObject).toBeDefined(); }); it('should throw error when db.exists throws error', () => __awaiter(void 0, void 0, void 0, function* () { // arrange mockExists.mockRejectedValueOnce(new Error()); let thrown = null; // act try { yield testObject.initializeDBConnection(username, password); } catch (e) { thrown = e; } // assert expect(thrown).not.toBeNull(); expect(mockExists).toBeCalled(); })); it('should not throw error when initializeDBConnection is called', () => __awaiter(void 0, void 0, void 0, function* () { // arrange let thrown = null; // act try { yield testObject.initializeDBConnection(username, password); } catch (e) { thrown = e; } // assert expect(thrown).toBeNull(); expect(mockExists).toBeCalled(); })); it('should call close when disconnectDB', () => __awaiter(void 0, void 0, void 0, function* () { // arrange const mockCallback = jest.fn(); // act yield testObject.initializeDBConnection(username, password); testObject.disconnectDB(mockCallback); // assert expect(mockCallback).toBeCalled(); expect(mockClose).toBeCalled(); })); it('should throw given db is not initialized when disconnectDB', () => __awaiter(void 0, void 0, void 0, function* () { // arrange const mockCallback = jest.fn(); // act & assert expect(() => testObject.disconnectDB(mockCallback)).toThrowError(TypeError); })); it('should call dropDatabase when removeDB', () => __awaiter(void 0, void 0, void 0, function* () { // act yield testObject.initializeDBConnection(username, password); yield testObject.removeDB(); // assert expect(mockDropDatabase).toBeCalled(); })); it('should call createDatabase when addDB', () => __awaiter(void 0, void 0, void 0, function* () { // act yield testObject.initializeDBConnection(username, password); yield testObject.addDB(); // assert expect(mockCreateDatabase).toBeCalled(); })); it('should call createCollection when createCollection called', () => __awaiter(void 0, void 0, void 0, function* () { // act yield testObject.initializeDBConnection(username, password); yield testObject.createCollection(collectionName); // assert expect(mockCreateCollection).toBeCalled(); })); it('should call edgeCollection when createEdgeCollection', () => __awaiter(void 0, void 0, void 0, function* () { // act yield testObject.initializeDBConnection(username, password); yield testObject.createEdgeCollection(collectionName); // assert expect(mockCreateEdgeCollection).toBeCalled(); })); it('should call createView when createView called', () => __awaiter(void 0, void 0, void 0, function* () { // act yield testObject.initializeDBConnection(username, password); yield testObject.createView(viewName, collectionName); // assert expect(mockCreateView).toBeCalled(); })); it('should call save when addToCollection', () => __awaiter(void 0, void 0, void 0, function* () { // act yield testObject.initializeDBConnection(username, password); const result = yield testObject.addToCollection(collectionName, {}); // assert expect(mockSave).toBeCalled(); expect(result).toBe(doc); })); it('should call query when cleanCollection', () => __awaiter(void 0, void 0, void 0, function* () { // act yield testObject.initializeDBConnection(username, password); yield testObject.cleanCollection(collectionName); // assert expect(mockTruncate).toBeCalled(); })); it('should call document when getByIdFromCollection', () => __awaiter(void 0, void 0, void 0, function* () { // act yield testObject.initializeDBConnection(username, password); const result = yield testObject.getByIdFromCollection(collectionName, id); // assert expect(mockDocument).toBeCalled(); expect(result).toBe(doc); })); it('should throw error given docment throws error when getByIdFromCollection', () => __awaiter(void 0, void 0, void 0, function* () { // arrange mockDocument.mockRejectedValueOnce(new Error()); let thrown = null; // act yield testObject.initializeDBConnection(username, password); try { yield testObject.getByIdFromCollection(collectionName, id); } catch (e) { thrown = e; } // assert expect(mockDocument).toBeCalled(); expect(thrown).not.toBeNull(); })); it('should call query when getByFieldNameFromCollection', () => __awaiter(void 0, void 0, void 0, function* () { // act yield testObject.initializeDBConnection(username, password); const result = yield testObject.getByFieldNameFromCollection(collectionName, 'field', 'value'); // assert expect(mockQuery).toBeCalled(); expect(result).toBe(queryResult); })); it('should call query when getByFieldNameFromCollection', () => __awaiter(void 0, void 0, void 0, function* () { // act yield testObject.initializeDBConnection(username, password); const result = yield testObject.getFromCollection(collectionName, 'order', 'sort', 1, 0); // assert expect(mockQuery).toBeCalled(); expect(result).toBe(queryResult); })); it('should call query given filter when getByFieldNameFromCollection', () => __awaiter(void 0, void 0, void 0, function* () { // arrange const filter = 'doc.someProperty == "some value"'; // act yield testObject.initializeDBConnection(username, password); const result = yield testObject.getFromCollection(collectionName, 'order', 'sort', 1, 0, filter); // assert expect(mockQuery).toBeCalled(); expect(result).toBe(queryResult); })); it('should create an instance with error parameter', () => { const errorMessage = 'SomeError'; const error = new Error(errorMessage); const testedError = new __1.ArangoError(error); expect(testedError.message).toBe(errorMessage); expect(testedError.code).toBe('error.unexpected'); expect(testedError.name).toBe('ArangoError'); expect(testedError.status).toBe(500); expect(testedError.isPublic).toBe(false); }); it('should create query object with the given options', () => __awaiter(void 0, void 0, void 0, function* () { // arrange yield testObject.initializeDBConnection(username, password); // act const results = yield testObject.getDocuments({ documentCollection: 'manifest', limitBy: { offset: 0, limit: 1, }, sortBy: { field: 'manifest.discoveryPort.createdDateTime', order: 'DESC', }, filterBy: { filter: { gitRepoInfo: { owner: 'Tospaa', }, status: 'InTest', }, }, }); // assert expect(results).not.toBeNull(); expect(mockQuery).toHaveBeenCalledWith({ query: 'FOR doc IN @@collection_1 FILTER doc.@path_2 == @filter_3 && doc.@path_4 == @filter_5 SORT doc.@sortField_6 @sortOrder_7 LIMIT @limitByOffset_8, @limitByLimit_9 RETURN doc', bindVars: { '@collection_1': 'manifest', filter_3: 'Tospaa', filter_5: 'InTest', limitByLimit_9: 1, limitByOffset_8: 0, path_2: ['gitRepoInfo', 'owner'], path_4: ['gitRepoInfo', 'status'], sortField_6: ['manifest', 'discoveryPort', 'createdDateTime'], sortOrder_7: 'DESC', }, }, undefined); })); it('should call query when getDocumentsWithCount', () => __awaiter(void 0, void 0, void 0, function* () { // arrange yield testObject.initializeDBConnection(username, password); // act const results = yield testObject.getDocumentsWithCount({ documentCollection: 'manifest' }); // assert expect(results).not.toBeNull(); expect(mockQuery).toHaveBeenCalledWith({ query: 'LET total = FIRST(FOR doc IN @@collection_1 COLLECT WITH COUNT INTO length RETURN length) LET docs = (FOR doc IN @@collection_1 RETURN doc) RETURN { docs, total }', bindVars: { '@collection_1': 'manifest', }, }, undefined); })); it('should call query when executeRawQuery', () => __awaiter(void 0, void 0, void 0, function* () { // arrange const queryLocal = 'FOR doc IN @@coll'; const bindVars = { '@coll': collectionName, }; // act yield testObject.initializeDBConnection(username, password); const result = yield testObject.executeRawQuery({ query: queryLocal, bindVars }); // assert expect(mockQuery).toBeCalled(); expect(result).toStrictEqual(['result']); })); it('should call query when executeRawQueryReturnFirst', () => __awaiter(void 0, void 0, void 0, function* () { // arrange const queryLocal = 'FOR doc IN @@coll'; const bindVars = { '@coll': collectionName, }; // act yield testObject.initializeDBConnection(username, password); const result = yield testObject.executeRawQueryReturnFirst({ query: queryLocal, bindVars }); // assert expect(mockQuery).toBeCalled(); expect(result).toStrictEqual('result'); })); it('should call query when getOutBoundDocumentsForEntityId', () => __awaiter(void 0, void 0, void 0, function* () { // act yield testObject.initializeDBConnection(username, password); const result = yield testObject.getOutBoundDocumentsForEntityId(edgeViewName, entityName, id, collectionName); // assert expect(mockQuery).toBeCalled(); expect(result).toStrictEqual(['result']); })); it('should call query when getLinksFromViewForToId', () => __awaiter(void 0, void 0, void 0, function* () { // act yield testObject.initializeDBConnection(username, password); const result = yield testObject.getLinksFromViewForToId(edgeViewName, collectionName, id); // assert expect(mockQuery).toBeCalled(); expect(result).toStrictEqual(['result']); })); it('should call query when getLinksFromViewForId', () => __awaiter(void 0, void 0, void 0, function* () { // act yield testObject.initializeDBConnection(username, password); const result = yield testObject.getLinksFromViewForId(edgeViewName, collectionName, id); // assert expect(mockQuery).toBeCalled(); expect(result).toStrictEqual(['result']); })); it('should call query when addToCollectionWithRelatedDocument', () => __awaiter(void 0, void 0, void 0, function* () { // arrange yield testObject.initializeDBConnection(username, password); // act const result = yield testObject.addToCollectionWithRelatedDocument({ rootDocCollectionName: 'upper_collection', relatedDoc: { data: 'value', }, edgeCollectionName: 'edge_collection', relatedCollectionName: 'related_collection', rootDoc: { data: 'this is related doc', }, }); // assert expect(mockQuery).toHaveBeenCalledWith({ bindVars: { '@edgeCollectionName': 'edge_collection', '@relatedCollectionName': 'related_collection', '@rootDocCollectionName': 'upper_collection', additionalLinkData: {}, relatedDoc: { data: 'value', }, rootDoc: { data: 'this is related doc', }, }, query: `LET rootDoc = FIRST(INSERT @rootDoc INTO @@rootDocCollectionName RETURN NEW) LET related = FIRST(INSERT @relatedDoc INTO @@relatedCollectionName RETURN NEW) INSERT MERGE({ _from: rootDoc._id, _to: related._id, _key: UUID() }, @additionalLinkData) into @@edgeCollectionName RETURN { rootDoc, related }`, }, undefined); expect(result).toStrictEqual('result'); })); it('should call query when addRelatedDocument', () => __awaiter(void 0, void 0, void 0, function* () { // arrange yield testObject.initializeDBConnection(username, password); // act const result = yield testObject.addRelatedDocument({ rootDocCollectionName: 'upper_collection', relatedDoc: { data: 'value', }, edgeCollectionName: 'edge_collection', relatedCollectionName: 'related_collection', rootDocId: 'some-uuid', }); // assert expect(mockQuery).toHaveBeenCalledWith({ bindVars: { '@edgeCollectionName': 'edge_collection', '@relatedCollectionName': 'related_collection', fromId: 'upper_collection/some-uuid', additionalLinkData: {}, relatedDoc: { data: 'value', }, }, query: `LET related = FIRST(INSERT @relatedDoc INTO @@relatedCollectionName RETURN NEW) INSERT MERGE({ _from: @fromId, _to: related._id, _key: UUID() }, @additionalLinkData) into @@edgeCollectionName RETURN related`, }, undefined); expect(result).toStrictEqual('result'); })); it('should call query when getDocumentWithRelatedDocuments', () => __awaiter(void 0, void 0, void 0, function* () { // arrange yield testObject.initializeDBConnection(username, password); // act const result = yield testObject.getDocumentWithRelatedDocuments(collectionName, id, { edgeCollections: edgeCollectionName, relatedCollections: relatedCollectionName, }); // assert expect(mockQuery).toHaveBeenCalledWith({ bindVars: { '@collection_1': 'collection', '@edge_5': 'edge_collection', '@target_4': 'related_collection', depth_6: 1, filter_3: 'id', path_2: ['_key'], }, query: 'WITH @@target_4 FOR rootDoc IN @@collection_1 FILTER rootDoc.@path_2 == @filter_3 LET related = (FOR vertex IN 1..@depth_6 ANY rootDoc._id @@edge_5 RETURN vertex) RETURN MERGE(rootDoc, { related })', }, undefined); expect(result).toStrictEqual('result'); })); it('should call query when getDocumentWithRelatedDocumentsWithCount', () => __awaiter(void 0, void 0, void 0, function* () { // arrange yield testObject.initializeDBConnection(username, password); // act const result = yield testObject.getDocumentWithRelatedDocumentsWithCount(collectionName, id, { edgeCollections: [edgeCollectionName, 'other_edge_collection'], relatedCollections: [relatedCollectionName, 'other_related_collection'], }); // assert expect(mockQuery).toHaveBeenCalledWith({ bindVars: { '@collection_1': 'collection', '@edge_6': 'edge_collection', '@edge_7': 'other_edge_collection', '@target_4': 'related_collection', '@target_5': 'other_related_collection', depth_8: 1, filter_3: 'id', path_2: ['_key'], }, query: 'WITH @@target_4, @@target_5 FOR rootDoc IN @@collection_1 FILTER rootDoc.@path_2 == @filter_3 LET related = FIRST(LET total = FIRST(FOR vertex IN 1..@depth_8 ANY rootDoc._id @@edge_6, @@edge_7 COLLECT WITH COUNT INTO length RETURN length) LET docs = (FOR vertex IN 1..@depth_8 ANY rootDoc._id @@edge_6, @@edge_7 RETURN vertex) RETURN { docs, total }) RETURN MERGE(rootDoc, { related })', }, undefined); expect(result).toStrictEqual('result'); })); }); //# sourceMappingURL=arango.test.js.map