-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
JaiPannu-IITI
committed
Feb 23, 2025
1 parent
ec6b0d2
commit 024d275
Showing
1 changed file
with
73 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,89 @@ | ||
import { suite, test, expect, vi, beforeEach } from "vitest"; | ||
import * as mainModule from "scripts/dbManagement/addSampleData"; | ||
import * as helpers from "scripts/dbManagement/helpers"; | ||
import { beforeEach, expect, suite, test, vi } from "vitest"; | ||
|
||
suite("addSampleData main function tests", () => { | ||
beforeEach(async () => { | ||
vi.resetModules(); | ||
}); | ||
beforeEach(async () => { | ||
vi.resetModules(); | ||
}); | ||
|
||
test("should execute all operations successfully", async () => { | ||
// Arrange: simulate successful operations. | ||
const pingDBSpy = vi.spyOn(helpers, "pingDB").mockResolvedValue(true); | ||
const ensureAdminSpy = vi | ||
.spyOn(helpers, "ensureAdministratorExists") | ||
.mockResolvedValue(true); | ||
const insertCollectionsSpy = vi | ||
.spyOn(helpers, "insertCollections") | ||
.mockResolvedValue(true); | ||
|
||
test("should execute all operations successfully", async () => { | ||
// Arrange: simulate successful operations. | ||
const pingDBSpy = vi.spyOn(helpers, "pingDB").mockResolvedValue(true); | ||
const ensureAdminSpy = vi | ||
.spyOn(helpers, "ensureAdministratorExists") | ||
.mockResolvedValue(true); | ||
const insertCollectionsSpy = vi | ||
.spyOn(helpers, "insertCollections") | ||
.mockResolvedValue(true); | ||
// Spy on console.log to verify the output messages. | ||
const consoleLogSpy = vi.spyOn(console, "log").mockImplementation(() => {}); | ||
|
||
// Spy on console.log to verify the output messages. | ||
const consoleLogSpy = vi.spyOn(console, "log").mockImplementation(() => {}); | ||
// Act: call main() | ||
await expect(mainModule.main()).resolves.toBeUndefined(); | ||
|
||
// Act: call main() | ||
await expect(mainModule.main()).resolves.toBeUndefined(); | ||
// Assert: verify that each helper was called as expected. | ||
expect(pingDBSpy).toHaveBeenCalled(); | ||
expect(ensureAdminSpy).toHaveBeenCalled(); | ||
expect(insertCollectionsSpy).toHaveBeenCalledWith([ | ||
"users", | ||
"organizations", | ||
"organization_memberships", | ||
"posts", | ||
"post_votes", | ||
"post_attachments", | ||
"comments", | ||
"comment_votes", | ||
]); | ||
|
||
// Assert: verify that each helper was called as expected. | ||
expect(pingDBSpy).toHaveBeenCalled(); | ||
expect(ensureAdminSpy).toHaveBeenCalled(); | ||
expect(insertCollectionsSpy).toHaveBeenCalledWith([ | ||
"users", | ||
"organizations", | ||
"organization_memberships", | ||
"posts", | ||
"post_votes", | ||
"post_attachments", | ||
"comments", | ||
"comment_votes", | ||
]); | ||
// Verify that success messages are logged. | ||
expect(consoleLogSpy).toHaveBeenCalledWith( | ||
expect.stringContaining("Database connected successfully"), | ||
); | ||
expect(consoleLogSpy).toHaveBeenCalledWith( | ||
expect.stringContaining("Administrator setup complete"), | ||
); | ||
expect(consoleLogSpy).toHaveBeenCalledWith( | ||
expect.stringContaining("Sample Data added to the database"), | ||
); | ||
}); | ||
|
||
// Verify that success messages are logged. | ||
expect(consoleLogSpy).toHaveBeenCalledWith( | ||
expect.stringContaining("Database connected successfully") | ||
); | ||
expect(consoleLogSpy).toHaveBeenCalledWith( | ||
expect.stringContaining("Administrator setup complete") | ||
); | ||
expect(consoleLogSpy).toHaveBeenCalledWith( | ||
expect.stringContaining("Sample Data added to the database") | ||
); | ||
}); | ||
test("should throw an error when pingDB fails", async () => { | ||
// Arrange: simulate failure of pingDB. | ||
const errorMsg = "pingDB error"; | ||
vi.spyOn(helpers, "pingDB").mockRejectedValue(new Error(errorMsg)); | ||
|
||
test("should throw an error when pingDB fails", async () => { | ||
// Arrange: simulate failure of pingDB. | ||
const errorMsg = "pingDB error"; | ||
vi.spyOn(helpers, "pingDB").mockRejectedValue(new Error(errorMsg)); | ||
// Act & Assert: main() should throw an error indicating DB connection failure. | ||
await expect(mainModule.main()).rejects.toThrow( | ||
`Database connection failed: Error: ${errorMsg}`, | ||
); | ||
}); | ||
|
||
// Act & Assert: main() should throw an error indicating DB connection failure. | ||
await expect(mainModule.main()).rejects.toThrow( | ||
`Database connection failed: Error: ${errorMsg}` | ||
); | ||
}); | ||
test("should throw an error when ensureAdministratorExists fails", async () => { | ||
// Arrange: pingDB succeeds, but ensureAdministratorExists fails. | ||
vi.spyOn(helpers, "pingDB").mockResolvedValue(true); | ||
const errorMsg = "admin error"; | ||
vi.spyOn(helpers, "ensureAdministratorExists").mockRejectedValue( | ||
new Error(errorMsg), | ||
); | ||
|
||
test("should throw an error when ensureAdministratorExists fails", async () => { | ||
// Arrange: pingDB succeeds, but ensureAdministratorExists fails. | ||
vi.spyOn(helpers, "pingDB").mockResolvedValue(true); | ||
const errorMsg = "admin error"; | ||
vi.spyOn(helpers, "ensureAdministratorExists").mockRejectedValue( | ||
new Error(errorMsg) | ||
); | ||
// Act & Assert: main() should throw the specific error message. | ||
await expect(mainModule.main()).rejects.toThrow( | ||
"\n\x1b[31mAdministrator access may be lost, try reimporting sample DB to restore access\x1b[0m\n", | ||
); | ||
}); | ||
|
||
// Act & Assert: main() should throw the specific error message. | ||
await expect(mainModule.main()).rejects.toThrow( | ||
"\n\x1b[31mAdministrator access may be lost, try reimporting sample DB to restore access\x1b[0m\n" | ||
); | ||
}); | ||
test("should throw an error when insertCollections fails", async () => { | ||
// Arrange: pingDB and ensureAdministratorExists succeed, but insertCollections fails. | ||
vi.spyOn(helpers, "pingDB").mockResolvedValue(true); | ||
vi.spyOn(helpers, "ensureAdministratorExists").mockResolvedValue(true); | ||
const errorMsg = "insert error"; | ||
vi.spyOn(helpers, "insertCollections").mockRejectedValue( | ||
new Error(errorMsg), | ||
); | ||
|
||
test("should throw an error when insertCollections fails", async () => { | ||
// Arrange: pingDB and ensureAdministratorExists succeed, but insertCollections fails. | ||
vi.spyOn(helpers, "pingDB").mockResolvedValue(true); | ||
vi.spyOn(helpers, "ensureAdministratorExists").mockResolvedValue(true); | ||
const errorMsg = "insert error"; | ||
vi.spyOn(helpers, "insertCollections").mockRejectedValue(new Error(errorMsg)); | ||
|
||
// Act & Assert: main() should throw an error indicating sample data insertion failure. | ||
await expect(mainModule.main()).rejects.toThrow("Error adding sample data"); | ||
}); | ||
// Act & Assert: main() should throw an error indicating sample data insertion failure. | ||
await expect(mainModule.main()).rejects.toThrow("Error adding sample data"); | ||
}); | ||
}); |