-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathcsv_converter_spec.rb
183 lines (156 loc) · 5.53 KB
/
csv_converter_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
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe RailsAdmin::CSVConverter do
it 'keeps headers ordering' do
RailsAdmin.config(Player) do
export do
field :number
field :name
end
end
FactoryBot.create :player
objects = Player.all
schema = {only: %i[number name]}
expect(RailsAdmin::CSVConverter.new(objects, schema).to_csv({})[2]).to match(/Number,Name/)
end
describe '#generate_csv_header' do
let(:objects) { FactoryBot.create_list :player, 1 }
before do
RailsAdmin.config(Player) do
export do
field :number
field :name
end
end
end
it 'does not break when non-existent fields are given' do
expect(RailsAdmin::CSVConverter.new(objects, {only: %i[name foo], include: {bar: :baz}}).send(:generate_csv_header)).
to eq ['Name']
end
it 'does not break when non-association fields are given to :include' do
expect(RailsAdmin::CSVConverter.new(objects, {only: %i[name foo], include: {name: :name}}).send(:generate_csv_header)).
to eq ['Name']
end
end
describe '#to_csv' do
before do
RailsAdmin.config(Player) do
export do
field :number
field :name
end
end
end
let(:objects) { FactoryBot.create_list :player, 1, number: 1, name: 'なまえ' }
let(:schema) { {only: %i[number name]} }
let(:options) { {encoding_to: encoding} }
subject { RailsAdmin::CSVConverter.new(objects, schema).to_csv(options) }
context 'when encoding FROM latin1', active_record: true do
let(:connection_config) do
if ActiveRecord::Base.respond_to?(:connection_db_config)
ActiveRecord::Base.connection_db_config.configuration_hash
else
ActiveRecord::Base.connection_config
end
end
let(:encoding) { '' }
let(:objects) { FactoryBot.create_list :player, 1, number: 1, name: 'Josè'.encode('ISO-8859-1') }
before do
case connection_config[:adapter]
when 'postgresql'
@connection = ActiveRecord::Base.connection.instance_variable_get(:@connection)
@connection.set_client_encoding('latin1')
when 'mysql2'
ActiveRecord::Base.connection.execute('SET NAMES latin1;')
end
end
after do
case connection_config[:adapter]
when 'postgresql'
@connection.set_client_encoding('utf8')
when 'mysql2'
ActiveRecord::Base.connection.execute('SET NAMES utf8;')
end
end
it 'exports to ISO-8859-1' do
expect(subject[1]).to eq 'ISO-8859-1'
expect(subject[2].encoding).to eq Encoding::ISO_8859_1
expect(subject[2].unpack1('H*')).
to eq '4e756d6265722c4e616d650a312c4a6f73e80a'
end
end
context 'when encoding to UTF-8' do
let(:encoding) { 'UTF-8' }
it 'exports to UTF-8 with BOM' do
expect(subject[1]).to eq 'UTF-8'
expect(subject[2].encoding).to eq Encoding::UTF_8
expect(subject[2].unpack1('H*')).
to eq 'efbbbf4e756d6265722c4e616d650a312ce381aae381bee381880a' # have BOM
end
end
context 'when encoding to Shift_JIS' do
let(:encoding) { 'Shift_JIS' }
it 'exports to Shift_JIS' do
expect(subject[1]).to eq 'Shift_JIS'
expect(subject[2].encoding).to eq Encoding::Shift_JIS
expect(subject[2].unpack1('H*')).
to eq '4e756d6265722c4e616d650a312c82c882dc82a60a'
end
end
context 'when encoding to UTF-16(ASCII-incompatible)' do
let(:encoding) { 'UTF-16' }
it 'encodes to expected byte sequence' do
expect(subject[1]).to eq 'UTF-16'
expect(subject[2].encoding).to eq Encoding::UTF_16
expect(subject[2].unpack1('H*').force_encoding('US-ASCII')).
to eq 'feff004e0075006d006200650072002c004e0061006d0065000a0031002c306a307e3048000a'
end
end
context 'when specifying a column separator' do
context 'when options keys are symbolized' do
let(:options) { {encoding_to: 'UTF-8', generator: {col_sep: '___'}} }
it 'uses the column separator specified' do
expect(subject[2].unpack1('H*')).
to eq 'efbbbf4e756d6265725f5f5f4e616d650a315f5f5fe381aae381bee381880a'
end
end
context 'when options keys are string' do
let(:options) { {'encoding_to' => 'UTF-8', 'generator' => {'col_sep' => '___'}} }
it 'uses the column separator specified' do
expect(subject[2].unpack1('H*')).
to eq 'efbbbf4e756d6265725f5f5f4e616d650a315f5f5fe381aae381bee381880a'
end
end
end
context 'when objects is empty' do
let(:objects) { [] }
let(:options) { {} }
it 'generates an empty csv' do
expect(subject[2]).to eq("\n")
end
end
context 'when more than one page of objects' do
before do
FactoryBot.create_list :player, 100
end
let(:objects) { Player.all }
let(:options) { {} }
it 'generates a csv with all objects' do
expect(subject[2].split("\n").count).to be 101
end
end
context 'when objects are ordered' do
before do
FactoryBot.create_list :player, 2 do |player, index|
player.name = "Player #{index}"
end
FactoryBot.create :player, name: 'Player zzz'
end
let(:objects) { Player.all.order(name: :desc) }
let(:options) { {} }
it 'preserves the ordering' do
expect(subject[2].split("\n")[1]).to include('Player zzz')
end
end
end
end