Add globalizable tests for all translatable models
So now we test in depth at the model level, and can be a bit more relaxed about integration tests for translations. Note we're defining some extra factories to make sure all translatable attributes with presence validation rules are mandatory. This way we can simplify the way we obtain required fields, using `required_attribute?`. Otherwise, fields having an `unless` condition in their presence validation rules would count as mandatory even when they're not.
This commit is contained in:
@@ -10,6 +10,10 @@ FactoryBot.define do
|
|||||||
sequence(:title) { |n| "Milestone #{n} title" }
|
sequence(:title) { |n| "Milestone #{n} title" }
|
||||||
description { "Milestone description" }
|
description { "Milestone description" }
|
||||||
publication_date { Date.current }
|
publication_date { Date.current }
|
||||||
|
|
||||||
|
factory :milestone_with_description do
|
||||||
|
status { nil }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
factory :progress_bar do
|
factory :progress_bar do
|
||||||
@@ -21,5 +25,7 @@ FactoryBot.define do
|
|||||||
kind { :secondary }
|
kind { :secondary }
|
||||||
sequence(:title) { |n| "Progress bar #{n} title" }
|
sequence(:title) { |n| "Progress bar #{n} title" }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
factory :secondary_progress_bar, traits: [:secondary]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -73,6 +73,8 @@ FactoryBot.define do
|
|||||||
trait :with_milestone_tags do
|
trait :with_milestone_tags do
|
||||||
after(:create) { |proposal| proposal.milestone_tags << create(:tag, :milestone) }
|
after(:create) { |proposal| proposal.milestone_tags << create(:tag, :milestone) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
factory :retired_proposal, traits: [:retired]
|
||||||
end
|
end
|
||||||
|
|
||||||
factory :proposal_notification do
|
factory :proposal_notification do
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ describe Banner do
|
|||||||
|
|
||||||
describe "Concerns" do
|
describe "Concerns" do
|
||||||
it_behaves_like "acts as paranoid", :banner
|
it_behaves_like "acts as paranoid", :banner
|
||||||
|
it_behaves_like "globalizable", :banner
|
||||||
end
|
end
|
||||||
|
|
||||||
it "is valid" do
|
it "is valid" do
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ require "rails_helper"
|
|||||||
|
|
||||||
describe Budget::Group do
|
describe Budget::Group do
|
||||||
it_behaves_like "sluggable", updatable_slug_trait: :drafting_budget
|
it_behaves_like "sluggable", updatable_slug_trait: :drafting_budget
|
||||||
|
it_behaves_like "globalizable", :budget_group
|
||||||
|
|
||||||
describe "Validations" do
|
describe "Validations" do
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ describe Budget::Heading do
|
|||||||
let(:group) { create(:budget_group, budget: budget) }
|
let(:group) { create(:budget_group, budget: budget) }
|
||||||
|
|
||||||
it_behaves_like "sluggable", updatable_slug_trait: :drafting_budget
|
it_behaves_like "sluggable", updatable_slug_trait: :drafting_budget
|
||||||
|
it_behaves_like "globalizable", :budget_heading
|
||||||
|
|
||||||
describe "OSM_DISTRICT_LEVEL_ZOOM constant" do
|
describe "OSM_DISTRICT_LEVEL_ZOOM constant" do
|
||||||
it "is defined" do
|
it "is defined" do
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ describe Budget::Phase do
|
|||||||
let(:fourth_phase) { budget.phases.reviewing }
|
let(:fourth_phase) { budget.phases.reviewing }
|
||||||
let(:final_phase) { budget.phases.finished }
|
let(:final_phase) { budget.phases.finished }
|
||||||
|
|
||||||
|
it_behaves_like "globalizable", :budget_phase
|
||||||
|
|
||||||
describe "validates" do
|
describe "validates" do
|
||||||
it "is not valid without a budget" do
|
it "is not valid without a budget" do
|
||||||
expect(build(:budget_phase, budget: nil)).not_to be_valid
|
expect(build(:budget_phase, budget: nil)).not_to be_valid
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ describe Budget do
|
|||||||
|
|
||||||
it_behaves_like "sluggable", updatable_slug_trait: :drafting
|
it_behaves_like "sluggable", updatable_slug_trait: :drafting
|
||||||
it_behaves_like "reportable"
|
it_behaves_like "reportable"
|
||||||
|
it_behaves_like "globalizable", :budget
|
||||||
|
|
||||||
describe "name" do
|
describe "name" do
|
||||||
before do
|
before do
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
require "spec_helper"
|
require "spec_helper"
|
||||||
|
|
||||||
shared_examples_for "globalizable" do |factory_name|
|
shared_examples_for "globalizable" do |factory_name|
|
||||||
let(:record) { create(:factory_name) }
|
let(:record) do
|
||||||
let(:fields) { record.translated_attribute_names }
|
if factory_name == :budget_phase
|
||||||
let(:required_fields) do
|
create(:budget).phases.last
|
||||||
fields.reject do |field|
|
else
|
||||||
record.translations.first.dup.tap { |duplicate| duplicate.send(:"#{field}=", "") }.valid?
|
create(factory_name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
let(:fields) { record.translated_attribute_names }
|
||||||
|
let(:required_fields) { fields.select { |field| record.send(:required_attribute?, field) } }
|
||||||
let(:attribute) { required_fields.sample || fields.sample }
|
let(:attribute) { required_fields.sample || fields.sample }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
@@ -177,8 +179,6 @@ shared_examples_for "globalizable" do |factory_name|
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "Falls back to the first available locale after removing a locale" do
|
it "Falls back to the first available locale after removing a locale" do
|
||||||
Globalize.set_fallbacks_to_all_available_locales
|
|
||||||
|
|
||||||
expect(record.send(attribute)).to eq "In English"
|
expect(record.send(attribute)).to eq "In English"
|
||||||
|
|
||||||
record.update(translations_attributes: [
|
record.update(translations_attributes: [
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ describe Legislation::DraftVersion do
|
|||||||
let(:legislation_draft_version) { build(:legislation_draft_version) }
|
let(:legislation_draft_version) { build(:legislation_draft_version) }
|
||||||
|
|
||||||
it_behaves_like "acts as paranoid", :legislation_draft_version
|
it_behaves_like "acts as paranoid", :legislation_draft_version
|
||||||
|
it_behaves_like "globalizable", :legislation_draft_version
|
||||||
|
|
||||||
it "is valid" do
|
it "is valid" do
|
||||||
expect(legislation_draft_version).to be_valid
|
expect(legislation_draft_version).to be_valid
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ describe Legislation::Process do
|
|||||||
let(:process) { create(:legislation_process) }
|
let(:process) { create(:legislation_process) }
|
||||||
|
|
||||||
it_behaves_like "acts as paranoid", :legislation_process
|
it_behaves_like "acts as paranoid", :legislation_process
|
||||||
|
it_behaves_like "globalizable", :legislation_process
|
||||||
|
|
||||||
it "is valid" do
|
it "is valid" do
|
||||||
expect(process).to be_valid
|
expect(process).to be_valid
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ describe Legislation::QuestionOption do
|
|||||||
let(:legislation_question_option) { build(:legislation_question_option) }
|
let(:legislation_question_option) { build(:legislation_question_option) }
|
||||||
|
|
||||||
it_behaves_like "acts as paranoid", :legislation_question_option
|
it_behaves_like "acts as paranoid", :legislation_question_option
|
||||||
|
it_behaves_like "globalizable", :legislation_question_option
|
||||||
|
|
||||||
it "is valid" do
|
it "is valid" do
|
||||||
expect(legislation_question_option).to be_valid
|
expect(legislation_question_option).to be_valid
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ describe Legislation::Question do
|
|||||||
|
|
||||||
describe "Concerns" do
|
describe "Concerns" do
|
||||||
it_behaves_like "notifiable"
|
it_behaves_like "notifiable"
|
||||||
|
it_behaves_like "globalizable", :legislation_question
|
||||||
end
|
end
|
||||||
|
|
||||||
it "is valid" do
|
it "is valid" do
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
require "rails_helper"
|
require "rails_helper"
|
||||||
|
|
||||||
describe Milestone do
|
describe Milestone do
|
||||||
|
it_behaves_like "globalizable", :milestone_with_description
|
||||||
|
|
||||||
describe "Validations" do
|
describe "Validations" do
|
||||||
let(:milestone) { build(:milestone) }
|
let(:milestone) { build(:milestone) }
|
||||||
|
|||||||
5
spec/models/poll/active_poll_spec.rb
Normal file
5
spec/models/poll/active_poll_spec.rb
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
require "rails_helper"
|
||||||
|
|
||||||
|
describe ActivePoll do
|
||||||
|
it_behaves_like "globalizable", :active_poll
|
||||||
|
end
|
||||||
@@ -8,6 +8,7 @@ describe Poll do
|
|||||||
it_behaves_like "notifiable"
|
it_behaves_like "notifiable"
|
||||||
it_behaves_like "acts as paranoid", :poll
|
it_behaves_like "acts as paranoid", :poll
|
||||||
it_behaves_like "reportable"
|
it_behaves_like "reportable"
|
||||||
|
it_behaves_like "globalizable", :poll
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "validations" do
|
describe "validations" do
|
||||||
|
|||||||
5
spec/models/poll/question/answer_spec.rb
Normal file
5
spec/models/poll/question/answer_spec.rb
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
require "rails_helper"
|
||||||
|
|
||||||
|
describe Poll::Question::Answer do
|
||||||
|
it_behaves_like "globalizable", :poll_question_answer
|
||||||
|
end
|
||||||
@@ -5,6 +5,7 @@ RSpec.describe Poll::Question, type: :model do
|
|||||||
|
|
||||||
describe "Concerns" do
|
describe "Concerns" do
|
||||||
it_behaves_like "acts as paranoid", :poll_question
|
it_behaves_like "acts as paranoid", :poll_question
|
||||||
|
it_behaves_like "globalizable", :poll_question
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#poll_question_id" do
|
describe "#poll_question_id" do
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ require "rails_helper"
|
|||||||
describe ProgressBar do
|
describe ProgressBar do
|
||||||
let(:progress_bar) { build(:progress_bar) }
|
let(:progress_bar) { build(:progress_bar) }
|
||||||
|
|
||||||
|
it_behaves_like "globalizable", :secondary_progress_bar
|
||||||
|
|
||||||
it "is valid" do
|
it "is valid" do
|
||||||
expect(progress_bar).to be_valid
|
expect(progress_bar).to be_valid
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ describe Proposal do
|
|||||||
it_behaves_like "has_public_author"
|
it_behaves_like "has_public_author"
|
||||||
it_behaves_like "notifiable"
|
it_behaves_like "notifiable"
|
||||||
it_behaves_like "map validations"
|
it_behaves_like "map validations"
|
||||||
it_behaves_like "globalizable", :proposal
|
it_behaves_like "globalizable", :retired_proposal
|
||||||
it_behaves_like "sanitizable"
|
it_behaves_like "sanitizable"
|
||||||
it_behaves_like "acts as paranoid", :proposal
|
it_behaves_like "acts as paranoid", :proposal
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ require "rails_helper"
|
|||||||
RSpec.describe SiteCustomization::Page, type: :model do
|
RSpec.describe SiteCustomization::Page, type: :model do
|
||||||
let(:custom_page) { build(:site_customization_page) }
|
let(:custom_page) { build(:site_customization_page) }
|
||||||
|
|
||||||
|
it_behaves_like "globalizable", :site_customization_page
|
||||||
|
|
||||||
it "is valid" do
|
it "is valid" do
|
||||||
expect(custom_page).to be_valid
|
expect(custom_page).to be_valid
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
require "rails_helper"
|
require "rails_helper"
|
||||||
|
|
||||||
describe Widget::Card do
|
describe Widget::Card do
|
||||||
|
|
||||||
let(:card) { build(:widget_card) }
|
let(:card) { build(:widget_card) }
|
||||||
|
|
||||||
|
it_behaves_like "globalizable", :widget_card
|
||||||
|
|
||||||
context "validations" do
|
context "validations" do
|
||||||
|
|
||||||
it "is valid" do
|
it "is valid" do
|
||||||
|
|||||||
Reference in New Issue
Block a user