From 959a5d78c7b6b1690f226250a232dd4b052819fe Mon Sep 17 00:00:00 2001 From: Dan Ribbens Date: Mon, 17 Apr 2023 16:43:00 -0400 Subject: [PATCH] chore: test coverage queries --- src/mongoose/buildQuery.ts | 6 +- test/collections-rest/int.spec.ts | 156 +----------------------------- 2 files changed, 8 insertions(+), 154 deletions(-) diff --git a/src/mongoose/buildQuery.ts b/src/mongoose/buildQuery.ts index 531a26971d..9c2f87b9e5 100644 --- a/src/mongoose/buildQuery.ts +++ b/src/mongoose/buildQuery.ts @@ -7,7 +7,7 @@ import { combineMerge } from '../utilities/combineMerge'; import { operatorMap } from './operatorMap'; import { sanitizeQueryValue } from './sanitizeQueryValue'; import { PayloadRequest, Where } from '../types'; -import { Field, FieldAffectingData, TabAsField, UIField, fieldAffectsData } from '../fields/config/types'; +import { Field, FieldAffectingData, fieldAffectsData, TabAsField, UIField } from '../fields/config/types'; import { CollectionPermission, FieldPermissions, GlobalPermission } from '../auth'; import flattenFields from '../utilities/flattenTopLevelFields'; import { getEntityPolicies } from '../utilities/getEntityPolicies'; @@ -566,8 +566,8 @@ const getBuildQueryPlugin = ({ }); const result = await paramParser.parse(); - if (this.errors.length > 0) { - throw new QueryError(this.errors); + if (paramParser.errors.length > 0) { + throw new QueryError(paramParser.errors); } return result; diff --git a/test/collections-rest/int.spec.ts b/test/collections-rest/int.spec.ts index b9803520c9..54058b259c 100644 --- a/test/collections-rest/int.spec.ts +++ b/test/collections-rest/int.spec.ts @@ -124,8 +124,6 @@ describe('collections-rest', () => { data: { description }, }); - console.log({ relationFieldStatus, relationFieldDocs, relationFieldErrors }); - const { status: relationMultiRelationToStatus } = await client.updateMany({ where: { 'relationMultiRelationTo.missing': { equals: 'title' } }, data: { description }, @@ -150,8 +148,8 @@ describe('collections-rest', () => { }); const description = 'description'; - const { status } = await client.updateMany({ - query: { restrictedField: { equals: 'restricted' } }, + const result = await client.updateMany({ + where: { restrictedField: { equals: 'restricted' } }, data: { description }, }); @@ -160,137 +158,12 @@ describe('collections-rest', () => { id, }); - expect(status).toEqual(400); + expect(result.status).toEqual(400); + expect(result.errors).toHaveLength(1); + expect(result.errors[0].message).toEqual('The following path cannot be queried: restrictedField'); expect(doc.description).toBeUndefined(); }); - it('should bulk update with a relationship field that exists in one collection and not another', async () => { - const relationOne = await payload.create({ - collection: 'dummy', - data: { - title: 'title', - }, - }); - const relationTwo = await payload.create({ - collection: 'relation', - data: { - name: 'name', - }, - }); - - const description = 'desc'; - const relationPost = await payload.create({ - collection: slug, - data: { - description, - relationMultiRelationTo: { - value: relationTwo.id, - relationTo: 'relation', - }, - }, - }); - - const relationToDummyPost = await payload.create({ - collection: slug, - data: { - description, - relationMultiRelationTo: { - value: relationOne.id, - relationTo: 'dummy', - }, - }, - }); - - const updatedDescription = 'updated'; - - const { status: relationMultiRelationToStatus, docs: updated } = await client.updateMany({ - where: { - 'relationMultiRelationTo.title': { - equals: relationOne.title, - }, - }, - data: { description: updatedDescription }, - }); - - const updatedDoc = await payload.findByID({ - collection: slug, - id: relationToDummyPost.id, - }); - - const otherDoc = await payload.findByID({ - collection: slug, - id: relationPost.id, - }); - - expect(relationMultiRelationToStatus).toEqual(200); - expect(updated).toHaveLength(1); - expect(updated[0].id).toEqual(relationToDummyPost.id); - expect(updatedDoc.description).toEqual(updatedDescription); - expect(otherDoc.description).toEqual(description); - }); - - it('should bulk update with a relationship field that exists in one collection and is restricted in another', async () => { - const name = 'name'; - const relationOne = await payload.create({ - collection: 'dummy', - data: { - title: 'title', - name, // read access: () => false - }, - }); - const relationTwo = await payload.create({ - collection: 'relation', - data: { - name, - }, - }); - - const description = 'desc'; - const relationPost = await payload.create({ - collection: slug, - data: { - description, - relationMultiRelationTo: { - value: relationTwo.id, - relationTo: 'relation', - }, - }, - }); - - const relationToDummyPost = await payload.create({ - collection: slug, - data: { - description, - relationMultiRelationTo: { - value: relationOne.id, - relationTo: 'dummy', - }, - }, - }); - - const updatedDescription = 'updated'; - - const { status } = await client.updateMany({ - where: { 'relationMultiRelationTo.name': { equals: name } }, - data: { description: updatedDescription }, - }); - - - const updatedDoc = await payload.findByID({ - collection: slug, - id: relationPost.id, - }); - - const otherDoc = await payload.findByID({ - collection: slug, - id: relationToDummyPost.id, - }); - - expect(status).toEqual(200); - expect(updatedDoc.description).toEqual(updatedDescription); - expect(otherDoc.description).toEqual(description); - }); - it('should return formatted errors for bulk updates', async () => { const text = 'bulk-update-test-errors'; const errorDoc = await payload.create({ @@ -554,25 +427,6 @@ describe('collections-rest', () => { expect(result.docs).toEqual([post1]); expect(result.totalDocs).toEqual(1); }); - - it('nested by property value', async () => { - const post1 = await createPost({ - relationMultiRelationTo: { relationTo: relationSlug, value: relation.id }, - }); - await createPost(); - - const { status, result } = await client.find({ - query: { - 'relationMultiRelationTo.value.name': { - equals: relation.name, - }, - }, - }); - - expect(status).toEqual(200); - expect(result.docs).toEqual([post1]); - expect(result.totalDocs).toEqual(1); - }); }); describe('relationTo multi hasMany', () => {