Skip to content

Add property-based testing to DateTime serialisation #999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions packages/bolt-connection/package-lock.json

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

1 change: 1 addition & 0 deletions packages/bolt-connection/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"homepage": "https://github.com/neo4j/neo4j-javascript-driver#readme",
"devDependencies": {
"@types/jest": "^27.1.4",
"fast-check": "^3.1.3",
"jest": "^27.1.4",
"lolex": "^6.0.0",
"ts-jest": "^27.1.4",
Expand Down
82 changes: 81 additions & 1 deletion packages/bolt-connection/test/bolt/bolt-protocol-v4x3.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ import {

import { alloc } from '../../src/channel'
import { structure } from '../../src/packstream'
import fc from 'fast-check'

const WRITE = 'WRITE'

const {
txConfig: { TxConfig },
bookmarks: { Bookmarks },
logger: { Logger }
logger: { Logger },
temporalUtil
} = internal

describe('#unit BoltProtocolV4x3', () => {
Expand Down Expand Up @@ -850,6 +852,84 @@ describe('#unit BoltProtocolV4x3', () => {
expect(unpackedDateTimeWithoutOffset).toEqual(object)
})

it('should pack and unpack DateTimeWithOffset', () => {
fc.assert(
fc.property(
fc.date({
min: temporalUtil.newDate(utils.MIN_UTC_IN_MS + utils.ONE_DAY_IN_MS),
max: temporalUtil.newDate(utils.MAX_UTC_IN_MS - utils.ONE_DAY_IN_MS)
}),
utils.arbitraryTimeZoneId(),
(date, timeZoneId) => {
const object = new DateTime(
date.getUTCFullYear(),
date.getUTCMonth() + 1,
date.getUTCDate(),
date.getUTCHours(),
date.getUTCMinutes(),
date.getUTCSeconds(),
date.getUTCMilliseconds() * 1_000_000,
undefined,
timeZoneId
)

buffer.reset()

const packable = protocol.packable(object)

expect(packable).not.toThrow()

buffer.reset()

const unpacked = protocol.unpack(buffer)

expect(unpacked.timeZoneOffsetSeconds).toBeDefined()

const unpackedDateTimeWithoutOffset = new DateTime(
unpacked.year,
unpacked.month,
unpacked.day,
unpacked.hour,
unpacked.minute,
unpacked.second,
unpacked.nanosecond,
undefined,
unpacked.timeZoneId
)

expect(loggerFunction)
.toBeCalledWith('warn',
'DateTime objects without "timeZoneOffsetSeconds" property ' +
'are prune to bugs related to ambiguous times. For instance, ' +
'2022-10-30T2:30:00[Europe/Berlin] could be GMT+1 or GMT+2.')

expect(unpackedDateTimeWithoutOffset).toEqual(object)
})
)
})

it('should pack and unpack DateTimeWithZoneIdAndNoOffset', () => {
fc.assert(
fc.property(fc.date(), date => {
const object = DateTime.fromStandardDate(date)

buffer.reset()

const packable = protocol.packable(object)

expect(packable).not.toThrow()

buffer.reset()

const unpacked = protocol.unpack(buffer)

expect(unpacked.timeZoneOffsetSeconds).toBeDefined()

expect(unpacked).toEqual(object)
})
)
})

it.each([
[
'DateTimeWithZoneOffset with less fields',
Expand Down
82 changes: 81 additions & 1 deletion packages/bolt-connection/test/bolt/bolt-protocol-v4x4.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ import {

import { alloc } from '../../src/channel'
import { structure } from '../../src/packstream'
import fc from 'fast-check'

const WRITE = 'WRITE'

const {
txConfig: { TxConfig },
bookmarks: { Bookmarks },
logger: { Logger }
logger: { Logger },
temporalUtil
} = internal

describe('#unit BoltProtocolV4x4', () => {
Expand Down Expand Up @@ -883,6 +885,84 @@ describe('#unit BoltProtocolV4x4', () => {
expect(unpackedDateTimeWithoutOffset).toEqual(object)
})

it('should pack and unpack DateTimeWithOffset', () => {
fc.assert(
fc.property(
fc.date({
min: temporalUtil.newDate(utils.MIN_UTC_IN_MS + utils.ONE_DAY_IN_MS),
max: temporalUtil.newDate(utils.MAX_UTC_IN_MS - utils.ONE_DAY_IN_MS)
}),
utils.arbitraryTimeZoneId(),
(date, timeZoneId) => {
const object = new DateTime(
date.getUTCFullYear(),
date.getUTCMonth() + 1,
date.getUTCDate(),
date.getUTCHours(),
date.getUTCMinutes(),
date.getUTCSeconds(),
date.getUTCMilliseconds() * 1_000_000,
undefined,
timeZoneId
)

buffer.reset()

const packable = protocol.packable(object)

expect(packable).not.toThrow()

buffer.reset()

const unpacked = protocol.unpack(buffer)

expect(unpacked.timeZoneOffsetSeconds).toBeDefined()

const unpackedDateTimeWithoutOffset = new DateTime(
unpacked.year,
unpacked.month,
unpacked.day,
unpacked.hour,
unpacked.minute,
unpacked.second,
unpacked.nanosecond,
undefined,
unpacked.timeZoneId
)

expect(loggerFunction)
.toBeCalledWith('warn',
'DateTime objects without "timeZoneOffsetSeconds" property ' +
'are prune to bugs related to ambiguous times. For instance, ' +
'2022-10-30T2:30:00[Europe/Berlin] could be GMT+1 or GMT+2.')

expect(unpackedDateTimeWithoutOffset).toEqual(object)
})
)
})

it('should pack and unpack DateTimeWithZoneIdAndNoOffset', () => {
fc.assert(
fc.property(fc.date(), date => {
const object = DateTime.fromStandardDate(date)

buffer.reset()

const packable = protocol.packable(object)

expect(packable).not.toThrow()

buffer.reset()

const unpacked = protocol.unpack(buffer)

expect(unpacked.timeZoneOffsetSeconds).toBeDefined()

expect(unpacked).toEqual(object)
})
)
})

it.each([
[
'DateTimeWithZoneOffset with less fields',
Expand Down
Loading