Last active
November 14, 2016 07:55
-
-
Save michels/59c81bea125b0f155b4325c58b39b36b to your computer and use it in GitHub Desktop.
Spec "VoiceToText"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe "VoiceToText" do | |
let(:valid_path_to_voice_file) { "./fixtures/voice-example.au" } | |
let(:valid_path_to_empty_voice_file) { "./fixtures/voice-example-empty.au" } | |
let(:soundfile) { | |
Soundfile.create( | |
:url => "uploader.url", | |
:description => "description", | |
:apikey => "apikey", | |
:project => "project", | |
:size => 100) } | |
let(:recognize_double) { double(:recognize_double, transcript: "Saved transcript") } | |
let(:audio_double) { double(:audio_double, recognize: recognize_double) } | |
before do | |
allow(Google::Cloud::Speech).to receive(:new).and_return(audio_double) | |
allow(Google::Cloud::Speech.new).to receive(:audio).and_return(audio_double) | |
allow(recognize_double).to receive(:empty?).and_return(false) | |
allow(recognize_double).to receive(:first).and_return(recognize_double) | |
end | |
context "when voice file has content" do | |
before do | |
@v = VoiceToText.getAndSave(valid_path_to_voice_file, soundfile.id) | |
@s = Soundfile.find_by(id: soundfile.id) | |
end | |
it "saves transcript to database" do | |
expect(@s.transcript).to eq("Saved transcript") | |
end | |
it "sets status = true to show that voice to text was done" do | |
expect(@s.transcribed).to be true | |
end | |
it "getAndSave returns true" do | |
expect(@v).to be true | |
end | |
end | |
context "when voice file has no content" do | |
before do | |
allow(recognize_double).to receive(:empty?).and_return(true) | |
@v = VoiceToText.getAndSave(valid_path_to_empty_voice_file, soundfile.id) | |
@s = Soundfile.find_by(id: soundfile.id) | |
end | |
it "saves empty transcript to database" do | |
expect(@s.transcript).to eq("") | |
end | |
it "sets status = true to show that voice to text was done" do | |
expect(@s.transcribed).to be true | |
end | |
it "getAndSave returns true" do | |
expect(@v).to be true | |
end | |
end | |
context "when google is down" do | |
it "keeps status = false to show that voice to text was not done" do | |
allow(Google::Cloud::Speech).to receive(:new).and_raise(Google::Cloud::UnavailableError) | |
VoiceToText.getAndSave(valid_path_to_voice_file, soundfile.id) | |
s = Soundfile.find_by(id: soundfile.id) | |
expect(s.transcribed).to be false | |
end | |
end | |
context "when Soundfile.id does not exist" do | |
it "returns false to show that an error occurred" do | |
v = VoiceToText.getAndSave(valid_path_to_voice_file, "11112222233333") #random id | |
expect(v).to be false | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment