Skip to content

Commit

Permalink
Added for unit tests for models
Browse files Browse the repository at this point in the history
  • Loading branch information
itsAdee committed May 2, 2024
1 parent 51aa5bf commit 18780f0
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
48 changes: 48 additions & 0 deletions UsageMntrServ/models/__test__/model.test.js
Original file line number Diff line number Diff line change
@@ -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);
});


});
20 changes: 20 additions & 0 deletions UserAccMgmtServ/models/__test__/models.test.js
Original file line number Diff line number Diff line change
@@ -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: '[email protected]' };
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.
});
9 changes: 8 additions & 1 deletion UserAccMgmtServ/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
28 changes: 28 additions & 0 deletions UserAccMgmtServ/test/setup.js
Original file line number Diff line number Diff line change
@@ -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();
});

0 comments on commit 18780f0

Please sign in to comment.