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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ae0027f2286732ef5034d98b32beb53e5b4f37a1
4
- data.tar.gz: 4b8cc9ec1fcbebadeaf8f9acfc6df620f754dd17
3
+ metadata.gz: ce61d44c20a946880e5ec4f83cac4b048d9dcf01
4
+ data.tar.gz: 81e5d38ac48355c2e61a07e46fd477cd614acb5d
5
5
  SHA512:
6
- metadata.gz: 0596c7a23b1f10d4c088b00744fde0fd5c4dd56918b22fd9b779a35dd472eb54c3deb7968f6501e4d3c2fc454131fdf4031f1a5fb7f22d58f6e6bad712e61af0
7
- data.tar.gz: c6c7a07b2aa71a12d01aea8e3d224dd0c9fcf8892edd692419696e1dfb380a77c285a68e657d6a093150c03cc5813e1cc58035fe4e87e184271e0ec4ff4950e1
6
+ metadata.gz: fdb63eae9a2ff22abbc0289030dfb3547a82fd4fbe5ec1349a99c0f638bbe43742009535010979ecdbd10da2b960dd1947cf87eacc47de7d4de0107e58db00ee
7
+ data.tar.gz: 665792e728af159ebb6746dfae7f6ae71009239056e5cd454654545bda238aad64403b5ef96d64f5d727b8a8059b99ba8fe2e86d889f082d96c66455df52de20
@@ -1,3 +1,7 @@
1
+ # v0.2.0
2
+
3
+ - Add `have_one` matcher
4
+
1
5
  # v0.1.0
2
6
 
3
7
  - Initial release
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
@@ -1,7 +1,7 @@
1
1
  module JSONAPI
2
2
  module Resources
3
3
  module Matchers
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
6
6
  end
7
7
  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.1.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-20 00:00:00.000000000 Z
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.5
136
+ rubygems_version: 2.4.6
136
137
  signing_key:
137
138
  specification_version: 4
138
139
  summary: Spec matchers for jsonapi-resources