Skip to content

Commit

Permalink
- Added normalizer tests using Bard.
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennenoel committed Feb 1, 2024
1 parent 552cfa3 commit 6815527
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 11 deletions.
14 changes: 7 additions & 7 deletions packages/data-mapping/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/data-mapping/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"@pristine-ts/class-validator": "^1.0.23",
"@pristine-ts/common": "file:../common",
"@pristine-ts/logging": "file:../logging",
"@pristine-ts/metadata": "~1.0.4",
"@pristine-ts/metadata": "~1.0.6",
"@pristine-ts/networking": "file:../networking",
"class-transformer": "^0.5.1",
"date-fns": "^3.3.1",
Expand Down
71 changes: 71 additions & 0 deletions packages/data-mapping/src/normalizers/date.normalizer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import "reflect-metadata"
import {DateNormalizer} from "./date.normalizer";

describe('DateNormalizer', () => {

it('should return undefined for undefined input', () => {
const normalizer = new DateNormalizer();
expect(normalizer.normalize(undefined)).toBeUndefined();
});

it('should return the current date for undefined input when returnUndefinedOnInvalidDate is false', () => {
const normalizer = new DateNormalizer();
const expectedDate = new Date();
expect(normalizer.normalize(undefined, {returnUndefinedOnInvalidDate: false})).toEqual(expectedDate);
});

it('should return a Date object for a valid date string', () => {
const normalizer = new DateNormalizer();
const dateString = '2024-01-31T18:53:00Z';
const expectedDate = new Date(dateString);
expect(normalizer.normalize(dateString)).toEqual(expectedDate);
});

it('should return undefined for an invalid date string', () => {
const normalizer = new DateNormalizer();
const invalidDateString = 'invalid-date';
expect(normalizer.normalize(invalidDateString)).toBeUndefined();
});

it('should return the current date for an invalid date string when returnUndefinedOnInvalidDate is false', () => {
const normalizer = new DateNormalizer();
const invalidDateString = 'invalid-date';
const expectedDate = new Date();
expect(normalizer.normalize(invalidDateString, {returnUndefinedOnInvalidDate: false})).toEqual(expectedDate);
});

it('should return a Date object for a number representing milliseconds', () => {
const normalizer = new DateNormalizer();
const milliseconds = 1675275580000; // Represents 2024-01-31T18:53:00Z
const expectedDate = new Date(milliseconds);
expect(normalizer.normalize(milliseconds)).toEqual(expectedDate);
});

it('should treat a number as seconds when options.treatNumbers is "seconds"', () => {
const normalizer = new DateNormalizer();
const seconds = 1675275580; // Represents 2024-01-31T18:53:00Z in seconds
const expectedDate = new Date(seconds * 1000);
expect(normalizer.normalize(seconds, {treatNumbers: 'seconds'})).toEqual(expectedDate);
});

it('should return a Date object for a valid date object', () => {
const normalizer = new DateNormalizer();
const dateObject = {year: 2024, month: 0, day: 31, hours: 18, minutes: 53, seconds: 0};
const expectedDate = new Date(2024, 0, 31, 18, 53, 0);

const normalizedDate = normalizer.normalize(dateObject);
expect(normalizedDate).toBeDefined()
expect(normalizedDate!.getSeconds()).toEqual(expectedDate.getSeconds());
expect(normalizedDate!.getMinutes()).toEqual(expectedDate.getMinutes());
expect(normalizedDate!.getHours()).toEqual(expectedDate.getHours());
expect(normalizedDate!.getDate()).toEqual(expectedDate.getDate());
expect(normalizedDate!.getMonth()).toEqual(expectedDate.getMonth());
expect(normalizedDate!.getFullYear()).toEqual(expectedDate.getFullYear());
});

it('should return undefined for an invalid date object', () => {
const normalizer = new DateNormalizer();
const invalidDateObject = {year: 'invalid', month: 100};
expect(normalizer.normalize(invalidDateObject)).toBeUndefined();
});
});
21 changes: 21 additions & 0 deletions packages/data-mapping/src/normalizers/lowercase.normalizer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,25 @@ describe('Lowercase Normalizer', () => {

expect(lowercaseNormalizer.normalize("AAA", new LowercaseNormalizerOptions())).toBe("aaa");
})

it('should return the original value for non-string types if shouldThrowIfTypeIsNotString is false', () => {
const normalizer = new LowercaseNormalizer();
expect(normalizer.normalize(null)).toBeNull();
expect(normalizer.normalize({})).toBeInstanceOf(Object);
expect(normalizer.normalize(123)).toBe(123);
});

it('should throw an error for non-string types if shouldThrowIfTypeIsNotString is true', () => {
const normalizer = new LowercaseNormalizer();
expect(() => normalizer.normalize(null, { shouldThrowIfTypeIsNotString: true })).toThrowError();
expect(() => normalizer.normalize({}, { shouldThrowIfTypeIsNotString: true })).toThrowError();
expect(() => normalizer.normalize(123, { shouldThrowIfTypeIsNotString: true })).toThrowError();
});

it('should lowercase strings', () => {
const normalizer = new LowercaseNormalizer();
expect(normalizer.normalize('HeLlO WoRlD')).toBe('hello world');
expect(normalizer.normalize('MIXED CASE')).toBe('mixed case');
expect(normalizer.normalize('already lowercase')).toBe('already lowercase');
});
});
35 changes: 35 additions & 0 deletions packages/data-mapping/src/normalizers/number.normalizer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import "reflect-metadata"
import {NumberNormalizer} from "./number.normalizer";

