From ffef819353024335c7beefc47d37958b91899df3 Mon Sep 17 00:00:00 2001 From: Aidan Cunniffe Date: Tue, 19 Apr 2022 13:21:06 -0400 Subject: [PATCH] fix: add tests --- .../__snapshots__/properties.test.ts.snap | 381 ++++++++++++++++++ src/rulesets/tests/properties.test.ts | 50 +++ 2 files changed, 431 insertions(+) diff --git a/src/rulesets/tests/__snapshots__/properties.test.ts.snap b/src/rulesets/tests/__snapshots__/properties.test.ts.snap index ec974a6e..e8e4378f 100644 --- a/src/rulesets/tests/__snapshots__/properties.test.ts.snap +++ b/src/rulesets/tests/__snapshots__/properties.test.ts.snap @@ -830,6 +830,387 @@ Object { } `; +exports[`body properties key fails when number in n=1 snake case component 1`] = ` +Object { + "base": Object { + "info": Object { + "title": "OpenAPI", + "version": "0.0.0", + }, + "openapi": "3.0.1", + "paths": Object { + "/example": Object { + "get": Object { + "responses": Object {}, + }, + }, + }, + }, + "changelog": Array [ + Object { + "added": Object { + "contentType": "application/json", + "flatSchema": Object { + "type": "object", + }, + }, + "changeType": "added", + "location": Object { + "conceptualLocation": Object { + "inResponse": Object { + "body": Object { + "contentType": "application/json", + }, + "statusCode": "200", + }, + "method": "get", + "path": "/example", + }, + "conceptualPath": Array [ + "operations", + "/example", + "get", + "responses", + "200", + "application/json", + ], + "jsonPath": "/paths/~1example/get/responses/200/content/application~1json", + "kind": "body", + }, + }, + Object { + "added": Object { + "flatSchema": Object { + "type": "string", + }, + "key": "30_days", + "required": false, + }, + "changeType": "added", + "location": Object { + "conceptualLocation": Object { + "inResponse": Object { + "body": Object { + "contentType": "application/json", + }, + "statusCode": "200", + }, + "jsonSchemaTrail": Array [ + "30_days", + ], + "method": "get", + "path": "/example", + }, + "conceptualPath": Array [ + "operations", + "/example", + "get", + "responses", + "200", + "application/json", + "30_days", + ], + "jsonPath": "/paths/~1example/get/responses/200/content/application~1json/schema/properties/30_days", + "kind": "field", + }, + }, + Object { + "added": Object { + "description": "", + "statusCode": "200", + }, + "changeType": "added", + "location": Object { + "conceptualLocation": Object { + "inResponse": Object { + "statusCode": "200", + }, + "method": "get", + "path": "/example", + }, + "conceptualPath": Array [ + "operations", + "/example", + "get", + "responses", + "200", + ], + "jsonPath": "/paths/~1example/get/responses/200", + "kind": "response", + }, + }, + ], + "next": Object { + "info": Object { + "title": "OpenAPI", + "version": "0.0.0", + }, + "openapi": "3.0.1", + "paths": Object { + "/example": Object { + "get": Object { + "responses": Object { + "200": Object { + "content": Object { + "application/json": Object { + "schema": Object { + "properties": Object { + "30_days": Object { + "type": "string", + }, + }, + "type": "object", + }, + }, + }, + "description": "", + }, + }, + }, + }, + }, + }, + "results": Array [ + Object { + "change": Object { + "added": Object { + "flatSchema": Object { + "type": "string", + }, + "key": "30_days", + "required": false, + }, + "changeType": "added", + "location": Object { + "conceptualLocation": Object { + "inResponse": Object { + "body": Object { + "contentType": "application/json", + }, + "statusCode": "200", + }, + "jsonSchemaTrail": Array [ + "30_days", + ], + "method": "get", + "path": "/example", + }, + "conceptualPath": Array [ + "operations", + "/example", + "get", + "responses", + "200", + "application/json", + "30_days", + ], + "jsonPath": "/paths/~1example/get/responses/200/content/application~1json/schema/properties/30_days", + "kind": "field", + }, + }, + "condition": "have snake case keys", + "docsLink": undefined, + "effectiveOnDate": undefined, + "error": "expected response property 30_days is be snake case", + "isMust": true, + "isShould": false, + "passed": false, + "where": "added field: 30_days", + }, + ], +} +`; + +exports[`body properties key passes when snake case with a number as an n+1 component 1`] = ` +Object { + "base": Object { + "info": Object { + "title": "OpenAPI", + "version": "0.0.0", + }, + "openapi": "3.0.1", + "paths": Object { + "/example": Object { + "get": Object { + "responses": Object {}, + }, + }, + }, + }, + "changelog": Array [ + Object { + "added": Object { + "contentType": "application/json", + "flatSchema": Object { + "type": "object", + }, + }, + "changeType": "added", + "location": Object { + "conceptualLocation": Object { + "inResponse": Object { + "body": Object { + "contentType": "application/json", + }, + "statusCode": "200", + }, + "method": "get", + "path": "/example", + }, + "conceptualPath": Array [ + "operations", + "/example", + "get", + "responses", + "200", + "application/json", + ], + "jsonPath": "/paths/~1example/get/responses/200/content/application~1json", + "kind": "body", + }, + }, + Object { + "added": Object { + "flatSchema": Object { + "type": "string", + }, + "key": "is_allowed_after_30_days", + "required": false, + }, + "changeType": "added", + "location": Object { + "conceptualLocation": Object { + "inResponse": Object { + "body": Object { + "contentType": "application/json", + }, + "statusCode": "200", + }, + "jsonSchemaTrail": Array [ + "is_allowed_after_30_days", + ], + "method": "get", + "path": "/example", + }, + "conceptualPath": Array [ + "operations", + "/example", + "get", + "responses", + "200", + "application/json", + "is_allowed_after_30_days", + ], + "jsonPath": "/paths/~1example/get/responses/200/content/application~1json/schema/properties/is_allowed_after_30_days", + "kind": "field", + }, + }, + Object { + "added": Object { + "description": "", + "statusCode": "200", + }, + "changeType": "added", + "location": Object { + "conceptualLocation": Object { + "inResponse": Object { + "statusCode": "200", + }, + "method": "get", + "path": "/example", + }, + "conceptualPath": Array [ + "operations", + "/example", + "get", + "responses", + "200", + ], + "jsonPath": "/paths/~1example/get/responses/200", + "kind": "response", + }, + }, + ], + "next": Object { + "info": Object { + "title": "OpenAPI", + "version": "0.0.0", + }, + "openapi": "3.0.1", + "paths": Object { + "/example": Object { + "get": Object { + "responses": Object { + "200": Object { + "content": Object { + "application/json": Object { + "schema": Object { + "properties": Object { + "is_allowed_after_30_days": Object { + "type": "string", + }, + }, + "type": "object", + }, + }, + }, + "description": "", + }, + }, + }, + }, + }, + }, + "results": Array [ + Object { + "change": Object { + "added": Object { + "flatSchema": Object { + "type": "string", + }, + "key": "is_allowed_after_30_days", + "required": false, + }, + "changeType": "added", + "location": Object { + "conceptualLocation": Object { + "inResponse": Object { + "body": Object { + "contentType": "application/json", + }, + "statusCode": "200", + }, + "jsonSchemaTrail": Array [ + "is_allowed_after_30_days", + ], + "method": "get", + "path": "/example", + }, + "conceptualPath": Array [ + "operations", + "/example", + "get", + "responses", + "200", + "application/json", + "is_allowed_after_30_days", + ], + "jsonPath": "/paths/~1example/get/responses/200/content/application~1json/schema/properties/is_allowed_after_30_days", + "kind": "field", + }, + }, + "condition": "have snake case keys", + "docsLink": undefined, + "effectiveOnDate": undefined, + "isMust": true, + "isShould": false, + "passed": true, + "where": "added field: is_allowed_after_30_days", + }, + ], +} +`; + exports[`body properties key passes when snake case with more than one component 1`] = ` Object { "base": Object { diff --git a/src/rulesets/tests/properties.test.ts b/src/rulesets/tests/properties.test.ts index 1fc54e5f..300ca9be 100644 --- a/src/rulesets/tests/properties.test.ts +++ b/src/rulesets/tests/properties.test.ts @@ -56,6 +56,56 @@ describe("body properties", () => { expect(result).toMatchSnapshot(); }); + it("passes when snake case with a number as an n+1 component ", async () => { + const result = await compare(baseOpenAPI) + .to((spec) => { + spec.paths!["/example"]!.get!.responses = { + "200": { + description: "", + content: { + "application/json": { + schema: { + type: "object", + properties: { + is_allowed_after_30_days: { type: "string" }, + }, + }, + }, + }, + }, + }; + return spec; + }) + .withRule(rules.propertyKey, emptyContext); + + expect(result.results[0].passed).toBeTruthy(); + expect(result).toMatchSnapshot(); + }); it("fails when number in n=1 snake case component ", async () => { + const result = await compare(baseOpenAPI) + .to((spec) => { + spec.paths!["/example"]!.get!.responses = { + "200": { + description: "", + content: { + "application/json": { + schema: { + type: "object", + properties: { + "30_days": { type: "string" }, + }, + }, + }, + }, + }, + }; + return spec; + }) + .withRule(rules.propertyKey, emptyContext); + + expect(result.results[0].passed).toBeFalsy(); + expect(result).toMatchSnapshot(); + }); + it("passes when snake case with one component", async () => { const result = await compare(baseOpenAPI) .to((spec) => {