-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimage_utils_spec.rb
More file actions
76 lines (63 loc) · 2.94 KB
/
image_utils_spec.rb
File metadata and controls
76 lines (63 loc) · 2.94 KB
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
# frozen_string_literal: true
# spec/image_utils_spec.rb
require 'mini_magick'
require 'stringio'
require 'mindee'
describe Mindee::Image::ImageUtils do
let(:sample_image_path) { "#{FILE_TYPES_DIR}/receipt.jpg" }
let(:sample_image) { MiniMagick::Image.open(sample_image_path) }
describe 'Image utility module' do
it 'Should convert StringIO to MiniMagick::Image' do
string_io = StringIO.new(File.read(sample_image_path))
result = Mindee::Image::ImageUtils.to_image(string_io)
expect(result).to be_a(MiniMagick::Image)
end
it 'Should return the same MiniMagick::Image object if passed as input' do
result = Mindee::Image::ImageUtils.to_image(sample_image)
expect(result).to eq(sample_image)
end
it 'Should raise an error for invalid input types' do
expect do
Mindee::Image::ImageUtils.to_image(123)
end.to raise_error(Mindee::Errors::MindeeImageError, %r{Expected an I/O object or a MiniMagick::Image})
end
it 'Should convert MiniMagick image to StringIO' do
result = Mindee::Image::ImageUtils.image_to_stringio(sample_image)
expect(result).to be_a(StringIO)
end
it 'Should set the format of the image correctly' do
result = Mindee::Image::ImageUtils.image_to_stringio(sample_image, 'PNG')
expect(result.string[1..3]).to eq('PNG')
end
it 'Should return original dimensions if no max_width or max_height is provided' do
result = Mindee::Image::ImageUtils.calculate_new_dimensions(sample_image)
expect(result).to eq([sample_image.width, sample_image.height])
end
it 'Should calculate new dimensions based on max_width and max_height' do
result = Mindee::Image::ImageUtils.calculate_new_dimensions(sample_image, max_width: 100, max_height: 100)
expect(result[0]).to be <= 100
expect(result[1]).to be <= 100
end
it 'Should raise an error if the original image is nil' do
expect do
Mindee::Image::ImageUtils.calculate_new_dimensions(nil)
end.to raise_error(Mindee::Errors::MindeeImageError, %r{Provided image could not be processed for resizing})
end
it 'Should return dimensions from media box if provided' do
media_box = [0, 0, 300, 400]
result = Mindee::Image::ImageUtils.calculate_dimensions_from_media_box(sample_image, media_box)
expect(result).to eq([300, 400])
end
it 'Should fall back to image dimensions if media box is nil or empty' do
result = Mindee::Image::ImageUtils.calculate_dimensions_from_media_box(sample_image, nil)
expect(result).to eq([sample_image.width.to_i, sample_image.height.to_i])
end
it 'Should raise an error if the PDF stream is invalid' do
invalid_pdf_stream = StringIO.new('invalid data')
# Adjust based on actual error raised by MiniMagick for invalid data.
expect do
Mindee::Image::ImageUtils.pdf_to_magick_image(invalid_pdf_stream, 75)
end.to raise_error(MiniMagick::Error)
end
end
end