/
downgrader_test.rb
304 lines (267 loc) · 12.7 KB
/
downgrader_test.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# frozen_string_literal: true
require 'test_helper'
module Downgrade
class DowngraderTest < ActiveSupport::TestCase
test 'actions contain only those with bitrate' do
assert_not Downgrade::Downgrader.actions.map(&:bitrate).include?(nil)
end
test 'creates lower-bitrate file and deletes higher-bitrate one' do
b1 = Broadcast.create!(show: shows(:g9s),
started_at: Time.zone.local(2012, 12, 12, 20),
finished_at: Time.zone.local(2012, 12, 12, 22))
file = AudioFile.create!(broadcast: b1,
path: 'dummy_higher',
codec: 'mp3',
bitrate: 224,
channels: 2)
home = FileStore::Structure.home
path = File.join('2012', '12', '12', '2012-12-12T200000+0100_120_gschach9schlimmers.192k_2.mp3')
AudioProcessor::Ffmpeg.any_instance
.expects(:transcode)
.with(is_a(String), AudioFormat.new('mp3', 192, 2))
FileUtils.expects(:mv).with(is_a(String), File.join(home, path))
assert_no_difference('AudioFile.count') do
downgrader.handle(file)
end
assert_not AudioFile.exists?(id: file.id)
lower = AudioFile.where(broadcast_id: b1.id, bitrate: 192, channels: 2).first
assert_equal path, lower.path
end
test 'creates one lower-bitrate file from highest and deletes all higher-bitrate ones' do
b1 = Broadcast.create!(show: shows(:g9s),
started_at: Time.zone.local(2012, 12, 12, 20),
finished_at: Time.zone.local(2012, 12, 12, 22))
file1 = AudioFile.create!(broadcast: b1,
path: 'dummy_highest',
codec: 'mp3',
bitrate: 320,
channels: 1)
file2 = AudioFile.create!(broadcast: b1,
path: 'dummy_higher1',
codec: 'mp3',
bitrate: 256,
channels: 2)
file3 = AudioFile.create!(broadcast: b1,
path: 'dummy_higher2',
codec: 'mp3',
bitrate: 224,
channels: 2)
home = FileStore::Structure.home
path = File.join('2012', '12', '12', '2012-12-12T200000+0100_120_gschach9schlimmers.192k_2.mp3')
proc = AudioProcessor::Ffmpeg.new('foo')
AudioProcessor.expects(:new).with(file2.absolute_path).returns(proc)
proc
.expects(:transcode)
.with(is_a(String), AudioFormat.new('mp3', 192, 2))
FileUtils.expects(:mv).with(is_a(String), File.join(home, path))
downgrader.expects(:file_exists?).with(File.join(home, path)).at_least(1).returns(false, true, true)
[file1, file2, file3].each do |file|
downgrader.expects(:file_exists?).with(file.absolute_path).returns(true)
FileUtils.expects(:rm).with(file.absolute_path)
end
assert_difference('AudioFile.count', -2) do
[file1, file2, file3].shuffle.each { |file| downgrader.handle(file) }
end
lower = AudioFile.where(broadcast_id: b1.id, bitrate: 192, channels: 2).first
assert_equal path, lower.path
old_ids = [file1, file2, file3].map(&:id) - [lower.id]
assert_not AudioFile.exists?(id: old_ids)
end
test 'just deletes higher-bitrate file when lower-bitrate already exists' do
higher = audio_files(:info_april_best)
lower = audio_files(:info_april_high)
downgrader.expects(:file_exists?).with(lower.absolute_path).returns(true)
downgrader.expects(:file_exists?).with(higher.absolute_path).returns(true)
FileUtils.expects(:rm).with(higher.absolute_path)
AudioProcessor::Ffmpeg.any_instance.expects(:transcode).never
assert_difference('AudioFile.count', -1) do
downgrader.handle(higher)
end
assert_not AudioFile.exists?(id: higher.id)
end
test 're-creates lower-bitrate file even if database entry exists' do
higher = audio_files(:info_april_best)
lower = audio_files(:info_april_high)
home = FileStore::Structure.home
downgrader.expects(:file_exists?).with(lower.absolute_path).returns(false)
downgrader.expects(:file_exists?).with(higher.absolute_path).returns(false)
AudioProcessor::Ffmpeg.any_instance
.expects(:transcode)
.with(is_a(String), AudioFormat.new('mp3', 192, 2))
FileUtils.expects(:mv).with(is_a(String), File.join(home, lower.path))
assert_difference('AudioFile.count', -1) do
downgrader.handle(higher)
end
assert_not AudioFile.exists?(id: higher.id)
end
test 're-creates database entry if lower-bitrate file exists' do
higher = audio_files(:info_april_best)
lower = audio_files(:info_april_high)
downgrader.expects(:file_exists?).with(lower.absolute_path).returns(true)
downgrader.expects(:file_exists?).with(higher.absolute_path).returns(false)
AudioProcessor::Ffmpeg.any_instance.expects(:transcode).never
lower.destroy!
assert_no_difference('AudioFile.count') do
downgrader.handle(higher)
end
assert_not AudioFile.exists?(id: higher.id)
assert AudioFile.exists?(broadcast_id: lower.broadcast_id,
codec: 'mp3',
bitrate: 192,
channels: 2)
end
test 'finds files with higher bitrate' do
b1 = Broadcast.create!(show: shows(:g9s),
started_at: Time.zone.local(2012, 12, 12, 20),
finished_at: Time.zone.local(2012, 12, 12, 22))
lower = AudioFile.create!(broadcast: b1,
path: 'dummy_lower',
codec: 'mp3',
bitrate: 128,
channels: 1)
same = AudioFile.create!(broadcast: b1,
path: 'dummy_same',
codec: 'mp3',
bitrate: 192,
channels: 2)
higher = AudioFile.create!(broadcast: b1,
path: 'dummy_higher',
codec: 'mp3',
bitrate: 224,
channels: 2)
higher_less_channels = AudioFile.create!(broadcast: b1,
path: 'dummy_higher_less',
codec: 'mp3',
bitrate: 256,
channels: 1)
start = Time.zone.now - action.months.months + 1.day
b2 = Broadcast.create!(show: shows(:g9s),
started_at: start,
finished_at: start + 2.hours)
newer = AudioFile.create!(broadcast: b2,
path: 'dummy_newer',
codec: 'mp3',
bitrate: 224,
channels: 2)
b3 = Broadcast.create!(show: shows(:klangbecken),
started_at: Time.zone.local(2012, 12, 12, 22),
finished_at: Time.zone.local(2012, 12, 13, 8))
different = AudioFile.create!(broadcast: b3,
path: 'dummy_different_profile',
codec: 'mp3',
bitrate: 224,
channels: 2)
assert_equal [higher, higher_less_channels].to_set, downgrader.pending_files.to_set
end
test 'finds files with higher bitrate with lower channel action' do
b1 = Broadcast.create!(show: shows(:g9s),
started_at: Time.zone.local(2012, 12, 12, 20),
finished_at: Time.zone.local(2012, 12, 12, 22))
lower = AudioFile.create!(broadcast: b1,
path: 'dummy_lower',
codec: 'mp3',
bitrate: 128,
channels: 2)
same = AudioFile.create!(broadcast: b1,
path: 'dummy_same',
codec: 'mp3',
bitrate: 192,
channels: 2)
higher = AudioFile.create!(broadcast: b1,
path: 'dummy_higher',
codec: 'mp3',
bitrate: 224,
channels: 2)
downgrade_actions(:default_mp3_2).destroy!
action.update!(channels: 1)
assert_equal [higher, same, audio_files(:g9s_mai_high)].to_set, downgrader.pending_files.to_set
end
test 'highest is true if other has higher bitrate but less channels' do
file = create_audio(bitrate: 224, channels: 2)
other = create_audio(bitrate: 320, channels: 1)
assert downgrader.highest?(file)
assert_not downgrader.highest?(other)
end
test 'highest is false if other has higher bitrate and same channels' do
file = create_audio(bitrate: 224, channels: 2)
other = create_audio(bitrate: 320, channels: 2)
assert_not downgrader.highest?(file)
assert downgrader.highest?(other)
end
test 'highest is false if other has same bitrate and more channels' do
file = create_audio(bitrate: 224, channels: 1)
other = create_audio(bitrate: 224, channels: 2)
assert_not downgrader.highest?(file)
assert downgrader.highest?(other)
end
test 'highest is true if other has bitrate below action but more channels' do
file = create_audio(bitrate: 320, channels: 1)
other = create_audio(bitrate: 128, channels: 2)
assert downgrader.highest?(file)
# no assert for other because it would not be in pending_files
end
test 'highest is true if other has lower bitrate and both have less channels than action' do
file = create_audio(bitrate: 320, channels: 1)
other = create_audio(bitrate: 224, channels: 1)
assert downgrader.highest?(file)
assert_not downgrader.highest?(other)
end
test 'highest is true if other has lower bitrate or less channels' do
file = create_audio(bitrate: 256, channels: 2)
other1 = create_audio(bitrate: 320, channels: 1)
other2 = create_audio(bitrate: 224, channels: 2)
assert downgrader.highest?(file)
assert_not downgrader.highest?(other1)
assert_not downgrader.highest?(other2)
end
test 'highest is true if other has lower bitrate and more channels than action' do
downgrade_actions(:default_mp3_2).destroy!
action.update!(channels: 1)
file = create_audio(bitrate: 320, channels: 1)
other = create_audio(bitrate: 224, channels: 2)
assert downgrader.highest?(file)
assert_not downgrader.highest?(other)
end
test 'highest is true if other has lower bitrate and both have more channels than action' do
downgrade_actions(:default_mp3_2).destroy!
action.update!(channels: 1)
file = create_audio(bitrate: 320, channels: 2)
other = create_audio(bitrate: 224, channels: 2)
assert downgrader.highest?(file)
assert_not downgrader.highest?(other)
end
test 'highest is true if other has lower bitrate and both have same channels as action' do
downgrade_actions(:default_mp3_2).destroy!
action.update!(channels: 1)
file = create_audio(bitrate: 320, channels: 1)
other = create_audio(bitrate: 224, channels: 1)
assert downgrader.highest?(file)
assert_not downgrader.highest?(other)
end
test 'highest is true if other has lower bitrate' do
downgrade_actions(:default_mp3_2).destroy!
action.update!(channels: 1)
file = create_audio(bitrate: 320, channels: 1)
other1 = create_audio(bitrate: 256, channels: 2)
other2 = create_audio(bitrate: 224, channels: 1)
assert downgrader.highest?(file)
assert_not downgrader.highest?(other1)
assert_not downgrader.highest?(other2)
end
def downgrader
@downgrader ||= Downgrade::Downgrader.new(action)
end
def action
downgrade_actions(:default_mp3_1)
end
def create_audio(attrs)
AudioFile.create!(attrs.reverse_merge(
broadcast: broadcasts(:g9s_juni),
path: "dummy-#{rand(9_999_999_999)}",
codec: 'mp3',
bitrate: 320,
channels: 2
))
end
end
end