Skip to content

Commit c8aad87

Browse files
authored
Merge pull request #445 from ml054/RDBC-866
RDBC-867 Get rid of moment.js (from tests)
2 parents 7718af3 + 852e070 commit c8aad87

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1124
-1123
lines changed

package-lock.json

Lines changed: 0 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
"http-proxy-agent": "^7.0.2",
7070
"lodash.orderby": "^4.6.0",
7171
"mocha": "^10.7.0",
72-
"moment": "^2.30.1",
7372
"open": "^10.1.0",
7473
"reflect-metadata": "^0.2.2",
7574
"rimraf": "^6.0.1",

src/Utility/DateUtil.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export class DateUtil {
1414
public static default: DateUtil = new DateUtil({});
1515

1616
public static utc: DateUtil = new DateUtil({ useUtcDates: true });
17+
public static tz: DateUtil = new DateUtil({ withTimezone: true });
1718

1819
public constructor(protected opts: DateUtilOpts) {}
1920

test/Issues/RDBC_236.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import assert from "node:assert"
2-
import moment from "moment";
32
import { testContext, disposeTestDocumentStore } from "../Utils/TestUtil.js";
43

54
import {
@@ -9,10 +8,7 @@ import {
98
import { DateUtil } from "../../src/Utility/DateUtil.js";
109
import { StringUtil } from "../../src/Utility/StringUtil.js";
1110
import { assertThat } from "../Utils/AssertExtensions.js";
12-
13-
14-
const momentDefaultDateFormat = "YYYY-MM-DDTHH:mm:ss.SSS0000";
15-
const momentDefaultDateTzFormat = "YYYY-MM-DDTHH:mm:ss.SSS0000Z";
11+
import { format } from "date-fns";
1612

1713
// getTimezoneOffset() returns reversed offset, hence the "-"
1814
const LOCAL_TIMEZONE_OFFSET = -(new Date(2018, 7, 1).getTimezoneOffset());
@@ -141,7 +137,7 @@ describe("DateUtil", function () {
141137
const dateUtil = new DateUtil({
142138
withTimezone: false
143139
});
144-
const date = moment("2018-10-15T09:46:28.306").toDate();
140+
const date = DateUtil.default.parse("2018-10-15T09:46:28.306");
145141
const stringified = dateUtil.stringify(date);
146142
assert.strictEqual(stringified, "2018-10-15T09:46:28.3060000");
147143

@@ -155,11 +151,11 @@ describe("DateUtil", function () {
155151
withTimezone: false,
156152
useUtcDates: true
157153
});
158-
const date = moment("2018-10-15T12:00:00.000").toDate();
154+
const date = DateUtil.default.parse("2018-10-15T12:00:00.000");
159155
const stringified = dateUtil.stringify(date);
160156

161157
const expected = new Date(2018, 9, 15, date.getHours() - LOCAL_TIMEZONE_OFFSET_HOURS, 0, 0, 0);
162-
const expectedStringified = moment(expected).format(momentDefaultDateFormat) + "Z";
158+
const expectedStringified = format(expected, DateUtil.DEFAULT_DATE_FORMAT) + "Z";
163159
assert.strictEqual(stringified, expectedStringified);
164160

165161
const parsed = dateUtil.parse(stringified);
@@ -178,15 +174,14 @@ describe("DateUtil", function () {
178174

179175
const hour6 = 12;
180176
const timezoneOffsetHours = 6;
181-
const date = moment.parseZone(`2018-10-15T${hour6}:00:00.0000000+06:00`).toDate();
177+
const date = DateUtil.tz.parse(`2018-10-15T${hour6}:00:00.0000000+06:00`);
182178
// preconditions check
183179
assert.strictEqual(
184180
date.getHours(), hour6 - timezoneOffsetHours + LOCAL_TIMEZONE_OFFSET_HOURS);
185181

186182
const expectedHours = date.getHours();
187183
const expected = new Date(2018, 9, 15, expectedHours, 0, 0, 0);
188-
const expectedStringified =
189-
moment(expected).format(momentDefaultDateFormat) + LOCAL_TIMEZONE_STRING;
184+
const expectedStringified = format(expected, DateUtil.DEFAULT_DATE_FORMAT) + LOCAL_TIMEZONE_STRING;
190185
const stringified = dateUtil.stringify(date);
191186
assert.strictEqual(stringified, expectedStringified);
192187

@@ -203,16 +198,15 @@ describe("DateUtil", function () {
203198

204199
const hour6 = 12;
205200
const timezoneOffsetHours = 6;
206-
const date = moment.parseZone(`2018-10-15T${hour6}:00:00.0000000+06:00`).toDate();
201+
const date = DateUtil.tz.parse(`2018-10-15T${hour6}:00:00.0000000+06:00`);
207202
// preconditions check
208203
assert.strictEqual(
209204
date.getHours(), hour6 - timezoneOffsetHours + LOCAL_TIMEZONE_OFFSET_HOURS);
210205

211206
const expectedHours = date.getHours() - LOCAL_TIMEZONE_OFFSET_HOURS;
212207
const utcTimezoneString = "+00:00";
213208
const expected = new Date(2018, 9, 15, expectedHours, 0, 0, 0);
214-
const expectedStringified =
215-
moment(expected).format(momentDefaultDateFormat) + utcTimezoneString;
209+
const expectedStringified = format(expected, DateUtil.DEFAULT_DATE_FORMAT) + utcTimezoneString;
216210
const stringified = dateUtil.stringify(date);
217211
assert.strictEqual(stringified, expectedStringified);
218212

test/Issues/RDBC_501.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
import { disposeTestDocumentStore, testContext } from "../Utils/TestUtil.js";
77

88
import { assertThat } from "../Utils/AssertExtensions.js";
9+
import { addDays, addHours } from "date-fns";
910

1011
class SymbolPrice {
1112
open: number;
@@ -65,9 +66,9 @@ describe("RDBC-501", function () {
6566

6667
const tsf = session.timeSeriesFor<SymbolPrice>(symbol, "history", SymbolPrice);
6768

68-
tsf.append(baseLine.clone().add(1, "hours").toDate(), price1);
69-
tsf.append(baseLine.clone().add(2, "hours").toDate(), price2);
70-
tsf.append(baseLine.clone().add(2, "days").toDate(), price3);
69+
tsf.append(addHours(baseLine, 1), price1);
70+
tsf.append(addHours(baseLine, 2), price2);
71+
tsf.append(addDays(baseLine, 2), price3);
7172

7273
await session.saveChanges();
7374
}

test/Ported/Core/Commands/Other.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import { disposeTestDocumentStore, testContext } from "../../../Utils/TestUtil.js";
88
import { User } from "../../../Assets/Entities.js";
99
import { assertThat } from "../../../Utils/AssertExtensions.js";
10+
import { addMinutes } from "date-fns";
1011

1112
describe("OtherTest", function () {
1213
let store: IDocumentStore;
@@ -32,7 +33,7 @@ describe("OtherTest", function () {
3233
const ts = session.timeSeriesFor(id, "HeartRates");
3334
const cf = session.countersFor(id);
3435
for (let j = 0; j < 20; j++) {
35-
ts.append(baseLine.clone().add(j, "minutes").toDate(), j, "watches/apple");
36+
ts.append(addMinutes(baseLine, j), j, "watches/apple");
3637
cf.increment("Likes", j);
3738
}
3839

test/Ported/Faceted/AggregationTest.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import moment from "moment";
21
import assert from "node:assert"
32
import { testContext, disposeTestDocumentStore } from "../../Utils/TestUtil.js";
43

54
import {
6-
AbstractJavaScriptIndexCreationTask,
5+
AbstractJavaScriptIndexCreationTask, DateUtil,
76
IDocumentStore,
87
RangeBuilder,
98
} from "../../../src/index.js";
9+
import { addDays, setYear } from "date-fns";
1010

1111
class ItemsOrders_All extends AbstractJavaScriptIndexCreationTask<ItemsOrder, Pick<ItemsOrder, "at" | "items">> {
1212
public constructor() {
@@ -309,24 +309,24 @@ describe("AggregationTest", function () {
309309
const idx = new ItemsOrders_All();
310310
await idx.execute(store);
311311

312-
const now = moment();
312+
const now = new Date();
313313
{
314314
const session = store.openSession();
315315
const item1 = new ItemsOrder();
316316
item1.items = ["first", "second"];
317-
item1.at = moment(now).toDate();
317+
item1.at = now;
318318

319319
const item2 = new ItemsOrder();
320320
item2.items = ["first", "second"];
321-
item2.at = moment(now).add(-1, "d").toDate();
321+
item2.at = addDays(now, -1);
322322

323323
const item3 = new ItemsOrder();
324324
item3.items = ["first"];
325-
item3.at = moment(now).toDate();
325+
item3.at = now;
326326

327327
const item4 = new ItemsOrder();
328328
item4.items = ["first"];
329-
item4.at = moment(now).toDate();
329+
item4.at = now;
330330

331331
await session.store(item1);
332332
await session.store(item2);
@@ -335,13 +335,12 @@ describe("AggregationTest", function () {
335335
await session.saveChanges();
336336
}
337337

338-
const oldDate = moment(now).toDate();
339-
oldDate.setFullYear(1980);
338+
const oldDate = setYear(now, 1980);
340339

341340
const minValue = oldDate;
342-
const end0 = moment(now).add(-2, "d").toDate();
343-
const end1 = moment(now).add(-1, "d").toDate();
344-
const end2 = moment(now).toDate();
341+
const end0 = addDays(now, -2);
342+
const end1 = addDays(now, -1);
343+
const end2 = now;
345344

346345
await testContext.waitForIndexing(store);
347346

test/Ported/Indexing/TimeSeries/BasicTimeSeriesIndexes_JavaScriptTest.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import {
55
AbstractRawJavaScriptTimeSeriesIndexCreationTask
66
} from "../../../../src/index.js";
77
import { disposeTestDocumentStore, testContext } from "../../../Utils/TestUtil.js";
8-
import moment from "moment";
98
import { Employee } from "../../../Assets/Orders.js";
109
import { Address, Company, User } from "../../../Assets/Entities.js";
1110
import { assertThat } from "../../../Utils/AssertExtensions.js";
1211
import { RavenTestHelper } from "../../../Utils/RavenTestHelper.js";
12+
import { addHours } from "date-fns";
1313

1414
describe("BasicTimeSeriesIndexes_JavaScript", function () {
1515

@@ -24,7 +24,6 @@ describe("BasicTimeSeriesIndexes_JavaScript", function () {
2424

2525
it("basicMapIndexWithLoad", async () => {
2626
const now1 = new Date();
27-
const now2 = moment().add(1, "second").toDate();
2827

2928
{
3029
const session = store.openSession();
@@ -107,7 +106,7 @@ describe("BasicTimeSeriesIndexes_JavaScript", function () {
107106

108107
for (let i = 0; i < 10; i++) {
109108
session.timeSeriesFor(user, "heartRate")
110-
.append(today.clone().add(i, "hours").toDate(), 180 + i, address.id);
109+
.append(addHours(today, i), 180 + i, address.id);
111110
}
112111

113112
await session.saveChanges();
@@ -157,7 +156,6 @@ describe("BasicTimeSeriesIndexes_JavaScript", function () {
157156

158157
it("canMapAllTimeSeriesFromCollection", async function () {
159158
const now1 = new Date();
160-
const now2 = moment().add(1, "seconds").toDate();
161159

162160
{
163161
const session = store.openSession();
@@ -194,14 +192,14 @@ describe("BasicTimeSeriesIndexes_JavaScript", function () {
194192
await session.store(company);
195193

196194
session.timeSeriesFor(company, "heartRate")
197-
.append(now.toDate(), 2.5, "tag1");
195+
.append(now, 2.5, "tag1");
198196
session.timeSeriesFor(company, "heartRate2")
199-
.append(now.toDate(), 3.5, "tag2");
197+
.append(now, 3.5, "tag2");
200198

201199
const user = new User();
202200
await session.store(user);
203201
session.timeSeriesFor(user, "heartRAte")
204-
.append(now.toDate(), 4.5, "tag3");
202+
.append(now, 4.5, "tag3");
205203

206204
await session.saveChanges();
207205
}
@@ -250,9 +248,9 @@ describe("BasicTimeSeriesIndexes_JavaScript", function () {
250248
const session = store.openSession();
251249
const company = await session.load("companies/1", Company);
252250
session.timeSeriesFor(company, "heartRate")
253-
.append(now.toDate(), 2.5, "tag1");
251+
.append(now, 2.5, "tag1");
254252
session.timeSeriesFor(company, "heartRate2")
255-
.append(now.toDate(), 3.5, "tag2");
253+
.append(now, 3.5, "tag2");
256254

257255
await session.saveChanges();
258256
}

test/Ported/Indexing/TimeSeries/BasicTimeSeriesIndexes_MixedSyntaxTest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe("BasicTimeSeriesIndexes_MixedSyntaxTest", function () {
2727
const company = new Company();
2828
await session.store(company, "companies/1");
2929
session.timeSeriesFor(company, "HeartRate")
30-
.append(now1.toDate(), 7, "tag");
30+
.append(now1, 7, "tag");
3131

3232
await session.saveChanges();
3333
}

test/Ported/Indexing/TimeSeries/BasicTimeSeriesIndexes_StrongSyntaxTest.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import {
22
AbstractCsharpTimeSeriesIndexCreationTask,
3-
AbstractMultiMapTimeSeriesIndexCreationTask,
3+
AbstractMultiMapTimeSeriesIndexCreationTask, DateUtil,
44
IDocumentStore
55
} from "../../../../src/index.js";
66
import { disposeTestDocumentStore, testContext } from "../../../Utils/TestUtil.js";
77
import { Company } from "../../../Assets/Orders.js";
88
import { assertThat } from "../../../Utils/AssertExtensions.js";
9-
import moment from "moment";
109
import { User } from "../../../Assets/Entities.js";
1110

1211
describe("BasicTimeSeriesIndexes_StrongSyntaxTest", function () {
@@ -28,7 +27,7 @@ describe("BasicTimeSeriesIndexes_StrongSyntaxTest", function () {
2827
const company = new Company();
2928
await session.store(company, "companies/1");
3029
session.timeSeriesFor(company, "HeartRate")
31-
.append(now1.toDate(), 7, "tag");
30+
.append(now1, 7, "tag");
3231

3332
await session.saveChanges();
3433
}
@@ -53,8 +52,8 @@ describe("BasicTimeSeriesIndexes_StrongSyntaxTest", function () {
5352

5453
const result = results[0];
5554

56-
assertThat(moment(result.date).toDate().getTime())
57-
.isEqualTo(now1.toDate().getTime());
55+
assertThat(DateUtil.utc.parse(result.date).getTime())
56+
.isEqualTo(now1.getTime());
5857
assertThat(result.user)
5958
.isNotNull();
6059
assertThat(result.heartBeat)
@@ -75,14 +74,14 @@ describe("BasicTimeSeriesIndexes_StrongSyntaxTest", function () {
7574
await session.store(company);
7675

7776
session.timeSeriesFor(company, "HeartRate")
78-
.append(now.toDate(), 2.5, "tag1");
77+
.append(now, 2.5, "tag1");
7978
session.timeSeriesFor(company, "HeartRate2")
80-
.append(now.toDate(), 3.5, "tag");
79+
.append(now, 3.5, "tag");
8180

8281
const user = new User();
8382
await session.store(user);
8483
session.timeSeriesFor(user, "HeartRate")
85-
.append(now.toDate(), 4.5, "tag3");
84+
.append(now, 4.5, "tag3");
8685

8786
await session.saveChanges();
8887
}
@@ -98,8 +97,8 @@ describe("BasicTimeSeriesIndexes_StrongSyntaxTest", function () {
9897

9998
const result = results[0];
10099

101-
assertThat(moment(result.date).toDate().getTime())
102-
.isEqualTo(now.toDate().getTime());
100+
assertThat(DateUtil.utc.parse(result.date).getTime())
101+
.isEqualTo(now.getTime());
103102
assertThat(result.user)
104103
.isNotNull();
105104
assertThat(result.heartBeat)

0 commit comments

Comments
 (0)