Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mcbrewster committed Sep 20, 2023
1 parent 61b0846 commit 26fabd1
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/datatypes/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ export interface PlywoodRangeJS {
export abstract class Range<T> {
static DEFAULT_BOUNDS = '[)';

static areEquivalentBounds(bounds1: string | undefined, bounds2: string | undefined): boolean {
return (
bounds1 === bounds2 ||
(!bounds1 && bounds2 === Range.DEFAULT_BOUNDS) ||
(!bounds2 && bounds1 === Range.DEFAULT_BOUNDS)
);
}

static isRange(candidate: any): candidate is PlywoodRange {
return candidate instanceof Range;
}
Expand Down
1 change: 1 addition & 0 deletions src/datatypes/timeRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export class TimeRange extends Range<Date> implements Instance<TimeRangeValue, T
): TimeRange {
if (!date) return null;
const start = duration.floor(date, timezone);

return new TimeRange({
start: start,
end: duration.shift(start, timezone, 1),
Expand Down
4 changes: 2 additions & 2 deletions src/expressions/timeBucketExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { Duration, Timezone } from 'chronoshift';
import { immutableEqual } from 'immutable-class';

import { PlywoodValue } from '../datatypes';
import { PlywoodValue, Range } from '../datatypes';
import { TimeRange } from '../datatypes/timeRange';
import { SQLDialect } from '../dialect/baseDialect';

Expand Down Expand Up @@ -75,7 +75,7 @@ export class TimeBucketExpression extends ChainableExpression {
return (
super.equals(other) &&
this.duration.equals(other.duration) &&
this.bounds === other.bounds &&
Range.areEquivalentBounds(this.bounds, other.bounds) &&
immutableEqual(this.timezone, other.timezone)
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/expressions/timeRangeExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { Duration, Timezone } from 'chronoshift';
import { immutableEqual } from 'immutable-class';

import { PlywoodValue } from '../datatypes';
import { PlywoodValue, Range } from '../datatypes';
import { TimeRange } from '../datatypes/timeRange';
import { SQLDialect } from '../dialect/baseDialect';
import { pluralIfNeeded } from '../helper/utils';
Expand Down Expand Up @@ -80,7 +80,7 @@ export class TimeRangeExpression extends ChainableExpression implements HasTimez
super.equals(other) &&
this.duration.equals(other.duration) &&
this.step === other.step &&
this.bounds === other.bounds &&
Range.areEquivalentBounds(this.bounds, other.bounds) &&
immutableEqual(this.timezone, other.timezone)
);
}
Expand Down
51 changes: 48 additions & 3 deletions test/datatypes/timeRange.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ const { expect } = require('chai');

const { testImmutableClass } = require('immutable-class-tester');

const { Timezone } = require('chronoshift');
const { Timezone, Duration } = require('chronoshift');
const plywood = require('../plywood');

const { TimeRange, $, ply, r } = plywood;
const { Range, TimeRange } = plywood;

describe('TimeRange', () => {
it('is immutable class', () => {
Expand Down Expand Up @@ -228,4 +227,50 @@ describe('TimeRange', () => {
});
});
});

describe('Accepts bounds', () => {
it('Can create a Time Range from a time bucket with bounds', () => {
const timeRange = TimeRange.timeBucket(
new Date('2015-02-26T05:00:00.000Z'),
Duration.fromJS('PT1H'),
Timezone.fromJS('Etc/UTC'),
'[]',
);
expect(timeRange.toJS()).to.deep.equal({
start: new Date('2015-02-26T05:00:00.000Z'),
end: new Date('2015-02-26T06:00:00.000Z'),
bounds: '[]',
});
});

it('Can create a Time Range from a time bucket without explicit bounds', () => {
const timeRange = TimeRange.timeBucket(
new Date('2015-02-26T05:00:00.000Z'),
Duration.fromJS('PT1H'),
Timezone.fromJS('Etc/UTC'),
);

// does not include bounds in toJS if default bounds are used
expect(timeRange.toJS()).to.deep.equal({
start: new Date('2015-02-26T05:00:00.000Z'),
end: new Date('2015-02-26T06:00:00.000Z'),
});
expect(timeRange.bounds).to.equal(Range.DEFAULT_BOUNDS);
});

it('Can change bounds', () => {
const timeRange = TimeRange.timeBucket(
new Date('2015-02-26T05:00:00.000Z'),
Duration.fromJS('PT1H'),
Timezone.fromJS('Etc/UTC'),
);

// does not include bounds in toJS if default bounds are used
expect(timeRange.changeBounds('()').toJS()).to.deep.equal({
start: new Date('2015-02-26T05:00:00.000Z'),
end: new Date('2015-02-26T06:00:00.000Z'),
bounds: '()',
});
});
});
});
73 changes: 73 additions & 0 deletions test/expression/timeBucketExpression.mocha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2015-2020 Imply Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const { expect } = require('chai');

const plywood = require('../plywood');
const { TimeBucketExpression } = plywood;

describe('TimeBucketExpression', () => {
const timeBucketWithoutBounds = TimeBucketExpression.fromJS({
duration: 'PT1H',
step: -2,
timezone: 'Etc/UTC',
});

describe('Accepts bounds', () => {
it('Can create an expression with bounds', () => {
const timeBucket = TimeBucketExpression.fromJS({
duration: 'PT1H',
timezone: 'Etc/UTC',
bounds: '[]',
});
expect(timeBucket.toJS()).to.deep.equal({
duration: 'PT1H',
timezone: 'Etc/UTC',
bounds: '[]',
op: 'timeBucket',
});
});

it('Can create an expression without bounds', () => {
expect(timeBucketWithoutBounds.toJS()).to.deep.equal({
duration: 'PT1H',
timezone: 'Etc/UTC',
op: 'timeBucket',
});
});

it('Can change bounds', () => {
expect(timeBucketWithoutBounds.changeBounds('(]').toJS()).to.deep.equal({
duration: 'PT1H',
timezone: 'Etc/UTC',
op: 'timeBucket',
bounds: '(]',
});
});

it('preforms equals properly with bounds', () => {
expect(timeBucketWithoutBounds.equals(timeBucketWithoutBounds.changeBounds('[)'))).to.equal(
true,
);
expect(timeBucketWithoutBounds.equals(timeBucketWithoutBounds.changeBounds('[]'))).to.equal(
false,
);
expect(timeBucketWithoutBounds.changeBounds('[)').equals(timeBucketWithoutBounds)).to.equal(
true,
);
});
});
});
77 changes: 77 additions & 0 deletions test/expression/timeRangeExpression.mocha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2015-2020 Imply Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const { expect } = require('chai');

const plywood = require('../plywood');
const { TimeRangeExpression } = plywood;

describe('TimeRangeExpression', () => {
const timeRangeWithoutBounds = TimeRangeExpression.fromJS({
duration: 'PT1H',
step: -2,
timezone: 'Etc/UTC',
});

describe('Accepts bounds', () => {
it('Can create an expression with bounds', () => {
const timeRange = TimeRangeExpression.fromJS({
duration: 'PT1H',
step: -2,
timezone: 'Etc/UTC',
bounds: '[]',
});
expect(timeRange.toJS()).to.deep.equal({
duration: 'PT1H',
step: -2,
timezone: 'Etc/UTC',
bounds: '[]',
op: 'timeRange',
});
});

it('Can create an expression without bounds', () => {
expect(timeRangeWithoutBounds.toJS()).to.deep.equal({
duration: 'PT1H',
step: -2,
timezone: 'Etc/UTC',
op: 'timeRange',
});
});

it('Can change bounds', () => {
expect(timeRangeWithoutBounds.changeBounds('(]').toJS()).to.deep.equal({
duration: 'PT1H',
step: -2,
timezone: 'Etc/UTC',
op: 'timeRange',
bounds: '(]',
});
});

it('preforms equals properly with bounds', () => {
expect(timeRangeWithoutBounds.equals(timeRangeWithoutBounds.changeBounds('[)'))).to.equal(
true,
);
expect(timeRangeWithoutBounds.equals(timeRangeWithoutBounds.changeBounds('[]'))).to.equal(
false,
);
expect(timeRangeWithoutBounds.changeBounds('[)').equals(timeRangeWithoutBounds)).to.equal(
true,
);
});
});
});

0 comments on commit 26fabd1

Please sign in to comment.