Since targets didn't have a title but only a long description, every form allowing to select targets was pretty much unusable: we either displayed just the code or the whole description. Now, with a concise title, it's easier to find and select the desired target. The titles have been copied from The Global Goals page [1]. Note we're using the `short_title` I18n key for the `title` method and the `long_title` I18n key for the `long_title` method. We can't use `title` as I18n key instead of `short_title` because it would affect existing translations. [1] https://www.globalgoals.org/
131 lines
4.2 KiB
Ruby
131 lines
4.2 KiB
Ruby
require "rails_helper"
|
|
|
|
describe SDG::LocalTarget do
|
|
describe "Concerns" do
|
|
it_behaves_like "globalizable", :sdg_local_target
|
|
end
|
|
|
|
it "is valid" do
|
|
expect(build(:sdg_local_target)).to be_valid
|
|
end
|
|
|
|
it "is not valid without a title" do
|
|
expect(build(:sdg_local_target, title: nil)).not_to be_valid
|
|
end
|
|
|
|
it "is not valid without a description" do
|
|
expect(build(:sdg_local_target, description: nil)).not_to be_valid
|
|
end
|
|
|
|
it "is not valid without a code" do
|
|
expect(build(:sdg_local_target, code: nil, target: SDG::Target[1.1], goal: SDG::Goal[1])).not_to be_valid
|
|
end
|
|
|
|
it "is not valid when code does not include associated target code" do
|
|
local_target = build(:sdg_local_target, code: "1.6.1", target: SDG::Target[1.1])
|
|
|
|
expect(local_target).not_to be_valid
|
|
expect(local_target.errors.full_messages).to include "Code must start with the same code as its target followed by a dot and end with a number"
|
|
end
|
|
|
|
it "is not valid when local target code part is not a number" do
|
|
local_target = build(:sdg_local_target, code: "1.1.A", target: SDG::Target[1.1])
|
|
|
|
expect(local_target).not_to be_valid
|
|
expect(local_target.errors.full_messages).to include "Code must start with the same code as its target followed by a dot and end with a number"
|
|
end
|
|
|
|
it "is not valid if code is not unique" do
|
|
create(:sdg_local_target, code: "1.1.1")
|
|
local_target = build(:sdg_local_target, code: "1.1.1")
|
|
|
|
expect(local_target).not_to be_valid
|
|
expect(local_target.errors.full_messages).to include "Code has already been taken"
|
|
end
|
|
|
|
it "is not valid without a target" do
|
|
expect(build(:sdg_local_target, target: nil)).not_to be_valid
|
|
end
|
|
|
|
describe "#code_and_title" do
|
|
it "returns the code and the title" do
|
|
target = build(:sdg_local_target, code: "4.4.1", title: "Build a university")
|
|
|
|
expect(target.code_and_title).to eq "4.4.1. Build a university"
|
|
end
|
|
end
|
|
|
|
describe "#set_related_goal" do
|
|
it "before validation set related goal" do
|
|
local_target = build(:sdg_local_target, code: "1.1.1", target: SDG::Target["1.1"], goal: nil)
|
|
|
|
expect(local_target).to be_valid
|
|
expect(local_target.goal).to eq(SDG::Goal[1])
|
|
end
|
|
end
|
|
|
|
describe "#goal" do
|
|
it "returns the target goal" do
|
|
local_target = create(:sdg_local_target, code: "1.1.1")
|
|
|
|
expect(local_target.goal).to eq(SDG::Goal[1])
|
|
end
|
|
end
|
|
|
|
describe "#<=>" do
|
|
let(:local_target,) { create(:sdg_local_target, code: "10.B.10") }
|
|
|
|
it "compares using the target first" do
|
|
lesser_local_target = create(:sdg_local_target, code: "10.A.1")
|
|
greater_local_target = create(:sdg_local_target, code: "11.1.1")
|
|
|
|
expect(local_target).to be > lesser_local_target
|
|
expect(local_target).to be < greater_local_target
|
|
end
|
|
|
|
it "compares using the local target code when the target is the same" do
|
|
lesser_local_target = create(:sdg_local_target, code: "10.B.9")
|
|
greater_local_target = create(:sdg_local_target, code: "10.B.11")
|
|
|
|
expect(local_target).to be > lesser_local_target
|
|
expect(local_target).to be < greater_local_target
|
|
end
|
|
|
|
it "can be compared against global targets" do
|
|
lesser_target = build(:sdg_target, code: "10.A", goal: SDG::Goal[10])
|
|
greater_target = build(:sdg_target, code: "11.1", goal: SDG::Goal[11])
|
|
|
|
expect(local_target).to be > lesser_target
|
|
expect(local_target).to be < greater_target
|
|
end
|
|
|
|
it "can be compared against goals" do
|
|
lesser_goal = build(:sdg_goal, code: "10")
|
|
greater_goal = build(:sdg_goal, code: "11")
|
|
|
|
expect(local_target).to be > lesser_goal
|
|
expect(local_target).to be < greater_goal
|
|
end
|
|
end
|
|
|
|
describe ".[]" do
|
|
it "finds existing local targets by code" do
|
|
create(:sdg_local_target, code: "1.1.1")
|
|
|
|
expect(SDG::LocalTarget["1.1.1"].code).to eq "1.1.1"
|
|
end
|
|
|
|
it "raises an exception for non-existing codes" do
|
|
expect { SDG::LocalTarget["1.1.99"] }.to raise_exception ActiveRecord::RecordNotFound
|
|
end
|
|
end
|
|
|
|
describe "#long_title" do
|
|
it "returns the title" do
|
|
target = build(:sdg_local_target, title: "Improve local water transport system")
|
|
|
|
expect(target.long_title).to eq "Improve local water transport system"
|
|
end
|
|
end
|
|
end
|