describe('NumberNormalizer', () => {

it('should return undefined for invalid source types', () => {
const normalizer = new NumberNormalizer();
expect(normalizer.normalize(null)).toBeUndefined();
expect(normalizer.normalize({})).toBeUndefined();
expect(normalizer.normalize(() => {})).toBeUndefined();
});

it('should return 0 if ignoreUndefined is false', () => {
const normalizer = new NumberNormalizer();
expect(normalizer.normalize(null, { ignoreUndefined: false })).toBe(0);
expect(normalizer.normalize({}, { ignoreUndefined: false })).toBeUndefined();
});

it('should return undefined for invalid numbers', () => {
const normalizer = new NumberNormalizer();
expect(normalizer.normalize('invalid number')).toBeUndefined();
});

it('should normalize numbers', () => {
const normalizer = new NumberNormalizer();
expect(normalizer.normalize(123)).toBe(123);
expect(normalizer.normalize(3.14159)).toBe(3.14159);
});

it('should normalize valid numeric strings', () => {
const normalizer = new NumberNormalizer();
expect(normalizer.normalize('123')).toBe(123);
expect(normalizer.normalize('45.67')).toBe(45.67);
});
});
2 changes: 1 addition & 1 deletion packages/data-mapping/src/normalizers/number.normalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class NumberNormalizer implements DataNormalizerInterface<number | undefi
return source;
}

if (typeEnum === undefined) {
if (typeEnum === undefined || typeEnum === TypeEnum.Null) {
if (options?.ignoreUndefined === false) {
return 0;
}
Expand Down
68 changes: 68 additions & 0 deletions packages/data-mapping/src/normalizers/string.normalizer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import "reflect-metadata"
import {StringNormalizer} from "./string.normalizer";

describe('StringNormalizer', () => {

it('should return undefined for invalid source types if ignoreUndefined is true', () => {
const normalizer = new StringNormalizer();
expect(normalizer.normalize(null)).toBeUndefined();
});

it('should return an empty string for invalid source types if ignoreUndefined is false', () => {
const normalizer = new StringNormalizer();
expect(normalizer.normalize(null, {ignoreUndefined: false})).toBe('');
});

it('should normalize strings', () => {
const normalizer = new StringNormalizer();
expect(normalizer.normalize('hello world')).toBe('hello world');
});

it('should convert numbers to strings', () => {
const normalizer = new StringNormalizer();
expect(normalizer.normalize(123)).toBe('123');
expect(normalizer.normalize(45.67)).toBe('45.67');
});

it('should convert booleans to strings', () => {
const normalizer = new StringNormalizer();
expect(normalizer.normalize(true)).toBe('true');
expect(normalizer.normalize(false)).toBe('false');
});

it('should format dates using the dateFormat option', () => {
const normalizer = new StringNormalizer();
const date = new Date(2024, 0, 31);
expect(normalizer.normalize(date, {dateFormat: 'yyyy-MM-dd'})).toBe('2024-01-31');
});

it('should format dates using toJSON() if no dateFormat is provided', () => {
const normalizer = new StringNormalizer();
const date = new Date(2024, 0, 31);
expect(normalizer.normalize(date)).toBe('2024-01-31T08:00:00.000Z');
});

it('should normalize arrays by normalizing each item', () => {
const normalizer = new StringNormalizer();
const array = [123, true, 'hello', new Date(2024, 0, 31)];
expect(normalizer.normalize(array)).toEqual(['123', 'true', 'hello', '2024-01-31T08:00:00.000Z']);
});

it('should normalize objects with a toString() method', () => {
const normalizer = new StringNormalizer();
const object = {toString: () => 'custom string'};
expect(normalizer.normalize(object)).toBe('custom string');
});

it('should normalize objects using JSON.stringify()', () => {
const normalizer = new StringNormalizer();
const object = {name: 'John Doe', age: 30};
expect(normalizer.normalize(object)).toBe('{"name":"John Doe","age":30}');
});

it('should convert other types to strings using "" + source', () => {
const normalizer = new StringNormalizer();
const symbol = Symbol('test');
expect(normalizer.normalize(symbol)).toBe('Symbol(test)');
});
});
5 changes: 3 additions & 2 deletions packages/data-mapping/src/normalizers/string.normalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class StringNormalizer implements DataNormalizerInterface<string | undefi
normalize(source: any, options?: StringNormalizerOptions): string | undefined {
const typeEnum = TypeUtils.getTypeOfValue(source);

if (typeEnum === undefined) {
if (typeEnum === undefined || typeEnum === TypeEnum.Null) {
if (options?.ignoreUndefined === false) {
return "";
}
Expand Down Expand Up @@ -42,8 +42,9 @@ export class StringNormalizer implements DataNormalizerInterface<string | undefi
case TypeEnum.Array:
return source.map( (item: any) => this.normalize(item, options));

case TypeEnum.Symbol:
case TypeEnum.Object:
if(source.hasOwnProperty("toString")) {
if(typeof source.toString === "function") {
return source.toString();
}

Expand Down

0 comments on commit 6815527

Please sign in to comment.