Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 68 additions & 85 deletions src/dataconnect/names.spec.ts
Original file line number Diff line number Diff line change
@@ -1,93 +1,76 @@
import { expect } from "chai";
import * as names from "./names";
import {
parseServiceName,
parseConnectorName,
parseCloudSQLInstanceName,
isGraphqlName,
} from "./names";

describe("names.ts", () => {
describe("dataconnect/names", () => {
describe("parseServiceName", () => {
const cases: {
desc: string;
input: string;
want: {
projectId?: string;
location?: string;
serviceId?: string;
error?: boolean;
};
}[] = [
{
desc: "should parse a well formed service name, and convert back",
input: "projects/proj/locations/us-central1/services/serve",
want: {
projectId: "proj",
location: "us-central1",
serviceId: "serve",
},
},
{
desc: "should error on an invalid service name",
input: "projects/proj/locations/us-central1/functions/funky",
want: {
error: true,
},
},
];
for (const c of cases) {
it(c.desc, () => {
try {
const got = names.parseServiceName(c.input);
expect(got.projectId).to.equal(c.want.projectId);
expect(got.location).to.equal(c.want.location);
expect(got.serviceId).to.equal(c.want.serviceId);
expect(got.toString()).to.equal(c.input);
} catch (err) {
expect(c.want.error, `Unexpected error: ${err}`).to.be.true;
}
});
}
it("should parse valid service name", () => {
const name = "projects/my-project/locations/us-central1/services/my-service";
const res = parseServiceName(name);
expect(res.projectId).to.equal("my-project");
expect(res.location).to.equal("us-central1");
expect(res.serviceId).to.equal("my-service");
expect(res.toString()).to.equal(name);
});

it("should throw on invalid service name", () => {
expect(() => parseServiceName("invalid-name")).to.throw(/not a valid service name/);
});
});

describe("parseConnectorName", () => {
const cases: {
desc: string;
input: string;
want: {
projectId?: string;
location?: string;
serviceId?: string;
connectorId?: string;
error?: boolean;
};
}[] = [
{
desc: "should parse a well formed service name, and convert back",
input: "projects/proj/locations/us-central1/services/serve/connectors/connect",
want: {
projectId: "proj",
location: "us-central1",
serviceId: "serve",
connectorId: "connect",
},
},
{
desc: "should error on an invalid connector name",
input: "projects/proj/locations/us-central1/functions/funky",
want: {
error: true,
},
},
];
for (const c of cases) {
it(c.desc, () => {
try {
const got = names.parseConnectorName(c.input);
expect(got.projectId).to.equal(c.want.projectId);
expect(got.location).to.equal(c.want.location);
expect(got.serviceId).to.equal(c.want.serviceId);
expect(got.connectorId).to.equal(c.want.connectorId);
expect(got.toString()).to.equal(c.input);
} catch (err) {
expect(c.want.error, `Unexpected error: ${err}`).to.be.true;
}
});
}
it("should parse valid connector name", () => {
const name =
"projects/my-project/locations/us-central1/services/my-service/connectors/my-connector";
const res = parseConnectorName(name);
expect(res.projectId).to.equal("my-project");
expect(res.location).to.equal("us-central1");
expect(res.serviceId).to.equal("my-service");
expect(res.connectorId).to.equal("my-connector");
expect(res.toString()).to.equal(name);
});

it("should throw on invalid connector name", () => {
expect(() => parseConnectorName("projects/my-project/services/only")).to.throw(
/not a valid connector name/,
);
});
});

describe("parseCloudSQLInstanceName", () => {
it("should parse valid CloudSQL instance name", () => {
const name = "projects/my-project/locations/us-central1/instances/my-instance";
const res = parseCloudSQLInstanceName(name);
expect(res.projectId).to.equal("my-project");
expect(res.location).to.equal("us-central1");
expect(res.instanceId).to.equal("my-instance");
expect(res.toString()).to.equal(
"projects/my-project/locations/us-central1/instances/my-instance",
);
Comment thread
joehan marked this conversation as resolved.
});

it("should throw on invalid CloudSQL instance name", () => {
expect(() => parseCloudSQLInstanceName("invalid")).to.throw(
/not a valid cloudSQL instance name/,
);
});
});

describe("isGraphqlName", () => {
it("should validate correct names", () => {
expect(isGraphqlName("User")).to.be.true;
expect(isGraphqlName("_test")).to.be.true;
expect(isGraphqlName("Test123")).to.be.true;
});

it("should invalidate wrong names", () => {
expect(isGraphqlName("1User")).to.be.false;
expect(isGraphqlName("user-name")).to.be.false;
expect(isGraphqlName("")).to.be.false;
});
});
});
2 changes: 1 addition & 1 deletion src/dataconnect/names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
}

const serviceNameRegex =
/projects\/(?<projectId>[^\/]+)\/locations\/(?<location>[^\/]+)\/services\/(?<serviceId>[^\/]+)/;

Check warning on line 11 in src/dataconnect/names.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unnecessary escape character: \/

Check warning on line 11 in src/dataconnect/names.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unnecessary escape character: \/

Check warning on line 11 in src/dataconnect/names.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unnecessary escape character: \/

export function parseServiceName(serviceName: string): serviceName {

Check warning on line 13 in src/dataconnect/names.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
const res = serviceNameRegex.exec(serviceName);
const projectId = res?.groups?.projectId;
const location = res?.groups?.location;
Expand All @@ -18,7 +18,7 @@
if (!projectId || !location || !serviceId) {
throw new FirebaseError(`${serviceName} is not a valid service name`);
}
const toString = () => {

Check warning on line 21 in src/dataconnect/names.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing return type on function
return `projects/${projectId}/locations/${location}/services/${serviceId}`;
};
return {
Expand All @@ -38,9 +38,9 @@
}

const connectorNameRegex =
/projects\/(?<projectId>[^\/]+)\/locations\/(?<location>[^\/]+)\/services\/(?<serviceId>[^\/]+)\/connectors\/(?<connectorId>[^\/]+)/;

Check warning on line 41 in src/dataconnect/names.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unnecessary escape character: \/

Check warning on line 41 in src/dataconnect/names.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unnecessary escape character: \/

Check warning on line 41 in src/dataconnect/names.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unnecessary escape character: \/

Check warning on line 41 in src/dataconnect/names.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unnecessary escape character: \/

export function parseConnectorName(connectorName: string): connectorName {

Check warning on line 43 in src/dataconnect/names.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
const res = connectorNameRegex.exec(connectorName);
const projectId = res?.groups?.projectId;
const location = res?.groups?.location;
Expand Down Expand Up @@ -80,7 +80,7 @@
throw new FirebaseError(`${cloudSQLInstanceName} is not a valid cloudSQL instance name`);
}
const toString = () => {
return `projects/${projectId}/locations/${location}/services/${instanceId}`;
return `projects/${projectId}/locations/${location}/instances/${instanceId}`;
};
return {
projectId,
Expand Down
Loading