-
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.
Added further unit and integration tests
- Loading branch information
Showing
7 changed files
with
4,832 additions
and
562 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
const mongoose = require('mongoose'); | ||
const UserStorageModel = require('../UserStorage'); | ||
const ImageModel = require('../Image'); | ||
|
||
describe('UserStorage and Image Integration', () => { | ||
beforeEach(async () => { | ||
await mongoose.connection.db.dropDatabase(); | ||
}); | ||
|
||
it('should create a user storage and associate an image', async () => { | ||
const userID = new mongoose.Types.ObjectId(); | ||
const imageName = 'test-image.jpg'; | ||
const imageSize = 1024; | ||
const contentType = 'image/jpeg'; | ||
const img = Buffer.from('test-image-data'); | ||
|
||
const createdUserStorage = await UserStorageModel.create({ userID }); | ||
const createdImage = await ImageModel.create({ | ||
userID, | ||
imageName, | ||
imageSize, | ||
contentType, | ||
img, | ||
}); | ||
|
||
expect(createdUserStorage.userID).toEqual(userID); | ||
expect(createdUserStorage.usedStorage).toBe(0); | ||
expect(createdUserStorage.totalStorage).toBe(10000000); | ||
|
||
expect(createdImage.userID).toEqual(userID); | ||
expect(createdImage.imageName).toBe(imageName); | ||
expect(createdImage.imageSize).toBe(imageSize); | ||
expect(createdImage.contentType).toBe(contentType); | ||
expect(createdImage.img.toString()).toBe(img.toString()); | ||
}); | ||
|
||
|
||
|
||
it('should find all images associated with a user', async () => { | ||
const userID = new mongoose.Types.ObjectId(); | ||
const image1 = { | ||
userID, | ||
imageName: 'image1.jpg', | ||
imageSize: 1024, | ||
contentType: 'image/jpeg', | ||
img: Buffer.from('image1-data'), | ||
}; | ||
const image2 = { | ||
userID, | ||
imageName: 'image2.jpg', | ||
imageSize: 2048, | ||
contentType: 'image/jpeg', | ||
img: Buffer.from('image2-data'), | ||
}; | ||
|
||
await ImageModel.create(image1); | ||
await ImageModel.create(image2); | ||
|
||
const userImages = await ImageModel.find({ userID }); | ||
|
||
expect(userImages.length).toBe(2); | ||
expect(userImages[0].imageName).toBe(image1.imageName); | ||
expect(userImages[1].imageName).toBe(image2.imageName); | ||
}); | ||
|
||
it('should update user storage when an image is added', async () => { | ||
const userID = new mongoose.Types.ObjectId(); | ||
const imageName = 'test-image.jpg'; | ||
const imageSize = 1024; | ||
const contentType = 'image/jpeg'; | ||
const img = Buffer.from('test-image-data'); | ||
|
||
await UserStorageModel.create({ userID }); | ||
await ImageModel.create({ | ||
userID, | ||
imageName, | ||
imageSize, | ||
contentType, | ||
img, | ||
}); | ||
|
||
await UserStorageModel.findOneAndUpdate( | ||
{ userID }, | ||
{ $inc: { usedStorage: imageSize } } | ||
); | ||
|
||
const updatedUserStorage = await UserStorageModel.findOne({ userID }); | ||
|
||
expect(updatedUserStorage.usedStorage).toBe(imageSize); | ||
}); | ||
|
||
it('should update user storage when an image is addedx2', async () => { | ||
const userID = new mongoose.Types.ObjectId(); | ||
const imageName = 'test-image.jpg'; | ||
const imageSize = 1024; | ||
const contentType = 'image/jpeg'; | ||
const img = Buffer.from('test-image-data'); | ||
|
||
await UserStorageModel.create({ userID }); | ||
await ImageModel.create({ | ||
userID, | ||
imageName, | ||
imageSize, | ||
contentType, | ||
img, | ||
}); | ||
|
||
// Update the usedStorage field manually | ||
await UserStorageModel.findOneAndUpdate( | ||
{ userID }, | ||
{ $inc: { usedStorage: imageSize } } | ||
); | ||
|
||
const updatedUserStorage = await UserStorageModel.findOne({ userID }); | ||
|
||
expect(updatedUserStorage.usedStorage).toBe(imageSize); | ||
}); | ||
|
||
}); |
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,71 @@ | ||
|
||
const mongoose = require('mongoose'); | ||
const UserStorageModel = require('../UserStorage'); | ||
|
||
describe('UserStorage Model', () => { | ||
beforeEach(async () => { | ||
await mongoose.connection.db.dropDatabase(); | ||
}); | ||
|
||
it('should create a new user storage with default values', async () => { | ||
const userID = new mongoose.Types.ObjectId(); | ||
|
||
const createdUserStorage = await UserStorageModel.create({ userID }); | ||
|
||
expect(createdUserStorage.userID).toEqual(userID); | ||
expect(createdUserStorage.usedStorage).toBe(0); | ||
expect(createdUserStorage.totalStorage).toBe(10000000); | ||
expect(createdUserStorage.createdAt).toBeDefined(); | ||
expect(createdUserStorage.updatedAt).toBeDefined(); | ||
}); | ||
|
||
it('should create a new user storage with custom values', async () => { | ||
const userID = new mongoose.Types.ObjectId(); | ||
const usedStorage = 5000000; | ||
const totalStorage = 20000000; | ||
|
||
const createdUserStorage = await UserStorageModel.create({ | ||
userID, | ||
usedStorage, | ||
totalStorage, | ||
}); | ||
|
||
expect(createdUserStorage.userID).toEqual(userID); | ||
expect(createdUserStorage.usedStorage).toBe(usedStorage); | ||
expect(createdUserStorage.totalStorage).toBe(totalStorage); | ||
expect(createdUserStorage.createdAt).toBeDefined(); | ||
expect(createdUserStorage.updatedAt).toBeDefined(); | ||
}); | ||
|
||
it('should find a user storage by user ID', async () => { | ||
const userID = new mongoose.Types.ObjectId(); | ||
|
||
const createdUserStorage = await UserStorageModel.create({ userID }); | ||
const foundUserStorage = await UserStorageModel.findOne({ userID }); | ||
|
||
expect(foundUserStorage.userID).toEqual(createdUserStorage.userID); | ||
expect(foundUserStorage.usedStorage).toBe(createdUserStorage.usedStorage); | ||
expect(foundUserStorage.totalStorage).toBe(createdUserStorage.totalStorage); | ||
expect(foundUserStorage.createdAt).toEqual(createdUserStorage.createdAt); | ||
expect(foundUserStorage.updatedAt).toEqual(createdUserStorage.updatedAt); | ||
}); | ||
|
||
it('should update used storage for a user', async () => { | ||
const userID = new mongoose.Types.ObjectId(); | ||
const initialUsedStorage = 0; | ||
const additionalUsedStorage = 2000000; | ||
|
||
const createdUserStorage = await UserStorageModel.create({ userID }); | ||
await UserStorageModel.findOneAndUpdate( | ||
{ userID }, | ||
{ $inc: { usedStorage: additionalUsedStorage } } | ||
); | ||
|
||
const updatedUserStorage = await UserStorageModel.findOne({ userID }); | ||
|
||
expect(updatedUserStorage.usedStorage).toBe(initialUsedStorage + additionalUsedStorage); | ||
}); | ||
|
||
// Add more test cases as needed | ||
|
||
}); |
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,52 @@ | ||
const mongoose = require('mongoose'); | ||
const ImageModel = require('../Image'); | ||
|
||
describe('Image Model', () => { | ||
beforeEach(async () => { | ||
await mongoose.connection.db.dropDatabase(); | ||
}); | ||
|
||
it('should create a new image', async () => { | ||
const imageData = { | ||
userID: new mongoose.Types.ObjectId(), | ||
imageName: 'test-image.jpg', | ||
imageSize: 1024, | ||
contentType: 'image/jpeg', | ||
img: Buffer.from('test-image-data'), | ||
}; | ||
|
||
const createdImage = await ImageModel.create(imageData); | ||
|
||
expect(createdImage.userID).toEqual(imageData.userID); | ||
expect(createdImage.imageName).toBe(imageData.imageName); | ||
expect(createdImage.imageSize).toBe(imageData.imageSize); | ||
expect(createdImage.contentType).toBe(imageData.contentType); | ||
expect(createdImage.img.toString()).toBe(imageData.img.toString()); | ||
expect(createdImage.createdAt).toBeDefined(); | ||
expect(createdImage.updatedAt).toBeDefined(); | ||
}); | ||
|
||
it('should find an image by ID', async () => { | ||
const imageData = { | ||
userID: new mongoose.Types.ObjectId(), | ||
imageName: 'test-image.jpg', | ||
imageSize: 1024, | ||
contentType: 'image/jpeg', | ||
img: Buffer.from('test-image-data'), | ||
}; | ||
|
||
const createdImage = await ImageModel.create(imageData); | ||
const foundImage = await ImageModel.findById(createdImage._id); | ||
|
||
expect(foundImage.userID).toEqual(createdImage.userID); | ||
expect(foundImage.imageName).toBe(createdImage.imageName); | ||
expect(foundImage.imageSize).toBe(createdImage.imageSize); | ||
expect(foundImage.contentType).toBe(createdImage.contentType); | ||
expect(foundImage.img.toString()).toBe(createdImage.img.toString()); | ||
expect(foundImage.createdAt).toEqual(createdImage.createdAt); | ||
expect(foundImage.updatedAt).toEqual(createdImage.updatedAt); | ||
}); | ||
|
||
// Add more test cases as needed | ||
|
||
}); |
Oops, something went wrong.