-
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 normalizer tests using Bard.
- Loading branch information
1 parent
552cfa3
commit 6815527
Showing
8 changed files
with
207 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
71 changes: 71 additions & 0 deletions
71
packages/data-mapping/src/normalizers/date.normalizer.spec.ts
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 @@ | ||
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(); | ||
}); | ||
}); |
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
35 changes: 35 additions & 0 deletions
35
packages/data-mapping/src/normalizers/number.normalizer.spec.ts
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,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); | ||
}); | ||
}); |
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
68 changes: 68 additions & 0 deletions
68
packages/data-mapping/src/normalizers/string.normalizer.spec.ts
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,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)'); | ||
}); | ||
}); |
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