Skip to content

Commit

Permalink
✅ Refacto test
Browse files Browse the repository at this point in the history
  • Loading branch information
marouria committed Nov 6, 2024
1 parent 6925a44 commit c51f10b
Showing 1 changed file with 89 additions and 128 deletions.
217 changes: 89 additions & 128 deletions spec/models/export_question_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,96 +3,69 @@
require 'rails_helper'

describe ExportQuestion do
describe 'pour tous les types de questions' do
subject(:response_service) do
described_class.new(question_clic, headers)
end
subject(:response_service) { described_class.new(question, headers) }

let(:headers) { [] }
let(:question) { create(:question) }
let(:spreadsheet) { Spreadsheet.open(StringIO.new(response_service.to_xls)) }
let(:worksheet) { spreadsheet.worksheet(0) }

let(:question_clic) do
describe 'pour une question clic' do
let(:question) do
create(:question_clic_dans_image, description: 'Ceci est une description',
nom_technique: 'clic')
end
let!(:intitule) do
create(:transcription, :avec_audio, question_id: question_clic.id, categorie: :intitule,
create(:transcription, :avec_audio, question_id: question.id, categorie: :intitule,
ecrit: 'Ceci est un intitulé')
end
let!(:consigne) do
create(:transcription, :avec_audio, question_id: question_clic.id,
create(:transcription, :avec_audio, question_id: question.id,
categorie: :modalite_reponse,
ecrit: 'Ceci est une consigne')
end
let(:headers) do
ImportExportQuestion::HEADERS_ATTENDUS[question_clic.type]
end

describe '#to_xls' do
it 'génére un fichier xls avec les entêtes sur chaque colonnes' do
xls = response_service.to_xls
spreadsheet = Spreadsheet.open(StringIO.new(xls))
worksheet = spreadsheet.worksheet(0)

expect(spreadsheet.worksheets.count).to eq(1)
expect(worksheet.row(0)[0]).to eq('Libelle')
expect(worksheet.row(0)[1]).to eq('Nom technique')
expect(worksheet.row(0)[2]).to eq('Illustration')
expect(worksheet.row(0)[3]).to eq('Intitule ecrit')
expect(worksheet.row(0)[4]).to eq('Intitule audio')
expect(worksheet.row(0)[5]).to eq('Consigne ecrit')
expect(worksheet.row(0)[6]).to eq('Consigne audio')
expect(worksheet.row(0)[7]).to eq('Description')
expect(worksheet.row(0)[8]).to eq('Zone cliquable')
expect(worksheet.row(0)[9]).to eq('Image au clic')
end

it 'génére un fichier xls avec les détails de la question' do
xls = response_service.to_xls
spreadsheet = Spreadsheet.open(StringIO.new(xls))
worksheet = spreadsheet.worksheet(0)
question = worksheet.row(1)
expect(question[0]).to eq('Question clic dans image')
expect(question[1]).to eq('clic')
expect(question[2]).to be_nil
expect(question[3]).to eq('Ceci est un intitulé')
expect(question[4]).to eq(intitule.audio_url)
expect(question[5]).to eq('Ceci est une consigne')
expect(question[6]).to eq(consigne.audio_url)
expect(question[7]).to eq('Ceci est une description')
expect(question[8]).to eq(question_clic.zone_cliquable_url)
expect(question[9]).to eq(question_clic.image_au_clic_url)
end
end

describe '#nom_du_fichier' do
it 'genere le nom du fichier' do
date = DateTime.current.strftime('%Y%m%d')
nom_du_fichier_attendu = "#{date}-#{question_clic.nom_technique}.xls"

expect(response_service.nom_du_fichier).to eq(nom_du_fichier_attendu)
end
end

describe '#content_type_xls' do
it { expect(response_service.content_type_xls).to eq 'application/vnd.ms-excel' }
ImportExportQuestion::HEADERS_ATTENDUS[question.type]
end
end

describe 'pour une question glisser deposer' do
subject(:response_service) do
described_class.new(question_glisser_deposer, headers)
it 'génére un fichier xls avec les entêtes sur chaque colonnes' do
expect(spreadsheet.worksheets.count).to eq(1)
expect(worksheet.row(0)[0]).to eq('Libelle')
expect(worksheet.row(0)[1]).to eq('Nom technique')
expect(worksheet.row(0)[2]).to eq('Illustration')
expect(worksheet.row(0)[3]).to eq('Intitule ecrit')
expect(worksheet.row(0)[4]).to eq('Intitule audio')
expect(worksheet.row(0)[5]).to eq('Consigne ecrit')
expect(worksheet.row(0)[6]).to eq('Consigne audio')
expect(worksheet.row(0)[7]).to eq('Description')
expect(worksheet.row(0)[8]).to eq('Zone cliquable')
expect(worksheet.row(0)[9]).to eq('Image au clic')
end

it 'génére un fichier xls avec les détails de la question' do
ligne = worksheet.row(1)
expect(ligne[0]).to eq('Question clic dans image')
expect(ligne[1]).to eq('clic')
expect(ligne[2]).to be_nil
expect(ligne[3]).to eq('Ceci est un intitulé')
expect(ligne[4]).to eq(intitule.audio_url)
expect(ligne[5]).to eq('Ceci est une consigne')
expect(ligne[6]).to eq(consigne.audio_url)
expect(ligne[7]).to eq('Ceci est une description')
expect(ligne[8]).to eq(question.zone_cliquable_url)
expect(ligne[9]).to eq(question.image_au_clic_url)
end
end

let(:question_glisser_deposer) { create(:question_glisser_deposer) }
describe 'pour une question glisser deposer' do
let(:question) { create(:question_glisser_deposer) }
let(:headers) do
ImportExportQuestion::HEADERS_ATTENDUS[question_glisser_deposer.type]
ImportExportQuestion::HEADERS_ATTENDUS[question.type]
end
let!(:reponse) { create(:choix, :bon, question_id: question_glisser_deposer.id) }
let!(:reponse2) { create(:choix, :mauvais, question_id: question_glisser_deposer.id) }
let!(:reponse) { create(:choix, :bon, question_id: question.id) }
let!(:reponse2) { create(:choix, :mauvais, question_id: question.id) }

it 'génére un fichier xls avec les entêtes spécifiques' do
xls = response_service.to_xls
spreadsheet = Spreadsheet.open(StringIO.new(xls))
worksheet = spreadsheet.worksheet(0)

expect(spreadsheet.worksheets.count).to eq(1)
expect(worksheet.row(0)[8]).to eq('Zone depot')
expect(worksheet.row(0)[9]).to eq('reponse_1_nom_technique')
Expand All @@ -106,38 +79,27 @@
end

it 'génére un fichier xls avec les détails de la question' do
xls = response_service.to_xls
spreadsheet = Spreadsheet.open(StringIO.new(xls))
worksheet = spreadsheet.worksheet(0)
question = worksheet.row(1)
expect(question[8]).to eq(question_glisser_deposer.zone_depot_url)
expect(question[9]).to eq(reponse.nom_technique)
expect(question[10]).to eq(reponse.position_client)
expect(question[11]).to eq(reponse.type_choix)
expect(question[12]).to eq(reponse.illustration_url)
expect(question[13]).to eq(reponse2.nom_technique)
expect(question[14]).to eq(reponse2.position_client)
expect(question[15]).to eq(reponse2.type_choix)
expect(question[16]).to eq(reponse2.illustration_url)
ligne = worksheet.row(1)
expect(ligne[8]).to eq(question.zone_depot_url)
expect(ligne[9]).to eq(reponse.nom_technique)
expect(ligne[10]).to eq(reponse.position_client)
expect(ligne[11]).to eq(reponse.type_choix)
expect(ligne[12]).to eq(reponse.illustration_url)
expect(ligne[13]).to eq(reponse2.nom_technique)
expect(ligne[14]).to eq(reponse2.position_client)
expect(ligne[15]).to eq(reponse2.type_choix)
expect(ligne[16]).to eq(reponse2.illustration_url)
end
end

describe 'pour une question saisie' do
subject(:response_service) do
described_class.new(question_saisie, headers)
end

let(:question_saisie) { create(:question_saisie) }
let(:question) { create(:question_saisie) }
let(:headers) do
ImportExportQuestion::HEADERS_ATTENDUS[question_saisie.type]
ImportExportQuestion::HEADERS_ATTENDUS[question.type]
end
let!(:reponse) { create(:choix, :bon, question_id: question_saisie.id) }
let!(:reponse) { create(:choix, :bon, question_id: question.id) }

it 'génére un fichier xls avec les entêtes spécifiques' do
xls = response_service.to_xls
spreadsheet = Spreadsheet.open(StringIO.new(xls))
worksheet = spreadsheet.worksheet(0)

expect(spreadsheet.worksheets.count).to eq(1)
expect(worksheet.row(0)[8]).to eq('Suffix reponse')
expect(worksheet.row(0)[9]).to eq('Reponse placeholder')
Expand All @@ -147,35 +109,24 @@
end

it 'génére un fichier xls avec les détails de la question' do
xls = response_service.to_xls
spreadsheet = Spreadsheet.open(StringIO.new(xls))
worksheet = spreadsheet.worksheet(0)
question = worksheet.row(1)
expect(question[8]).to eq(question_saisie.suffix_reponse)
expect(question[9]).to eq(question_saisie.reponse_placeholder)
expect(question[10]).to eq(question_saisie.type_saisie)
expect(question[11]).to eq(question_saisie.bonne_reponse.intitule)
expect(question[12]).to eq(question_saisie.bonne_reponse.nom_technique)
ligne = worksheet.row(1)
expect(ligne[8]).to eq(question.suffix_reponse)
expect(ligne[9]).to eq(question.reponse_placeholder)
expect(ligne[10]).to eq(question.type_saisie)
expect(ligne[11]).to eq(question.bonne_reponse.intitule)
expect(ligne[12]).to eq(question.bonne_reponse.nom_technique)
end
end

describe 'pour une question qcm' do
subject(:response_service) do
described_class.new(question_qcm, headers)
end

let(:question_qcm) { create(:question_qcm) }
let(:question) { create(:question_qcm) }
let(:headers) do
ImportExportQuestion::HEADERS_ATTENDUS[question_qcm.type]
ImportExportQuestion::HEADERS_ATTENDUS[question.type]
end
let!(:reponse) { create(:choix, :bon, question_id: question_qcm.id) }
let!(:reponse2) { create(:choix, :mauvais, question_id: question_qcm.id) }
let!(:reponse) { create(:choix, :bon, question_id: question.id) }
let!(:reponse2) { create(:choix, :mauvais, question_id: question.id) }

it 'génére un fichier xls avec les entêtes spécifiques' do
xls = response_service.to_xls
spreadsheet = Spreadsheet.open(StringIO.new(xls))
worksheet = spreadsheet.worksheet(0)

expect(spreadsheet.worksheets.count).to eq(1)
expect(worksheet.row(0)[8]).to eq('Type qcm')
expect(worksheet.row(0)[9]).to eq('choix_1_intitule')
Expand All @@ -189,19 +140,29 @@
end

it 'génére un fichier xls avec les détails de la question' do
xls = response_service.to_xls
spreadsheet = Spreadsheet.open(StringIO.new(xls))
worksheet = spreadsheet.worksheet(0)
question = worksheet.row(1)
expect(question[8]).to eq(question_qcm.type_qcm)
expect(question[9]).to eq(reponse.intitule)
expect(question[10]).to eq(reponse.nom_technique)
expect(question[11]).to eq(reponse.type_choix)
expect(question[12]).to eq(reponse.audio_url)
expect(question[13]).to eq(reponse2.intitule)
expect(question[14]).to eq(reponse2.nom_technique)
expect(question[15]).to eq(reponse2.type_choix)
expect(question[16]).to eq(reponse2.audio_url)
ligne = worksheet.row(1)
expect(ligne[8]).to eq(question.type_qcm)
expect(ligne[9]).to eq(reponse.intitule)
expect(ligne[10]).to eq(reponse.nom_technique)
expect(ligne[11]).to eq(reponse.type_choix)
expect(ligne[12]).to eq(reponse.audio_url)
expect(ligne[13]).to eq(reponse2.intitule)
expect(ligne[14]).to eq(reponse2.nom_technique)
expect(ligne[15]).to eq(reponse2.type_choix)
expect(ligne[16]).to eq(reponse2.audio_url)
end
end

describe '#nom_du_fichier' do
it 'genere le nom du fichier' do
date = DateTime.current.strftime('%Y%m%d')
nom_du_fichier_attendu = "#{date}-#{question.nom_technique}.xls"

expect(response_service.nom_du_fichier).to eq(nom_du_fichier_attendu)
end
end

describe '#content_type_xls' do
it { expect(response_service.content_type_xls).to eq 'application/vnd.ms-excel' }
end
end

0 comments on commit c51f10b

Please sign in to comment.