Skip to content

Commit

Permalink
Merge branch 'bug-user-query' of github.com:NishantSinghhhhh/talawa-a…
Browse files Browse the repository at this point in the history
…pi into bug-user-query
  • Loading branch information
NishantSinghhhhh committed Dec 30, 2024
2 parents 8834a77 + 1ed6306 commit 9b837fa
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/utilities/loadSampleData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "fs/promises";
import path from "path";
const dirname: string = path.dirname(new URL(import.meta.url).pathname);
import { fileURLToPath } from "url";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { connect } from "../db";
Expand All @@ -17,6 +17,8 @@ import {
} from "../models";
import { RecurrenceRule } from "../models/RecurrenceRule";

const dirname: string = path.dirname(fileURLToPath(import.meta.url));

interface InterfaceArgs {
items?: string;
format?: boolean;
Expand All @@ -26,9 +28,9 @@ interface InterfaceArgs {
/**
* Lists sample data files and their document counts in the sample_data directory.
*/
async function listSampleData(): Promise<void> {
export async function listSampleData(): Promise<void> {
try {
const sampleDataPath = path.join(dirname, "../../sample_data");
const sampleDataPath = path.resolve(dirname, "../../sample_data");
const files = await fs.readdir(sampleDataPath);

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

for (const file of files) {
const filePath = path.join(sampleDataPath, file);
const filePath = path.resolve(sampleDataPath, file);
const stats = await fs.stat(filePath);
if (stats.isFile()) {
const data = await fs.readFile(filePath, "utf8");
Expand Down Expand Up @@ -110,7 +112,7 @@ async function insertCollections(collections: string[]): Promise<void> {
// Insert data into each specified collection
for (const collection of collections) {
const data = await fs.readFile(
path.join(dirname, `../../sample_data/${collection}.json`),
path.resolve(dirname, `../../sample_data/${collection}.json`),
"utf8",
);
const docs = JSON.parse(data) as Record<string, unknown>[];
Expand Down
54 changes: 54 additions & 0 deletions tests/utilities/loadSampleData.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { describe, it, expect, beforeAll, afterAll } from "vitest";
import { connect, disconnect } from "../../src/db";
import {
User,
Organization,
Post,
Event,
Venue,
RecurrenceRule,
AppUserProfile,
ActionItemCategory,
AgendaCategoryModel,
} from "../../src/models";
import { execSync } from "child_process";

describe("Sample Data Import Tests", () => {
beforeAll(async () => {
await connect();
});

afterAll(async () => {
await disconnect();
});

it("should import sample data and verify document counts", async () => {
try {
execSync("npm run import:sample-data -- --format", { stdio: "pipe" });
console.log("Sample data imported successfully.");
} catch (error) {
console.error("Failed to import sample data:", error);
throw error;
}

const userCount = await User.countDocuments();
const organizationCount = await Organization.countDocuments();
const postCount = await Post.countDocuments();
const eventCount = await Event.countDocuments();
const venueCount = await Venue.countDocuments();
const recurrenceRuleCount = await RecurrenceRule.countDocuments();
const appUserProfileCount = await AppUserProfile.countDocuments();
const actionItemCategoryCount = await ActionItemCategory.countDocuments();
const agendaCategoryCount = await AgendaCategoryModel.countDocuments();

expect(userCount).toBe(15);
expect(organizationCount).toBe(4);
expect(postCount).toBe(17);
expect(eventCount).toBe(17280);
expect(venueCount).toBe(4);
expect(recurrenceRuleCount).toBe(100);
expect(appUserProfileCount).toBe(14);
expect(actionItemCategoryCount).toBe(4);
expect(agendaCategoryCount).toBe(4);
});
});

0 comments on commit 9b837fa

Please sign in to comment.