|
| 1 | +// Copyright IBM Corp. 2019. All Rights Reserved. |
| 2 | +// Node module: @loopback/repository |
| 3 | +// This file is licensed under the MIT License. |
| 4 | +// License text available at https://opensource.org/licenses/MIT |
| 5 | + |
| 6 | +import {expect, toJSON} from '@loopback/testlab'; |
| 7 | +import {includeRelatedModels, InclusionResolver} from '../../../..'; |
| 8 | +import { |
| 9 | + Category, |
| 10 | + CategoryRepository, |
| 11 | + Product, |
| 12 | + ProductRepository, |
| 13 | + testdb, |
| 14 | +} from './relations-helpers-fixtures'; |
| 15 | + |
| 16 | +describe('includeRelatedModels', () => { |
| 17 | + let productRepo: ProductRepository; |
| 18 | + let categoryRepo: CategoryRepository; |
| 19 | + |
| 20 | + before(() => { |
| 21 | + productRepo = new ProductRepository(testdb); |
| 22 | + categoryRepo = new CategoryRepository(testdb, async () => productRepo); |
| 23 | + }); |
| 24 | + |
| 25 | + beforeEach(async () => { |
| 26 | + await productRepo.deleteAll(); |
| 27 | + await categoryRepo.deleteAll(); |
| 28 | + }); |
| 29 | + |
| 30 | + it("defines a repository's inclusionResolvers property", () => { |
| 31 | + expect(categoryRepo.inclusionResolvers).to.not.be.undefined(); |
| 32 | + expect(productRepo.inclusionResolvers).to.not.be.undefined(); |
| 33 | + }); |
| 34 | + |
| 35 | + it('returns source model if no filter is passed in', async () => { |
| 36 | + const category = await categoryRepo.create({name: 'category 1'}); |
| 37 | + await categoryRepo.create({name: 'category 2'}); |
| 38 | + const result = await includeRelatedModels(categoryRepo, [category]); |
| 39 | + expect(result).to.eql([category]); |
| 40 | + }); |
| 41 | + |
| 42 | + it('throws error if the target repository does not have the registered resolver', async () => { |
| 43 | + const category = await categoryRepo.create({name: 'category 1'}); |
| 44 | + await expect( |
| 45 | + includeRelatedModels(categoryRepo, [category], [{relation: 'products'}]), |
| 46 | + ).to.be.rejectedWith( |
| 47 | + /Invalid "filter.include" entries: {"relation":"products"}/, |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + it('returns an empty array if target model of the source entity does not have any matched instances', async () => { |
| 52 | + const category = await categoryRepo.create({name: 'category'}); |
| 53 | + |
| 54 | + categoryRepo.inclusionResolvers.set('products', hasManyResolver); |
| 55 | + |
| 56 | + const categories = await includeRelatedModels( |
| 57 | + categoryRepo, |
| 58 | + [category], |
| 59 | + [{relation: 'products'}], |
| 60 | + ); |
| 61 | + |
| 62 | + expect(categories[0].products).to.be.empty(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('includes related model for one instance - belongsTo', async () => { |
| 66 | + const category = await categoryRepo.create({name: 'category'}); |
| 67 | + const product = await productRepo.create({ |
| 68 | + name: 'product', |
| 69 | + categoryId: category.id, |
| 70 | + }); |
| 71 | + |
| 72 | + productRepo.inclusionResolvers.set('category', belongsToResolver); |
| 73 | + |
| 74 | + const productWithCategories = await includeRelatedModels( |
| 75 | + productRepo, |
| 76 | + [product], |
| 77 | + [{relation: 'category'}], |
| 78 | + ); |
| 79 | + |
| 80 | + expect(productWithCategories[0].toJSON()).to.deepEqual({ |
| 81 | + ...product.toJSON(), |
| 82 | + category: category.toJSON(), |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it('includes related model for more than one instance - belongsTo', async () => { |
| 87 | + const categoryOne = await categoryRepo.create({name: 'category 1'}); |
| 88 | + const productOne = await productRepo.create({ |
| 89 | + name: 'product 1', |
| 90 | + categoryId: categoryOne.id, |
| 91 | + }); |
| 92 | + |
| 93 | + const categoryTwo = await categoryRepo.create({name: 'category 2'}); |
| 94 | + const productTwo = await productRepo.create({ |
| 95 | + name: 'product 2', |
| 96 | + categoryId: categoryTwo.id, |
| 97 | + }); |
| 98 | + |
| 99 | + const productThree = await productRepo.create({ |
| 100 | + name: 'product 3', |
| 101 | + categoryId: categoryTwo.id, |
| 102 | + }); |
| 103 | + |
| 104 | + productRepo.inclusionResolvers.set('category', belongsToResolver); |
| 105 | + |
| 106 | + const productWithCategories = await includeRelatedModels( |
| 107 | + productRepo, |
| 108 | + [productOne, productTwo, productThree], |
| 109 | + [{relation: 'category'}], |
| 110 | + ); |
| 111 | + |
| 112 | + expect(toJSON(productWithCategories)).to.deepEqual([ |
| 113 | + {...productOne.toJSON(), category: categoryOne.toJSON()}, |
| 114 | + {...productTwo.toJSON(), category: categoryTwo.toJSON()}, |
| 115 | + {...productThree.toJSON(), category: categoryTwo.toJSON()}, |
| 116 | + ]); |
| 117 | + }); |
| 118 | + |
| 119 | + it('includes related models for one instance - hasMany', async () => { |
| 120 | + const category = await categoryRepo.create({name: 'category'}); |
| 121 | + const productOne = await productRepo.create({ |
| 122 | + name: 'product 1', |
| 123 | + categoryId: category.id, |
| 124 | + }); |
| 125 | + |
| 126 | + const productTwo = await productRepo.create({ |
| 127 | + name: 'product 2', |
| 128 | + categoryId: category.id, |
| 129 | + }); |
| 130 | + |
| 131 | + categoryRepo.inclusionResolvers.set('products', hasManyResolver); |
| 132 | + |
| 133 | + const categoryWithProducts = await includeRelatedModels( |
| 134 | + categoryRepo, |
| 135 | + [category], |
| 136 | + [{relation: 'products'}], |
| 137 | + ); |
| 138 | + |
| 139 | + expect(toJSON(categoryWithProducts)).to.deepEqual([ |
| 140 | + { |
| 141 | + ...category.toJSON(), |
| 142 | + products: [productOne.toJSON(), productTwo.toJSON()], |
| 143 | + }, |
| 144 | + ]); |
| 145 | + }); |
| 146 | + |
| 147 | + it('includes related models for more than one instance - hasMany', async () => { |
| 148 | + const categoryOne = await categoryRepo.create({name: 'category 1'}); |
| 149 | + const productOne = await productRepo.create({ |
| 150 | + name: 'product 1', |
| 151 | + categoryId: categoryOne.id, |
| 152 | + }); |
| 153 | + |
| 154 | + const categoryTwo = await categoryRepo.create({name: 'category 2'}); |
| 155 | + const productTwo = await productRepo.create({ |
| 156 | + name: 'product 2', |
| 157 | + categoryId: categoryTwo.id, |
| 158 | + }); |
| 159 | + |
| 160 | + const categoryThree = await categoryRepo.create({name: 'category 3'}); |
| 161 | + const productThree = await productRepo.create({ |
| 162 | + name: 'product 3', |
| 163 | + categoryId: categoryTwo.id, |
| 164 | + }); |
| 165 | + |
| 166 | + categoryRepo.inclusionResolvers.set('products', hasManyResolver); |
| 167 | + |
| 168 | + const categoryWithProducts = await includeRelatedModels( |
| 169 | + categoryRepo, |
| 170 | + [categoryOne, categoryTwo, categoryThree], |
| 171 | + [{relation: 'products'}], |
| 172 | + ); |
| 173 | + |
| 174 | + expect(toJSON(categoryWithProducts)).to.deepEqual([ |
| 175 | + {...categoryOne.toJSON(), products: [productOne.toJSON()]}, |
| 176 | + { |
| 177 | + ...categoryTwo.toJSON(), |
| 178 | + products: [productTwo.toJSON(), productThree.toJSON()], |
| 179 | + }, |
| 180 | + {...categoryThree.toJSON(), products: []}, |
| 181 | + ]); |
| 182 | + }); |
| 183 | + |
| 184 | + // stubbed resolvers |
| 185 | + |
| 186 | + const belongsToResolver: InclusionResolver< |
| 187 | + Product, |
| 188 | + Category |
| 189 | + > = async entities => { |
| 190 | + const categories = []; |
| 191 | + |
| 192 | + for (const product of entities) { |
| 193 | + const category = await categoryRepo.findById(product.categoryId); |
| 194 | + categories.push(category); |
| 195 | + } |
| 196 | + |
| 197 | + return categories; |
| 198 | + }; |
| 199 | + |
| 200 | + const hasManyResolver: InclusionResolver< |
| 201 | + Category, |
| 202 | + Product |
| 203 | + > = async entities => { |
| 204 | + const products = []; |
| 205 | + |
| 206 | + for (const category of entities) { |
| 207 | + const product = await categoryRepo.products(category.id).find(); |
| 208 | + products.push(product); |
| 209 | + } |
| 210 | + return products; |
| 211 | + }; |
| 212 | +}); |
0 commit comments