-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathextracted_image_spec.rb
More file actions
125 lines (96 loc) · 4.2 KB
/
extracted_image_spec.rb
File metadata and controls
125 lines (96 loc) · 4.2 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
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
# frozen_string_literal: true
require 'mindee'
require 'pathname'
require 'fileutils'
require_relative '../data'
describe 'Mindee::Image::ExtractedImage', :all_deps do
require 'mini_magick' if Mindee::Dependencies.all_deps_available?
# Workaround for mindee-lite
if Mindee::Dependencies.all_deps_available?
let(:described_class) do
Mindee::Image::ExtractedImage
end
end
let(:file_path) do
File.join(V1_DATA_DIR, 'products', 'invoices', 'default_sample.jpg')
end
let(:input_source) do
Mindee::Input::Source::PathInputSource.new(file_path)
end
let(:page_id) { 1 }
let(:element_id) { 42 }
let(:output_dir) { "#{ROOT_DATA_DIR}/output" }
describe '#initialize' do
it 'initializes with correct attributes' do
extracted_image = described_class.new(input_source, page_id, element_id)
expect(extracted_image.page_id).to eq(page_id)
expect(extracted_image.element_id).to eq(element_id)
expect(extracted_image.filename).to eq('default_sample_p1_42.jpg')
# NOTE: ruby messes up the formatting of binary strings, I don't think it worth it to correct this behavior, but
# the result is that we have to remove them from the comparisons.
input_source.io_stream.rewind
source_content = extracted_image.buffer.read.gsub("\r", '').gsub("\n", '')
input_content = input_source.io_stream.read.gsub("\r", '').gsub("\n", '')
expect(source_content).to eq(input_content)
input_source.io_stream.rewind
end
it 'defaults element_id to 0 if nil is provided' do
extracted_image = described_class.new(input_source, page_id, nil)
expect(extracted_image.element_id).to eq(0)
end
it 'appends .jpg extension for PDF input sources' do
allow(input_source).to receive(:pdf?).and_return(true)
extracted_image = described_class.new(input_source, page_id, element_id)
expect(extracted_image.filename).to eq('default_sample_p1_42.jpg')
end
end
describe '#write_to_file' do
it 'saves the buffer to a file with the correct format' do
extracted_image = described_class.new(input_source, page_id, element_id)
output_path = "#{output_dir}/output_test.jpg"
extracted_image.write_to_file(output_path)
expect(File.exist?(output_path)).to be true
expect(File.size(output_path)).to be > 0
end
it 'raises an error if file format is invalid' do
extracted_image = described_class.new(input_source, page_id, element_id)
invalid_output_path = "#{output_dir}/output_test"
expect do
extracted_image.write_to_file(invalid_output_path)
end.to raise_error(Mindee::Error::MindeeImageError, %r{Invalid file format})
end
it 'raises an error if the file cannot be saved' do
extracted_image = described_class.new(input_source, page_id, element_id)
invalid_output_path = '/invalid/path/output_test.jpg'
expect do
extracted_image.write_to_file(invalid_output_path)
end.to raise_error(Mindee::Error::MindeeImageError)
end
end
describe '#as_source' do
it 'returns a BytesInputSource with the correct content and filename' do
extracted_image = described_class.new(input_source, page_id, element_id)
source = extracted_image.as_source
expect(source).to be_a(Mindee::Input::Source::BytesInputSource)
expect(source.filename).to eq('default_sample_p1_42.jpg')
source.io_stream.rewind
input_source.io_stream.rewind
source_content = source.io_stream.read.gsub("\r", '').gsub("\n", '')
input_content = input_source.io_stream.read.gsub("\r", '').gsub("\n", '')
expect(source_content).to eq(input_content)
input_source.io_stream.rewind
end
it 'should raise an error when MiniMagick fails during save' do
allow(MiniMagick::Image).to receive(:read).and_raise(StandardError)
extracted_image = Mindee::Image::ExtractedImage.new(input_source, 1, 2)
Tempfile.create(['output', '.jpg']) do |tempfile|
expect do
extracted_image.write_to_file(tempfile.path, 'jpg')
end.to raise_error(Mindee::Error::MindeeImageError, %r{Could not save file})
end
end
after(:each) do
FileUtils.rm_f("#{output_dir}/output_test.jpg")
end
end
end