jsonapi_expectations 0.0.3 → 0.0.5

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 +75 -29
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 92b5555e5bab6d04f1d2486e77f846c2e95e85e4
4
- data.tar.gz: a33d3230ab71f091242495eb432664835bcddead
3
+ metadata.gz: 21c2096f2fc743d92f1a54390a688d52086e9788
4
+ data.tar.gz: 95cf32b87db3ef08d7321d7f6efad2c023ea633e
5
5
  SHA512:
6
- metadata.gz: f858de7a6b34ba397a632a3292ec4aeaf4748827328f197e4a51d97b7ab79ace9588aec1d111523009461341f32c8fa178d72e66bfb91b023557f237c62406c0
7
- data.tar.gz: 7e6332856db23ec73c237d7ee458d5175cf0f175568788dd8d01258e8b54b88b210b5a4bfd8e61de8b9d9ffe17f418c0884a9f8a4ff65fee83a4763d2b2eebfd
6
+ metadata.gz: 80cf32bab52f34beb07fe627ffd0abcce9f76830814076756e0b31afb0ccd3ab3c7e50c99185971843fb991e89979f6b47880d0d04090fbc686d7966cbea9a3f
7
+ data.tar.gz: 9ab15ccbe87fd48208c946984f606e2d8cb03b7a4f52c3fbdcd53abbf80f858069cd9a9c71323a6099e4d37524dbcb685f724553d5c9b44247be88e51056ff1d
@@ -3,18 +3,35 @@ require 'active_support/inflector'
3
3
 
4
4
  module JsonapiExpectations
5
5
  def expect_attributes attrs
6
- expect(json_body[:data]).to_not be_empty
7
- expect_json 'data.attributes', dasherize_keys(attrs)
6
+ expect_valid_data
7
+ if is_array_response?
8
+ location = 'data.?.attributes'
9
+ else
10
+ location = 'data.attributes'
11
+ end
12
+ expect_json location, dasherize_keys(attrs)
8
13
  end
9
-
10
- def expect_attributes_in_list attrs
11
- expect(json_body[:data]).to_not be_empty
12
- expect_json 'data.?.attributes', dasherize_keys(attrs)
14
+ alias_method :expect_attributes_in_list, :expect_attributes
15
+
16
+ def expect_attributes_absent *keys
17
+ expect_valid_data
18
+ if is_array_response?
19
+ json_body[:data].each do |data|
20
+ dasherize_array(keys).each do |key|
21
+ expect(data[key].present?).to be_falsey
22
+ end
23
+ end
24
+ else
25
+ dasherize_array(keys).each do |key|
26
+ expect(json_body[:data][:attributes][key].present?).to be_falsey
27
+ end
28
+ end
13
29
  end
30
+ alias_method :expect_attributes_absent_in_list, :expect_attributes_absent
14
31
 
15
32
  def expect_relationship opts
16
- # If looking for item in a list, need to change location string
17
- location = if opts[:in_list]
33
+ expect_valid_data
34
+ location = if is_array_response?
18
35
  "data.?.relationships.#{opts[:key]}"
19
36
  else
20
37
  "data.relationships.#{opts[:key]}"
@@ -41,48 +58,69 @@ module JsonapiExpectations
41
58
  end
42
59
  end
43
60
  end
44
-
45
- def expect_relationship_in_list opts
46
- opts[:in_list] = true
47
- expect_relationship opts
48
- end
61
+ alias_method :expect_relationship_in_list, :expect_relationship
49
62
 
50
63
  def expect_item_count number
64
+ expect_valid_data
51
65
  expect_json_sizes data: number
52
66
  end
53
67
 
54
- def expect_item_in_list find_me, opts = {}
68
+ def expect_record find_me, opts = {}
55
69
  opts[:type] ||= jsonapi_type find_me
56
- expect(json_body[:data]).to_not be_empty
57
- found = json_body[:data].detect do |item|
70
+ if opts[:included]
71
+ location = json_body[:included]
72
+ else
73
+ location = json_body[:data]
74
+ end
75
+ expect_valid_data location
76
+ found = location.detect do |item|
58
77
  jsonapi_match? find_me, item, opts[:type]
59
78
  end
60
79
  expect(found).to be_truthy
61
80
  end
81
+ alias_method :expect_item_in_list, :expect_record
62
82
 
63
- def expect_item_not_in_list dont_find_me, opts = {}
83
+ def expect_record_absent dont_find_me, opts = {}
64
84
  opts[:type] ||= jsonapi_type dont_find_me
65
- expect(json_body[:data]).to_not be_empty
66
- json_body[:data].each do |item|
85
+ if opts[:included]
86
+ location = json_body[:included]
87
+ else
88
+ location = json_body[:data]
89
+ end
90
+ expect_valid_data location
91
+ location.each do |item|
67
92
  expect(jsonapi_match?(dont_find_me, item, opts[:type])).to be_falsey
68
93
  end
69
94
  end
70
- alias_method :expect_item_not_to_be_in_list,
71
- :expect_item_not_in_list
72
- alias_method :expect_item_to_not_be_in_list,
73
- :expect_item_not_in_list
95
+ alias_method :expect_item_not_in_list, :expect_record_absent
96
+ alias_method :expect_item_not_to_be_in_list, :expect_record_absent
97
+ alias_method :expect_item_to_not_be_in_list, :expect_record_absent
74
98
 
75
- ## Finder helpers
76
-
77
- def find_record_in_response record, opts = {}
99
+ def find_record record, opts = {}
78
100
  opts[:type] ||= jsonapi_type(record)
79
- json_body[:data].select do |item|
80
- item[:id] == record.id && item[:type] == opts[:type]
101
+ if opts[:included]
102
+ location = json_body[:included]
103
+ else
104
+ location = json_body[:data]
105
+ end
106
+ expect_valid_data location
107
+ location.select do |item|
108
+ jsonapi_match? record, item, opts[:type]
81
109
  end.first
82
110
  end
83
111
 
112
+ def expect_valid_data location = nil
113
+ location ||= json_body[:data]
114
+ expect(location).to_not be_nil
115
+ expect(location).to_not be_empty
116
+ end
117
+
84
118
  private
85
119
 
120
+ def is_array_response?
121
+ json_body[:data].is_a? Array
122
+ end
123
+
86
124
  def expect_linkage_data location, relationship_data, included
87
125
  begin
88
126
  expect_json location, relationship_data
@@ -93,8 +131,16 @@ module JsonapiExpectations
93
131
  expect_json "included.?", relationship_data if included
94
132
  end
95
133
 
134
+ def dasherize_array array
135
+ array.map { |item| dasherize item }
136
+ end
137
+
96
138
  def dasherize_keys hash
97
- hash.deep_transform_keys { |key| key.to_s.tr('_', '-').to_sym }
139
+ hash.deep_transform_keys { |key| dasherize key }
140
+ end
141
+
142
+ def dasherize thing
143
+ thing.to_s.tr('_', '-').to_sym
98
144
  end
99
145
 
100
146
  def jsonapi_match? model, data, type
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.0.3
4
+ version: 0.0.5
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-23 00:00:00.000000000 Z
11
+ date: 2017-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: airborne