-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathhash_spec.rb
89 lines (77 loc) · 2.74 KB
/
hash_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# frozen_string_literal: true
describe Grape::Entity do
it 'except option for nested entity', :aggregate_failures do
module EntitySpec
class Address < Grape::Entity
expose :post, if: :full
expose :city
expose :street
expose :house
end
class AddressWithString < Grape::Entity
self.hash_access = :string
expose :post, if: :full
expose :city
expose :street
expose :house, expose_nil: false
end
class Company < Grape::Entity
expose :full_name, if: :full
expose :name
expose :address do |c, o|
Address.represent c[:address], Grape::Entity::Options.new(o.opts_hash.except(:full))
end
end
class CompanyWithString < Grape::Entity
self.hash_access = :string
expose :full_name, if: :full
expose :name
expose :address do |c, o|
AddressWithString.represent c['address'], Grape::Entity::Options.new(o.opts_hash.except(:full))
end
end
end
company = {
full_name: 'full_name',
name: 'name',
address: {
post: '123456',
city: 'city',
street: 'street',
house: 'house',
something_else: 'something_else'
}
}
company_with_string = {
'full_name' => 'full_name',
'name' => 'name',
'address' => {
'post' => '123456',
'city' => 'city',
'street' => 'street',
'house' => 'house',
'something_else' => 'something_else'
}
}
company_without_house_with_string = {
'full_name' => 'full_name',
'name' => 'name',
'address' => {
'post' => '123456',
'city' => 'city',
'street' => 'street',
'something_else' => 'something_else'
}
}
expect(EntitySpec::CompanyWithString.represent(company_with_string).serializable_hash).to eq \
company.slice(:name).merge(address: company[:address].slice(:city, :street, :house))
expect(EntitySpec::CompanyWithString.represent(company_without_house_with_string).serializable_hash).to eq \
company.slice(:name).merge(address: company[:address].slice(:city, :street))
expect(EntitySpec::CompanyWithString.represent(company_with_string, full: true).serializable_hash).to eq \
company.slice(:full_name, :name).merge(address: company[:address].slice(:city, :street, :house))
expect(EntitySpec::Company.represent(company).serializable_hash).to eq \
company.slice(:name).merge(address: company[:address].slice(:city, :street, :house))
expect(EntitySpec::Company.represent(company, full: true).serializable_hash).to eq \
company.slice(:full_name, :name).merge(address: company[:address].slice(:city, :street, :house))
end
end