-
Notifications
You must be signed in to change notification settings - Fork 0
/
exporter_spec.rb
186 lines (142 loc) · 3.81 KB
/
exporter_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
require 'spec_helper'
###
Company = Struct.new(:name, :address) do
def creator
User.new('dux', '[email protected]')
end
def user
User.new('dux', '[email protected]')
end
end
User = Struct.new(:name, :email) do
def company
Company.new('ACME', 'Nowhere 123')
end
end
class SimpleExporter < JsonExporter
before do
opts[:version] ||= 1
end
after do
prop :foo, :bar
response[:meta] = {
class: model.class.to_s
}
end
###
def user
opts[:user]
end
###
define :company do
prop :name
prop :address
prop :v_check, :version_one
if opts.version >= 3
prop :v_check, :version_three
end
if opts.version >= 4
prop :extra, :spicy
end
prop :creator, export(model.user)
end
define :generic_name do
prop :name
response[:foo] = :bar
end
define User do
export :company, version: opts.version
prop :v_check, :version_one
if opts.version == 3
prop :v_check, :version_three
end
prop :name
prop :email
prop :is_admin do
user && user.name.include?('dux') ? true : false
end
end
end
class GenericExporter < SimpleExporter
before do
response[:bhistory] = [:first]
end
after do
response[:ahistory] ||= []
response[:ahistory].push :parent
end
define do
prop :name
prop(:calc) { model.num * 3 }
end
end
class GenericExporterChild < GenericExporter
before do
response[:bhistory].push :second
end
after do
response[:ahistory].push :child
end
define do
prop :name
prop :ahistory, [:start]
response[:bhistory].push :third
prop(:calc) { model.num * 3 }
end
end
###
describe JsonExporter do
it 'expects basic export to work' do
name = 'ACME 1'
address = 'Nowhere 123'
company = Company.new(name, address)
result = SimpleExporter.export(company)
expect(result[:name]).to eq(name)
expect(result[:address]).to eq(address)
end
it 'exports complex object' do
some_user = User.new 'dux', '[email protected]'
response = SimpleExporter.export some_user, user: some_user
expect(response[:is_admin]).to eq(true)
user = User.new 'dino', '[email protected]'
response = SimpleExporter.export user, user: user
expect(response[:is_admin]).to eq(false)
end
it 'exports naked object' do
company = Company.new 'ACME 1', 'Nowhere 123'
data = SimpleExporter.export company, exporter: :generic_name
expect(data[:address]).to be(nil)
expect(data[:foo]).to be(:bar)
end
it 'exports deep if needed' do
user = User.new 'dux', '[email protected]'
response = SimpleExporter.export user, user: user, export_depth: 3
expect(response[:company][:creator][:company][:name]).to eq('ACME')
end
it 'uses after filter' do
user = User.new 'dux', '[email protected]'
response = SimpleExporter.export user, user: user, export_depth: 3
expect(response[:foo]).to eq(:bar)
expect(response[:meta][:class]).to eq('User')
end
it 'uses right versions' do
user = User.new 'dux', '[email protected]'
response = SimpleExporter.export user, version: 3
expect(response[:company][:v_check]).to eq(:version_three)
expect(response[:company][:extra]).to eq(nil)
response = SimpleExporter.export user, version: 4
expect(response[:company][:v_check]).to eq(:version_three)
expect(response[:company][:extra]).to eq(:spicy)
end
it 'exports via generic exporter' do
data = HashWia.new({ name: 'foo', surname: 'bar', num: 5 })
result = GenericExporter.export data
expect(result[:calc]).to eq(15)
end
it 'applies filters as it should' do
data = HashWia.new({ name: 'dux', num: 1 })
result = GenericExporterChild.export data
expect(result[:bhistory].join('-')).to eq('first-second-third')
expect(result[:ahistory].join('-')).to eq('start-parent-child')
end
end