Skip to content

Commit

Permalink
Added tests for number type inference.
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelZoerner committed Jun 22, 2018
1 parent ff4668e commit 342d17f
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
24 changes: 20 additions & 4 deletions packages/gatsby/src/schema/__tests__/data-tree-utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,34 +176,50 @@ describe(`Gatsby data tree utils`, () => {
it(`prefers float when multiple number types`, () => {
let example

// nodes starting with integer
// nodes starting with 32-bit integer ("big" ints are float)
example = getExampleValues({ nodes: [{ number: 5 }, { number: 2.5 }] })
expect(example.number).toBeDefined()
expect(example.number).toEqual(2.5)
example = getExampleValues({ nodes: [{ number: 5 }, { number: 3000000000 }] })
expect(example.number).toBeDefined()
expect(example.number).toEqual(3000000000)

// with node not containing number field
example = getExampleValues({ nodes: [{ number: 5 }, {}, { number: 2.5 }] })
expect(example.number).toBeDefined()
expect(example.number).toEqual(2.5)

// nodes starting with float
// nodes starting with float ("big" ints are float)
example = getExampleValues({ nodes: [{ number: 2.5 }, { number: 5 }] })
expect(example.number).toBeDefined()
expect(example.number).toEqual(2.5)
example = getExampleValues({ nodes: [{ number: 3000000000 }, { number: 5 }] })
expect(example.number).toBeDefined()
expect(example.number).toEqual(3000000000)

// array of numbers - starting with integer
// array of numbers - starting with float
example = getExampleValues({ nodes: [{ numbers: [2.5, 5] }] })
expect(example.numbers).toBeDefined()
expect(Array.isArray(example.numbers)).toBe(true)
expect(example.numbers.length).toBe(1)
expect(example.numbers[0]).toBe(2.5)
example = getExampleValues({ nodes: [{ numbers: [3000000000, 5] }] })
expect(example.numbers).toBeDefined()
expect(Array.isArray(example.numbers)).toBe(true)
expect(example.numbers.length).toBe(1)
expect(example.numbers[0]).toBe(3000000000)

// array of numbers - starting with float
// array of numbers - starting with 32-bit integer
example = getExampleValues({ nodes: [{ numbers: [5, 2.5] }] })
expect(example.numbers).toBeDefined()
expect(Array.isArray(example.numbers)).toBe(true)
expect(example.numbers.length).toBe(1)
expect(example.numbers[0]).toBe(2.5)
example = getExampleValues({ nodes: [{ numbers: [5, 3000000000] }] })
expect(example.numbers).toBeDefined()
expect(Array.isArray(example.numbers)).toBe(true)
expect(example.numbers.length).toBe(1)
expect(example.numbers[0]).toBe(3000000000)
})

it(`handles mix of date strings and date objects`, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,21 @@ describe(`GraphQL Input args`, () => {
expect(Object.keys(fields.foo.type.getFields())).toHaveLength(2)
})

it(`infers number types`, () => {
const fields = inferInputObjectStructureFromNodes({
nodes: [
{
int32: 42,
float: 2.5,
longint: 3000000000,
},
],
}).inferredFields
expect(fields.int32.type.name.endsWith(`Integer`)).toBe(true)
expect(fields.float.type.name.endsWith(`Float`)).toBe(true)
expect(fields.longint.type.name.endsWith(`Float`)).toBe(true)
})

it(`handles eq operator`, async () => {
let result = await queryResult(
nodes,
Expand Down
29 changes: 29 additions & 0 deletions packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ describe(`GraphQL type inferance`, () => {
)
})

it(`doesn't throw errors at ints longer than 32-bit`, async () => {
const result = await queryResult(
[
{
longint: 3000000000,
},
],
`
longint
`
)
expect(result.errors).toBeUndefined()
})

it(`prefers float when multiple number types`, async () => {
let result = await queryResult(
[{ number: 1.1 }, { number: 1 }],
Expand Down Expand Up @@ -199,6 +213,21 @@ describe(`GraphQL type inferance`, () => {
expect(Object.keys(fields.foo.type.getFields())).toHaveLength(4)
})

it(`infers number types`, () => {
const fields = inferObjectStructureFromNodes({
nodes: [
{
int32: 42,
float: 2.5,
longint: 3000000000,
},
],
})
expect(fields.int32.type.name).toEqual(`Int`)
expect(fields.float.type.name).toEqual(`Float`)
expect(fields.longint.type.name).toEqual(`Float`)
})

it(`Handle invalid graphql field names`, async () => {
let result = await queryResult(
nodes,
Expand Down
21 changes: 21 additions & 0 deletions packages/gatsby/src/utils/__tests__/is-32-bit-integer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const is32BitInteger = require(`../is-32-bit-integer.js`)

const MAX_INT = 2147483647
const MIN_INT = -2147483648

describe(`is32BitInteger`, () => {
it(`works with all kind of values`, () => {
expect(is32BitInteger(MAX_INT)).toBe(true)
expect(is32BitInteger(MIN_INT)).toBe(true)
expect(is32BitInteger(MAX_INT + 1)).toBe(false)
expect(is32BitInteger(MIN_INT - 1)).toBe(false)
expect(is32BitInteger(2.4)).toBe(false)
expect(is32BitInteger(`42`)).toBe(false)
expect(is32BitInteger({})).toBe(false)
expect(is32BitInteger([1])).toBe(false)
expect(is32BitInteger(true)).toBe(false)
expect(is32BitInteger(false)).toBe(false)
expect(is32BitInteger(undefined)).toBe(false)
expect(is32BitInteger(null)).toBe(false)
})
})

0 comments on commit 342d17f

Please sign in to comment.