|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'mindee/input/local_response' |
| 4 | +require_relative '../../data' |
| 5 | + |
| 6 | +def assert_local_response(local_response) |
| 7 | + dummy_secret_key = 'ogNjY44MhvKPGTtVsI8zG82JqWQa68woYQH' |
| 8 | + signature = 'b82a515c832fd2c4f4ce3a7e6f53c12e8d10e19223f6cf0e3a9809a7a3da26be' |
| 9 | + expect(local_response.file).to_not be(nil) |
| 10 | + expect(local_response.valid_hmac_signature?( |
| 11 | + dummy_secret_key, 'invalid signature' |
| 12 | + )).to be(false) |
| 13 | + expect(local_response.get_hmac_signature(dummy_secret_key)).to eq(signature) |
| 14 | + inference_response = local_response.deserialize_response(Mindee::Parsing::V2::InferenceResponse) |
| 15 | + expect(inference_response).to be_a(Mindee::Parsing::V2::InferenceResponse) |
| 16 | + expect(inference_response).not_to be_nil |
| 17 | + expect(inference_response.inference).not_to be_nil |
| 18 | +end |
| 19 | + |
| 20 | +describe Mindee::Input::LocalResponse do |
| 21 | + let(:file_path) { File.join(V2_DATA_DIR, 'inference', 'standard_field_types.json') } |
| 22 | + context 'A V2 local response' do |
| 23 | + it 'should load from a path' do |
| 24 | + response = Mindee::Input::LocalResponse.new(file_path) |
| 25 | + assert_local_response(response) |
| 26 | + end |
| 27 | + |
| 28 | + it 'should load from a string' do |
| 29 | + str_file = File.read(file_path) |
| 30 | + response = Mindee::Input::LocalResponse.new(str_file) |
| 31 | + assert_local_response(response) |
| 32 | + end |
| 33 | + |
| 34 | + it 'should load from a StringIO' do |
| 35 | + strio_file = StringIO.new(File.read(file_path)) |
| 36 | + response = Mindee::Input::LocalResponse.new(strio_file) |
| 37 | + assert_local_response(response) |
| 38 | + end |
| 39 | + |
| 40 | + it 'should load from a file-like object' do |
| 41 | + str_file = File.read(file_path) |
| 42 | + Tempfile.open do |tempfile| |
| 43 | + tempfile.write(str_file) |
| 44 | + tempfile.rewind |
| 45 | + response = Mindee::Input::LocalResponse.new(tempfile) |
| 46 | + assert_local_response(response) |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + it 'should trigger an error when something invalid is passed' do |
| 51 | + expect do |
| 52 | + Mindee::Input::LocalResponse.new(123) |
| 53 | + end.to raise_error Mindee::Errors::MindeeInputError |
| 54 | + end |
| 55 | + |
| 56 | + it 'should trigger an error when the payload is not hashable' do |
| 57 | + local_response = Mindee::Input::LocalResponse.new('Your mother was a hamster.') |
| 58 | + expect do |
| 59 | + local_response.as_hash |
| 60 | + end.to raise_error Mindee::Errors::MindeeInputError |
| 61 | + end |
| 62 | + end |
| 63 | +end |
0 commit comments