Skip to content

Commit 938812c

Browse files
authored
fixed loadSampleData.ts and added tests (#2807)
* fixed loadSampleData.ts and tests added * fix: formatted code to meet Prettier standards * Revert changes to tsconfig.json
1 parent a3a085b commit 938812c

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

src/utilities/loadSampleData.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from "fs/promises";
22
import path from "path";
3-
const dirname: string = path.dirname(new URL(import.meta.url).pathname);
3+
import { fileURLToPath } from "url";
44
import yargs from "yargs";
55
import { hideBin } from "yargs/helpers";
66
import { connect } from "../db";
@@ -17,6 +17,8 @@ import {
1717
} from "../models";
1818
import { RecurrenceRule } from "../models/RecurrenceRule";
1919

20+
const dirname: string = path.dirname(fileURLToPath(import.meta.url));
21+
2022
interface InterfaceArgs {
2123
items?: string;
2224
format?: boolean;
@@ -26,9 +28,9 @@ interface InterfaceArgs {
2628
/**
2729
* Lists sample data files and their document counts in the sample_data directory.
2830
*/
29-
async function listSampleData(): Promise<void> {
31+
export async function listSampleData(): Promise<void> {
3032
try {
31-
const sampleDataPath = path.join(dirname, "../../sample_data");
33+
const sampleDataPath = path.resolve(dirname, "../../sample_data");
3234
const files = await fs.readdir(sampleDataPath);
3335

3436
console.log("Sample Data Files:\n");
@@ -41,7 +43,7 @@ async function listSampleData(): Promise<void> {
4143
);
4244

4345
for (const file of files) {
44-
const filePath = path.join(sampleDataPath, file);
46+
const filePath = path.resolve(sampleDataPath, file);
4547
const stats = await fs.stat(filePath);
4648
if (stats.isFile()) {
4749
const data = await fs.readFile(filePath, "utf8");
@@ -110,7 +112,7 @@ async function insertCollections(collections: string[]): Promise<void> {
110112
// Insert data into each specified collection
111113
for (const collection of collections) {
112114
const data = await fs.readFile(
113-
path.join(dirname, `../../sample_data/${collection}.json`),
115+
path.resolve(dirname, `../../sample_data/${collection}.json`),
114116
"utf8",
115117
);
116118
const docs = JSON.parse(data) as Record<string, unknown>[];
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { describe, it, expect, beforeAll, afterAll } from "vitest";
2+
import { connect, disconnect } from "../../src/db";
3+
import {
4+
User,
5+
Organization,
6+
Post,
7+
Event,
8+
Venue,
9+
RecurrenceRule,
10+
AppUserProfile,
11+
ActionItemCategory,
12+
AgendaCategoryModel,
13+
} from "../../src/models";
14+
import { execSync } from "child_process";
15+
16+
describe("Sample Data Import Tests", () => {
17+
beforeAll(async () => {
18+
await connect();
19+
});
20+
21+
afterAll(async () => {
22+
await disconnect();
23+
});
24+
25+
it("should import sample data and verify document counts", async () => {
26+
try {
27+
execSync("npm run import:sample-data -- --format", { stdio: "pipe" });
28+
console.log("Sample data imported successfully.");
29+
} catch (error) {
30+
console.error("Failed to import sample data:", error);
31+
throw error;
32+
}
33+
34+
const userCount = await User.countDocuments();
35+
const organizationCount = await Organization.countDocuments();
36+
const postCount = await Post.countDocuments();
37+
const eventCount = await Event.countDocuments();
38+
const venueCount = await Venue.countDocuments();
39+
const recurrenceRuleCount = await RecurrenceRule.countDocuments();
40+
const appUserProfileCount = await AppUserProfile.countDocuments();
41+
const actionItemCategoryCount = await ActionItemCategory.countDocuments();
42+
const agendaCategoryCount = await AgendaCategoryModel.countDocuments();
43+
44+
expect(userCount).toBe(15);
45+
expect(organizationCount).toBe(4);
46+
expect(postCount).toBe(17);
47+
expect(eventCount).toBe(17280);
48+
expect(venueCount).toBe(4);
49+
expect(recurrenceRuleCount).toBe(100);
50+
expect(appUserProfileCount).toBe(14);
51+
expect(actionItemCategoryCount).toBe(4);
52+
expect(agendaCategoryCount).toBe(4);
53+
});
54+
});

0 commit comments

Comments
 (0)