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

Seed support #54

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions lib/mockBuilder.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
'use strict'

const schemaParser = require('easygraphql-parser')
const { memoizedField, clearMemoizedFields } = require('../util')
const settings = require('./settings')

function mockBuilder (schemaCode, customMock, seed) {
if (seed) settings['seed'] = seed

const { memoizedField, clearMemoizedFields } = require('../util')

function mockBuilder (schemaCode, customMock) {
// Parse the schema on the GQL file to create a cutom object easier to loop
// and create mocks.
const schema = schemaParser(schemaCode)
Expand Down
2 changes: 2 additions & 0 deletions lib/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const settings = {}
module.exports = settings
7 changes: 7 additions & 0 deletions util/chance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const settings = require('../lib/settings')

const Chance = require('chance')

const chance = settings.seed ? new Chance(settings.seed) : new Chance()

module.exports = chance
13 changes: 7 additions & 6 deletions util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ const randomBooleanData = require('./randomData/boolean')
const { randomNumber } = require('./utils')

let cache = {}
const memoize = (fn) => {
const memoize = fn => {
return (type, customMock, schema) => {
if (type in cache) {
return cache[type]
} else {
// To handle cycles in schema types put a reference to the mocked field in
// the cache before attempting to compute its properties.
const result = {}
if (schema[type].type === 'InterfaceTypeDefinition' && schema[type].implementedTypes.length) {
if (
schema[type].type === 'InterfaceTypeDefinition' &&
schema[type].implementedTypes.length
) {
result['__typename'] = schema[type].implementedTypes[0]
} else {
result['__typename'] = type
Expand Down Expand Up @@ -99,16 +102,14 @@ function createData (field, schemaName, customMock = {}, schema) {
// Validate if is enun value
if (schema[field.type].values.length > 0) {
dataArr.push(selecteEnumVal(field, schema))
// validate if is union
// validate if is union
} else if (schema[field.type].types.length > 0) {
const types = getUnionVals(field, schema)
types.forEach(type =>
dataArr.push(memoizedField(type, customMock, schema))
)
} else {
dataArr.push(
memoizedField(field.type, customMock, schema)
)
dataArr.push(memoizedField(field.type, customMock, schema))
}
}
return dataArr
Expand Down
9 changes: 3 additions & 6 deletions util/randomData/number.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict'

const Chance = require('chance')
const chance = require('../chance')
const constants = require('../constants')
const { randomNumber } = require('../utils')

const chance = new Chance()

function randomNumberData (field, float) {
let data

Expand Down Expand Up @@ -73,11 +71,10 @@ function createDataType (field, float) {

function createRandomNumber (float) {
if (float) {
const precision = 100
return Math.floor(Math.random() * (10 * precision - 1 * precision) + 1 * precision) / (1 * precision)
return chance.floating({ min: 0, max: 10, fixed: 2 })
}

return Math.floor(Math.random() * 11)
return chance.integer({ min: 0, max: 10 })
}

module.exports = randomNumberData
6 changes: 2 additions & 4 deletions util/randomData/string.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict'

const Chance = require('chance')
const chance = require('../chance')
const constants = require('../constants')
const { randomNumber } = require('../utils')

const chance = new Chance()

function randomStringData (field) {
let data

Expand Down Expand Up @@ -102,7 +100,7 @@ function createRandomString (length) {
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
length = length || randomNumber(5, 20)
let result = ''
for (let i = length; i > 0; --i) result += chars[randomNumber(0, chars.length)]
for (let i = length; i > 0; --i) { result += chars[randomNumber(0, chars.length)] }
return result
}

Expand Down
4 changes: 3 additions & 1 deletion util/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const chance = require('./chance')

function randomNumber (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min
return chance.integer({ min, max })
}

module.exports = { randomNumber }