Skip to content

Commit

Permalink
added test coverage for Gaussian and Median blur
Browse files Browse the repository at this point in the history
  • Loading branch information
abdullahranginwala authored and SahanDisa committed Aug 10, 2023
1 parent d72872a commit 8ad1886
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 3 deletions.
2 changes: 1 addition & 1 deletion imagelab_electron/src/operator/blurring/MedianBlur.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MedianBlur extends OpenCvOperator {
setParams(param, value) {
if (param === "kernelSize") {
if (value % 2 === 0) {
throw new Error("Kernal Size should be an odd number");
throw new Error("Kernel Size should be an odd number");
}
this.#kernalSize = value;
}
Expand Down
68 changes: 68 additions & 0 deletions imagelab_electron/tests/unit/blurring/GaussianBlur.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const GaussianBlur = require('../../../src/operator/blurring/GaussianBlur')
const opencvMock = require('../opencv-mocks/blurring.mock');

describe('GaussianBlur Operator', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('should process Gaussian blur function with given parameters', () => {
const operator = new GaussianBlur('type', 'id');
operator.cv2 = opencvMock.cv2;

// Set private variables using setParams
operator.setParams('widthSize', 3);
operator.setParams('heightSize', 3);

const imageMock = 'mockImageData';

// Mock behavior for cv2 functions
opencvMock.cv2.Size.mockReturnValue({ width: 3, height: 3 });

operator.compute(imageMock);

// Assertions
expect(opencvMock.cv2.Mat).toHaveBeenCalledTimes(1);
expect(opencvMock.cv2.Size).toHaveBeenCalledWith(3, 3);
expect(opencvMock.cv2.GaussianBlur).toHaveBeenCalledWith(
imageMock,
{},
opencvMock.cv2.Size(),
0,
0,
opencvMock.cv2.BORDER_DEFAULT
);
});

it('should throw an error if the height is even', () => {
const operator = new GaussianBlur('type', 'id');
operator.cv2 = opencvMock.cv2;

// Set private variables using setParams
operator.setParams('widthSize', 3);
operator.setParams('heightSize', 2);

const imageMock = 'mockImageData';

// Assertions
expect(() => {
operator.compute(imageMock);
}).toThrow('Height should be an odd number!');
});

it('should throw an error if the width is even', () => {
const operator = new GaussianBlur('type', 'id');
operator.cv2 = opencvMock.cv2;

// Set private variables using setParams
operator.setParams('widthSize', 2);
operator.setParams('heightSize', 3);

const imageMock = 'mockImageData';

// Assertions
expect(() => {
operator.compute(imageMock);
}).toThrow('Width should be an odd number!');
});
});
39 changes: 39 additions & 0 deletions imagelab_electron/tests/unit/blurring/MedianBlur.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const MedianBlur = require('../../../src/operator/blurring/MedianBlur')
const opencvMock = require('../opencv-mocks/blurring.mock');

describe('MedianBlur Operator', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('should process median blur function with given kernel size', () => {
const operator = new MedianBlur('type', 'id');
operator.cv2 = opencvMock.cv2; // Inject the cv2 mock

// Set private variable using setParams
operator.setParams('kernelSize', 5);

const imageMock = 'mockImageData';

operator.compute(imageMock);

// Assertions
expect(opencvMock.cv2.Mat).toHaveBeenCalledTimes(1);
expect(opencvMock.cv2.medianBlur).toHaveBeenCalledWith(
imageMock,
{},
5
);
});

it('should throw an error if kernel size is even', () => {
const operator = new MedianBlur('type', 'id');
operator.cv2 = opencvMock.cv2;

// Assertions
expect(() => {
operator.setParams('kernelSize', 4);
}).toThrow('Kernel Size should be an odd number');
});

});
6 changes: 4 additions & 2 deletions imagelab_electron/tests/unit/opencv-mocks/blurring.mock.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module.exports = {
cv2: {
Mat: jest.fn(),
Size: jest.fn((width, height) => ({ width, height })), // Mock Size with a function that returns an object
Point: jest.fn((x, y) => ({ x, y })), // Mock Point with a function that returns an object
Size: jest.fn((width, height) => ({ width, height })),
Point: jest.fn((x, y) => ({ x, y })),
blur: jest.fn(),
GaussianBlur: jest.fn(),
medianBlur: jest.fn(),
},
};

0 comments on commit 8ad1886

Please sign in to comment.