Files
grecia/spec/models/sdg/target_spec.rb
Senén Rodero Rodríguez c39c7213c7 Add SDG target model
and its relation with the SDG goal model.

Add comparable module be able to sort collections of targets
by code attribute.

Co-Authored-By: Javi Martín <35156+javierm@users.noreply.github.com>
2020-12-04 15:15:32 +01:00

76 lines
2.2 KiB
Ruby

require "rails_helper"
describe SDG::Target do
it "is valid" do
expect(build(:sdg_target, code: "1.Z", goal: SDG::Goal[1])).to be_valid
end
it "is not valid without a code" do
expect(build(:sdg_target, code: nil)).not_to be_valid
end
it "is not valid without a goal" do
target = build(:sdg_target, goal: nil)
expect(target).not_to be_valid
end
it "is not valid if code is not unique" do
goal = SDG::Goal[1]
create(:sdg_target, code: "1.Z", goal: goal)
target = build(:sdg_target, code: "1.Z", goal: goal)
expect(target).not_to be_valid
expect(target.errors.full_messages).to include "Code has already been taken"
end
it "translates title" do
target = SDG::Target["1.1"]
expect(target.title).to start_with "By 2030, eradicate extreme poverty"
I18n.with_locale(:es) do
expect(target.title).to start_with "Para 2030, erradicar la pobreza extrema"
end
target = SDG::Target["17.11"]
expect(target.title).to start_with "Significantly increase the exports of developing countries"
I18n.with_locale(:es) do
expect(target.title).to start_with "Aumentar significativamente las exportaciones de los países"
end
end
describe "#<=>" do
let(:goal) { build(:sdg_goal, code: 10) }
let(:target) { build(:sdg_target, code: "10.19", goal: goal) }
it "compares using the goal first" do
lesser_target = build(:sdg_target, code: "2.14", goal: build(:sdg_goal, code: 2))
greater_target = build(:sdg_target, code: "11.1", goal: build(:sdg_goal, code: 11))
expect(target).to be > lesser_target
expect(target).to be < greater_target
end
it "compares using the target code when the goal is the same" do
lesser_target = build(:sdg_target, code: "10.2", goal: goal)
greater_target = build(:sdg_target, code: "10.A", goal: goal)
expect(target).to be > lesser_target
expect(target).to be < greater_target
end
end
describe ".[]" do
it "finds existing targets by code" do
expect(SDG::Target["1.1"].code).to eq "1.1"
end
it "raises an exception for non-existing codes" do
expect { SDG::Target["Z.j3"] }.to raise_exception ActiveRecord::RecordNotFound
end
end
end