graphiti_spec_helpers 1.0.alpha.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e2dd06fa716ef3c85458ecec2eabc4e3a9414f13
4
+ data.tar.gz: 98850e048dcacf2492dee1977f5492c98be78f35
5
+ SHA512:
6
+ metadata.gz: 38f19368c9473f355d43f119f7912231401b3d0e038d7b5f1a675998f7eec4648d6e3159ae95025eb975c58e8b94be3cc07fbb5762bc22fde76d1dce0dd6f5c9
7
+ data.tar.gz: f05c237713ea1dfd8981f893195514e23a4f30fb677889e23e6d977602684c1077629860a12baee694181b7e74bc94c6664f70d5e19345c6ede48d3b353a340f
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 graphiti_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,7 @@
1
+ # JsonapiSpecHelpers
2
+
3
+ [![Build Status](https://travis-ci.org/jsonapi-suite/jsonapi_spec_helpers.svg?branch=master)](https://travis-ci.org/jsonapi-suite/jsonapi_spec_helpers)
4
+
5
+ A collection of spec helpers for asserting on jsonapi.org responses.
6
+
7
+ [View official documentation](http://jsonapi-suite.github.io/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 "grpahiti_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,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'graphiti_spec_helpers/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "graphiti_spec_helpers"
8
+ spec.version = GraphitiSpecHelpers::VERSION
9
+ spec.authors = ["Lee Richmond"]
10
+ spec.email = ["lrichmond1@bloomberg.net"]
11
+
12
+ spec.summary = %q{Spec helpers for Graphiti and JSONAPI}
13
+ spec.description = %q{Easily test JSONAPIs and Graphiti Resources}
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 "graphiti", '>= 1.0.alpha.1'
27
+ spec.add_dependency "rspec", "~> 3.0"
28
+ end
@@ -0,0 +1,37 @@
1
+ require 'json'
2
+ require 'pp'
3
+ require 'active_support/core_ext/string'
4
+ require 'active_support/core_ext/hash'
5
+ require 'graphiti'
6
+
7
+ require 'graphiti_spec_helpers/version'
8
+ require 'graphiti_spec_helpers/helpers'
9
+ require 'graphiti_spec_helpers/node'
10
+ require 'graphiti_spec_helpers/errors_proxy'
11
+ require 'graphiti_spec_helpers/errors'
12
+
13
+ module GraphitiSpecHelpers
14
+ def self.included(klass)
15
+ klass.send(:include, Helpers)
16
+ end
17
+
18
+ class TestRunner < ::Graphiti::Runner
19
+ def current_user
20
+ nil
21
+ end
22
+ end
23
+
24
+ module Sugar
25
+ def d
26
+ jsonapi_data
27
+ end
28
+
29
+ def included(type = nil)
30
+ jsonapi_included(type)
31
+ end
32
+
33
+ def errors
34
+ jsonapi_errors
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,51 @@
1
+ module GraphitiSpecHelpers
2
+ module Errors
3
+ class Base < StandardError; end
4
+
5
+ class LinksNotFound < Base
6
+ def initialize(name)
7
+ @name = name
8
+ end
9
+
10
+ def message
11
+ "Relationship with name '#{@name}' has no links!"
12
+ end
13
+ end
14
+
15
+ class SideloadNotFound < Base
16
+ def initialize(name)
17
+ @name = name
18
+ end
19
+
20
+ def message
21
+ "Relationship with name '#{@name}' not found!"
22
+ end
23
+ end
24
+
25
+ class NoResponse < Base
26
+ def message
27
+ "Cannot parse response - missing #response.body!"
28
+ end
29
+ end
30
+
31
+ class NoData < Base
32
+ def initialize(payload)
33
+ @payload = payload
34
+ end
35
+
36
+ def message
37
+ "Payload did not contain 'data'! Payload was:\n\n#{JSON.pretty_generate(@payload)}"
38
+ end
39
+ end
40
+
41
+ class NoSideloads < Base
42
+ def initialize(payload)
43
+ @payload = payload
44
+ end
45
+
46
+ def message
47
+ "Tried to find sideload, but the payload did not contain 'included'! Payload was:\n\n#{JSON.pretty_generate(@payload)}"
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,70 @@
1
+ module GraphitiSpecHelpers
2
+ class ErrorsProxy
3
+ class Error
4
+ attr_reader :json
5
+
6
+ def initialize(json)
7
+ @json = json
8
+ end
9
+
10
+ def attribute
11
+ @json[:meta][:attribute]
12
+ end
13
+
14
+ # TODO: move to top-level code in errorable
15
+ def code
16
+ @json[:meta][:code]
17
+ end
18
+
19
+ def message
20
+ @json[:meta][:message]
21
+ end
22
+
23
+ def title
24
+ @json[:title]
25
+ end
26
+
27
+ def detail
28
+ @json[:detail]
29
+ end
30
+
31
+ def status
32
+ @json[:status]
33
+ end
34
+ end
35
+
36
+ include Enumerable
37
+
38
+ def initialize(array)
39
+ @errors = array.map { |e| Error.new(e) }
40
+ end
41
+
42
+ def [](key)
43
+ @errors[key]
44
+ end
45
+
46
+ def each(&blk)
47
+ @errors.each(&blk)
48
+ end
49
+
50
+ def length
51
+ count
52
+ end
53
+
54
+ def to_h
55
+ {}.tap do |hash|
56
+ @errors.each do |e|
57
+ hash[e.attribute] = e.message
58
+ end
59
+ end
60
+ end
61
+
62
+ def method_missing(id, *args, &blk)
63
+ if error = @errors.find { |e| e.attribute == id }
64
+ return error
65
+ else
66
+ super
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,97 @@
1
+ module GraphitiSpecHelpers
2
+ module Helpers
3
+ extend ActiveSupport::Concern
4
+
5
+ def json
6
+ if response && response.body
7
+ JSON.parse(response.body).with_indifferent_access
8
+ else
9
+ raise Errors::NoResponse.new
10
+ end
11
+ end
12
+
13
+ def jsonapi_data
14
+ @jsonapi_data ||= begin
15
+ if _jsonapi_data.is_a?(Hash)
16
+ node(from: _jsonapi_data)
17
+ else
18
+ _jsonapi_data.map { |datum| node(from: datum) }
19
+ end
20
+ end
21
+ end
22
+
23
+ def jsonapi_included(type = nil)
24
+ variable = :"@jsonapi_included#{type}"
25
+ memo = instance_variable_get(variable)
26
+ return memo if memo
27
+
28
+ nodes = _jsonapi_included.map { |i| node(from: i) }
29
+ if type
30
+ nodes.select! { |n| n.jsonapi_type == type }
31
+ end
32
+ instance_variable_set(variable, nodes)
33
+ nodes
34
+ end
35
+
36
+ def jsonapi_errors
37
+ @jsonapi_errors ||= ErrorsProxy.new(json['errors'] || [])
38
+ end
39
+
40
+ def jsonapi_headers
41
+ {
42
+ 'CONTENT_TYPE' => 'application/vnd.api+json'
43
+ }
44
+ end
45
+
46
+ def jsonapi_get(url, params: {})
47
+ get url, params: params, headers: jsonapi_headers
48
+ end
49
+
50
+ def jsonapi_post(url, payload)
51
+ post url, params: payload.to_json, headers: jsonapi_headers
52
+ end
53
+
54
+ def jsonapi_put(url, payload)
55
+ put url, params: payload.to_json, headers: jsonapi_headers
56
+ end
57
+
58
+ def jsonapi_patch(url, payload)
59
+ patch url, params: payload.to_json, headers: jsonapi_headers
60
+ end
61
+
62
+ def jsonapi_delete(url)
63
+ delete url, headers: jsonapi_headers
64
+ end
65
+
66
+ def datetime(value)
67
+ Graphiti::Types[:datetime][:read][value]
68
+ end
69
+
70
+ # @api private
71
+ def node(from: nil)
72
+ from = json if from.nil?
73
+ data = from.has_key?('data') ? from['data'] : from
74
+ hash = {}
75
+ hash['id'] = data['id']
76
+ hash['jsonapi_type'] = data['type']
77
+ hash.merge!(data['attributes']) if data.has_key?('attributes')
78
+ Node.new(hash, data['relationships'], self)
79
+ end
80
+
81
+ private
82
+
83
+ # @api private
84
+ def _jsonapi_data
85
+ json['data'] || raise(Errors::NoData.new(json))
86
+ end
87
+
88
+ # @api private
89
+ def _jsonapi_included
90
+ if json.has_key?('included')
91
+ json['included']
92
+ else
93
+ raise Errors::NoSideloads.new(json)
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,81 @@
1
+ module GraphitiSpecHelpers
2
+ class Node
3
+ attr_reader :attributes, :relationships
4
+
5
+ def initialize(attributes, relationships, context)
6
+ @attributes = attributes.with_indifferent_access
7
+ @relationships = relationships
8
+ @context = context
9
+ end
10
+
11
+ def id
12
+ rawid.to_i
13
+ end
14
+
15
+ def rawid
16
+ @attributes['id']
17
+ end
18
+
19
+ def jsonapi_type
20
+ @attributes['jsonapi_type']
21
+ end
22
+
23
+ def has_key?(key)
24
+ @attributes.has_key?(key)
25
+ end
26
+
27
+ def [](key)
28
+ @attributes[key] || @attributes[key.to_s]
29
+ end
30
+
31
+ def []=(key, val)
32
+ @attributes[key] = val
33
+ end
34
+
35
+ def attributes
36
+ @attributes
37
+ end
38
+
39
+ def method_missing(id, *args, &blk)
40
+ if @attributes.has_key?(id)
41
+ @attributes[id]
42
+ else
43
+ super
44
+ end
45
+ end
46
+
47
+ def link(relationship_name, name)
48
+ if @relationships.has_key?(relationship_name)
49
+ links = @relationships[relationship_name][:links]
50
+ raise Errors::LinksNotFound.new(relationship_name) unless links
51
+ links[name]
52
+ else
53
+ raise Errors::SideloadNotFound.new(relationship_name)
54
+ end
55
+ end
56
+
57
+ def sideload(relationship_name)
58
+ unless @relationships.has_key?(relationship_name)
59
+ raise Errors::SideloadNotFound.new(relationship_name)
60
+ end
61
+ rel = @relationships[relationship_name]
62
+ rel = rel[:data]
63
+ return if rel.nil?
64
+ if rel.is_a?(Hash)
65
+ include_for(rel[:type], rel[:id])
66
+ else
67
+ rel.map { |r| include_for(r[:type], r[:id]) }
68
+ end
69
+ end
70
+ alias :sideloads :sideload
71
+
72
+ private
73
+
74
+ def include_for(type, id)
75
+ data = @context.json[:included].find do |i|
76
+ i[:type] == type && i[:id] == id
77
+ end
78
+ @context.node(from: data)
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,64 @@
1
+ require 'graphiti_spec_helpers'
2
+
3
+ ::RSpec.shared_context 'resource testing', type: :resource do |parameter|
4
+ let(:resource) { described_class }
5
+ let(:params) { {} }
6
+
7
+ # If you need to set context:
8
+ #
9
+ # Graphiti.with_context my_context, {} do
10
+ # render
11
+ # end
12
+ def render(runtime_options = {})
13
+ json = proxy.to_jsonapi(runtime_options)
14
+ response.body = json
15
+ json
16
+ end
17
+
18
+ def proxy
19
+ @proxy ||= begin
20
+ ctx = ::GraphitiSpecHelpers::TestRunner.new(resource, params)
21
+ defined?(base_scope) ? ctx.proxy(base_scope) : ctx.proxy
22
+ end
23
+ end
24
+
25
+ def records
26
+ proxy.data
27
+ end
28
+
29
+ def response
30
+ @response ||= OpenStruct.new
31
+ end
32
+ end
33
+
34
+ module GraphitiSpecHelpers
35
+ module RSpec
36
+ def self.included(klass)
37
+ klass.send(:include, GraphitiSpecHelpers)
38
+
39
+ ::RSpec.configure do |rspec|
40
+ rspec.include_context "resource testing", type: :resource
41
+ end
42
+ end
43
+
44
+ def self.schema!(resources = nil)
45
+ ::RSpec.describe 'Graphiti Schema' do
46
+ it 'generates a backwards-compatible schema' do
47
+ message = <<-MSG
48
+ Found backwards-incompatibilities in schema! Run with FORCE_SCHEMA=true to ignore.
49
+
50
+ Incompatibilities:
51
+
52
+ MSG
53
+
54
+ errors = Graphiti::Schema.generate!(resources)
55
+ errors.each do |e|
56
+ message << "#{e}\n"
57
+ end
58
+
59
+ expect(errors.empty?).to eq(true), message
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,3 @@
1
+ module GraphitiSpecHelpers
2
+ VERSION = "1.0.alpha.2"
3
+ end
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: graphiti_spec_helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.alpha.2
5
+ platform: ruby
6
+ authors:
7
+ - Lee Richmond
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-08-03 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: graphiti
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 1.0.alpha.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 1.0.alpha.1
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ description: Easily test JSONAPIs and Graphiti Resources
112
+ email:
113
+ - lrichmond1@bloomberg.net
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - bin/console
126
+ - bin/rspec
127
+ - bin/setup
128
+ - graphiti_spec_helpers.gemspec
129
+ - lib/graphiti_spec_helpers.rb
130
+ - lib/graphiti_spec_helpers/errors.rb
131
+ - lib/graphiti_spec_helpers/errors_proxy.rb
132
+ - lib/graphiti_spec_helpers/helpers.rb
133
+ - lib/graphiti_spec_helpers/node.rb
134
+ - lib/graphiti_spec_helpers/rspec.rb
135
+ - lib/graphiti_spec_helpers/version.rb
136
+ homepage:
137
+ licenses:
138
+ - MIT
139
+ metadata: {}
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">"
152
+ - !ruby/object:Gem::Version
153
+ version: 1.3.1
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 2.6.13
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: Spec helpers for Graphiti and JSONAPI
160
+ test_files: []