From 18780f0ab7d1084fc0ab84cdb0e2c20ec9b23152 Mon Sep 17 00:00:00 2001 From: Adeel Ahmed Qureshi Date: Thu, 2 May 2024 21:59:26 +0500 Subject: [PATCH] Added for unit tests for models --- UsageMntrServ/models/__test__/model.test.js | 48 +++++++++++++++++++ .../models/__test__/models.test.js | 20 ++++++++ UserAccMgmtServ/package.json | 9 +++- UserAccMgmtServ/test/setup.js | 28 +++++++++++ 4 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 UsageMntrServ/models/__test__/model.test.js create mode 100644 UserAccMgmtServ/models/__test__/models.test.js create mode 100644 UserAccMgmtServ/test/setup.js diff --git a/UsageMntrServ/models/__test__/model.test.js b/UsageMntrServ/models/__test__/model.test.js new file mode 100644 index 0000000..c611d9a --- /dev/null +++ b/UsageMntrServ/models/__test__/model.test.js @@ -0,0 +1,48 @@ +const mongoose = require('mongoose'); +const DailyUsage = require('../DailyUsage'); + +describe('DailyUsage Model Test', () => { + + it('create & save daily usage successfully', async () => { + const dailyUsageData = { + userID: new mongoose.Types.ObjectId(), + usedBandwidth: 0, + totalBandwidth: 25000000 + }; + const validDailyUsage = new DailyUsage(dailyUsageData); + const savedDailyUsage = await validDailyUsage.save(); + + + expect(savedDailyUsage._id).toBeDefined(); + expect(savedDailyUsage.userID).toBe(dailyUsageData.userID); + expect(savedDailyUsage.usedBandwidth).toBe(dailyUsageData.usedBandwidth); + expect(savedDailyUsage.totalBandwidth).toBe(dailyUsageData.totalBandwidth); + }); + + +}); + + +const mongoose = require('mongoose'); +const UsageRequest = require('../UsageRequest'); + +describe('UsageRequest Model Test', () => { + + it('create & save usage request successfully', async () => { + const usageRequestData = { + userID: new mongoose.Types.ObjectId(), + usedBandwidth: 0, + requestType: 'testType' + }; + const validUsageRequest = new UsageRequest(usageRequestData); + const savedUsageRequest = await validUsageRequest.save(); + + // Object Id should be defined when successfully saved to MongoDB. + expect(savedUsageRequest._id).toBeDefined(); + expect(savedUsageRequest.userID).toBe(usageRequestData.userID); + expect(savedUsageRequest.usedBandwidth).toBe(usageRequestData.usedBandwidth); + expect(savedUsageRequest.requestType).toBe(usageRequestData.requestType); + }); + + +}); \ No newline at end of file diff --git a/UserAccMgmtServ/models/__test__/models.test.js b/UserAccMgmtServ/models/__test__/models.test.js new file mode 100644 index 0000000..ffa7c99 --- /dev/null +++ b/UserAccMgmtServ/models/__test__/models.test.js @@ -0,0 +1,20 @@ +const mongoose = require('mongoose'); +const User = require('../user'); // Import your User model + +describe('User Model Test', () => { + + + it('create & save user successfully', async () => { + const userData = { username: 'test', password: 'test', email: 'test@test.com' }; + const validUser = new User(userData); + const savedUser = await validUser.save(); + + // Object Id should be defined when successfully saved to MongoDB. + expect(savedUser._id).toBeDefined(); + expect(savedUser.username).toBe(userData.username); + expect(savedUser.password).toBe(userData.password); + expect(savedUser.email).toBe(userData.email); + }); + + // You should add more tests to validate your schema. +}); \ No newline at end of file diff --git a/UserAccMgmtServ/package.json b/UserAccMgmtServ/package.json index 2c3e0f0..210cb7c 100644 --- a/UserAccMgmtServ/package.json +++ b/UserAccMgmtServ/package.json @@ -4,7 +4,14 @@ "description": "", "main": "index.js", "scripts": { - "start": "nodemon index.js" + "start": "nodemon index.js", + "test": "jest --watchAll " + }, + "jest": { + "testEnvironment": "node", + "setupFilesAfterEnv": [ + "./test/setup.js" + ] }, "author": "", "license": "ISC", diff --git a/UserAccMgmtServ/test/setup.js b/UserAccMgmtServ/test/setup.js new file mode 100644 index 0000000..8e9db90 --- /dev/null +++ b/UserAccMgmtServ/test/setup.js @@ -0,0 +1,28 @@ +const MongoMemoryServer = require('mongodb-memory-server').MongoMemoryServer; +const mongoose = require('mongoose'); +const app = require("../app").app; + + +let mongoServer; +beforeAll(async () => { + mongoServer = new MongoMemoryServer(); + await mongoServer.start(); + const mongoUri = await mongoServer.getUri(); + await mongoose.connect(mongoUri, { + useNewUrlParser: true, + useUnifiedTopology: true + }); +}); + + +beforeEach(async () => { + const collections = await mongoose.connection.db.collections(); + for (let collection of collections) { + await collection.deleteMany({}); + } +}); + +afterAll(async () => { + await mongoServer.stop(); + await mongoose.connection.close(); +}); \ No newline at end of file