jsonapi_spec_helpers 0.2.0

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: 87e14751c4b6dcc5f51ea355dff331af2808a441
4
+ data.tar.gz: bd553e0fbdb64709c17912793c5c6d79d06ae871
5
+ SHA512:
6
+ metadata.gz: 346d1d9f491b556fb25014d907e58431092758abc348d13cd134265abbc6962a039ed4ef14fce37f65c097733dfa641bc78c6453095daf6c02e87be027618908
7
+ data.tar.gz: 3f69503875a7f47dd496c6f3f4e53a463760752b24b669244d378e44245f483a77a6fb94cc8a81140f1e64baf08f0a55641f75539f6a2927693c3ce2a8295364
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jsonapi_spec_helpers.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Lee Richmond
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # JsonapiSpecHelpers
2
+
3
+ A collection of spec helpers for asserting on jsonapi.org responses.
4
+
5
+ [View official documentation](https://bbgithub.dev.bloomberg.com/pages/InfrastructureExperience/jsonapi_spec_helpers)
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "jsonapi_spec_helpers"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/rspec ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rspec' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require "pathname"
10
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require "rubygems"
14
+ require "bundler/setup"
15
+
16
+ load Gem.bin_path("rspec-core", "rspec")
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'jsonapi_spec_helpers/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jsonapi_spec_helpers"
8
+ spec.version = JsonapiSpecHelpers::VERSION
9
+ spec.authors = ["Lee Richmond"]
10
+ spec.email = ["lrichmond1@bloomberg.net"]
11
+
12
+ spec.summary = %q{Spec helpers for jsonapi}
13
+ spec.description = %q{Includes factory-style payload assertions}
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "pry"
22
+ spec.add_development_dependency "pry-byebug"
23
+ spec.add_development_dependency "actionpack", "~> 5.0"
24
+ spec.add_development_dependency "bundler", "~> 1.11"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
@@ -0,0 +1,133 @@
1
+ require 'pry'
2
+ require 'pry-byebug'
3
+
4
+ require 'json'
5
+ require 'jsonapi_spec_helpers/version'
6
+ require 'jsonapi_spec_helpers/payload'
7
+
8
+ RSpec::Matchers.define :match_payload do |attribute, expected|
9
+ match do |actual|
10
+ # cast to_json for things like Time
11
+ actual.to_json == expected.to_json
12
+ end
13
+
14
+ failure_message do |actual|
15
+ "Expected JSON payload to have key '#{attribute}' == #{expected.inspect} but was #{actual.inspect}"
16
+ end
17
+ end
18
+
19
+ RSpec::Matchers.define :have_payload_key do |expected|
20
+ match do |json|
21
+ json.has_key?(expected.to_s)
22
+ end
23
+
24
+ failure_message do |actual|
25
+ "Expected JSON payload to have key '#{expected}' but was not present"
26
+ end
27
+
28
+ failure_message_when_negated do |actual|
29
+ "Expected JSON payload to NOT have key '#{expected}' but was present"
30
+ end
31
+ end
32
+
33
+ RSpec::Matchers.define :be_not_in_payload do |expected|
34
+ match do |json|
35
+ false
36
+ end
37
+
38
+ failure_message do |actual|
39
+ "JSON payload contained unexpected key '#{actual}'"
40
+ end
41
+ end
42
+
43
+ module JsonapiSpecHelpers
44
+ def self.included(klass)
45
+ if defined?(Rails)
46
+ Dir[Rails.root.join('spec/payloads/**/*.rb')].each { |f| require f }
47
+ end
48
+ end
49
+
50
+ def json
51
+ JSON.parse(response.body)
52
+ end
53
+
54
+ def json_item(from: nil)
55
+ from = json if from.nil?
56
+ data = from.has_key?('data') ? from['data'] : from
57
+
58
+ {}.tap do |item|
59
+ item['id'] = data['id']
60
+ item['jsonapi_type'] = data['type']
61
+ item.merge!(data['attributes'])
62
+ end
63
+ end
64
+
65
+ def json_items(*indices)
66
+ items = []
67
+ json['data'].each_with_index do |datum, index|
68
+ included = indices.empty? || indices.include?(index)
69
+ items << json_item(from: datum) if included
70
+ end
71
+ indices.length == 1 ? items[0] : items
72
+ end
73
+
74
+ def json_related_link(payload, assn_name)
75
+ link = payload['relationships'][assn_name]['links']['related']['href']
76
+ fail "link for #{assn_name} not found" unless link
77
+ URI.decode(link)
78
+ end
79
+
80
+ def json_included_types
81
+ (json['included'] || []).map { |i| i['type'] }.uniq
82
+ end
83
+
84
+ def json_includes(type, *indices)
85
+ included = (json['included'] || []).select { |data| data['type'] == type }
86
+ indices = (0...included.length).to_a if indices.empty?
87
+ includes = []
88
+ indices.each do |index|
89
+ includes << json_item(from: included.at(index))
90
+ end
91
+ includes
92
+ end
93
+
94
+ def json_include(type, index = 0)
95
+ json_includes(type, index)[0]
96
+ end
97
+
98
+ def json_ids(integers = false)
99
+ ids = json['data'].map { |d| d['id'] }
100
+ ids.map!(&:to_i) if integers
101
+ ids
102
+ end
103
+
104
+ def assert_payload(name, record, json, &blk)
105
+ unless payload = JsonapiSpecHelpers::Payload.registry[name]
106
+ raise "No payloads registered for '#{name}'"
107
+ end
108
+
109
+ # todo dup payload
110
+ if blk
111
+ payload = payload.fork
112
+ payload.instance_eval(&blk)
113
+ end
114
+
115
+ aggregate_failures "payload has correct key/values" do
116
+ payload.keys.each_pair do |attribute, prc|
117
+ if (expect(json).to have_payload_key(attribute)) == true
118
+ expect(json[attribute.to_s]).to match_payload(attribute, prc.call(record))
119
+ end
120
+ end
121
+
122
+ payload.no_keys.each do |no_key|
123
+ expect(json).to_not have_payload_key(no_key)
124
+ end
125
+
126
+ unexpected_keys = json.keys - payload.keys.keys.map(&:to_s)
127
+ unexpected_keys.reject! { |k| %w(id jsonapi_type).include?(k) }
128
+ unexpected_keys.each do |key|
129
+ expect(key).to be_not_in_payload
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,45 @@
1
+ module JsonapiSpecHelpers
2
+ class Payload
3
+ class << self
4
+ attr_accessor :registry
5
+ end
6
+ self.registry = {}
7
+
8
+ attr_accessor :keys, :no_keys
9
+
10
+ def self.register(name, &blk)
11
+ instance = new
12
+ instance.instance_eval(&blk)
13
+ registry[name] = instance
14
+ end
15
+
16
+ def fork
17
+ instance = self.class.new
18
+ instance.keys = keys.dup
19
+ instance.no_keys = no_keys.dup
20
+ instance
21
+ end
22
+
23
+ def initialize
24
+ @keys = {}
25
+ @no_keys = []
26
+ end
27
+
28
+ def no_key(name)
29
+ @keys.delete(name)
30
+ @no_keys << name
31
+ end
32
+
33
+ def key(name, &blk)
34
+ @no_keys.reject! { |k| k == name }
35
+ prc = blk
36
+ prc = ->(record) { record.send(name) } if prc.nil?
37
+ @keys[name] = prc
38
+ end
39
+
40
+ def timestamps!
41
+ @keys[:created_at] = ->(record) { record.created_at }
42
+ @keys[:updated_at] = ->(record) { record.updated_at }
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module JsonapiSpecHelpers
2
+ VERSION = "0.2.0"
3
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsonapi_spec_helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Lee Richmond
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
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: pry-byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: actionpack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.11'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.11'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ description: Includes factory-style payload assertions
98
+ email:
99
+ - lrichmond1@bloomberg.net
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/rspec
113
+ - bin/setup
114
+ - jsonapi_spec_helpers.gemspec
115
+ - lib/jsonapi_spec_helpers.rb
116
+ - lib/jsonapi_spec_helpers/payload.rb
117
+ - lib/jsonapi_spec_helpers/version.rb
118
+ homepage:
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.4.5.1
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Spec helpers for jsonapi
142
+ test_files: []
143
+ has_rdoc: