Skip to content

Commit

Permalink
Merge pull request #710 from Green-Software-Foundation/migrate-mock-o…
Browse files Browse the repository at this point in the history
…bs-to-builtins

Migrate `mock-observations` to `builtins`
  • Loading branch information
jmcook1186 authored May 28, 2024
2 parents 0b8434c + 80063fc commit 15d6581
Show file tree
Hide file tree
Showing 12 changed files with 869 additions and 7 deletions.
44 changes: 44 additions & 0 deletions src/__tests__/unit/builtins/CommonGenerator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {KeyValuePair} from '../../../types/common';

import {ERRORS} from '../../../util/errors';

import {CommonGenerator} from '../../../builtins/mock-observations/helpers/common-generator';

const {InputValidationError} = ERRORS;

describe('lib/mock-observations/CommonGenerator: ', () => {
describe('initialize: ', () => {
it('throws an error when config is not empty object.', async () => {
const commonGenerator = CommonGenerator({});

expect.assertions(1);

try {
commonGenerator.next([]);
} catch (error) {
expect(error).toEqual(
new InputValidationError(
'CommonGenerator: Config must not be null or empty.'
)
);
}
});
});

describe('next(): ', () => {
it('returns a result with valid data.', async () => {
const config: KeyValuePair = {
key1: 'value1',
key2: 'value2',
};
const commonGenerator = CommonGenerator(config);

expect.assertions(1);

expect(commonGenerator.next([])).toStrictEqual({
key1: 'value1',
key2: 'value2',
});
});
});
});
71 changes: 71 additions & 0 deletions src/__tests__/unit/builtins/RandIntGenerator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {KeyValuePair} from '../../../types/common';

import {ERRORS} from '../../../util/errors';

import {RandIntGenerator} from '../../../builtins/mock-observations/helpers/rand-int-generator';

const {InputValidationError} = ERRORS;

describe('lib/mock-observations/RandIntGenerator: ', () => {
describe('initialize', () => {
it('throws an error when the generator name is empty string.', async () => {
expect.assertions(1);
try {
RandIntGenerator('', {});
} catch (error) {
expect(error).toEqual(
new InputValidationError(
'RandIntGenerator: `name` is empty or all spaces.'
)
);
}
});

it('throws an error when config is empty object.', async () => {
expect.assertions(1);
try {
RandIntGenerator('generator-name', {});
} catch (error) {
expect(error).toEqual(
new InputValidationError(
'RandIntGenerator: Config must not be null or empty.'
)
);
}
});

it('throws an error `min` is missing from the config.', async () => {
const config = {max: 90};

expect.assertions(1);

try {
RandIntGenerator('random', config);
} catch (error) {
expect(error).toEqual(
new InputValidationError(
'RandIntGenerator: Config is missing min or max.'
)
);
}
});
});

describe('next(): ', () => {
it('returns a result with valid data.', async () => {
const config: KeyValuePair = {
min: 10,
max: 90,
};
const randIntGenerator = RandIntGenerator('random', config);
const result = randIntGenerator.next([]) as {random: number};

expect.assertions(4);

expect(result).toBeInstanceOf(Object);
expect(result).toHaveProperty('random');
expect(result.random).toBeGreaterThanOrEqual(10);
expect(result.random).toBeLessThanOrEqual(90);
});
});
});
Loading

0 comments on commit 15d6581

Please sign in to comment.