jsonapi_expectations 0.1.1 → 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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jsonapi_expectations.rb +18 -19
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2601f5db12cdcdd320710f13892bb3b0f2737bb0
4
- data.tar.gz: b2c4050e0a5496d0c0c5d7f2fcf069dc060ec43c
3
+ metadata.gz: 9f0c4c0c422747b4796bbe5438a70f967a05e2f8
4
+ data.tar.gz: 4ab37b714cb057cad02088e8a352c1e342c2ea11
5
5
  SHA512:
6
- metadata.gz: 71a8dff71c02084597c964cf4f2db4fae13cef3781e01baf4eecf78529d670ed5a976c5b26bd9510f2518d9da9fba90b6e652cb3fecc914767617b0f9e21c41b
7
- data.tar.gz: 6a3ccc5f53ccb07267ef3000116b0cbf2ae3aa00003a2ce16c457109cd224672bef356de39aa748e830d14725198c4931cce2ecb0d0361437285c9ab50d62610
6
+ metadata.gz: 2325d776f3713ce042da27536b7e4101507830456d179184567d77f84079ca274a1d27739b49b415f2ccce57e82599a2d928ec48fbd9d9d4ee6cb28fc4fefcdd
7
+ data.tar.gz: b76b63d83bccaa24d2c32d987b5ef08f0eb3ab584ec8edcb6f769b44691e9b80192bff654539213877d6018c47b3b68829194afec4de5a01f66ba264a8f79841
@@ -75,20 +75,11 @@ module JsonapiExpectations
75
75
  expect_json_sizes data: number
76
76
  end
77
77
 
78
- def expect_record find_me, opts = {}
79
- opts[:type] ||= jsonapi_type find_me
80
- location = if opts[:included]
81
- json_body[:included]
82
- else
83
- json_body[:data]
84
- end
85
- expect_valid_data location
86
- found = location.detect do |item|
87
- jsonapi_match? find_me, item, opts[:type]
88
- end
78
+ def expect_record record, opts = {}
79
+ found = find_record record, opts
89
80
  expect(found).to be_truthy
90
81
  rescue RSpec::Expectations::ExpectationNotMetError
91
- msg = "Expected #{find_me} to be present in json response"
82
+ msg = "Expected #{record} to be present in json response"
92
83
  raise Exceptions::ExpectationError, msg
93
84
  end
94
85
 
@@ -108,19 +99,28 @@ module JsonapiExpectations
108
99
  raise Exceptions::ExpectationError, msg
109
100
  end
110
101
 
111
- def expect_records_sorted_by attr, direction = :asc
102
+ def expect_records_sorted_by attr, opts = {}
103
+ direction = opts.fetch :direction, :asc
104
+ allow_nils = opts.fetch :allow_nils, false
105
+
112
106
  json_body[:data].each_with_index do |item, index|
113
- return if json_body[:data].last == item
107
+ return if item == json_body[:data].last
114
108
 
115
109
  this_one = item[:attributes][attr]
116
110
  next_one = json_body[:data][index + 1][:attributes][attr]
117
111
 
112
+ unless this_one && next_one
113
+ return if allow_nils
114
+ msg = "Expected response to be sorted by #{attr} #{direction}, but some attribues were nil"
115
+ raise Exceptions::ExpectationError, msg
116
+ end
117
+
118
118
  if direction == :asc
119
119
  expect(this_one).to be <= next_one
120
120
  elsif direction == :desc
121
121
  expect(next_one).to be <= this_one
122
122
  else
123
- raise "2nd argument needs to be :asc or :desc"
123
+ raise "Direction option needs to be :asc or :desc"
124
124
  end
125
125
  end
126
126
  rescue RSpec::Expectations::ExpectationNotMetError
@@ -129,19 +129,18 @@ module JsonapiExpectations
129
129
  end
130
130
 
131
131
  def find_record record, opts = {}
132
- opts[:type] ||= jsonapi_type(record)
132
+ opts[:type] ||= jsonapi_type record
133
133
  location = if opts[:included]
134
134
  json_body[:included]
135
135
  else
136
136
  json_body[:data]
137
137
  end
138
138
  expect_valid_data location
139
- location.select do |item|
139
+ location.detect do |item|
140
140
  jsonapi_match? record, item, opts[:type]
141
- end.first
141
+ end
142
142
  end
143
143
 
144
- # This will go away in the 0.1.0 release
145
144
  # TODO: deprecate these
146
145
  alias expect_attributes_in_list expect_attributes
147
146
  alias expect_attributes_absent_in_list expect_attributes_absent
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi_expectations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ross-Hunter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-29 00:00:00.000000000 Z
11
+ date: 2018-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: airborne
@@ -136,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
136
  version: '0'
137
137
  requirements: []
138
138
  rubyforge_project:
139
- rubygems_version: 2.6.12
139
+ rubygems_version: 2.6.13
140
140
  signing_key:
141
141
  specification_version: 4
142
142
  summary: Semantic expectation helpers for JSON API testing using Airborne and RSpec.