|
1 | 1 | import { ObjectType } from "../utils/types"; |
2 | | -import { validateNonNullableFields, validateNonNullableFieldsAssert, validateNonNullListField, validateNonNullableFieldsTraverse, flattenListField } from "../../src/mongoDbUpdateValidation"; |
| 2 | +import { validateUpdateArgs, validateNonNullableFields, validateNonNullableFieldsAssert, validateNonNullListField, validateNonNullableFieldsTraverse, flattenListField } from "../../src/mongoDbUpdateValidation"; |
3 | 3 | import { expect } from "chai"; |
4 | | -import { GraphQLObjectType, GraphQLType, GraphQLList, GraphQLNonNull, GraphQLString, execute } from "graphql"; |
| 4 | +import { GraphQLObjectType, GraphQLType, GraphQLList, GraphQLNonNull, GraphQLString } from "graphql"; |
5 | 5 | import { UpdateArgs } from "../../src/mongoDbUpdate"; |
6 | 6 |
|
7 | | -describe("mongoDbProjection", () => { |
| 7 | +describe("mongoDbUpdateValidation", () => { |
| 8 | + describe("validateUpdateArgs", () => { |
| 9 | + const tests: { description: string, type: GraphQLObjectType, updateArgs: UpdateArgs, expectedErrors: string[] }[] = [{ |
| 10 | + description: "Should invalidate non-null on upsert", |
| 11 | + type: ObjectType, |
| 12 | + updateArgs: { |
| 13 | + setOnInsert: { |
| 14 | + stringScalar: "x", |
| 15 | + }, |
| 16 | + set: { |
| 17 | + nonNullScalar: null |
| 18 | + } |
| 19 | + }, |
| 20 | + expectedErrors: ["Missing non-nullable field \"nonNullList\"", "Non-nullable field \"nonNullScalar\" is set to null"] |
| 21 | + }, { |
| 22 | + description: "Should ignore non-null on update", |
| 23 | + type: ObjectType, |
| 24 | + updateArgs: { |
| 25 | + set: { |
| 26 | + nonNullScalar: null |
| 27 | + } |
| 28 | + }, |
| 29 | + expectedErrors: [] |
| 30 | + }, { |
| 31 | + description: "Should invalidate non-null on update list item", |
| 32 | + type: ObjectType, |
| 33 | + updateArgs: { |
| 34 | + set: { |
| 35 | + nonNullScalar: null, |
| 36 | + nestedList: [{ |
| 37 | + }] |
| 38 | + } |
| 39 | + }, |
| 40 | + expectedErrors: ["Missing non-nullable field \"nestedList.nonNullList\"", "Missing non-nullable field \"nestedList.nonNullScalar\""] |
| 41 | + }, { |
| 42 | + description: "Should validate update correct non-null", |
| 43 | + type: ObjectType, |
| 44 | + updateArgs: { |
| 45 | + setOnInsert: { |
| 46 | + stringScalar: "x", |
| 47 | + }, |
| 48 | + set: { |
| 49 | + nonNullScalar: "x", |
| 50 | + nonNullList: [] |
| 51 | + } |
| 52 | + }, |
| 53 | + expectedErrors: [] |
| 54 | + }]; |
| 55 | + |
| 56 | + tests.forEach(test => it(test.description, () => { |
| 57 | + // Arrange |
| 58 | + let error; |
| 59 | + |
| 60 | + // Act |
| 61 | + try { |
| 62 | + validateUpdateArgs(test.updateArgs, test.type); |
| 63 | + } catch (err) { |
| 64 | + error = err; |
| 65 | + } |
| 66 | + |
| 67 | + // Assert |
| 68 | + if (test.expectedErrors.length > 0) { |
| 69 | + expect(error, "error object expected").to.not.be.undefined; |
| 70 | + |
| 71 | + const errorString: string = typeof error == "string" ? error : (error as Error).message; |
| 72 | + const errors = errorString.split("\n"); |
| 73 | + expect(errors).to.have.members(test.expectedErrors, "Should detect correct errors"); |
| 74 | + } else { |
| 75 | + if (error) throw error |
| 76 | + } |
| 77 | + })); |
| 78 | + }); |
| 79 | + |
8 | 80 | describe("validateNonNullableFields", () => { |
9 | 81 | const tests: { description: string, type: GraphQLObjectType, updateArgs: UpdateArgs, expectedErrors: string[] }[] = [{ |
10 | 82 | description: "Should invalidate root fields", |
@@ -91,7 +163,7 @@ describe("mongoDbProjection", () => { |
91 | 163 | const objects = Object.keys(test.updateArgs).map(_ => test.updateArgs[_]); |
92 | 164 |
|
93 | 165 | // Act |
94 | | - const errors = validateNonNullableFields(objects, test.type); |
| 166 | + const errors = validateNonNullableFields(objects, test.type, true); |
95 | 167 |
|
96 | 168 | // Assert |
97 | 169 | expect(errors).to.have.members(test.expectedErrors, "Should detect correct errors"); |
@@ -217,7 +289,7 @@ describe("mongoDbProjection", () => { |
217 | 289 |
|
218 | 290 | tests.forEach(test => it(test.description, () => { |
219 | 291 | // act |
220 | | - const errors = validateNonNullableFieldsTraverse(test.objects, test.type.getFields(), test.path); |
| 292 | + const errors = validateNonNullableFieldsTraverse(test.objects, test.type.getFields(), true, test.path); |
221 | 293 |
|
222 | 294 | // Assert |
223 | 295 | expect(errors).to.have.members(test.expectedErrors, "Should detect correct errors"); |
|
0 commit comments