From f55e2ac68627e74f7b071b078859bd5d08c69774 Mon Sep 17 00:00:00 2001 From: Agnes Lin Date: Tue, 3 Dec 2019 13:10:04 -0500 Subject: [PATCH] test: unit test for entityToData --- .../legacy-juggler-bridge.unit.ts | 129 ++---------------- 1 file changed, 13 insertions(+), 116 deletions(-) diff --git a/packages/repository/src/__tests__/unit/repositories/legacy-juggler-bridge.unit.ts b/packages/repository/src/__tests__/unit/repositories/legacy-juggler-bridge.unit.ts index d53fdda04df4..3dfec058e727 100644 --- a/packages/repository/src/__tests__/unit/repositories/legacy-juggler-bridge.unit.ts +++ b/packages/repository/src/__tests__/unit/repositories/legacy-juggler-bridge.unit.ts @@ -461,6 +461,19 @@ describe('DefaultCrudRepository', () => { }); }); + it('throws if the target data passes to CRUD methods contains nav properties', async () => { + // a unit test for entityToData, which is invoked by create() method + // it would be the same of other CRUD methods. + await expect( + folderRepo.create({ + name: 'f1', + files: [{title: 'nav property'}], + }), + ).to.be.rejectedWith( + 'Navigational properties are not allowed in model data (model "Folder" property "files")', + ); + }); + // stub resolvers const hasManyResolver: InclusionResolver< Folder, @@ -503,122 +516,6 @@ describe('DefaultCrudRepository', () => { return authors; }; }); - context( - 'checks navigational properties for create and update operations (ensurePersistable)', - () => { - it('throws if create() tries to create an entity with DataObject with nav properties', async () => { - await expect( - folderRepo.create({ - name: 'f1', - files: [{title: 'nav property'}], - }), - ).to.be.rejectedWith( - 'Navigational properties are not allowed in model data (model "Folder" property "files")', - ); - }); - - it('throws if createAll() tries to create all entities that some if them have navigational properties', async () => { - await expect( - folderRepo.createAll([ - {name: 'f1'}, - {name: 'f2', files: [{title: 'nav property'}]}, - ]), - ).to.be.rejectedWith( - 'Navigational properties are not allowed in model data (model "Folder" property "files")', - ); - }); - it('throws if updateAll() tries to update entities with DataObject with nav properties', async () => { - await folderRepo.create({name: 'f1', id: 1}); - await folderRepo.create({name: 'f2', id: 2}); - await expect( - folderRepo.updateAll({ - name: 'unified name', - files: [{title: 'nav property'}], - }), - ).to.be.rejectedWith( - 'Navigational properties are not allowed in model data (model "Folder" property "files")', - ); - }); - - it('throws if updateById() tries to update entities with an entity with nav properties', async () => { - folderRepo.registerInclusionResolver('files', hasManyResolver); - await folderRepo.create({name: 'folder', id: 1}); - - await expect( - folderRepo.updateById(1, { - name: 'update', - files: [{title: 't', id: 1, folderId: 1}], - }), - ).to.be.rejectedWith( - 'Navigational properties are not allowed in model data (model "Folder" property "files")', - ); - }); - - it('throws if update() tries to update entities with Entity.toJSON() with nav properties', async () => { - folderRepo.registerInclusionResolver('files', hasManyResolver); - await folderRepo.create({name: 'folder', id: 1}); - await fileRepo.create({title: 'file title', id: 1, folderId: 1}); - const folder = await folderRepo.findById(1, { - include: [{relation: 'files'}], - }); - folder.name = 'new name'; - - await expect(folderRepo.update(folder)).to.be.rejectedWith( - 'Navigational properties are not allowed in model data (model "Folder" property "files")', - ); - }); - // only test replaceById and save with normal id type here. Tests for MongoDB are in repository-tests - it('implements Repository.save() without id', async () => { - const fol = await folderRepo.create({name: 'folder'}); - const folder = await folderRepo.save(fol); - const result = await folderRepo.findById(folder!.id); - expect(result.toJSON()).to.eql(fol!.toJSON()); - }); - - it('implements Repository.save() with id', async () => { - const fol = await folderRepo.create({name: 'f3', id: 1}); - fol.name = 'new name'; - const saved = await folderRepo.save(fol); - const result = await folderRepo.findById(saved!.id); - expect(result.toJSON()).to.eql(fol.toJSON()); - }); - - it('implements Repository.replaceById() without id property provided in the target data', async () => { - const fol = await folderRepo.create({name: 'f3', id: 1}); - await folderRepo.replaceById(fol.id, { - name: 'new name', - }); - const result = await folderRepo.findById(fol.id); - expect(result.toJSON()).to.eql({ - id: fol.id, - name: 'new name', - }); - }); - - it('throws when Repository.replaceById() with id property provided in the target data', async () => { - const fol = await folderRepo.create({name: 'f3', id: 1}); - fol.name = 'new name'; - await folderRepo.replaceById(fol.id, fol); - const result = await folderRepo.findById(fol.id); - expect(result.toJSON()).to.eql({ - id: fol.id, - name: 'new name', - }); - }); - - const hasManyResolver: InclusionResolver< - Folder, - File - > = async entities => { - const files = []; - for (const entity of entities) { - const file = await folderFiles(entity.id).find(); - files.push(file); - } - return files; - }; - }, - ); }); it('implements Repository.delete()', async () => {