forked from hiteshchoudhary/apihub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request hiteshchoudhary#81 from shrey-dadhaniya/feat/cover…
…age-seed feat: added test cases for seeds
- Loading branch information
Showing
11 changed files
with
240 additions
and
28 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
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import mongoose, { mongo } from "mongoose"; | ||
import { MongoMemoryServer } from "mongodb-memory-server"; | ||
|
||
const MONGO_MEMORY_SERVER_PORT = process.env.MONGO_MEMORY_SERVER_PORT || 10000; | ||
const MONGODB_URL = `mongodb://127.0.0.1:${MONGO_MEMORY_SERVER_PORT}/`; | ||
|
||
let mongoServer = null; | ||
let dbInstance = undefined; | ||
|
||
const connectDB = async () => { | ||
try { | ||
await mongoose.disconnect(); | ||
mongoServer = await MongoMemoryServer.create({ | ||
instance: { | ||
port: +MONGO_MEMORY_SERVER_PORT, | ||
}, | ||
}); | ||
dbInstance = await mongoose.connect(MONGODB_URL); | ||
} catch (error) { | ||
console.error("Mongo db connect error: ", error); | ||
process.exit(1); | ||
} | ||
}; | ||
export const clearDB = async (collectionName = null) => { | ||
if (!dbInstance) { | ||
dbInstance = await mongoose.connect(MONGODB_URL); | ||
} | ||
const connection = mongoose.connection; | ||
if (collectionName) { | ||
await connection.db.collection(collectionName).deleteMany({}); | ||
} else { | ||
const collections = await connection.db.listCollections().toArray(); | ||
const collectionNames = collections.map((col) => col.name); | ||
for (let name of collectionNames) { | ||
await connection.db.collection(name).deleteMany({}); | ||
} | ||
} | ||
}; | ||
|
||
export default connectDB; |
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
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { test, expect } from "@playwright/test"; | ||
import { getApiContext } from "../../common.js"; | ||
import { clearDB } from "../../db.js"; | ||
|
||
let apiContext; | ||
|
||
test.describe("Seed Chat App", () => { | ||
test.beforeAll(async ({ playwright }) => { | ||
apiContext = await getApiContext(playwright); | ||
await clearDB(); | ||
}); | ||
test.afterAll(async ({}) => { | ||
await apiContext.dispose(); | ||
}); | ||
|
||
test.describe("POST:/api/v1/seed/chat-app - Seed Chat", async () => { | ||
test("should seed Chat App DB", async ({ page }) => { | ||
const res = await apiContext.post("/api/v1/seed/chat-app"); | ||
expect(res.status()).toEqual(201); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { test, expect } from "@playwright/test"; | ||
import { getApiContext } from "../../common.js"; | ||
import { clearDB } from "../../db.js"; | ||
import { | ||
CATEGORIES_COUNT, | ||
PRODUCTS_COUNT, | ||
} from "../../../src/seeds/_constants.js"; | ||
|
||
let apiContext; | ||
|
||
test.describe("Seed Ecommerce App", () => { | ||
test.beforeAll(async ({ playwright }) => { | ||
apiContext = await getApiContext(playwright); | ||
await clearDB(); | ||
}); | ||
test.afterAll(async ({}) => { | ||
await apiContext.dispose(); | ||
}); | ||
|
||
test.describe("POST:/api/v1/seed/ecommerce - Seed Ecommerce", async () => { | ||
test("should return 0 products before seed", async ({ page }) => { | ||
const res = await apiContext.get( | ||
"/api/v1/ecommerce/products?page=1&limit=1" | ||
); | ||
const json = await res.json(); | ||
expect(res.status()).toEqual(200); | ||
expect(json.data.totalProducts).toEqual(0); | ||
}); | ||
test("should return 0 categories before seed", async ({ page }) => { | ||
const res = await apiContext.get( | ||
"/api/v1/ecommerce/categories?page=1&limit=1" | ||
); | ||
const json = await res.json(); | ||
expect(res.status()).toEqual(200); | ||
expect(json.data.totalCategories).toEqual(0); | ||
}); | ||
test("should seed ecommerce DB", async ({ page }) => { | ||
const res = await apiContext.post("/api/v1/seed/ecommerce"); | ||
expect(res.status()).toEqual(201); | ||
}); | ||
test(`should return ${PRODUCTS_COUNT} products after seed`, async ({ | ||
page, | ||
}) => { | ||
const res = await apiContext.get( | ||
"/api/v1/ecommerce/products?page=1&limit=1" | ||
); | ||
const json = await res.json(); | ||
expect(res.status()).toEqual(200); | ||
expect(json.data.totalProducts).toEqual(PRODUCTS_COUNT); | ||
}); | ||
test(`should return ${CATEGORIES_COUNT} categories after seed`, async ({ | ||
page, | ||
}) => { | ||
const res = await apiContext.get( | ||
"/api/v1/ecommerce/categories?page=1&limit=1" | ||
); | ||
const json = await res.json(); | ||
expect(res.status()).toEqual(200); | ||
expect(json.data.totalCategories).toEqual(CATEGORIES_COUNT); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import fs from "fs"; | ||
import { test, expect } from "@playwright/test"; | ||
import { getApiContext } from "../../common.js"; | ||
|
||
let apiContext; | ||
|
||
test.describe("Get credentials", () => { | ||
test.beforeAll(async ({ playwright }) => { | ||
apiContext = await getApiContext(playwright); | ||
}); | ||
test.afterAll(async ({}) => { | ||
await apiContext.dispose(); | ||
}); | ||
|
||
test.describe("GET:/api/v1/seed/generated-credentials - Get credentials", async () => { | ||
test("should return public/temp/seed-credentials.json content", async ({ | ||
page, | ||
}) => { | ||
const seedCredentialsText = fs.readFileSync( | ||
"./public/temp/seed-credentials.json", | ||
"utf8" | ||
); | ||
const seedCredentials = JSON.parse(seedCredentialsText); | ||
const res = await apiContext.get("/api/v1/seed/generated-credentials"); | ||
const json = await res.json(); | ||
expect(res.status()).toEqual(200); | ||
expect(json.data).toMatchObject(seedCredentials); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { test, expect } from "@playwright/test"; | ||
import { getApiContext } from "../../common.js"; | ||
import { clearDB } from "../../db.js"; | ||
import { SOCIAL_POSTS_COUNT } from "../../../src/seeds/_constants.js"; | ||
|
||
let apiContext; | ||
|
||
test.describe("Seed social-media App", () => { | ||
test.beforeAll(async ({ playwright }) => { | ||
apiContext = await getApiContext(playwright); | ||
await clearDB(); | ||
}); | ||
test.afterAll(async ({}) => { | ||
await apiContext.dispose(); | ||
}); | ||
|
||
test.describe("POST:/api/v1/seed/social-media - Seed social-media", async () => { | ||
test("should return 0 posts before seed", async ({ page }) => { | ||
const res = await apiContext.get( | ||
"/api/v1/social-media/posts?page=1&limit=1" | ||
); | ||
const json = await res.json(); | ||
expect(res.status()).toEqual(200); | ||
expect(json.data.totalPosts).toEqual(0); | ||
}); | ||
test("should seed social-media DB", async ({ page }) => { | ||
const res = await apiContext.post("/api/v1/seed/social-media"); | ||
expect(res.status()).toEqual(201); | ||
}); | ||
test(`should return ${SOCIAL_POSTS_COUNT} post after seed`, async ({ | ||
page, | ||
}) => { | ||
const res = await apiContext.get( | ||
"/api/v1/social-media/posts?page=1&limit=1" | ||
); | ||
const json = await res.json(); | ||
expect(res.status()).toEqual(200); | ||
expect(json.data.totalPosts).toEqual(SOCIAL_POSTS_COUNT); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { test, expect } from "@playwright/test"; | ||
import { getApiContext } from "../../common.js"; | ||
import { clearDB } from "../../db.js"; | ||
import { TODOS_COUNT } from "../../../src/seeds/_constants.js"; | ||
|
||
let apiContext; | ||
|
||
test.describe("Seed Todo App", () => { | ||
test.beforeAll(async ({ playwright }) => { | ||
apiContext = await getApiContext(playwright); | ||
await clearDB(); | ||
}); | ||
test.afterAll(async ({}) => { | ||
await apiContext.dispose(); | ||
}); | ||
|
||
test.describe("POST:/api/v1/seed/todos - Seed Todos", async () => { | ||
test("should return 0 todos before seed", async ({ page }) => { | ||
const res = await apiContext.get("/api/v1/todos"); | ||
const json = await res.json(); | ||
expect(res.status()).toEqual(200); | ||
expect(json.data.length).toEqual(0); | ||
}); | ||
|
||
test("should seed todo DB", async ({ page }) => { | ||
const res = await apiContext.post("/api/v1/seed/todos"); | ||
expect(res.status()).toEqual(201); | ||
}); | ||
|
||
test(`should return ${TODOS_COUNT} todos after seed`, async ({ page }) => { | ||
const res = await apiContext.get("/api/v1/todos"); | ||
const json = await res.json(); | ||
expect(res.status()).toEqual(200); | ||
expect(json.data.length).toEqual(TODOS_COUNT); | ||
}); | ||
}); | ||
}); |
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
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
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