Skip to content

Commit

Permalink
test: date range check
Browse files Browse the repository at this point in the history
  • Loading branch information
mShan0 committed Feb 7, 2024
1 parent e30d927 commit ecc7fb3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/data-types/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ const EPOCH_DATE = LocalDate.ofYearDay(1, 1);
const NULL_LENGTH = Buffer.from([0x00]);
const DATA_LENGTH = Buffer.from([0x03]);

const MIN_DATE = new globalDate('January 1, 0001');
const MAX_DATE = new globalDate('December 31, 9999');

const Date: DataType = {
id: 0x28,
type: 'DATEN',
Expand Down Expand Up @@ -65,11 +62,14 @@ const Date: DataType = {

// TODO: check date range: January 1, 0001, through December 31, 9999
// : time range: 00:00:00 through 23:59:59.997
let year;
if (options && options.useUTC) {
value = new globalDate(value.toUTCString());
year = value.getUTCFullYear();
} else {
year = value.getFullYear();
}

if (value < MIN_DATE || value > MAX_DATE) {
if (year < 1 || year > 9999) {
throw new TypeError('Out of range.');
}

Expand Down
6 changes: 4 additions & 2 deletions test/unit/data-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ describe('Date', function() {
describe('.validate', function() {
it('returns a TypeError for dates that are out of range', function() {
assert.throws(() => {
TYPES.Date.validate(new Date('Dec 31 2000'));
const testDate = new Date();
testDate.setFullYear(0);
TYPES.Date.validate(testDate);
}, TypeError, 'Out of range.');

assert.throws(() => {
Expand Down Expand Up @@ -742,7 +744,7 @@ describe('Money', function() {
});
});

describe.only('.validate', function() {
describe('.validate', function() {
it('throws Invalid number error for NaN input', function() {
assert.throws(() => {
TYPES.TinyInt.validate('string');
Expand Down

0 comments on commit ecc7fb3

Please sign in to comment.