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

Handle Infinity and -Infinity results correctly #950

Merged
merged 1 commit into from
Aug 8, 2019
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
4 changes: 2 additions & 2 deletions src/browser/modules/Stream/CypherFrame/AsciiView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
transformResultRecordsToResultArray,
stringifyResultArray
} from './helpers'
import { stringFormat } from 'services/bolt/cypherTypesFormatting'
import { stringModifier } from 'services/bolt/cypherTypesFormatting'

export class AsciiView extends Component {
state = {
Expand Down Expand Up @@ -77,7 +77,7 @@ export class AsciiView extends Component {
const records = getRecordsToDisplayInTable(props.result, props.maxRows)
const serializedRows =
stringifyResultArray(
stringFormat,
stringModifier,
transformResultRecordsToResultArray(records)
) || []
this.setState({ serializedRows })
Expand Down
6 changes: 3 additions & 3 deletions src/browser/modules/Stream/CypherFrame/TableView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
getRecordsToDisplayInTable,
transformResultRecordsToResultArray
} from './helpers'
import { stringFormat } from 'services/bolt/cypherTypesFormatting'
import { stringModifier } from 'services/bolt/cypherTypesFormatting'

const renderCell = entry => {
if (Array.isArray(entry)) {
Expand All @@ -54,14 +54,14 @@ const renderCell = entry => {
} else if (typeof entry === 'object') {
return renderObject(entry)
} else {
return stringifyMod(entry, stringFormat, true)
return stringifyMod(entry, stringModifier, true)
}
}
export const renderObject = entry => {
if (neo4j.isInt(entry)) return entry.toString()
if (entry === null) return <em>null</em>
return (
<StyledJsonPre>{stringifyMod(entry, stringFormat, true)}</StyledJsonPre>
<StyledJsonPre>{stringifyMod(entry, stringModifier, true)}</StyledJsonPre>
)
}
const buildData = entries => {
Expand Down
4 changes: 2 additions & 2 deletions src/browser/modules/Stream/CypherFrame/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
flattenArray
} from 'services/bolt/boltMappings'
import { stringifyMod } from 'services/utils'
import { stringFormat } from 'services/bolt/cypherTypesFormatting'
import { stringModifier } from 'services/bolt/cypherTypesFormatting'

export function getBodyAndStatusBarMessages (result, maxRows) {
if (!result || !result.summary || !result.summary.resultAvailableAfter) {
Expand Down Expand Up @@ -171,7 +171,7 @@ export const initialView = (props, state = {}) => {
* It takes a replacer without enforcing quoting rules to it.
* Used so we can have Neo4j integers as string without quotes.
*/
export const stringifyResultArray = (formatter = stringFormat, arr = []) => {
export const stringifyResultArray = (formatter = stringModifier, arr = []) => {
return arr.map(col => {
if (!col) return col
return col.map(fVal => {
Expand Down
6 changes: 3 additions & 3 deletions src/browser/modules/Stream/CypherFrame/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
flattenGraphItemsInResultArray,
stringifyResultArray
} from './helpers'
import { stringFormat, csvFormat } from 'services/bolt/cypherTypesFormatting'
import { stringModifier, csvFormat } from 'services/bolt/cypherTypesFormatting'

describe('helpers', () => {
test('getRecordsToDisplayInTable should report if there are rows or not in the result', () => {
Expand Down Expand Up @@ -639,7 +639,7 @@ describe('helpers', () => {
neo4j.isInt,
step1
)
const res = stringifyResultArray(stringFormat, step2)
const res = stringifyResultArray(stringModifier, step2)
// Then
expect(res).toEqual([
['""neoInt""', '""int""', '""any""', '""backslash""'],
Expand Down Expand Up @@ -713,7 +713,7 @@ describe('helpers', () => {
neo4j.isInt,
step1
)
const res = stringifyResultArray(stringFormat, step2)
const res = stringifyResultArray(stringModifier, step2)
// Then
expect(res).toEqual([
['""x""', '""y""', '""n""'],
Expand Down
4 changes: 2 additions & 2 deletions src/browser/modules/Stream/ParamsFrame.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import React from 'react'
import Render from 'browser-components/Render'
import { ExclamationTriangleIcon } from 'browser-components/icons/Icons'
import Ellipsis from 'browser-components/Ellipsis'
import { stringFormat } from 'services/bolt/cypherTypesFormatting'
import { stringModifier } from 'services/bolt/cypherTypesFormatting'
import { stringifyMod } from 'services/utils'
import FrameTemplate from './FrameTemplate'
import { PaddedDiv, ErrorText, SuccessText, StyledStatsBar } from './styled'
Expand All @@ -34,7 +34,7 @@ const ParamsFrame = ({ frame }) => {
<PaddedDiv>
<Render if={frame.success !== false}>
<pre data-testid='rawParamData'>
{stringifyMod(params, stringFormat, true)}
{stringifyMod(params, stringModifier, true)}
</pre>
</Render>
<div style={{ marginTop: '20px' }}>
Expand Down
4 changes: 2 additions & 2 deletions src/shared/services/bolt/boltMappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import updateStatsFields from './updateStatisticsFields'
import { v1 as neo4j } from 'neo4j-driver'
import { stringFormat } from 'services/bolt/cypherTypesFormatting'
import { stringModifier } from 'services/bolt/cypherTypesFormatting'
import {
safetlyRemoveObjectProp,
safetlyAddObjectProp,
Expand All @@ -46,7 +46,7 @@ export function recordsToTableArray (records, converters) {
}

export function itemIntToString (item, converters) {
const res = stringFormat(item)
const res = stringModifier(item)
if (res) return res
if (converters.intChecker(item)) return converters.intConverter(item)
if (Array.isArray(item)) return arrayIntToString(item, converters)
Expand Down
6 changes: 5 additions & 1 deletion src/shared/services/bolt/cypherTypesFormatting.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const csvFormat = anything => {
return undefined
}

export const stringFormat = anything => {
export const stringModifier = anything => {
if (typeof anything === 'number') {
return numberFormat(anything)
}
Expand All @@ -36,6 +36,10 @@ export const stringFormat = anything => {
}

const numberFormat = anything => {
// Exclude false positives and return early
if ([Infinity, -Infinity].includes(anything)) {
return `${anything}`
}
if (Math.floor(anything) === anything) {
return `${anything}.0`
}
Expand Down
35 changes: 35 additions & 0 deletions src/shared/services/bolt/cypherTypesFormatting.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2002-2019 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { stringModifier } from 'services/bolt/cypherTypesFormatting'

describe('Cypher Types Number modifier only modifies where needed', () => {
const tests = [
[Number(123), '123.0'],
[Number(123.1), undefined],
[Number(-123.1), undefined],
[Number(Infinity), 'Infinity'],
[Number(-Infinity), '-Infinity']
]

test.each(tests)('Modifies %s correctly if needed', (input, output) => {
expect(stringModifier(input)).toEqual(output)
})
})