Skip to content

Commit 6b794c1

Browse files
committed
refactor(cli): clean up bookmarks test - shared DDB client, config-based table lookup
Remove ListTablesCommand discovery in favor of reading table name from amplifyconfiguration.json. Share a single DynamoDBDocumentClient across all bookmark test functions instead of creating one per call.
1 parent d207e68 commit 6b794c1

1 file changed

Lines changed: 13 additions & 28 deletions

File tree

amplify-migration-apps/discussions/test-utils.ts

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Amplify } from 'aws-amplify';
44
import { generateClient } from 'aws-amplify/api';
55
import { getCurrentUser } from 'aws-amplify/auth';
66
import { uploadData, getUrl, remove } from 'aws-amplify/storage';
7-
import { DynamoDBClient, ListTablesCommand } from '@aws-sdk/client-dynamodb';
7+
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
88
import { DynamoDBDocumentClient, PutCommand, GetCommand, DeleteCommand } from '@aws-sdk/lib-dynamodb';
99
import { getTopic, listTopics, getPost, listPosts, getComment, listComments, fetchUserActivity } from './src/graphql/queries';
1010
import {
@@ -373,14 +373,15 @@ export function createTestFunctions() {
373373
// Bookmarks DDB Test Functions
374374
// ============================================================
375375

376+
const ddbClient = DynamoDBDocumentClient.from(
377+
new DynamoDBClient({ region: (amplifyconfig as any).aws_project_region }),
378+
);
379+
376380
async function testCreateBookmark(tableName: string, userId: string, postId: string): Promise<void> {
377381
console.log('\n🔖 Testing PutItem (create bookmark)...');
378382
console.log(` Table: ${tableName}`);
379383
console.log(` userId: ${userId.substring(0, 8)}..., postId: ${postId.substring(0, 8)}...`);
380384

381-
const ddbClient = DynamoDBDocumentClient.from(
382-
new DynamoDBClient({ region: (amplifyconfig as any).aws_project_region }),
383-
);
384385
await ddbClient.send(
385386
new PutCommand({
386387
TableName: tableName,
@@ -395,9 +396,6 @@ export function createTestFunctions() {
395396
console.log(` Table: ${tableName}`);
396397
console.log(` userId: ${userId.substring(0, 8)}..., postId: ${postId.substring(0, 8)}...`);
397398

398-
const ddbClient = DynamoDBDocumentClient.from(
399-
new DynamoDBClient({ region: (amplifyconfig as any).aws_project_region }),
400-
);
401399
const result = await ddbClient.send(
402400
new GetCommand({
403401
TableName: tableName,
@@ -419,9 +417,6 @@ export function createTestFunctions() {
419417
console.log(` Table: ${tableName}`);
420418
console.log(` userId: ${userId.substring(0, 8)}..., postId: ${postId.substring(0, 8)}...`);
421419

422-
const ddbClient = DynamoDBDocumentClient.from(
423-
new DynamoDBClient({ region: (amplifyconfig as any).aws_project_region }),
424-
);
425420
await ddbClient.send(
426421
new DeleteCommand({
427422
TableName: tableName,
@@ -457,22 +452,13 @@ export function createTestFunctions() {
457452
};
458453
}
459454

460-
// ============================================================
461-
// Bookmarks Table Discovery
462-
// ============================================================
463-
464-
export async function discoverBookmarksTable(region: string): Promise<string> {
465-
const client = new DynamoDBClient({ region });
466-
const result = await client.send(new ListTablesCommand({}));
467-
const tables = result.TableNames ?? [];
468-
const bookmarksTables = tables.filter((t) => t.startsWith('bookmarks-'));
469-
if (bookmarksTables.length === 0) {
470-
throw new Error('No bookmarks table found. Expected a DynamoDB table starting with "bookmarks-".');
471-
}
472-
if (bookmarksTables.length > 1) {
473-
console.log(`⚠️ Found multiple bookmarks tables: ${bookmarksTables.join(', ')}. Using first one.`);
455+
function getBookmarksTableName(): string {
456+
const schemas = (amplifyconfig as any).aws_dynamodb_table_schemas ?? [];
457+
const entry = schemas.find((s: any) => s.tableName?.startsWith('bookmarks-'));
458+
if (!entry) {
459+
throw new Error('No bookmarks table found in amplifyconfiguration.json');
474460
}
475-
return bookmarksTables[0];
461+
return entry.tableName;
476462
}
477463

478464
// ============================================================
@@ -593,9 +579,8 @@ export function createTestOrchestrator(testFunctions: ReturnType<typeof createTe
593579
console.log('🔖 PART 7: Bookmarks DDB');
594580
console.log('='.repeat(60));
595581

596-
const region = (amplifyconfig as any).aws_project_region || 'us-east-1';
597-
const tableName = await discoverBookmarksTable(region);
598-
console.log(` Discovered bookmarks table: ${tableName}`);
582+
const tableName = getBookmarksTableName();
583+
console.log(` Bookmarks table: ${tableName}`);
599584

600585
await runner.runTest('createBookmark', () => testFunctions.testCreateBookmark(tableName, userId, postId));
601586
await runner.runTest('getBookmark', () => testFunctions.testGetBookmark(tableName, userId, postId));

0 commit comments

Comments
 (0)