jsonapi-resources-matchers 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +1 -1
- data/lib/jsonapi/resources/matchers.rb +5 -0
- data/lib/jsonapi/resources/matchers/have_one.rb +55 -0
- data/lib/jsonapi/resources/matchers/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ce61d44c20a946880e5ec4f83cac4b048d9dcf01
|
4
|
+
data.tar.gz: 81e5d38ac48355c2e61a07e46fd477cd614acb5d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fdb63eae9a2ff22abbc0289030dfb3547a82fd4fbe5ec1349a99c0f638bbe43742009535010979ecdbd10da2b960dd1947cf87eacc47de7d4de0107e58db00ee
|
7
|
+
data.tar.gz: 665792e728af159ebb6746dfae7f6ae71009239056e5cd454654545bda238aad64403b5ef96d64f5d727b8a8059b99ba8fe2e86d889f082d96c66455df52de20
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -54,7 +54,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
54
54
|
|
55
55
|
## Contributing
|
56
56
|
|
57
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/g5/jsonapi-resources-matchers. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
57
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/g5/jsonapi-resources-matchers. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org/) code of conduct.
|
58
58
|
|
59
59
|
## MIT License
|
60
60
|
|
@@ -2,6 +2,7 @@ require "jsonapi/resources/matchers/version"
|
|
2
2
|
require "jsonapi/resources/matchers/have_attribute"
|
3
3
|
require "jsonapi/resources/matchers/filter"
|
4
4
|
require "jsonapi/resources/matchers/have_many"
|
5
|
+
require "jsonapi/resources/matchers/have_one"
|
5
6
|
require "jsonapi/resources/matchers/have_model_name"
|
6
7
|
require "jsonapi/resources/matchers/have_primary_key"
|
7
8
|
|
@@ -21,6 +22,10 @@ module JSONAPI
|
|
21
22
|
HaveMany.new(name)
|
22
23
|
end
|
23
24
|
|
25
|
+
def have_one(name)
|
26
|
+
HaveOne.new(name)
|
27
|
+
end
|
28
|
+
|
24
29
|
def have_model_name(name)
|
25
30
|
HaveModelName.new(name)
|
26
31
|
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module JSONAPI
|
2
|
+
module Resources
|
3
|
+
module Matchers
|
4
|
+
class HaveOne
|
5
|
+
|
6
|
+
attr_accessor :name, :resource, :expected_class_name
|
7
|
+
|
8
|
+
def initialize(name)
|
9
|
+
self.name = name
|
10
|
+
end
|
11
|
+
|
12
|
+
def description
|
13
|
+
"have one `#{name}`"
|
14
|
+
end
|
15
|
+
|
16
|
+
def matches?(resource)
|
17
|
+
self.resource = resource
|
18
|
+
|
19
|
+
has_key_in_relationships? && matches_class_name?
|
20
|
+
end
|
21
|
+
|
22
|
+
def has_key_in_relationships?
|
23
|
+
serialized_hash = JSONAPI::ResourceSerializer.new(resource.class).
|
24
|
+
serialize_to_hash(resource).with_indifferent_access
|
25
|
+
expected_key = name.to_s.dasherize
|
26
|
+
relationships = serialized_hash["data"]["relationships"]
|
27
|
+
return false if relationships.nil?
|
28
|
+
relationships.has_key?(expected_key)
|
29
|
+
end
|
30
|
+
|
31
|
+
def with_class_name(name)
|
32
|
+
self.expected_class_name = name
|
33
|
+
self
|
34
|
+
end
|
35
|
+
|
36
|
+
def failure_message
|
37
|
+
resource_name = resource.class.name.demodulize
|
38
|
+
message = ["expected `#{resource_name}` to have one `#{name}`"]
|
39
|
+
if self.expected_class_name
|
40
|
+
message << "with class name `#{self.expected_class_name}`"
|
41
|
+
end
|
42
|
+
message.join(" ")
|
43
|
+
end
|
44
|
+
|
45
|
+
def matches_class_name?
|
46
|
+
return true if self.expected_class_name.nil?
|
47
|
+
association = resource.class._relationships[name]
|
48
|
+
actual_class_name = association.class_name
|
49
|
+
self.expected_class_name == actual_class_name
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-resources-matchers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- G5
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-07-
|
12
|
+
date: 2015-07-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: jsonapi-resources
|
@@ -108,6 +108,7 @@ files:
|
|
108
108
|
- lib/jsonapi/resources/matchers/have_attribute.rb
|
109
109
|
- lib/jsonapi/resources/matchers/have_many.rb
|
110
110
|
- lib/jsonapi/resources/matchers/have_model_name.rb
|
111
|
+
- lib/jsonapi/resources/matchers/have_one.rb
|
111
112
|
- lib/jsonapi/resources/matchers/have_primary_key.rb
|
112
113
|
- lib/jsonapi/resources/matchers/integrations/rspec.rb
|
113
114
|
- lib/jsonapi/resources/matchers/version.rb
|
@@ -132,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
132
133
|
version: '0'
|
133
134
|
requirements: []
|
134
135
|
rubyforge_project:
|
135
|
-
rubygems_version: 2.4.
|
136
|
+
rubygems_version: 2.4.6
|
136
137
|
signing_key:
|
137
138
|
specification_version: 4
|
138
139
|
summary: Spec matchers for jsonapi-resources
|