This repository has been archived by the owner on Jul 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include mockData module to generate random data samples based on JSON…
… Schemas #40
- Loading branch information
1 parent
77d8510
commit 6e9f34b
Showing
6 changed files
with
151 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,4 @@ | |
`$ yarn test` | ||
|
||
### Modo watch | ||
`$ yarn test --watch` | ||
`$ yarn test -- --watch` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import jsf from 'json-schema-faker' | ||
|
||
/** | ||
* Get random samples | ||
* @param {object} schema schema of the desired model to get samples | ||
* @param {number} amount amount of samples to return | ||
* @return {array} retrun list of samples | ||
*/ | ||
const mockData = (schema = {}, amount) => { | ||
const validAmount = (typeof amount == 'number' && !isNaN(amount)) ? parseInt(amount, 10) : 1 | ||
const samples = new Array(validAmount) | ||
for (var i = samples.length - 1; i >= 0; i--) { | ||
samples[i] = jsf(schema) | ||
} | ||
return samples | ||
} | ||
|
||
export default mockData |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import mockData from './mockData.js' | ||
|
||
// Sample of a JSON Schema | ||
const schema = { | ||
"properties": { | ||
"UID": { | ||
"properties": { | ||
"conhecimentos": { | ||
"items": { | ||
"type": "string" | ||
}, | ||
"type": "array" | ||
}, | ||
"contatos": { | ||
"properties": { | ||
"FaceBook": { | ||
"type": "string" | ||
}, | ||
"GitHub": { | ||
"type": "string" | ||
}, | ||
"LinkedIn": { | ||
"type": "string" | ||
}, | ||
"Site": { | ||
"type": "string" | ||
}, | ||
"Twitter": { | ||
"type": "string" | ||
}, | ||
"Youtube": { | ||
"type": "string" | ||
} | ||
}, | ||
"type": "object" | ||
}, | ||
"email": { | ||
"type": "string" | ||
}, | ||
"nome": { | ||
"type": "string" | ||
}, | ||
"termos": { | ||
"type": "boolean" | ||
} | ||
}, | ||
"type": "object" | ||
} | ||
}, | ||
"type": "object" | ||
} | ||
|
||
describe('mockData() with random samples', () => { | ||
|
||
it('mockData(schema) return 1 sample', () => { | ||
const sampleData = mockData(schema) | ||
expect(sampleData.length).toBe(1) | ||
}) | ||
|
||
it('mockData(schema, 1) return 1 sample', () => { | ||
const sampleData = mockData(schema, 1) | ||
expect(sampleData.length).toBe(1) | ||
}) | ||
|
||
it('mockData(schema, 5) return 5 samples', () => { | ||
const sampleData = mockData(schema, 5) | ||
expect(sampleData.length).toBe(5) | ||
}) | ||
|
||
it('mockData(schema, 0) return 0 samples', () => { | ||
const sampleData = mockData(schema, 0) | ||
expect(sampleData.length).toBe(0) | ||
}) | ||
|
||
it('mockData(schema, undefined) return 1 sample', () => { | ||
const sampleData = mockData(undefined) | ||
expect(sampleData.length).toBe(1) | ||
}) | ||
|
||
it('mockData(schema, null) return 1 sample', () => { | ||
const sampleData = mockData(schema, null) | ||
expect(sampleData.length).toBe(1) | ||
}) | ||
|
||
it('mockData(schema, "asdasd") return 1 sample', () => { | ||
const sampleData = mockData(schema, 'asdasd') | ||
expect(sampleData.length).toBe(1) | ||
}) | ||
|
||
it('mockData(schema, NaN) return 1 sample', () => { | ||
const sampleData = mockData(schema, NaN) | ||
expect(sampleData.length).toBe(1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1855,6 +1855,10 @@ chalk@^2.0.0, chalk@^2.0.1, chalk@^2.1.0: | |
escape-string-regexp "^1.0.5" | ||
supports-color "^4.0.0" | ||
|
||
chance@^1.0.11: | ||
version "1.0.11" | ||
resolved "https://registry.yarnpkg.com/chance/-/chance-1.0.11.tgz#48e82f7583df356053e0ad122d4654c5066c570d" | ||
|
||
cheerio@^1.0.0-rc.2: | ||
version "1.0.0-rc.2" | ||
resolved "https://registry.yarnpkg.com/cheerio/-/cheerio-1.0.0-rc.2.tgz#4b9f53a81b27e4d5dac31c0ffd0cfa03cc6830db" | ||
|
@@ -2381,7 +2385,7 @@ deep-equal@^1.0.1: | |
version "1.0.1" | ||
resolved "https://registry.yarnpkg.com/deep-equal/-/deep-equal-1.0.1.tgz#f5d260292b660e084eff4cdbc9f08ad3247448b5" | ||
|
||
deep-extend@~0.4.0: | ||
deep-extend@^0.4.0, deep-extend@~0.4.0: | ||
version "0.4.2" | ||
resolved "https://registry.yarnpkg.com/deep-extend/-/deep-extend-0.4.2.tgz#48b699c27e334bf89f10892be432f6e4c7d34a7f" | ||
|
||
|
@@ -2441,6 +2445,12 @@ [email protected], depd@~1.1.1: | |
version "1.1.1" | ||
resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.1.tgz#5783b4e1c459f06fa5ca27f991f3d06e7a310359" | ||
|
||
deref@^0.7.0: | ||
version "0.7.0" | ||
resolved "https://registry.yarnpkg.com/deref/-/deref-0.7.0.tgz#f862485b971f51e9aad590d149b9eb46dcc074c1" | ||
dependencies: | ||
deep-extend "^0.4.0" | ||
|
||
des.js@^1.0.0: | ||
version "1.0.0" | ||
resolved "https://registry.yarnpkg.com/des.js/-/des.js-1.0.0.tgz#c074d2e2aa6a8a9a07dbd61f9a15c2cd83ec8ecc" | ||
|
@@ -3159,6 +3169,10 @@ [email protected], extsprintf@^1.2.0: | |
version "1.3.0" | ||
resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.3.0.tgz#96918440e3041a7a414f8c52e3c574eb3c3e1e05" | ||
|
||
faker@^4.1.0: | ||
version "4.1.0" | ||
resolved "https://registry.yarnpkg.com/faker/-/faker-4.1.0.tgz#1e45bbbecc6774b3c195fad2835109c6d748cc3f" | ||
|
||
fast-deep-equal@^1.0.0: | ||
version "1.0.0" | ||
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-1.0.0.tgz#96256a3bc975595eb36d82e9929d060d893439ff" | ||
|
@@ -4590,6 +4604,16 @@ json-loader@^0.5.4, json-loader@^0.5.7: | |
version "0.5.7" | ||
resolved "https://registry.yarnpkg.com/json-loader/-/json-loader-0.5.7.tgz#dca14a70235ff82f0ac9a3abeb60d337a365185d" | ||
|
||
json-schema-faker@^0.4.6: | ||
version "0.4.6" | ||
resolved "https://registry.yarnpkg.com/json-schema-faker/-/json-schema-faker-0.4.6.tgz#34ba1b3aa32690f390db5939376c10544a73e8bb" | ||
dependencies: | ||
chance "^1.0.11" | ||
deref "^0.7.0" | ||
faker "^4.1.0" | ||
randexp "^0.4.6" | ||
tslib "^1.8.0" | ||
|
||
json-schema-traverse@^0.3.0: | ||
version "0.3.1" | ||
resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-0.3.1.tgz#349a6d44c53a51de89b40805c5d5e59b417d3340" | ||
|
@@ -6060,7 +6084,7 @@ radium@^0.19.0: | |
inline-style-prefixer "^2.0.5" | ||
prop-types "^15.5.8" | ||
|
||
raf@^3.3.2: | ||
raf@^3.3.2, raf@^3.4.0: | ||
version "3.4.0" | ||
resolved "https://registry.yarnpkg.com/raf/-/raf-3.4.0.tgz#a28876881b4bc2ca9117d4138163ddb80f781575" | ||
dependencies: | ||
|
@@ -6070,7 +6094,7 @@ railroad-diagrams@^1.0.0: | |
version "1.0.0" | ||
resolved "https://registry.yarnpkg.com/railroad-diagrams/-/railroad-diagrams-1.0.0.tgz#eb7e6267548ddedfb899c1b90e57374559cddb7e" | ||
|
||
randexp@^0.4.2: | ||
randexp@^0.4.2, randexp@^0.4.6: | ||
version "0.4.6" | ||
resolved "https://registry.yarnpkg.com/randexp/-/randexp-0.4.6.tgz#e986ad5e5e31dae13ddd6f7b3019aa7c87f60ca3" | ||
dependencies: | ||
|
@@ -7310,6 +7334,10 @@ tryit@^1.0.1: | |
version "1.0.3" | ||
resolved "https://registry.yarnpkg.com/tryit/-/tryit-1.0.3.tgz#393be730a9446fd1ead6da59a014308f36c289cb" | ||
|
||
tslib@^1.8.0: | ||
version "1.8.0" | ||
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.8.0.tgz#dc604ebad64bcbf696d613da6c954aa0e7ea1eb6" | ||
|
||
[email protected]: | ||
version "0.0.0" | ||
resolved "https://registry.yarnpkg.com/tty-browserify/-/tty-browserify-0.0.0.tgz#a157ba402da24e9bf957f9aa69d524eed42901a6" | ||
|