Skip to content

Commit

Permalink
🐛 Ne renvoie pas d'erreur quand il n'y a pas de transcription pour in…
Browse files Browse the repository at this point in the history
…titule ni écrit ni audio
  • Loading branch information
marouria committed Sep 17, 2024
1 parent b556153 commit b9205b4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 31 deletions.
16 changes: 6 additions & 10 deletions app/models/question_glisser_deposer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,16 @@ def base_json
end

def audio_fields
fields = { 'audio_url' => audio_principal }
if transcription_intitule&.ecrit.blank? && transcription_intitule&.audio_url
fields['intitule_audio'] = transcription_intitule&.audio_url
fields = {}
if transcription_intitule&.complete?
fields['audio_url'] = transcription_intitule.audio_url
elsif transcription_modalite_reponse&.complete?
fields['audio_url'] = transcription_modalite_reponse&.audio_url
fields['intitule_url'] = transcription_intitule&.audio_url
end
fields
end

def audio_principal
return unless transcription_intitule.complete? || transcription_modalite_reponse.complete?
return transcription_intitule.audio_url if transcription_intitule.complete?

transcription_modalite_reponse.audio_url
end

def reponses_fields
reponses_non_classees = reponses.to_a.shuffle.map do |reponse|
illustration_url = cdn_for(reponse.illustration) if reponse.illustration.attached?
Expand Down
80 changes: 59 additions & 21 deletions spec/models/question_glisser_deposer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,76 @@
describe QuestionGlisserDeposer, type: :model do
it { is_expected.to have_many(:reponses).with_foreign_key(:question_id) }

describe '#as_json' do
let(:question) do
create(:question_glisser_deposer,
illustration: Rack::Test::UploadedFile.new(
Rails.root.join('spec/support/programme_tele.png')
))
end
let!(:modalite) do
create(:transcription, :avec_audio, question_id: question.id,
categorie: :modalite_reponse)
end
let!(:reponse1) { create(:choix, :avec_illustration, :bon, question_id: question.id) }
let!(:reponse2) { create(:choix, :avec_illustration, :bon, question_id: question.id) }
let(:json) { question.as_json }
let(:question) do
create(:question_glisser_deposer,
illustration: Rack::Test::UploadedFile.new(
Rails.root.join('spec/support/programme_tele.png')
))
end
let!(:reponse1) { create(:choix, :avec_illustration, :bon, question_id: question.id) }
let!(:reponse2) { create(:choix, :avec_illustration, :bon, question_id: question.id) }
let!(:modalite) do
create(:transcription, :avec_audio, question_id: question.id,
categorie: :modalite_reponse)
end
let(:json) { question.as_json }

describe '#as_json' do
it 'serialise les champs' do
intitule = create(:transcription, :avec_audio, question_id: question.id,
ecrit: 'Mon Intitulé')
expect(json.keys).to match_array(%w[id intitule audio_url nom_technique
description illustration modalite_reponse type
reponsesNonClassees])
reponsesNonClassees intitule_url])
expect(json['type']).to eql('glisser-deposer-billets')
expect(json['intitule']).to eql('Mon Intitulé')
expect(json['modalite_reponse']).to eql(modalite.ecrit)
expect(json['illustration']).to eql(Rails.application.routes.url_helpers.url_for(
question.illustration
))
expect(json['audio_url']).to eql(Rails.application.routes.url_helpers.url_for(
intitule.audio
))
expect(json['reponsesNonClassees'].size).to eql(2)
expect(json['reponsesNonClassees'].first['illustration']).to_not be(nil)
end
end

describe 'les audios' do
context 'quand il y a une modalité de réponse compléte' do
context 'avec un intitulé complet' do
let!(:intitule) do
create(:transcription, :avec_audio, question_id: question.id,
ecrit: 'Mon Intitulé')
end
it do
expect(json['audio_url']).to eql(Rails.application.routes.url_helpers.url_for(
intitule.audio
))
expect(json['intitule']).to eql('Mon Intitulé')
expect(json['intitule_url']).to eql(nil)
end
end

context 'sans intitulé' do
it do
expect(json['audio_url']).to eql(Rails.application.routes.url_helpers.url_for(
modalite.audio
))
expect(json['intitule']).to eql(nil)
expect(json['intitule_url']).to eql(nil)
end
end

context 'avec un intitulé incomplet' do
let!(:intitule) do
create(:transcription, :avec_audio, question_id: question.id,
ecrit: nil)
end
it do
expect(json['audio_url']).to eql(Rails.application.routes.url_helpers.url_for(
modalite.audio
))
expect(json['intitule']).to eql(nil)
expect(json['intitule_url']).to eql(Rails.application.routes.url_helpers.url_for(
intitule.audio
))
end
end
end
end
end

0 comments on commit b9205b4

Please sign in to comment.