json_schema_spec 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e2e4dab0d8a9b42b0207788642574435b6638aff
4
+ data.tar.gz: 8b883259c69b57cfc5ebcb58362d8224813b15ab
5
+ SHA512:
6
+ metadata.gz: b3eeb7ae1f605095dc0a2606e0530becf9abc70e42f76ba13523511ad11f29af490907efc342f4c1154861581119ca2d419c469f9d171ebf33db5226818af88c
7
+ data.tar.gz: dd9cab9f394957edd620b5f5ee9f70a4fca5dff6449b6d0e14ece7c989e14c70730f145f389c506eff106f5aaf6f4f84be4c31c0aecb7aaa28c8c44036c8c713
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .DS_Store
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ json_schema_spec
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.0-preview1
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,122 @@
1
+ ##JsonSchemaSpec
2
+
3
+ Generate fixtures from JSON schemas
4
+
5
+ ###Goals
6
+
7
+ * Generate webmock requests and responses from JSON schema
8
+ * Easily alter parameters for testing
9
+ * Automatically validate altered parameters against schema
10
+ * Rake helpers for client code to download JSON schema
11
+
12
+ ###Requirements
13
+
14
+ You serve a `schema.json` file at the root of a URL that describes your resources.
15
+
16
+ ####Example schema.json
17
+
18
+ {
19
+ "user.json": {
20
+ "get": {
21
+ "request": {
22
+ "title": "GET user.json (request)",
23
+ "type": "object",
24
+ "additionalProperties": false,
25
+ "properties": {
26
+ "id": { "type": "integer" },
27
+ "token": { "type": "string" }
28
+ }
29
+ },
30
+ "response": {
31
+ "title": "GET user.json (response)",
32
+ "type": "object",
33
+ "additionalProperties": false,
34
+ "properties": {
35
+ "id": { "type": "integer" },
36
+ "avatar": { "type": "string", "optional": true },
37
+ "name": { "type": "string" }
38
+ }
39
+ }
40
+ }
41
+ }
42
+ }
43
+
44
+ ###Install
45
+
46
+ gem install json_schema_spec
47
+
48
+ ###Require
49
+
50
+ In your `spec_helper`:
51
+
52
+ require "json_schema_spec"
53
+
54
+ ###Client side project setup
55
+
56
+ In your `Rakefile`:
57
+
58
+ require "json_schema_spec"
59
+ JsonSchemaSpec::Tasks.new("http://127.0.0.1:3000/schema.json")
60
+
61
+ ####Download schema
62
+
63
+ Download `schema.json` from the URL specified in your `Rakefile`:
64
+
65
+ rake spec:schema
66
+
67
+ The schema lives at `schema/fixtures/schema.yml`.
68
+
69
+ ###Server side project setup
70
+
71
+ In Rails, your schema is automatically detected at `/schema.json`.
72
+
73
+ ###Generate test parameters
74
+
75
+ request, response = json_schema_params(:user, :get)
76
+
77
+ The `request` hash looks like this:
78
+
79
+ { :id => 72238, :token => "token" }
80
+
81
+ The `response` hash looks like this:
82
+
83
+ { :avatar => "avatar", :name => "name" }
84
+
85
+ **Integer** values are given a random number.
86
+
87
+ **String** values are given the same name as the key.
88
+
89
+ **Optional** parameters are not included.
90
+
91
+ ###Modify test parameters
92
+
93
+ request, response = json_schema_params(:user, :get, :request => { :id => 1 })
94
+
95
+ The `request` hash now looks like this:
96
+
97
+ { :id => 1, :token => "token" }
98
+
99
+ Modified parameters are validated against your schema using [json-schema](https://github.com/hoxworth/json-schema).
100
+
101
+ ###Webmock example
102
+
103
+ request, response = json_schema_params(:user, :get)
104
+
105
+ request = params[:request]
106
+ response = params[:response]
107
+
108
+ stub_request(:get, "http://127.0.0.1:3000/user.json").
109
+ with(:body => request).
110
+ to_return(:body => response.to_json)
111
+
112
+ ### Contribute
113
+
114
+ [Create an issue](https://github.com/winton/json_schema_spec/issues/new) to discuss template changes.
115
+
116
+ Pull requests for template changes and new branches are even better.
117
+
118
+ ### Stay up to date
119
+
120
+ [Star this project](https://github.com/winton/json_schema_spec#) on Github.
121
+
122
+ [Follow Winton Welsh](http://twitter.com/intent/user?screen_name=wintonius) on Twitter.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ require File.dirname(__FILE__) + "/lib/json_schema_spec"
5
+ JsonSchemaSpec::Tasks.new("http://127.0.0.1:3000/schema.json")
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+ task :default => :spec
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env ruby
2
+ require File.expand_path("../../lib/json_schema_spec", __FILE__)
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "json_schema_spec"
7
+ spec.version = "0.0.1"
8
+ spec.authors = ["Winton Welsh"]
9
+ spec.email = ["mail@wintoni.us"]
10
+ spec.description = %q{Generate fixtures from JSON schemas.}
11
+ spec.summary = %q{Generate fixtures from JSON schemas}
12
+ spec.homepage = "https://github.com/winton/json_schema_spec"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "json-schema"
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "simplecov"
26
+ end
@@ -0,0 +1,23 @@
1
+ module JsonSchemaSpec
2
+ class Tasks
3
+
4
+ include Rake::DSL if defined?(Rake::DSL)
5
+
6
+ def initialize(url)
7
+ return unless defined?(Rake)
8
+
9
+ namespace :spec do
10
+
11
+ desc "Download JSON schema to `spec/fixtures`"
12
+ task :schema do
13
+ puts "\nDownloading JSON schema to `spec/fixtures`..."
14
+
15
+ path = "#{Rake.original_dir}/spec/fixtures/schema.yml"
16
+ JsonSchemaSpec.download(path, url)
17
+
18
+ puts "Finished!\n\n"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,31 @@
1
+ module JsonSchemaSpec
2
+ module Util
3
+ class <<self
4
+ def deep_dup(hash)
5
+ duplicate = hash.dup
6
+ duplicate.each_pair do |k,v|
7
+ tv = duplicate[k]
8
+ duplicate[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? deep_dup(tv) : v
9
+ end
10
+ duplicate
11
+ end
12
+
13
+ def deep_merge(hash, other_hash)
14
+ other_hash.each_pair do |k,v|
15
+ tv = hash[k]
16
+ hash[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? deep_merge(tv, v) : v
17
+ end
18
+ hash
19
+ end
20
+
21
+ def symbolize_keys(hash)
22
+ hash.inject({}) do |memo, (key, value)|
23
+ key = (key.to_sym rescue key) || key
24
+ value = symbolize_keys(value) if value.is_a?(Hash)
25
+ memo[key] = value
26
+ memo
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,97 @@
1
+ require "json"
2
+ require "json-schema"
3
+ require "open-uri"
4
+ require "yaml"
5
+
6
+ $:.unshift File.dirname(__FILE__)
7
+
8
+ require "json_schema_spec/tasks"
9
+ require "json_schema_spec/util"
10
+
11
+ module JsonSchemaSpec
12
+ class <<self
13
+
14
+ def download(path, url)
15
+ FileUtils.mkdir_p(File.dirname(path))
16
+
17
+ File.open(path, 'w') do |f|
18
+ json = open(url).string
19
+ yaml = JSON.parse(json).to_yaml
20
+ f.write(yaml)
21
+ end
22
+ end
23
+ end
24
+
25
+ def json_schema(resource, method)
26
+ schema = pick_json_schema
27
+ schema = Util.symbolize_keys(schema)
28
+ schema[:"#{resource}.json"][method]
29
+ end
30
+
31
+ def json_schema_params(resource, method, merge={})
32
+ schema = json_schema(resource, method)
33
+ params = json_schema_to_params(schema)
34
+ params = Util.deep_merge(params, merge)
35
+
36
+ unless merge.empty?
37
+ validate_json_schema(resource, method, params)
38
+ end
39
+
40
+ params = Util.deep_dup(params)
41
+ [ params[:request], params[:response] ]
42
+ end
43
+
44
+ def json_schema_to_params(schema, prefix=[])
45
+ return schema unless schema.is_a?(Hash)
46
+
47
+ schema.inject({}) do |memo, (key, value)|
48
+ memo[key] = json_schema_value(key, value, prefix.dup)
49
+ memo.delete(key) unless memo[key]
50
+ memo
51
+ end
52
+ end
53
+
54
+ def json_schema_value(key, value, prefix)
55
+ if !value.is_a?(Hash) || value[:optional]
56
+ nil
57
+ elsif value[:type] == 'string'
58
+ json_schema_value_prefix(prefix) + key.to_s
59
+ elsif value[:type] == 'integer'
60
+ Random.rand(1_000_000)
61
+ elsif value[:type] == 'object'
62
+ json_schema_to_params(value[:properties], prefix << key)
63
+ else
64
+ json_schema_to_params(value)
65
+ end
66
+ end
67
+
68
+ def json_schema_value_prefix(prefix)
69
+ prefix = prefix.join(':')
70
+ prefix = "#{prefix}:" unless prefix.empty?
71
+ prefix.gsub(/^[^:]*:*/, '')
72
+ end
73
+
74
+ def pick_json_schema
75
+ if defined?(Rails) && defined?(get)
76
+ JSON.parse(get("/schema.json"))
77
+ else
78
+ path = "#{File.expand_path(".")}/spec/fixtures/schema.yml"
79
+ YAML.load(File.read(path))
80
+ end
81
+ end
82
+
83
+ def validate_json_schema(resource, method, params)
84
+ return if RUBY_VERSION =~ /^1\.8\./
85
+
86
+ schema = json_schema(resource, method)
87
+
88
+ [ :request, :response ].each do |direction|
89
+ validates = JSON::Validator.fully_validate(
90
+ schema[direction],
91
+ params[direction],
92
+ :validate_schema => true
93
+ )
94
+ expect(validates).to eq([])
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,30 @@
1
+ ---
2
+ user.json:
3
+ get:
4
+ request:
5
+ title: GET user.json (request)
6
+ type: object
7
+ additionalProperties: false
8
+ properties:
9
+ id:
10
+ type: integer
11
+ token:
12
+ type: string
13
+ response:
14
+ title: GET user.json (response)
15
+ type: object
16
+ additionalProperties: false
17
+ properties:
18
+ id:
19
+ type: integer
20
+ avatar:
21
+ type: string
22
+ optional: true
23
+ name:
24
+ type: string
25
+ company:
26
+ type: object
27
+ additionalProperties: false
28
+ properties:
29
+ name:
30
+ type: string
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe JsonSchemaSpec do
4
+ describe "#json_schema_params" do
5
+
6
+ it "generates proper params" do
7
+ request, response = json_schema_params(:user, :get)
8
+
9
+ expect(request).to eq(
10
+ :id => request[:id],
11
+ :token => "token"
12
+ )
13
+
14
+ expect(response).to eq(
15
+ :id => response[:id],
16
+ :name => "name",
17
+ :company => {
18
+ :name => "company:name"
19
+ }
20
+ )
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ unless ENV['CI']
2
+ require 'simplecov'
3
+ SimpleCov.start
4
+ end
5
+
6
+ $root = File.expand_path('../../', __FILE__)
7
+ require "#{$root}/lib/json_schema_spec"
8
+
9
+ RSpec.configure do |c|
10
+ c.include JsonSchemaSpec
11
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_schema_spec
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Winton Welsh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json-schema
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Generate fixtures from JSON schemas.
84
+ email:
85
+ - mail@wintoni.us
86
+ executables:
87
+ - json_schema_spec
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".ruby-gemset"
93
+ - ".ruby-version"
94
+ - ".travis.yml"
95
+ - Gemfile
96
+ - LICENSE
97
+ - README.md
98
+ - Rakefile
99
+ - bin/json_schema_spec
100
+ - json_schema_spec.gemspec
101
+ - lib/json_schema_spec.rb
102
+ - lib/json_schema_spec/tasks.rb
103
+ - lib/json_schema_spec/util.rb
104
+ - spec/fixtures/schema.yml
105
+ - spec/json_schema_spec_spec.rb
106
+ - spec/spec_helper.rb
107
+ homepage: https://github.com/winton/json_schema_spec
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.1.5
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Generate fixtures from JSON schemas
131
+ test_files:
132
+ - spec/fixtures/schema.yml
133
+ - spec/json_schema_spec_spec.rb
134
+ - spec/spec_helper.rb