json_api_test_helpers 1.1 → 1.2.1.1

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
  SHA256:
3
- metadata.gz: 453f2d1cabd8d90513c4249388b3d2280d05bd8648d669d3f330685c7825bead
4
- data.tar.gz: 6bb89648163de76a82e4e0ea496c4ef71df7a595aec82239aca744172fb0f763
3
+ metadata.gz: f89a3b6ed25400e5159dce0a7387b14187f1e499168d0b3f6a9a83f00dd59625
4
+ data.tar.gz: e3b0656e29ac91694ccd32fcd11cc22499de49259d3771cfcb679abd4be2838b
5
5
  SHA512:
6
- metadata.gz: b46bad2d8208984f79ebe977563097ccf68121b646d9a4783427e86933566f8714dd840a50e4f7864705b684f2c48699c3ea0109d5642062494713222872c9d1
7
- data.tar.gz: 142399cd11e7dae1ab9ed9364a5a6609f7d8e68658455f037b5e032fd17ef7a5bffebd283a7bb163a85928e8ab9a5f5fdc7cfa32fc4121da7cdb4489f1ead778
6
+ metadata.gz: 482ae47f476482ed8f08b86bfc8d9e202a9951a3836eec2b0777a39c21e93cad4b4f887025a96cdad3bfa328ca8114087310584dc300bf96cd361d90c21eef3b
7
+ data.tar.gz: 40435013ab21964b0c3f23a3f76c9dbcb950191a8120272d631c0d579bfc1425a8f12004c7bf253bdd8170f12ef0a8274a0d334a030a0ae4d324abc48c90e6bf
data/README.md CHANGED
@@ -27,18 +27,22 @@ Methods:
27
27
  | json_response | | Returns parsed `response.body` as Hash with indifferent access or Array |
28
28
  | json_api_record | `record`, `attributes`, `relationships: nil`, `additional: nil`| Returns Hash in JSON API format with serialized object |
29
29
  | json_api_collection | `collection`, `attributes = nil`, `relationships: nil` | Returns Hash in JSON API format with serialized array |
30
+ | json_api_relationships | `record`, `relationships` | Returns Hash of the record's relationships. When `relationships` argument is Array, all releations have name same as model class names. When it's Hash { relationship_name => class_name } |
30
31
  | fix_value_for_json | `value` | Fix values which are usually used in Rails apps. `DateTime`, `ActiveSupport::TimeWithZone` to `iso8601`; `CarrierWave::Uploader::Serialization` to `serializable_hash`; everything else to Hash with replaced underscores `_` to minus `-` in attribute names |
31
32
  | fix_comparing_types | `value` | Fix type value to easy compare. `DateTime`, `ActiveSupport::TimeWithZone` to `datetime` with UTC; `ActiveRecord::Point` to string `"#{value.x}, #{value.y}"`. |
32
33
  | attributes_for_nested | `attributes`, `**associations` | Merge `attributes` with associations with JSON API format |
34
+ | json_api_params | `attributes` | Put params to `{ data: { attributes: your_params }}` object |
33
35
 
34
36
  ## Examples
35
37
 
36
38
  ```ruby
37
- expect(json_response).to include_json(json_api_record(User.first))
39
+ expect(json_response).to include_json(json_api_record(User.first, [ :email, :another_attribute ]))
38
40
 
39
41
  expect(json_response).to include_json(json_api_collection(User.all))
40
42
 
41
- expect(json_response).to include_json(json_api
43
+ expect(json_response).to include_json(json_api_relationships(user, posts: :user_posts))
44
+
45
+ post 'some_post', params: json_api_params({ attribute1: :value1, attribute2: :value2 })
42
46
  ```
43
47
 
44
48
  ## Development
@@ -1,8 +1,8 @@
1
1
  module JsonApiTestHelpers
2
2
  module Response
3
3
  def json_response
4
- if response.body.present?
5
- parsed_json = JSON.parse response.body
4
+ if response[:body].present?
5
+ parsed_json = JSON.parse response[:body]
6
6
  parsed_json.with_indifferent_access unless parsed_json.is_a? Array
7
7
  else
8
8
  raise 'Response body is empty'
@@ -12,7 +12,7 @@ module JsonApiTestHelpers
12
12
  def json_api_record(record, attributes, relationships: nil, additional: nil)
13
13
  json = {
14
14
  'data' => {
15
- 'id' => record.id.to_s,
15
+ 'id' => record.uuid,
16
16
  'attributes' => json_api_record_attributes(record, attributes)
17
17
  }
18
18
  }
@@ -24,7 +24,7 @@ module JsonApiTestHelpers
24
24
  {
25
25
  'data' => collection.map do |record|
26
26
  hash = {
27
- 'id' => record.id.to_s
27
+ 'id' => record.uuid
28
28
  }
29
29
  hash['attributes'] = json_api_record_attributes(record, attributes) if attributes
30
30
  hash['relationships'] = json_api_relationships(record, relationships) if relationships
@@ -66,23 +66,23 @@ module JsonApiTestHelpers
66
66
  end
67
67
  end
68
68
 
69
- private
70
-
71
69
  def json_api_record_attributes(record, attributes)
72
70
  attributes.reduce({}) do |hash, attr|
73
- hash.merge! attr[0].to_s.tr('_', '-') => fix_value_for_json(record.send(attr[0]))
71
+ hash.merge! attr.to_s.tr('_', '-') => fix_value_for_json(record.send(attr))
74
72
  end
75
73
  end
76
74
 
77
75
  def json_api_relationships(record, relationships)
78
76
  relationships.reduce({}) do |hash, relationship|
79
- object = record.send relationship
77
+ name = relation_has_another_class_name?(relationship) ? relationship[0] : relationship
78
+ class_name = relation_has_another_class_name?(relationship) ? relationship[1] : relationship
79
+ object = record.send name
80
80
  data = if object.is_a?(ActiveRecord::Associations::CollectionProxy)
81
- object.reduce([]) { |result, item| result << data_relationship_object(item, relationship) }
81
+ object.reduce([]) { |result, item| result << data_relationship_object(item, class_name) }
82
82
  else
83
- object.present? ? data_relationship_object(object, relationship) : {}
83
+ object.present? ? data_relationship_object(object, class_name) : {}
84
84
  end
85
- hash.merge! json_api_model_name(relationship) => { 'data' => data }
85
+ hash.merge! json_api_model_name(name) => { 'data' => data }
86
86
  end
87
87
  end
88
88
 
@@ -92,9 +92,13 @@ module JsonApiTestHelpers
92
92
 
93
93
  def data_relationship_object(object, relationship)
94
94
  {
95
- 'id' => object.id.to_s,
95
+ 'id' => object.uuid,
96
96
  'type' => json_api_model_name(relationship).pluralize
97
97
  }
98
98
  end
99
+
100
+ def relation_has_another_class_name?(relationship)
101
+ relationship.is_a? Array
102
+ end
99
103
  end
100
104
  end
@@ -1,3 +1,3 @@
1
1
  module JsonApiTestHelpers
2
- VERSION = '1.1'
2
+ VERSION = '1.2.1.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_api_test_helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.1'
4
+ version: 1.2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Kalashnikov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-07-23 00:00:00.000000000 Z
11
+ date: 2022-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -93,8 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  - !ruby/object:Gem::Version
94
94
  version: '0'
95
95
  requirements: []
96
- rubyforge_project:
97
- rubygems_version: 2.7.6
96
+ rubygems_version: 3.0.3.1
98
97
  signing_key:
99
98
  specification_version: 4
100
99
  summary: JSON API helpers for testing