Skip to content

Commit 344be33

Browse files
committed
added tests for data importation
1 parent 9de22b4 commit 344be33

File tree

2 files changed

+532
-102
lines changed

2 files changed

+532
-102
lines changed

setup.ts

Lines changed: 133 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ export function getDockerComposeCommand(): { command: string; args: string[] } {
487487
}
488488

489489
const DOCKER_COMPOSE_TIMEOUT_MS = 300000;
490-
async function runDockerComposeWithLogs(): Promise<void> {
490+
export async function runDockerComposeWithLogs(): Promise<void> {
491491
// Check if Docker daemon is running
492492
try {
493493
await new Promise((resolve, reject) => {
@@ -915,6 +915,126 @@ export async function configureMinio(
915915
console.log("[MINIO] MinIO configuration added successfully.\n");
916916
}
917917

918+
export async function dataImportWithoutDocker(
919+
checkDb: (url: string) => Promise<boolean>,
920+
wipeExistingData: (url: string) => Promise<void>,
921+
importDefaultData: () => Promise<void>,
922+
importData: () => Promise<void>,
923+
): Promise<void> {
924+
if (!process.env.MONGO_DB_URL) {
925+
console.log("Couldn't find mongodb url");
926+
return;
927+
}
928+
929+
const isDbEmpty = await checkDb(process.env.MONGO_DB_URL);
930+
if (!isDbEmpty) {
931+
const { shouldOverwriteData } = await inquirer.prompt({
932+
type: "confirm",
933+
name: "shouldOverwriteData",
934+
message:
935+
"Do you want to delete the existing data and import required default Data to start using Talawa?",
936+
default: false,
937+
});
938+
if (shouldOverwriteData) {
939+
await wipeExistingData(process.env.MONGO_DB_URL);
940+
// Import Default Data
941+
942+
// Prompt to import Sample Data
943+
const { importSampleData } = await inquirer.prompt({
944+
type: "confirm",
945+
name: "importSampleData",
946+
message:
947+
"Do you want to import Talawa sample data for testing and evaluation purposes?",
948+
default: false,
949+
});
950+
951+
if (importSampleData) {
952+
await importData();
953+
} else await importDefaultData();
954+
}
955+
} else {
956+
const { shouldImportSampleData } = await inquirer.prompt({
957+
type: "confirm",
958+
name: "shouldImportSampleData",
959+
message:
960+
"Do you want to import Talawa sample data for testing and evaluation purposes?",
961+
default: false,
962+
});
963+
if (shouldImportSampleData) {
964+
await importData();
965+
} else {
966+
await importDefaultData();
967+
}
968+
}
969+
}
970+
971+
async function connectToDatabase(): Promise<void> {
972+
console.log("Waiting for mongoDB to be ready...");
973+
let isConnected = false;
974+
const maxRetries = 30; // 30 seconds timeout
975+
let retryCount = 0;
976+
while (!isConnected) {
977+
if (retryCount >= maxRetries) {
978+
throw new Error(
979+
"Timed out waiting for MongoDB to be ready after 30 seconds",
980+
);
981+
}
982+
try {
983+
const client = new MongoClient(process.env.MONGO_DB_URL as string);
984+
await client.connect();
985+
await client.db().command({ ping: 1 });
986+
client.close();
987+
isConnected = true;
988+
console.log("MongoDB is ready!");
989+
} catch (err) {
990+
const error = err instanceof Error ? err.message : String(err);
991+
console.log(
992+
`Waiting for MongoDB to be ready... Retry ${retryCount + 1}/${maxRetries}. Details: ${error}`,
993+
);
994+
await new Promise((resolve) => setTimeout(resolve, 1000));
995+
retryCount++;
996+
}
997+
}
998+
return Promise.resolve();
999+
}
1000+
1001+
export async function dataImportWithDocker(
1002+
runDockerComposeWithLogs: () => Promise<void>,
1003+
importDefaultData: () => Promise<void>,
1004+
importData: () => Promise<void>,
1005+
connectToDatabase: () => Promise<void>,
1006+
): Promise<void> {
1007+
const { shouldStartDockerContainers } = await inquirer.prompt({
1008+
type: "confirm",
1009+
name: "shouldStartDockerContainers",
1010+
message: "Do you want to start the Docker containers now?",
1011+
default: true,
1012+
});
1013+
if (shouldStartDockerContainers) {
1014+
console.log("Starting docker container...");
1015+
try {
1016+
await runDockerComposeWithLogs();
1017+
console.log("Docker containers have been built successfully!");
1018+
// Wait for mongoDB to be ready
1019+
await connectToDatabase();
1020+
1021+
const { shouldImportSampleData } = await inquirer.prompt({
1022+
type: "confirm",
1023+
name: "shouldImportSampleData",
1024+
message:
1025+
"Do you want to import Talawa sample data for testing and evaluation purposes?",
1026+
default: true,
1027+
});
1028+
1029+
if (shouldImportSampleData) {
1030+
await importData();
1031+
} else await importDefaultData();
1032+
} catch (err) {
1033+
console.log("Some error occurred: " + err);
1034+
}
1035+
}
1036+
}
1037+
9181038
/**
9191039
* The main function sets up the Talawa API by prompting the user to configure various environment
9201040
* variables and import sample data if desired.
@@ -1226,108 +1346,19 @@ async function main(): Promise<void> {
12261346
await setImageUploadSize(imageSizeLimit * 1000);
12271347

12281348
if (!isDockerInstallation) {
1229-
if (!process.env.MONGO_DB_URL) {
1230-
console.log("Couldn't find mongodb url");
1231-
return;
1232-
}
1233-
1234-
const isDbEmpty = await checkDb(process.env.MONGO_DB_URL);
1235-
if (!isDbEmpty) {
1236-
const { shouldOverwriteData } = await inquirer.prompt({
1237-
type: "confirm",
1238-
name: "shouldOverwriteData",
1239-
message:
1240-
"Do you want to delete the existing data and import required default Data to start using Talawa?",
1241-
default: false,
1242-
});
1243-
if (shouldOverwriteData) {
1244-
await wipeExistingData(process.env.MONGO_DB_URL);
1245-
// Import Default Data
1246-
await importDefaultData();
1247-
1248-
// Prompt to import Sample Data
1249-
const { importSampleData } = await inquirer.prompt({
1250-
type: "confirm",
1251-
name: "importSampleData",
1252-
message:
1253-
"Do you want to import Talawa sample data for testing and evaluation purposes?",
1254-
default: false,
1255-
});
1256-
1257-
if (importSampleData) {
1258-
await importData();
1259-
}
1260-
}
1261-
} else {
1262-
const { shouldImportSampleData } = await inquirer.prompt({
1263-
type: "confirm",
1264-
name: "shouldImportSampleData",
1265-
message:
1266-
"Do you want to import Talawa sample data for testing and evaluation purposes?",
1267-
default: false,
1268-
});
1269-
if (shouldImportSampleData) {
1270-
await importData();
1271-
} else {
1272-
await importDefaultData();
1273-
}
1274-
}
1349+
await dataImportWithoutDocker(
1350+
checkDb,
1351+
wipeExistingData,
1352+
importDefaultData,
1353+
importData,
1354+
);
12751355
} else {
1276-
const { shouldStartDockerContainers } = await inquirer.prompt({
1277-
type: "confirm",
1278-
name: "shouldStartDockerContainers",
1279-
message: "Do you want to start the Docker containers now?",
1280-
default: true,
1281-
});
1282-
if (shouldStartDockerContainers) {
1283-
console.log("Starting docker container...");
1284-
try {
1285-
await runDockerComposeWithLogs();
1286-
console.log("Docker containers have been built successfully!");
1287-
// Wait for mongoDB to be ready
1288-
console.log("Waiting for mongoDB to be ready...");
1289-
let isConnected = false;
1290-
const maxRetries = 30; // 30 seconds timeout
1291-
let retryCount = 0;
1292-
while (!isConnected) {
1293-
if (retryCount >= maxRetries) {
1294-
throw new Error(
1295-
"Timed out waiting for MongoDB to be ready after 30 seconds",
1296-
);
1297-
}
1298-
try {
1299-
const client = new MongoClient(process.env.MONGO_DB_URL as string);
1300-
await client.connect();
1301-
await client.db().command({ ping: 1 });
1302-
client.close();
1303-
isConnected = true;
1304-
console.log("MongoDB is ready!");
1305-
} catch (err) {
1306-
const error = err instanceof Error ? err.message : String(err);
1307-
console.log(
1308-
`Waiting for MongoDB to be ready... Retry ${retryCount + 1}/${maxRetries}. Details: ${error}`,
1309-
);
1310-
await new Promise((resolve) => setTimeout(resolve, 1000));
1311-
retryCount++;
1312-
}
1313-
}
1314-
1315-
await importDefaultData();
1316-
const { shouldImportSampleData } = await inquirer.prompt({
1317-
type: "confirm",
1318-
name: "shouldImportSampleData",
1319-
message:
1320-
"Do you want to import Talawa sample data for testing and evaluation purposes?",
1321-
default: true,
1322-
});
1323-
1324-
if (shouldImportSampleData) {
1325-
await importData();
1326-
}
1327-
} catch (err) {
1328-
console.log("Some error occurred: " + err);
1329-
}
1330-
}
1356+
await dataImportWithDocker(
1357+
runDockerComposeWithLogs,
1358+
importDefaultData,
1359+
importData,
1360+
connectToDatabase,
1361+
);
13311362
}
13321363

13331364
console.log(

0 commit comments

Comments
 (0)