Skip to content

Commit

Permalink
Allow running tests against DC (#1519)
Browse files Browse the repository at this point in the history
  • Loading branch information
SferaDev committed Jul 4, 2024
1 parent 3ef6df2 commit 76227bc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
68 changes: 36 additions & 32 deletions test/integration/smoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,28 @@ if (host === null) {

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

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

describe('API Client Integration Tests', () => {
test('Create, get and delete workspace with new apiKey', async () => {
const workspaceName = getWorkspaceName();

const newApiKey = await api.authentication.createUserAPIKey({ pathParams: { keyName: `${workspaceName}-key` } });
const newApiKey = await api.authentication.createUserAPIKey({ pathParams: { keyName: `smoke-${hash}-key` } });

expect(newApiKey).toBeDefined();
expect(newApiKey.name).toBe(`${workspaceName}-key`);
expect(newApiKey.name).toBe(`smoke-${hash}-key`);
expect(newApiKey.key).toBeDefined();

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

const { id: workspace, name } = await newApi.workspaces.createWorkspace({
body: { name: workspaceName, slug: `${workspaceName}-slug` }
});

await waitForReplication(newApi, workspace);

expect(workspace).toBeDefined();
expect(name).toBe(workspaceName);

console.log('Created workspace', workspace);

const foo = await newApi.workspaces.getWorkspace({ pathParams: { workspaceId: workspace } });

expect(foo.id).toBe(workspace);
expect(foo.slug).toBe(`${workspaceName}-slug`);

const bar = await newApi.workspaces.getWorkspace({ pathParams: { workspaceId: workspace } });

expect(bar.id).toBe(workspace);
expect(bar.slug).toBe(`${workspaceName}-slug`);

const { databaseName: database } = await newApi.databases.createDatabase({
pathParams: { workspaceId: workspace, dbName: `data-${workspace}` },
body: { region }
pathParams: { workspaceId: workspace, dbName: `data-${workspace}-${hash}` },
body: { region, defaultClusterID: clusterId }
});

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

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

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

await expect(api.workspaces.getWorkspace({ pathParams: { workspaceId: workspace } })).rejects.toHaveProperty(
'message'
);
await expect(api.workspaces.getWorkspace({ pathParams: { workspaceId: workspace } })).rejects.toHaveProperty(
'message'
);
}
});
});

async function getOrCreateWorkspace(workspaceName: string): Promise<string> {
if (clusterId === 'shared-cluster') {
const { id: workspace, name } = await api.workspaces.createWorkspace({
body: { name: workspaceName, slug: `${workspaceName}-slug` }
});

await waitForReplication(api, workspace);

expect(workspace).toBeDefined();
expect(name).toBe(workspaceName);

console.log('Created workspace', workspace);

return workspace;
}

return workspaceName;
}

async function waitForReplication(api: XataApiClient, workspace: string, database?: string): Promise<void> {
try {
if (database === undefined) {
Expand Down
3 changes: 2 additions & 1 deletion test/utils/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const workspace = process.env.XATA_WORKSPACE ?? '';
if (workspace === '') throw new Error('XATA_WORKSPACE environment variable is not set');

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

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

Expand Down Expand Up @@ -79,7 +80,7 @@ export async function setUpTestEnvironment(

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

Expand Down

0 comments on commit 76227bc

Please sign in to comment.