Skip to content

Commit c2e3494

Browse files
feat(dataconnect): Add unit tests for dataconnect files (#8943)
* feat(dataconnect): Add unit tests for dataconnect files Adds unit tests for the following files in `src/dataconnect`: - `errors.ts` - `graphqlError.ts` - `prompts.ts` This improves test coverage for the dataconnect feature. * feat(dataconnect): Add unit tests for dataconnect files Adds unit tests for the following files in `src/dataconnect`: - `errors.ts` - `graphqlError.ts` - `prompts.ts` This improves test coverage for the dataconnect feature. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent db27b6f commit c2e3494

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed

src/dataconnect/errors.spec.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { expect } from "chai";
2+
import { getIncompatibleSchemaError, getInvalidConnectors } from "./errors";
3+
4+
describe("errors", () => {
5+
describe("getIncompatibleSchemaError", () => {
6+
it("should return undefined if no incompatible schema error", () => {
7+
const err = {
8+
context: {
9+
body: {
10+
error: {
11+
details: [{ "@type": "some.other.Error" }],
12+
},
13+
},
14+
},
15+
};
16+
expect(getIncompatibleSchemaError(err)).to.be.undefined;
17+
});
18+
19+
it("should extract violation type from precondition failure", () => {
20+
const err = {
21+
context: {
22+
body: {
23+
error: {
24+
details: [
25+
{
26+
"@type": "type.googleapis.com/google.rpc.PreconditionFailure",
27+
violations: [{ type: "INCOMPATIBLE_SCHEMA" }],
28+
},
29+
{ "@type": "IncompatibleSqlSchemaError" },
30+
],
31+
},
32+
},
33+
},
34+
};
35+
const result = getIncompatibleSchemaError(err);
36+
expect(result).to.not.be.undefined;
37+
expect(result?.violationType).to.equal("INCOMPATIBLE_SCHEMA");
38+
});
39+
});
40+
41+
describe("getInvalidConnectors", () => {
42+
it("should return an empty array if no invalid connectors", () => {
43+
const err = {
44+
context: {
45+
body: {
46+
error: {
47+
details: [{ "@type": "some.other.Error" }],
48+
},
49+
},
50+
},
51+
};
52+
expect(getInvalidConnectors(err)).to.be.empty;
53+
});
54+
55+
it("should extract invalid connectors from precondition failure", () => {
56+
const err = {
57+
context: {
58+
body: {
59+
error: {
60+
details: [
61+
{
62+
"@type": "type.googleapis.com/google.rpc.PreconditionFailure",
63+
violations: [
64+
{ type: "INCOMPATIBLE_CONNECTOR", subject: "users" },
65+
{ type: "INCOMPATIBLE_CONNECTOR", subject: "posts" },
66+
],
67+
},
68+
],
69+
},
70+
},
71+
},
72+
};
73+
const result = getInvalidConnectors(err);
74+
expect(result).to.deep.equal(["users", "posts"]);
75+
});
76+
});
77+
});

src/dataconnect/graphqlError.spec.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { expect } from "chai";
2+
import { prettify, prettifyTable } from "./graphqlError";
3+
import { GraphqlError } from "./types";
4+
5+
describe("graphqlError", () => {
6+
describe("prettify", () => {
7+
it("should format a simple error", () => {
8+
const err: GraphqlError = {
9+
message: "Something went wrong",
10+
path: ["users", 0, "name"],
11+
locations: [{ line: 10, column: 2 }],
12+
extensions: { file: "users.gql" },
13+
};
14+
const result = prettify(err);
15+
expect(result).to.equal("users.gql:10: On users[0].name: Something went wrong");
16+
});
17+
18+
it("should handle missing path", () => {
19+
const err: GraphqlError = {
20+
message: "Another issue",
21+
locations: [{ line: 5, column: 1 }],
22+
extensions: { file: "posts.gql" },
23+
};
24+
const result = prettify(err);
25+
expect(result).to.equal("posts.gql:5: Another issue");
26+
});
27+
});
28+
29+
describe("prettifyTable", () => {
30+
it("should format a list of errors into a table", () => {
31+
const errs: GraphqlError[] = [
32+
{
33+
message: "BREAKING: A breaking change",
34+
path: ["users"],
35+
locations: [{ line: 1, column: 1 }],
36+
extensions: {
37+
file: "schema.gql",
38+
workarounds: [{ description: "Do this", reason: "Because", replaceWith: "That" }],
39+
},
40+
},
41+
{
42+
message: "INSECURE: An insecure change",
43+
path: ["posts"],
44+
locations: [{ line: 2, column: 2 }],
45+
extensions: {
46+
file: "schema.gql",
47+
},
48+
},
49+
];
50+
const result = prettifyTable(errs);
51+
// We don't assert the exact table output due to formatting,
52+
// but we can check for key elements.
53+
expect(result).to.include("BREAKING");
54+
expect(result).to.include("A breaking change");
55+
expect(result).to.include("Do this");
56+
expect(result).to.include("Because");
57+
expect(result).to.include("INSECURE");
58+
expect(result).to.include("An insecure change");
59+
});
60+
});
61+
});

src/dataconnect/prompts.spec.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { expect } from "chai";
2+
import * as sinon from "sinon";
3+
import * as prompt from "../prompt";
4+
import * as client from "./client";
5+
import { promptDeleteConnector } from "./prompts";
6+
7+
describe("prompts", () => {
8+
let confirmStub: sinon.SinonStub;
9+
let deleteConnectorStub: sinon.SinonStub;
10+
11+
beforeEach(() => {
12+
confirmStub = sinon.stub(prompt, "confirm");
13+
deleteConnectorStub = sinon.stub(client, "deleteConnector");
14+
});
15+
16+
afterEach(() => {
17+
sinon.restore();
18+
});
19+
20+
describe("promptDeleteConnector", () => {
21+
it("should delete connector if user confirms", async () => {
22+
confirmStub.resolves(true);
23+
deleteConnectorStub.resolves();
24+
25+
await promptDeleteConnector({ force: false, nonInteractive: false }, "my-connector");
26+
27+
expect(confirmStub.calledOnce).to.be.true;
28+
expect(deleteConnectorStub.calledOnceWith("my-connector")).to.be.true;
29+
});
30+
31+
it("should not delete connector if user denies", async () => {
32+
confirmStub.resolves(false);
33+
34+
await promptDeleteConnector({ force: false, nonInteractive: false }, "my-connector");
35+
36+
expect(confirmStub.calledOnce).to.be.true;
37+
expect(deleteConnectorStub.notCalled).to.be.true;
38+
});
39+
});
40+
});

0 commit comments

Comments
 (0)