spec/api/deepl_spec.rb in deepl-rb-2.5.3 vs spec/api/deepl_spec.rb in deepl-rb-3.0.0

- old
+ new

@@ -1,42 +1,57 @@ +# Copyright 2018 Daniel Herzog +# Use of this source code is governed by an MIT +# license that can be found in the LICENSE.md file. # frozen_string_literal: true require 'spec_helper' +require 'tempfile' describe DeepL do - subject { DeepL.dup } + subject(:deepl) { described_class.dup } + around do |tests| + tmp_env = replace_env_preserving_deepl_vars_except_mock_server + tests.call + ENV.replace(tmp_env) + end + describe '#configure' do - context 'When providing no block' do + context 'when providing no block' do let(:configuration) { DeepL::Configuration.new } - before { subject.configure } - it 'should use default configuration' do - expect(subject.configuration).to eq(configuration) + before do + deepl.configure end + + it 'uses default configuration' do + expect(deepl.configuration).to eq(configuration) + end end - context 'When providing a valid configuration' do + context 'when providing a valid configuration' do let(:configuration) do - DeepL::Configuration.new(auth_key: 'VALID', host: 'http://www.example.org', version: 'v1') + DeepL::Configuration.new({ auth_key: 'VALID', host: 'http://www.example.org', + version: 'v1' }) end + before do - subject.configure do |config| + deepl.configure do |config| config.auth_key = configuration.auth_key config.host = configuration.host config.version = configuration.version end end - it 'should use the provided configuration' do - expect(subject.configuration).to eq(configuration) + it 'uses the provided configuration' do + expect(deepl.configuration).to eq(configuration) end end - context 'When providing an invalid configuration' do - it 'should raise an error' do - expect { subject.configure { |c| c.auth_key = '' } } + context 'when providing an invalid configuration' do + it 'raises an error' do + expect { deepl.configure { |c| c.auth_key = '' } } .to raise_error(DeepL::Exceptions::Error) end end end @@ -45,80 +60,203 @@ let(:source_lang) { 'EN' } let(:target_lang) { 'ES' } let(:options) { { param: 'fake' } } around do |example| - subject.configure { |config| config.host = 'https://api-free.deepl.com' } + deepl.configure VCR.use_cassette('deepl_translate') { example.call } end - context 'When translating a text' do - it 'should create and call a request object' do + context 'when translating a text' do + it 'creates and call a request object' do expect(DeepL::Requests::Translate).to receive(:new) - .with(subject.api, input, source_lang, target_lang, options).and_call_original + .with(deepl.api, input, source_lang, target_lang, options).and_call_original - text = subject.translate(input, source_lang, target_lang, options) + text = deepl.translate(input, source_lang, target_lang, options) expect(text).to be_a(DeepL::Resources::Text) end end - context 'When translating a text using a glossary' do - before(:each) do - @glossary = subject.glossaries.create('fixture', 'EN', 'ES', [%w[car auto]]) + context 'when translating a text using a glossary' do + before do + @glossary = deepl.glossaries.create('fixture', 'EN', 'ES', [%w[car auto]]) end + let(:input) { 'I wish we had a car.' } + # rubocop:disable RSpec/InstanceVariable let(:options) { { glossary_id: @glossary.id } } - it 'should create and call a request object' do + after do + deepl.glossaries.destroy(@glossary.id) + end + # rubocop:enable RSpec/InstanceVariable + + it 'creates and call a request object' do expect(DeepL::Requests::Translate).to receive(:new) - .with(subject.api, input, source_lang, target_lang, options).and_call_original - text = subject.translate(input, source_lang, target_lang, options) + .with(deepl.api, input, source_lang, target_lang, options).and_call_original + text = deepl.translate(input, source_lang, target_lang, options) expect(text).to be_a(DeepL::Resources::Text) - expect(text.text).to eq('Ojalá tuviéramos un auto.') + expect(text.text).to eq('Ojalá tuviéramos auto.') end - - after(:each) do - subject.glossaries.destroy(@glossary.id) - end end end describe '#usage' do let(:options) { {} } around do |example| - subject.configure { |config| config.host = 'https://api-free.deepl.com' } + deepl.configure VCR.use_cassette('deepl_usage') { example.call } end - context 'When checking usage' do - it 'should create and call a request object' do + context 'when checking usage' do + it 'creates and call a request object' do expect(DeepL::Requests::Usage).to receive(:new) - .with(subject.api, options).and_call_original + .with(deepl.api, options).and_call_original - usage = subject.usage(options) + usage = deepl.usage(options) expect(usage).to be_a(DeepL::Resources::Usage) end end end + # rubocop:disable RSpec/InstanceVariable + describe '#document' do + describe '#document.upload' do + before do + @tmpfile = Tempfile.new('foo') + @tmpfile.write("Geology for Beginners Report + A test report for the DeepL API + It is with great pleasure, that I, Urna Semper, write this fake document on geology. + Geology is an excellent field that deals with how to extract oil from the earth.") + @tmpfile.close + end + + after do + @tmpfile.unlink + end + + let(:input_file) { @tmpfile.path } + let(:source_lang) { 'EN' } + let(:target_lang) { 'ES' } + let(:output_file) { 'test_translated_doc.txt' } + let(:options) { { param: 'fake' } } + let(:additional_headers) { { 'Fake-Header': 'fake_value' } } + + around do |example| + deepl.configure + VCR.use_cassette('deepl_document') { example.call } + end + + context 'when uploading a document' do + it 'creates an upload object' do + expect(DeepL::Requests::Document::Upload).to receive(:new) + .with(deepl.api, input_file, source_lang, target_lang, + "#{File.basename(@tmpfile.path)}.txt", options, + additional_headers).and_call_original + doc_handle = deepl.document.upload(input_file, source_lang, target_lang, + "#{File.basename(@tmpfile.path)}.txt", options, + additional_headers) + expect(doc_handle).to be_a(DeepL::Resources::DocumentHandle) + end + end + end + # rubocop:enable RSpec/InstanceVariable + + describe '#document.get_status' do + let(:document_handle) do + DeepL::Resources::DocumentHandle.new('9B7CB9418DCDEBF2C4C519F65A32B99F', + 'EA637EA43BB3F8A52A2A25B76EF3E0C72CE9CD00C881148D1236CB584CB34815', # rubocop:disable Layout/LineLength + nil, + nil) + end + let(:options) { { param: 'fake' } } + let(:additional_headers) { { 'Fake-Header': 'fake_value' } } + + around do |example| + deepl.configure + VCR.use_cassette('deepl_document') { example.call } + end + + context 'when querying the status of a document' do + it 'creates a GetStatus object' do + expect(DeepL::Requests::Document::GetStatus).to receive(:new) + .with( + deepl.api, + document_handle.document_id, + document_handle.document_key, + options, + additional_headers + ).and_call_original + status = deepl.document.get_status(document_handle, options, additional_headers) + expect(status).to be_a(DeepL::Resources::DocumentTranslationStatus) + end + end + end + + # rubocop:disable RSpec/InstanceVariable + describe '#document.download' do + before do + @tmpfile = Tempfile.new('bar') + end + + after do + @tmpfile.close + @tmpfile.unlink + end + + let(:document_handle) do + DeepL::Resources::DocumentHandle.new('9B7CB9418DCDEBF2C4C519F65A32B99F', + 'EA637EA43BB3F8A52A2A25B76EF3E0C72CE9CD00C881148D1236CB584CB34815', # rubocop:disable Layout/LineLength + nil, + nil) + end + let(:output_file_path) { @tmpfile.path } + let(:options) { { param: 'fake' } } + + around do |example| + deepl.configure + VCR.use_cassette('deepl_document_download', preserve_exact_body_bytes: true) do + example.call + end + end + + context 'when downloading a document' do + it 'creates an downloading object and writes to the output file' do # rubocop:disable RSpec/ExampleLength + expect(DeepL::Requests::Document::Download).to receive(:new) + .with( + deepl.api, + document_handle.document_id, + document_handle.document_key, + output_file_path + ).and_call_original + deepl.document.download(document_handle, output_file_path) + file_contents = File.read(output_file_path) + expect(file_contents).to be_a(String) + expect(file_contents.length).to be > 200 + end + end + end + end + # rubocop:enable RSpec/InstanceVariable + describe '#languages' do let(:options) { { type: :target } } around do |example| - subject.configure { |config| config.host = 'https://api-free.deepl.com' } + deepl.configure VCR.use_cassette('deepl_languages') { example.call } end - context 'When checking languages' do - it 'should create and call a request object' do + context 'when checking languages' do + it 'creates and call a request object' do expect(DeepL::Requests::Languages).to receive(:new) - .with(subject.api, options).and_call_original + .with(deepl.api, options).and_call_original - languages = subject.languages(options) + languages = deepl.languages(options) expect(languages).to be_an(Array) - expect(languages.all? { |l| l.is_a?(DeepL::Resources::Language) }).to be_truthy + expect(languages).to(be_all { |l| l.is_a?(DeepL::Resources::Language) }) end end end describe '#glossaries' do @@ -133,158 +271,159 @@ ] end let(:options) { { param: 'fake', entries_format: 'tsv' } } around do |example| - subject.configure { |config| config.host = 'https://api-free.deepl.com' } + deepl.configure VCR.use_cassette('deepl_glossaries') { example.call } end - context 'When creating a glossary' do - it 'should create and call a request object' do + context 'when creating a glossary' do + it 'creates and call a request object' do expect(DeepL::Requests::Glossary::Create).to receive(:new) - .with(subject.api, name, source_lang, target_lang, entries, options).and_call_original + .with(deepl.api, name, source_lang, target_lang, entries, options).and_call_original - glossary = subject.glossaries.create(name, source_lang, target_lang, entries, options) + glossary = deepl.glossaries.create(name, source_lang, target_lang, entries, options) expect(glossary).to be_a(DeepL::Resources::Glossary) end end end describe '#glossaries.find' do - let(:id) { 'd9ad833f-c818-430c-a3c9-47071384fa3e' } + let(:id) { 'e7a62637-7ef4-4959-a355-09ba61dd0126' } let(:options) { {} } around do |example| - subject.configure { |config| config.host = 'https://api-free.deepl.com' } + deepl.configure VCR.use_cassette('deepl_glossaries') { example.call } end - context 'When fetching a glossary' do - it 'should create and call a request object' do + context 'when fetching a glossary' do + it 'creates and call a request object' do expect(DeepL::Requests::Glossary::Find).to receive(:new) - .with(subject.api, id, options).and_call_original + .with(deepl.api, id, options).and_call_original - glossary = subject.glossaries.find(id, options) + glossary = deepl.glossaries.find(id, options) expect(glossary).to be_a(DeepL::Resources::Glossary) end end - context 'When fetching a non existing glossary' do + context 'when fetching a non existing glossary' do let(:id) { '00000000-0000-0000-0000-000000000000' } - it 'should raise an exception when the glossary does not exist' do + it 'raises an exception when the glossary does not exist' do expect(DeepL::Requests::Glossary::Find).to receive(:new) - .with(subject.api, id, options).and_call_original - expect { subject.glossaries.find(id, options) } + .with(deepl.api, id, options).and_call_original + expect { deepl.glossaries.find(id, options) } .to raise_error(DeepL::Exceptions::NotFound) end end end describe '#glossaries.list' do let(:options) { {} } around do |example| - subject.configure { |config| config.host = 'https://api-free.deepl.com' } + deepl.configure VCR.use_cassette('deepl_glossaries') { example.call } end - context 'When fetching glossaries' do - it 'should create and call a request object' do + context 'when fetching glossaries' do + it 'creates and call a request object' do expect(DeepL::Requests::Glossary::List).to receive(:new) - .with(subject.api, options).and_call_original + .with(deepl.api, options).and_call_original - glossaries = subject.glossaries.list(options) + glossaries = deepl.glossaries.list(options) expect(glossaries).to all(be_a(DeepL::Resources::Glossary)) end end end describe '#glossaries.destroy' do - let(:id) { 'd9ad833f-c818-430c-a3c9-47071384fa3e' } + let(:id) { 'e7a62637-7ef4-4959-a355-09ba61dd0126' } let(:options) { {} } around do |example| - subject.configure { |config| config.host = 'https://api-free.deepl.com' } + deepl.configure VCR.use_cassette('deepl_glossaries') { example.call } end - context 'When destroy a glossary' do + context 'when destroy a glossary' do let(:new_glossary) do - subject.glossaries.create('fixture', 'EN', 'ES', [%w[Hello Hola]]) + deepl.glossaries.create('fixture', 'EN', 'ES', [%w[Hello Hola]]) end - it 'should create and call a request object' do + + it 'creates and call a request object' do expect(DeepL::Requests::Glossary::Destroy).to receive(:new) - .with(subject.api, new_glossary.id, options).and_call_original + .with(deepl.api, new_glossary.id, options).and_call_original - glossary_id = subject.glossaries.destroy(new_glossary.id, options) + glossary_id = deepl.glossaries.destroy(new_glossary.id, options) expect(glossary_id).to eq(new_glossary.id) end end - context 'When destroying a non existing glossary' do + context 'when destroying a non existing glossary' do let(:id) { '00000000-0000-0000-0000-000000000000' } - it 'should raise an exception when the glossary does not exist' do + it 'raises an exception when the glossary does not exist' do expect(DeepL::Requests::Glossary::Destroy).to receive(:new) - .with(subject.api, id, options).and_call_original - expect { subject.glossaries.destroy(id, options) } + .with(deepl.api, id, options).and_call_original + expect { deepl.glossaries.destroy(id, options) } .to raise_error(DeepL::Exceptions::NotFound) end end end describe '#glossaries.entries' do - let(:id) { '012a5576-b551-4d4c-b917-ce01bc8debb6' } + let(:id) { 'e7a62637-7ef4-4959-a355-09ba61dd0126' } let(:options) { {} } around do |example| - subject.configure { |config| config.host = 'https://api-free.deepl.com' } + deepl.configure VCR.use_cassette('deepl_glossaries') { example.call } end - context 'When listing glossary entries' do - it 'should create and call a request object' do + context 'when listing glossary entries' do + it 'creates and call a request object' do expect(DeepL::Requests::Glossary::Entries).to receive(:new) - .with(subject.api, id, options).and_call_original + .with(deepl.api, id, options).and_call_original - entries = subject.glossaries.entries(id, options) + entries = deepl.glossaries.entries(id, options) expect(entries).to all(be_a(Array)) entries.each do |entry| expect(entry.size).to eq(2) expect(entry.first).to be_a(String) expect(entry.last).to be_a(String) end end end - context 'When listing entries of a non existing glossary' do + context 'when listing entries of a non existing glossary' do let(:id) { '00000000-0000-0000-0000-000000000000' } - it 'should raise an exception when the glossary does not exist' do + it 'raises an exception when the glossary does not exist' do expect(DeepL::Requests::Glossary::Entries).to receive(:new) - .with(subject.api, id, options).and_call_original - expect { subject.glossaries.entries(id, options) } + .with(deepl.api, id, options).and_call_original + expect { deepl.glossaries.entries(id, options) } .to raise_error(DeepL::Exceptions::NotFound) end end end describe '#glossaries.language_pairs' do let(:options) { {} } around do |example| - subject.configure { |config| config.host = 'https://api-free.deepl.com' } + deepl.configure VCR.use_cassette('deepl_glossaries') { example.call } end - context 'When fetching language pairs supported by glossaries' do - it 'should create and call a request object' do + context 'when fetching language pairs supported by glossaries' do + it 'creates and call a request object' do expect(DeepL::Requests::Glossary::LanguagePairs).to receive(:new) - .with(subject.api, options).and_call_original + .with(deepl.api, options).and_call_original - language_pairs = subject.glossaries.language_pairs(options) + language_pairs = deepl.glossaries.language_pairs(options) expect(language_pairs).to all(be_a(DeepL::Resources::LanguagePair)) end end end end