Skip to content
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

fix: preserve date values set to null #160

Merged
merged 3 commits into from
Dec 6, 2023
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
21 changes: 18 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ const createMeasurement = async (req, res, client, getCurrentRound) => {
measurement.protocol,
measurement.participantAddress,
measurement.timeout || false,
new Date(measurement.startAt),
parseOptionalDate(measurement.startAt),
measurement.statusCode,
new Date(measurement.firstByteAt),
new Date(measurement.endAt),
parseOptionalDate(measurement.firstByteAt),
parseOptionalDate(measurement.endAt),
measurement.byteLength,
measurement.attestation,
inetGroup,
Expand Down Expand Up @@ -285,3 +285,18 @@ export const createHandler = async ({
})
}
}

/**
* Parse a date string field that may be `undefined` or `null`.
*
* - undefined -> undefined
* - null -> undefined
* - "iso-date-string" -> new Date("iso-date-string")
*
* @param {string | null | undefined} str
* @returns {Date | undefined}
*/
const parseOptionalDate = (str) => {
if (str === undefined || str === null) return undefined
return new Date(str)
}
3 changes: 2 additions & 1 deletion spark-publish/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('integration', () => {
startAt: new Date('2023-09-18T13:33:51.239Z'),
statusCode: 200,
firstByteAt: new Date('2023-09-18T13:33:51.239Z'),
endAt: new Date('2023-09-18T13:33:51.239Z'),
endAt: null,
byteLength: 100,
attestation: 'json.sig',
inetGroup: 'MTIzNDU2Nzg',
Expand Down Expand Up @@ -208,6 +208,7 @@ describe('integration', () => {
assert.strictEqual(published.cid, measurementRecorded.cid)
assert.strictEqual(published.inet_group, measurementRecorded.inetGroup)
assert.strictEqual(published.car_too_large, measurementRecorded.carTooLarge)
assert.strictEqual(published.end_at, null)
// TODO: test other fields

// We are publishing records with invalid wallet addresses too
Expand Down
37 changes: 37 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,43 @@ describe('Routes', () => {
])
assert.strictEqual(measurementRow.participant_address, participantAddress)
})

it('handles date fields set to null', async () => {
await client.query('DELETE FROM measurements')

const measurement = {
cid: 'bafytest',
providerAddress: '/dns4/localhost/tcp/8080',
protocol: 'graphsync',
sparkVersion: '1.2.3',
zinniaVersion: '2.3.4',
participantAddress,
startAt: new Date(),
statusCode: undefined,
firstByteAt: null,
endAt: null
}

const createRequest = await fetch(`${spark}/measurements`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(measurement)
})
await assertResponseStatus(createRequest, 200)
const { id } = await createRequest.json()

const { rows: [measurementRow] } = await client.query(`
SELECT *
FROM measurements
WHERE id = $1
`, [
id
])

assert.strictEqual(measurementRow.status_code, null)
bajtos marked this conversation as resolved.
Show resolved Hide resolved
assert.strictEqual(measurementRow.first_byte_at, null)
assert.strictEqual(measurementRow.end_at, null)
})
})

describe('GET /measurements/:id', () => {
Expand Down