UNPKG

mongodb-dynamic-api

Version:

Auto generated CRUD API for MongoDB using NestJS

638 lines 30.2 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); const mongoose_1 = require("@nestjs/mongoose"); const testing_1 = require("@nestjs/testing"); const mongoose_2 = require("mongoose"); const src_1 = require("../src"); const e2e_setup_1 = require("./e2e.setup"); require("dotenv/config"); const utils_1 = require("./utils"); describe('DynamicApiModule forFeature (e2e)', () => { const uri = process.env.MONGO_DB_URL; const initApp = async (forFeatureOptions, forRootOptions = {}, initFixtures, initMainCb) => { const moduleRef = await testing_1.Test.createTestingModule({ imports: [ src_1.DynamicApiModule.forRoot(uri, forRootOptions), src_1.DynamicApiModule.forFeature(forFeatureOptions), ], }).compile(); await (0, e2e_setup_1.createTestingApp)(moduleRef, initFixtures, initMainCb); }; beforeEach(() => { src_1.DynamicApiModule.state['resetState'](); }); afterEach(async () => { await (0, e2e_setup_1.closeTestingApp)(mongoose_2.default.connections); }); describe('Entity extends BaseEntity', () => { let TestEntity = class TestEntity extends src_1.BaseEntity { }; __decorate([ (0, mongoose_1.Prop)({ type: String, required: true }), __metadata("design:type", String) ], TestEntity.prototype, "name", void 0); __decorate([ (0, mongoose_1.Prop)({ type: String }), __metadata("design:type", String) ], TestEntity.prototype, "group", void 0); TestEntity = __decorate([ (0, src_1.DynamicAPISchemaOptions)({ indexes: [{ fields: { name: 1 }, options: { unique: true } }], }), (0, mongoose_1.Schema)({ collection: 'test-entities' }) ], TestEntity); beforeEach(async () => { const fixtures = async (_) => { const model = await (0, utils_1.getModelFromEntity)(TestEntity); await model.insertMany([ { name: 'test1' }, { name: 'test2' }, ]); }; await initApp({ entity: TestEntity, controllerOptions: { path: 'test-entities', }, }, {}, fixtures); }); describe('GET /test-entities', () => { it('should return all test entities', async () => { const { body, status } = await e2e_setup_1.server.get('/test-entities'); expect(status).toBe(200); expect(body).toEqual([ { id: expect.any(String), name: 'test1', createdAt: expect.any(String), updatedAt: expect.any(String), }, { id: expect.any(String), name: 'test2', createdAt: expect.any(String), updatedAt: expect.any(String), }, ]); }); }); describe('GET /test-entities/:id', () => { let entities; beforeEach(async () => { const { body } = await e2e_setup_1.server.get('/test-entities'); entities = body; }); it('should return a test entity by id', async () => { const { body, status } = await e2e_setup_1.server.get(`/test-entities/${entities[0].id}`); expect(status).toBe(200); expect(body).toEqual(entities[0]); }); it('should return 404 if entity not found', async () => { const { body, status } = await e2e_setup_1.server.get(`/test-entities/123`); expect(status).toBe(404); expect(body).toHaveProperty('error', 'Not Found'); expect(body).toHaveProperty('statusCode', 404); expect(body).toHaveProperty('message', 'TestEntity not found'); }); }); describe('POST /test-entities/many', () => { it('should create many test entities', async () => { const { body, status } = await e2e_setup_1.server.post('/test-entities/many', { list: [ { name: 'test3' }, { name: 'test4' }, ], }); expect(status).toBe(201); expect(body).toEqual([ { id: expect.any(String), name: 'test3', createdAt: expect.any(String), updatedAt: expect.any(String), }, { id: expect.any(String), name: 'test4', createdAt: expect.any(String), updatedAt: expect.any(String), }, ]); }); }); describe('POST /test-entities', () => { it('should create a test entity', async () => { const { body, status } = await e2e_setup_1.server.post('/test-entities', { name: 'test3' }); expect(status).toBe(201); expect(body).toEqual({ id: expect.any(String), name: 'test3', createdAt: expect.any(String), updatedAt: expect.any(String), }); }); }); describe('PUT /test-entities/:id', () => { let entityToReplace; beforeEach(async () => { const { body } = await e2e_setup_1.server.get('/test-entities'); entityToReplace = body.pop(); }); it('should replace a test entity by id', async () => { const { body, status } = await e2e_setup_1.server.put(`/test-entities/${entityToReplace.id}`, { name: 'test-replaced' }); expect(status).toBe(200); expect(body).toEqual({ id: expect.any(String), name: 'test-replaced', createdAt: expect.any(String), updatedAt: expect.any(String), }); }); it('should return 404 if entity not found', async () => { const { body, status } = await e2e_setup_1.server.put(`/test-entities/123`, { name: 'test1-updated' }); expect(status).toBe(404); expect(body).toHaveProperty('error', 'Not Found'); expect(body).toHaveProperty('statusCode', 404); expect(body).toHaveProperty('message', 'TestEntity not found'); }); }); describe('PATCH /test-entities', () => { let entities; beforeEach(async () => { const { body } = await e2e_setup_1.server.get('/test-entities'); entities = body; }); it('should update many test entities', async () => { const { body, status } = await e2e_setup_1.server.patch('/test-entities', { group: 'many-patched', }, { query: { ids: entities.map(({ id }) => id) } }); expect(status).toBe(200); expect(body).toEqual([ { ...entities[0], group: 'many-patched', updatedAt: expect.any(String), }, { ...entities[1], group: 'many-patched', updatedAt: expect.any(String), }, ]); }); it('should return 404 if entity not found', async () => { const { body, status } = await e2e_setup_1.server.patch('/test-entities', { group: 'many-patched', }, { query: { ids: ['123'] } }); expect(status).toBe(404); expect(body).toHaveProperty('error', 'Not Found'); expect(body).toHaveProperty('statusCode', 404); expect(body).toHaveProperty('message', 'TestEntity not found'); }); }); describe('PATCH /test-entities/:id', () => { let entityToUpdate; beforeEach(async () => { const { body } = await e2e_setup_1.server.get('/test-entities'); entityToUpdate = body.pop(); }); it('should update a test entity by id', async () => { const { body, status } = await e2e_setup_1.server.patch(`/test-entities/${entityToUpdate.id}`, { name: 'test-updated' }); expect(status).toBe(200); expect(body).toEqual({ id: expect.any(String), name: 'test-updated', createdAt: expect.any(String), updatedAt: expect.any(String), }); }); it('should return 404 if entity not found', async () => { const { body, status } = await e2e_setup_1.server.patch(`/test-entities/123`, { name: 'test1-updated' }); expect(status).toBe(404); expect(body).toHaveProperty('error', 'Not Found'); expect(body).toHaveProperty('statusCode', 404); expect(body).toHaveProperty('message', 'TestEntity not found'); }); }); describe('DELETE /test-entities', () => { let entities; beforeEach(async () => { const { body } = await e2e_setup_1.server.get('/test-entities'); entities = body; }); it('should delete many test entities', async () => { const { body, status } = await e2e_setup_1.server.delete('/test-entities', { query: { ids: entities.map(({ id }) => id) } }); expect(status).toBe(200); expect(body).toEqual({ deletedCount: 2 }); const model = await (0, utils_1.getModelFromEntity)(TestEntity); const updatedList = await model.find().lean().exec(); expect(updatedList).toEqual([]); }); it('should return 200 with 0 for deletedCount if one of entities is not found', async () => { const { body, status } = await e2e_setup_1.server.delete('/test-entities', { query: { ids: [entities[0].id, '123'] } }); expect(status).toBe(200); expect(body).toEqual({ deletedCount: 0 }); }); }); describe('DELETE /test-entities/:id', () => { let entityToDelete; beforeEach(async () => { const { body } = await e2e_setup_1.server.get('/test-entities'); entityToDelete = body.pop(); }); it('should delete a test entity by id', async () => { const { body, status } = await e2e_setup_1.server.delete(`/test-entities/${entityToDelete.id}`); expect(status).toBe(200); expect(body).toEqual({ deletedCount: 1 }); }); it('should return 200 with deletedCount at 0 if entity not found', async () => { const { body, status } = await e2e_setup_1.server.delete(`/test-entities/123`); expect(status).toBe(200); expect(body).toEqual({ deletedCount: 0 }); }); }); describe('POST /test-entities/duplicate', () => { let entities; beforeEach(async () => { const { body } = await e2e_setup_1.server.get('/test-entities'); entities = body; }); it('should duplicate test entities', async () => { const { body, status } = await e2e_setup_1.server.post('/test-entities/duplicate', { name: 'test1-duplicated', group: 'duplicated', }, { query: { ids: [entities[0].id] } }); expect(status).toBe(201); expect(body).toEqual([ { ...entities[0], id: expect.any(String), name: 'test1-duplicated', group: 'duplicated', createdAt: expect.any(String), updatedAt: expect.any(String), }, ]); }); it('should return 404 if one of entities is not found', async () => { const { body, status } = await e2e_setup_1.server.post(`/test-entities/duplicate`, { group: 'duplicated', }, { query: { ids: [entities[0].id, '123'] } }); expect(status).toBe(404); expect(body).toHaveProperty('error', 'Not Found'); expect(body).toHaveProperty('statusCode', 404); expect(body).toHaveProperty('message', 'TestEntity not found'); }); }); describe('POST /test-entities/duplicate/:id', () => { let entityToDuplicate; beforeEach(async () => { const { body } = await e2e_setup_1.server.get('/test-entities'); entityToDuplicate = body.pop(); }); it('should duplicate a test entity by id', async () => { const { body, status } = await e2e_setup_1.server.post(`/test-entities/duplicate/${entityToDuplicate.id}`, { name: 'test2-duplicated', }); expect(status).toBe(201); expect(body).toEqual({ ...entityToDuplicate, id: expect.any(String), name: 'test2-duplicated', createdAt: expect.any(String), updatedAt: expect.any(String), }); }); it('should return 404 if entity not found', async () => { const { body, status } = await e2e_setup_1.server.post(`/test-entities/duplicate/123`, { name: 'test1-duplicated', group: 'duplicated', }); expect(status).toBe(404); expect(body).toHaveProperty('error', 'Not Found'); expect(body).toHaveProperty('statusCode', 404); expect(body).toHaveProperty('message', 'TestEntity not found'); }); }); }); describe('Entity extends SoftDeletableEntity', () => { let TestDeletableEntity = class TestDeletableEntity extends src_1.SoftDeletableEntity { }; __decorate([ (0, mongoose_1.Prop)({ type: String, required: true }), __metadata("design:type", String) ], TestDeletableEntity.prototype, "name", void 0); __decorate([ (0, mongoose_1.Prop)({ type: String }), __metadata("design:type", String) ], TestDeletableEntity.prototype, "group", void 0); TestDeletableEntity = __decorate([ (0, src_1.DynamicAPISchemaOptions)({ indexes: [{ fields: { name: 1 }, options: { unique: true, partialFilterExpression: { deletedAt: { $eq: null } } } }], }), (0, mongoose_1.Schema)({ collection: 'test-entities' }) ], TestDeletableEntity); beforeEach(async () => { const fixtures = async (_) => { const model = await (0, utils_1.getModelFromEntity)(TestDeletableEntity); await model.insertMany([ { name: 'test1' }, { name: 'test2' }, ]); }; await initApp({ entity: TestDeletableEntity, controllerOptions: { path: 'test-entities', }, }, {}, fixtures); }); describe('GET /test-entities', () => { it('should return all test entities', async () => { const { body, status } = await e2e_setup_1.server.get('/test-entities'); expect(status).toBe(200); expect(body).toEqual([ { id: expect.any(String), name: 'test1', createdAt: expect.any(String), updatedAt: expect.any(String), }, { id: expect.any(String), name: 'test2', createdAt: expect.any(String), updatedAt: expect.any(String), }, ]); }); }); describe('GET /test-entities/:id', () => { let entities; beforeEach(async () => { const { body } = await e2e_setup_1.server.get('/test-entities'); entities = body; }); it('should return a test entity by id', async () => { const { body, status } = await e2e_setup_1.server.get(`/test-entities/${entities[0].id}`); expect(status).toBe(200); expect(body).toEqual(entities[0]); }); it('should return 404 if entity not found', async () => { const { body, status } = await e2e_setup_1.server.get(`/test-entities/123`); expect(status).toBe(404); expect(body).toHaveProperty('error', 'Not Found'); expect(body).toHaveProperty('statusCode', 404); expect(body).toHaveProperty('message', 'TestDeletableEntity not found'); }); }); describe('POST /test-entities/many', () => { it('should create many test entities', async () => { const { body, status } = await e2e_setup_1.server.post('/test-entities/many', { list: [ { name: 'test3' }, { name: 'test4' }, ], }); expect(status).toBe(201); expect(body).toEqual([ { id: expect.any(String), name: 'test3', createdAt: expect.any(String), updatedAt: expect.any(String), }, { id: expect.any(String), name: 'test4', createdAt: expect.any(String), updatedAt: expect.any(String), }, ]); }); }); describe('POST /test-entities', () => { it('should create a test entity', async () => { const { body, status } = await e2e_setup_1.server.post('/test-entities', { name: 'test3' }); expect(status).toBe(201); expect(body).toEqual({ id: expect.any(String), name: 'test3', createdAt: expect.any(String), updatedAt: expect.any(String), }); }); }); describe('PUT /test-entities/:id', () => { let entityToReplace; beforeEach(async () => { const { body } = await e2e_setup_1.server.get('/test-entities'); entityToReplace = body.pop(); }); it('should replace a test entity by id', async () => { const { body, status } = await e2e_setup_1.server.put(`/test-entities/${entityToReplace.id}`, { name: 'test-replaced' }); expect(status).toBe(200); expect(body).toEqual({ id: expect.any(String), name: 'test-replaced', createdAt: expect.any(String), updatedAt: expect.any(String), }); }); it('should return 404 if entity not found', async () => { const { body, status } = await e2e_setup_1.server.put(`/test-entities/123`, { name: 'test1-updated' }); expect(status).toBe(404); expect(body).toHaveProperty('error', 'Not Found'); expect(body).toHaveProperty('statusCode', 404); expect(body).toHaveProperty('message', 'TestDeletableEntity not found'); }); }); describe('PATCH /test-entities', () => { let entities; beforeEach(async () => { const { body } = await e2e_setup_1.server.get('/test-entities'); entities = body; }); it('should update many test entities', async () => { const { body, status } = await e2e_setup_1.server.patch('/test-entities', { group: 'many-patched', }, { query: { ids: entities.map(({ id }) => id) } }); expect(status).toBe(200); expect(body).toEqual([ { ...entities[0], group: 'many-patched', updatedAt: expect.any(String), }, { ...entities[1], group: 'many-patched', updatedAt: expect.any(String), }, ]); }); it('should return 404 if entity not found', async () => { const { body, status } = await e2e_setup_1.server.patch('/test-entities', { group: 'many-patched', }, { query: { ids: ['123'] } }); expect(status).toBe(404); expect(body).toHaveProperty('error', 'Not Found'); expect(body).toHaveProperty('statusCode', 404); expect(body).toHaveProperty('message', 'TestDeletableEntity not found'); }); }); describe('PATCH /test-entities/:id', () => { let entityToUpdate; beforeEach(async () => { const { body } = await e2e_setup_1.server.get('/test-entities'); entityToUpdate = body.pop(); }); it('should update a test entity by id', async () => { const { body, status } = await e2e_setup_1.server.patch(`/test-entities/${entityToUpdate.id}`, { name: 'test-updated' }); expect(status).toBe(200); expect(body).toEqual({ id: expect.any(String), name: 'test-updated', createdAt: expect.any(String), updatedAt: expect.any(String), }); }); it('should return 404 if entity not found', async () => { const { body, status } = await e2e_setup_1.server.patch(`/test-entities/123`, { name: 'test1-updated' }); expect(status).toBe(404); expect(body).toHaveProperty('error', 'Not Found'); expect(body).toHaveProperty('statusCode', 404); expect(body).toHaveProperty('message', 'TestDeletableEntity not found'); }); }); describe('DELETE /test-entities', () => { let entities; beforeEach(async () => { const { body } = await e2e_setup_1.server.get('/test-entities'); entities = body; }); it('should delete many test entities', async () => { const { body, status } = await e2e_setup_1.server.delete('/test-entities', { query: { ids: entities.map(({ id }) => id) } }); expect(status).toBe(200); expect(body).toEqual({ deletedCount: 2 }); const model = await (0, utils_1.getModelFromEntity)(TestDeletableEntity); const updatedList = await model.find().lean().exec(); expect(updatedList).toEqual([ { _id: expect.any(Object), __v: expect.any(Number), name: 'test1', isDeleted: true, createdAt: expect.any(Date), updatedAt: expect.any(Date), deletedAt: expect.any(Date), }, { _id: expect.any(Object), __v: expect.any(Number), name: 'test2', isDeleted: true, createdAt: expect.any(Date), updatedAt: expect.any(Date), deletedAt: expect.any(Date), }, ]); }); it('should return 200 with 0 for deletedCount if one of entities is not found', async () => { const { body, status } = await e2e_setup_1.server.delete('/test-entities', { query: { ids: [entities[0].id, '123'] } }); expect(status).toBe(200); expect(body).toEqual({ deletedCount: 0 }); }); }); describe('DELETE /test-entities/:id', () => { let entityToDelete; beforeEach(async () => { const { body } = await e2e_setup_1.server.get('/test-entities'); entityToDelete = body.pop(); }); it('should delete a test entity by id', async () => { const { body, status } = await e2e_setup_1.server.delete(`/test-entities/${entityToDelete.id}`); expect(status).toBe(200); expect(body).toEqual({ deletedCount: 1 }); }); it('should return 200 with deletedCount at 0 if entity not found', async () => { const { body, status } = await e2e_setup_1.server.delete(`/test-entities/123`); expect(status).toBe(200); expect(body).toEqual({ deletedCount: 0 }); }); }); describe('POST /test-entities/duplicate', () => { let entities; beforeEach(async () => { const { body } = await e2e_setup_1.server.get('/test-entities'); entities = body; }); it('should duplicate test entities', async () => { const { body, status } = await e2e_setup_1.server.post('/test-entities/duplicate', { name: 'test1-duplicated', group: 'duplicated', }, { query: { ids: [entities[0].id] } }); expect(status).toBe(201); expect(body).toEqual([ { ...entities[0], id: expect.any(String), name: 'test1-duplicated', group: 'duplicated', createdAt: expect.any(String), updatedAt: expect.any(String), }, ]); }); it('should return 404 if one of entities is not found', async () => { const { body, status } = await e2e_setup_1.server.post(`/test-entities/duplicate`, { group: 'duplicated', }, { query: { ids: [entities[0].id, '123'] } }); expect(status).toBe(404); expect(body).toHaveProperty('error', 'Not Found'); expect(body).toHaveProperty('statusCode', 404); expect(body).toHaveProperty('message', 'TestDeletableEntity not found'); }); }); describe('POST /test-entities/duplicate/:id', () => { let entityToDuplicate; beforeEach(async () => { const { body } = await e2e_setup_1.server.get('/test-entities'); entityToDuplicate = body.pop(); }); it('should duplicate a test entity by id', async () => { const { body, status } = await e2e_setup_1.server.post(`/test-entities/duplicate/${entityToDuplicate.id}`, { name: 'test2-duplicated', }); expect(status).toBe(201); expect(body).toEqual({ ...entityToDuplicate, id: expect.any(String), name: 'test2-duplicated', createdAt: expect.any(String), updatedAt: expect.any(String), }); }); it('should return 404 if entity not found', async () => { const { body, status } = await e2e_setup_1.server.post(`/test-entities/duplicate/123`, { name: 'test1-duplicated', group: 'duplicated', }); expect(status).toBe(404); expect(body).toHaveProperty('error', 'Not Found'); expect(body).toHaveProperty('statusCode', 404); expect(body).toHaveProperty('message', 'TestDeletableEntity not found'); }); }); }); }); //# sourceMappingURL=dynamic-api-for-feature.e2e-spec.js.map