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 all 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
93 changes: 86 additions & 7 deletions 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 @@ -822,6 +824,11 @@ describe('#unit BoltProtocolV4x3', () => {
const packable = protocol.packable(object)

expect(packable).not.toThrow()
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.')

buffer.reset()

Expand All @@ -841,15 +848,87 @@ describe('#unit BoltProtocolV4x3', () => {
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 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)
}),
fc.integer({ min: 0, max: 999_999 }),
utils.arbitraryTimeZoneId(),
(date, nanoseconds, timeZoneId) => {
const object = new DateTime(
date.getUTCFullYear(),
date.getUTCMonth() + 1,
date.getUTCDate(),
date.getUTCHours(),
date.getUTCMinutes(),
date.getUTCSeconds(),
date.getUTCMilliseconds() * 1_000_000 + nanoseconds,
undefined,
timeZoneId
)

buffer.reset()

const packable = protocol.packable(object)

expect(packable).not.toThrow()
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.')

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(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
93 changes: 86 additions & 7 deletions 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 @@ -855,6 +857,11 @@ describe('#unit BoltProtocolV4x4', () => {
const packable = protocol.packable(object)

expect(packable).not.toThrow()
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.')

buffer.reset()

Expand All @@ -874,15 +881,87 @@ describe('#unit BoltProtocolV4x4', () => {
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 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)
}),
fc.integer({ min: 0, max: 999_999 }),
utils.arbitraryTimeZoneId(),
(date, nanoseconds, timeZoneId) => {
const object = new DateTime(
date.getUTCFullYear(),
date.getUTCMonth() + 1,
date.getUTCDate(),
date.getUTCHours(),
date.getUTCMinutes(),
date.getUTCSeconds(),
date.getUTCMilliseconds() * 1_000_000 + nanoseconds,
undefined,
timeZoneId
)

buffer.reset()

const packable = protocol.packable(object)

expect(packable).not.toThrow()
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.')

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(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