Skip to content

Commit de33c6a

Browse files
authored
Allow running tests against DC (#1519)
1 parent 0b9131e commit de33c6a

File tree

2 files changed

+38
-33
lines changed

2 files changed

+38
-33
lines changed

test/integration/smoke.test.ts

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,28 @@ if (host === null) {
1515

1616
const api = new XataApiClient({ apiKey: process.env.XATA_API_KEY, host });
1717
const region = process.env.XATA_REGION || 'eu-west-1';
18+
const clusterId = process.env.XATA_CLUSTER_ID ?? 'shared-cluster';
19+
const hash = Math.random().toString(36).substr(2, 9);
1820

19-
const getWorkspaceName = () => `sdk-integration-api-client-${Math.random().toString(36).substr(2, 9)}`;
21+
// For shared-cluster, we create a new workspace with a unique name
22+
// while in dedicated clusters, we use the provided workspace name
23+
const workspaceName = clusterId === 'shared-cluster' ? `sdk-smoke-${hash}` : process.env.XATA_WORKSPACE;
24+
if (!workspaceName) throw new Error('XATA_WORKSPACE environment variable is not set');
2025

2126
describe('API Client Integration Tests', () => {
2227
test('Create, get and delete workspace with new apiKey', async () => {
23-
const workspaceName = getWorkspaceName();
24-
25-
const newApiKey = await api.authentication.createUserAPIKey({ pathParams: { keyName: `${workspaceName}-key` } });
28+
const newApiKey = await api.authentication.createUserAPIKey({ pathParams: { keyName: `smoke-${hash}-key` } });
2629

2730
expect(newApiKey).toBeDefined();
28-
expect(newApiKey.name).toBe(`${workspaceName}-key`);
31+
expect(newApiKey.name).toBe(`smoke-${hash}-key`);
2932
expect(newApiKey.key).toBeDefined();
3033

34+
const workspace = await getOrCreateWorkspace(workspaceName);
3135
const newApi = new XataApiClient({ apiKey: newApiKey.key, host });
3236

33-
const { id: workspace, name } = await newApi.workspaces.createWorkspace({
34-
body: { name: workspaceName, slug: `${workspaceName}-slug` }
35-
});
36-
37-
await waitForReplication(newApi, workspace);
38-
39-
expect(workspace).toBeDefined();
40-
expect(name).toBe(workspaceName);
41-
42-
console.log('Created workspace', workspace);
43-
44-
const foo = await newApi.workspaces.getWorkspace({ pathParams: { workspaceId: workspace } });
45-
46-
expect(foo.id).toBe(workspace);
47-
expect(foo.slug).toBe(`${workspaceName}-slug`);
48-
49-
const bar = await newApi.workspaces.getWorkspace({ pathParams: { workspaceId: workspace } });
50-
51-
expect(bar.id).toBe(workspace);
52-
expect(bar.slug).toBe(`${workspaceName}-slug`);
53-
5437
const { databaseName: database } = await newApi.databases.createDatabase({
55-
pathParams: { workspaceId: workspace, dbName: `data-${workspace}` },
56-
body: { region }
38+
pathParams: { workspaceId: workspace, dbName: `data-${workspace}-${hash}` },
39+
body: { region, defaultClusterID: clusterId }
5740
});
5841

5942
await waitForReplication(newApi, workspace, database);
@@ -122,14 +105,35 @@ describe('API Client Integration Tests', () => {
122105

123106
console.log('Deleted API key, record is no longer accessible');
124107

125-
await api.workspaces.deleteWorkspace({ pathParams: { workspaceId: workspace } });
108+
if (clusterId === 'shared-cluster') {
109+
await api.workspaces.deleteWorkspace({ pathParams: { workspaceId: workspace } });
126110

127-
await expect(api.workspaces.getWorkspace({ pathParams: { workspaceId: workspace } })).rejects.toHaveProperty(
128-
'message'
129-
);
111+
await expect(api.workspaces.getWorkspace({ pathParams: { workspaceId: workspace } })).rejects.toHaveProperty(
112+
'message'
113+
);
114+
}
130115
});
131116
});
132117

118+
async function getOrCreateWorkspace(workspaceName: string): Promise<string> {
119+
if (clusterId === 'shared-cluster') {
120+
const { id: workspace, name } = await api.workspaces.createWorkspace({
121+
body: { name: workspaceName, slug: `${workspaceName}-slug` }
122+
});
123+
124+
await waitForReplication(api, workspace);
125+
126+
expect(workspace).toBeDefined();
127+
expect(name).toBe(workspaceName);
128+
129+
console.log('Created workspace', workspace);
130+
131+
return workspace;
132+
}
133+
134+
return workspaceName;
135+
}
136+
133137
async function waitForReplication(api: XataApiClient, workspace: string, database?: string): Promise<void> {
134138
try {
135139
if (database === undefined) {

test/utils/setup.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const workspace = process.env.XATA_WORKSPACE ?? '';
2525
if (workspace === '') throw new Error('XATA_WORKSPACE environment variable is not set');
2626

2727
const host = parseProviderString(process.env.XATA_API_PROVIDER);
28+
const clusterId = process.env.XATA_CLUSTER_ID ?? 'shared-cluster';
2829

2930
const region = process.env.XATA_REGION || 'us-east-1';
3031

@@ -79,7 +80,7 @@ export async function setUpTestEnvironment(
7980

8081
const { databaseName: database } = await api.databases.createDatabase({
8182
pathParams: { workspaceId: workspace, dbName: `sdk-integration-test-${prefix}-${id}` },
82-
body: { region },
83+
body: { region, defaultClusterID: clusterId },
8384
headers: { 'X-Features': 'feat-pgroll-migrations=1' }
8485
});
8586

0 commit comments

Comments
 (0)