Skip to content

Commit d1cccc0

Browse files
committed
changed the flow of prompts and unit tests
1 parent 5f6b0a0 commit d1cccc0

File tree

2 files changed

+184
-42
lines changed

2 files changed

+184
-42
lines changed

setup.ts

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,8 @@ export async function dataImportWithoutDocker(
922922
importData: () => Promise<void>,
923923
): Promise<void> {
924924
if (!process.env.MONGO_DB_URL) {
925-
throw new Error("MongoDB URL is not configured. Please run setup first.");
925+
console.log("Couldn't find mongodb url");
926+
return;
926927
}
927928

928929
let isDbEmpty: boolean;
@@ -937,39 +938,55 @@ export async function dataImportWithoutDocker(
937938
const { shouldOverwriteData } = await inquirer.prompt({
938939
type: "confirm",
939940
name: "shouldOverwriteData",
940-
message:
941-
"Do you want to delete the existing data and import required default Data to start using Talawa?",
941+
message: "Do you want to overwrite the existing data?",
942942
default: false,
943943
});
944944
if (shouldOverwriteData) {
945-
await wipeExistingData(process.env.MONGO_DB_URL);
946-
// Import Default Data
947-
948-
// Prompt to import Sample Data
949-
const { importSampleData } = await inquirer.prompt({
945+
const { overwriteDefaultData } = await inquirer.prompt({
950946
type: "confirm",
951-
name: "importSampleData",
947+
name: "overwriteDefaultData",
952948
message:
953-
"Do you want to import Talawa sample data for testing and evaluation purposes?",
949+
"Do you want to DESTROY the existing data, replacing it with the default data required for a fresh production system?",
954950
default: false,
955951
});
956-
957-
if (importSampleData) {
958-
await importData();
959-
} else await importDefaultData();
952+
if (overwriteDefaultData) {
953+
await wipeExistingData(process.env.MONGO_DB_URL);
954+
await importDefaultData();
955+
} else {
956+
const { overwriteSampleData } = await inquirer.prompt({
957+
type: "confirm",
958+
name: "overwriteSampleData",
959+
message:
960+
"Do you want to DESTROY the existing data, replacing it with data suitable for testing and evaluation?",
961+
default: false,
962+
});
963+
if (overwriteSampleData) {
964+
await wipeExistingData(process.env.MONGO_DB_URL);
965+
await importData();
966+
}
967+
}
960968
}
961969
} else {
962-
const { shouldImportSampleData } = await inquirer.prompt({
970+
const { shouldImportDefaultData } = await inquirer.prompt({
963971
type: "confirm",
964-
name: "shouldImportSampleData",
972+
name: "shouldImportDefaultData",
965973
message:
966-
"Do you want to import Talawa sample data for testing and evaluation purposes?",
974+
"Do you want to import default data required for a fresh production system?",
967975
default: false,
968976
});
969-
if (shouldImportSampleData) {
970-
await importData();
971-
} else {
977+
if (shouldImportDefaultData) {
972978
await importDefaultData();
979+
} else {
980+
const { shouldImportSampleData } = await inquirer.prompt({
981+
type: "confirm",
982+
name: "shouldImportSampleData",
983+
message:
984+
"Do you want to import data suitable for testing and evaluation?",
985+
default: false,
986+
});
987+
if (shouldImportSampleData) {
988+
await importData();
989+
}
973990
}
974991
}
975992
}
@@ -1024,17 +1041,27 @@ export async function dataImportWithDocker(
10241041
// Wait for mongoDB to be ready
10251042
await connectToDatabase();
10261043

1027-
const { shouldImportSampleData } = await inquirer.prompt({
1044+
const { shouldImportDefaultData } = await inquirer.prompt({
10281045
type: "confirm",
1029-
name: "shouldImportSampleData",
1046+
name: "shouldImportDefaultData",
10301047
message:
1031-
"Do you want to import Talawa sample data for testing and evaluation purposes?",
1032-
default: true,
1048+
"Do you want to import default data required for a fresh production system?",
1049+
default: false,
10331050
});
1034-
1035-
if (shouldImportSampleData) {
1036-
await importData();
1037-
} else await importDefaultData();
1051+
if (shouldImportDefaultData) {
1052+
await importDefaultData();
1053+
} else {
1054+
const { shouldImportSampleData } = await inquirer.prompt({
1055+
type: "confirm",
1056+
name: "shouldImportSampleData",
1057+
message:
1058+
"Do you want to import data suitable for testing and evaluation?",
1059+
default: false,
1060+
});
1061+
if (shouldImportSampleData) {
1062+
await importData();
1063+
}
1064+
}
10381065
} catch (err) {
10391066
console.log("Some error occurred: " + err);
10401067
}

tests/setup/dataImportFlow.spec.ts

Lines changed: 129 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe("Data Importation Without Docker", () => {
1414
process.env = { ...mockEnv }; // Restore environment variables
1515
});
1616

17-
it("should import sample data if the database is empty and user opts to import sample data", async () => {
17+
it("should import default data if the database is empty and user opts to import default data", async () => {
1818
const checkDbMock = vi
1919
.fn()
2020
.mockImplementation(async (): Promise<boolean> => {
@@ -36,9 +36,46 @@ describe("Data Importation Without Docker", () => {
3636
return Promise.resolve();
3737
});
3838
vi.spyOn(inquirer, "prompt").mockResolvedValueOnce({
39-
shouldImportSampleData: true,
39+
shouldImportDefaultData: true,
4040
});
4141

42+
await dataImportWithoutDocker(
43+
checkDbMock,
44+
wipeExistingDataMock,
45+
importDefaultDataMock,
46+
importDataMock,
47+
);
48+
expect(checkDbMock).toBeCalled();
49+
expect(wipeExistingDataMock).not.toBeCalled();
50+
expect(importDefaultDataMock).toBeCalled();
51+
expect(importDataMock).not.toBeCalled();
52+
});
53+
54+
it("should import sample data if the database is empty and user opts not to import default data but import sample data", async () => {
55+
const checkDbMock = vi
56+
.fn()
57+
.mockImplementation(async (): Promise<boolean> => {
58+
return true;
59+
});
60+
const wipeExistingDataMock = vi
61+
.fn()
62+
.mockImplementation(async (): Promise<void> => {
63+
return Promise.resolve();
64+
});
65+
const importDataMock = vi
66+
.fn()
67+
.mockImplementation(async (): Promise<void> => {
68+
return Promise.resolve();
69+
});
70+
const importDefaultDataMock = vi
71+
.fn()
72+
.mockImplementation(async (): Promise<void> => {
73+
return Promise.resolve();
74+
});
75+
vi.spyOn(inquirer, "prompt")
76+
.mockResolvedValueOnce({ shouldImportDefaultData: false })
77+
.mockResolvedValueOnce({ shouldImportSampleData: true });
78+
4279
await dataImportWithoutDocker(
4380
checkDbMock,
4481
wipeExistingDataMock,
@@ -51,7 +88,7 @@ describe("Data Importation Without Docker", () => {
5188
expect(importDataMock).toBeCalled();
5289
});
5390

54-
it("should not import sample data if the database is empty and user opts not to import sample data", async () => {
91+
it("should do no-op if the database is empty and user imports neither default nor sample data", async () => {
5592
const checkDbMock = vi
5693
.fn()
5794
.mockImplementation(async (): Promise<boolean> => {
@@ -72,9 +109,9 @@ describe("Data Importation Without Docker", () => {
72109
.mockImplementation(async (): Promise<void> => {
73110
return Promise.resolve();
74111
});
75-
vi.spyOn(inquirer, "prompt").mockResolvedValueOnce({
76-
shouldImportSampleData: false,
77-
});
112+
vi.spyOn(inquirer, "prompt")
113+
.mockResolvedValueOnce({ shouldImportDefaultData: false })
114+
.mockResolvedValueOnce({ shouldImportSampleData: false });
78115

79116
await dataImportWithoutDocker(
80117
checkDbMock,
@@ -84,7 +121,7 @@ describe("Data Importation Without Docker", () => {
84121
);
85122
expect(checkDbMock).toBeCalled();
86123
expect(wipeExistingDataMock).not.toBeCalled();
87-
expect(importDefaultDataMock).toBeCalled();
124+
expect(importDefaultDataMock).not.toBeCalled();
88125
expect(importDataMock).not.toBeCalled();
89126
});
90127

@@ -111,7 +148,8 @@ describe("Data Importation Without Docker", () => {
111148
});
112149
vi.spyOn(inquirer, "prompt")
113150
.mockResolvedValueOnce({ shouldOverwriteData: true })
114-
.mockResolvedValueOnce({ importSampleData: true });
151+
.mockResolvedValueOnce({ overwriteDefaultData: false })
152+
.mockResolvedValueOnce({ overwriteSampleData: true });
115153

116154
await dataImportWithoutDocker(
117155
checkDbMock,
@@ -125,7 +163,7 @@ describe("Data Importation Without Docker", () => {
125163
expect(importDataMock).toBeCalled();
126164
});
127165

128-
it("should not import sample data if the database is not empty and user opts to overwrite and not import sample data", async () => {
166+
it("should import default data if the database is not empty and user opts to overwrite and import default data", async () => {
129167
const checkDbMock = vi
130168
.fn()
131169
.mockImplementation(async (): Promise<boolean> => {
@@ -148,7 +186,7 @@ describe("Data Importation Without Docker", () => {
148186
});
149187
vi.spyOn(inquirer, "prompt")
150188
.mockResolvedValueOnce({ shouldOverwriteData: true })
151-
.mockResolvedValueOnce({ importSampleData: false });
189+
.mockResolvedValueOnce({ overwriteDefaultData: true });
152190

153191
await dataImportWithoutDocker(
154192
checkDbMock,
@@ -162,7 +200,45 @@ describe("Data Importation Without Docker", () => {
162200
expect(importDataMock).not.toBeCalled();
163201
});
164202

165-
it("should complete the data importation if the database is not empty and user does not opt to overwrite", async () => {
203+
it("should do no-op if db not empty and user imports neither default nor sample data", async () => {
204+
const checkDbMock = vi
205+
.fn()
206+
.mockImplementation(async (): Promise<boolean> => {
207+
return false;
208+
});
209+
const wipeExistingDataMock = vi
210+
.fn()
211+
.mockImplementation(async (): Promise<void> => {
212+
return Promise.resolve();
213+
});
214+
const importDataMock = vi
215+
.fn()
216+
.mockImplementation(async (): Promise<void> => {
217+
return Promise.resolve();
218+
});
219+
const importDefaultDataMock = vi
220+
.fn()
221+
.mockImplementation(async (): Promise<void> => {
222+
return Promise.resolve();
223+
});
224+
vi.spyOn(inquirer, "prompt")
225+
.mockResolvedValueOnce({ shouldOverwriteData: true })
226+
.mockResolvedValueOnce({ overwriteDefaultData: false })
227+
.mockResolvedValueOnce({ overwriteSampleData: false });
228+
229+
await dataImportWithoutDocker(
230+
checkDbMock,
231+
wipeExistingDataMock,
232+
importDefaultDataMock,
233+
importDataMock,
234+
);
235+
expect(checkDbMock).toBeCalled();
236+
expect(wipeExistingDataMock).not.toBeCalled();
237+
expect(importDefaultDataMock).not.toBeCalled();
238+
expect(importDataMock).not.toBeCalled();
239+
});
240+
241+
it("should do no-op if db not empty and user opts not to overwrite", async () => {
166242
const checkDbMock = vi
167243
.fn()
168244
.mockImplementation(async (): Promise<boolean> => {
@@ -250,7 +326,7 @@ describe("Data Importation With Docker", () => {
250326
process.env = { ...mockEnv }; // Restore environment variables
251327
});
252328

253-
it("should complete data importation if user opts not to start containers", async () => {
329+
it("should do no-op if user opts not to start containers", async () => {
254330
const runDockerComposeMock = vi
255331
.fn()
256332
.mockImplementation(async (): Promise<void> => {
@@ -361,6 +437,43 @@ describe("Data Importation With Docker", () => {
361437
expect(importDataMock).not.toBeCalled();
362438
});
363439

440+
it("should import default data if user opts to import default data", async () => {
441+
const runDockerComposeMock = vi
442+
.fn()
443+
.mockImplementation(async (): Promise<void> => {
444+
return Promise.resolve();
445+
});
446+
const importDataMock = vi
447+
.fn()
448+
.mockImplementation(async (): Promise<void> => {
449+
return Promise.resolve();
450+
});
451+
const importDefaultDataMock = vi
452+
.fn()
453+
.mockImplementation(async (): Promise<void> => {
454+
return Promise.resolve();
455+
});
456+
const connectDatabaseMock = vi
457+
.fn()
458+
.mockImplementation(async (): Promise<void> => {
459+
return Promise.resolve();
460+
});
461+
vi.spyOn(inquirer, "prompt")
462+
.mockResolvedValueOnce({ shouldStartDockerContainers: true })
463+
.mockResolvedValueOnce({ shouldImportDefaultData: true });
464+
465+
await dataImportWithDocker(
466+
runDockerComposeMock,
467+
importDefaultDataMock,
468+
importDataMock,
469+
connectDatabaseMock,
470+
);
471+
expect(runDockerComposeMock).toBeCalled();
472+
expect(connectDatabaseMock).toBeCalled();
473+
expect(importDefaultDataMock).toBeCalled();
474+
expect(importDataMock).not.toBeCalled();
475+
});
476+
364477
it("should import sample data if user opts to import sample data", async () => {
365478
const runDockerComposeMock = vi
366479
.fn()
@@ -384,6 +497,7 @@ describe("Data Importation With Docker", () => {
384497
});
385498
vi.spyOn(inquirer, "prompt")
386499
.mockResolvedValueOnce({ shouldStartDockerContainers: true })
500+
.mockResolvedValueOnce({ shouldImportDefaultData: false })
387501
.mockResolvedValueOnce({ shouldImportSampleData: true });
388502

389503
await dataImportWithDocker(
@@ -398,7 +512,7 @@ describe("Data Importation With Docker", () => {
398512
expect(importDataMock).toBeCalled();
399513
});
400514

401-
it("should import default data if user does not opt to import sample data", async () => {
515+
it("should do no-op if user opts to import neither sample data nor default data", async () => {
402516
const runDockerComposeMock = vi
403517
.fn()
404518
.mockImplementation(async (): Promise<void> => {
@@ -421,6 +535,7 @@ describe("Data Importation With Docker", () => {
421535
});
422536
vi.spyOn(inquirer, "prompt")
423537
.mockResolvedValueOnce({ shouldStartDockerContainers: true })
538+
.mockResolvedValueOnce({ shouldImportDefaultData: false })
424539
.mockResolvedValueOnce({ shouldImportSampleData: false });
425540

426541
await dataImportWithDocker(
@@ -431,7 +546,7 @@ describe("Data Importation With Docker", () => {
431546
);
432547
expect(runDockerComposeMock).toBeCalled();
433548
expect(connectDatabaseMock).toBeCalled();
434-
expect(importDefaultDataMock).toBeCalled();
549+
expect(importDefaultDataMock).not.toBeCalled();
435550
expect(importDataMock).not.toBeCalled();
436551
});
437552

0 commit comments

Comments
 (0)