aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/models/custom_field_spec.rb43
-rw-r--r--spec/models/referential_metadata_spec.rb2
2 files changed, 36 insertions, 9 deletions
diff --git a/spec/models/custom_field_spec.rb b/spec/models/custom_field_spec.rb
index ce6ce9fa5..54fd4f9d8 100644
--- a/spec/models/custom_field_spec.rb
+++ b/spec/models/custom_field_spec.rb
@@ -3,6 +3,11 @@ require 'rails_helper'
RSpec.describe CustomField, type: :model do
let( :vj ){ create :vehicle_journey, custom_field_values: {energy: 99} }
+ let(:referential){ create :workbench_referential }
+ let(:workgroup){ referential.workgroup }
+ before do
+ referential.switch
+ end
context "validates" do
it { should validate_uniqueness_of(:name).scoped_to(:resource_type, :workgroup_id) }
@@ -18,14 +23,14 @@ RSpec.describe CustomField, type: :model do
end
context "custom fields for a resource" do
- let!( :fields ){ [create(:custom_field), create(:custom_field, code: :energy)] }
+ let!( :fields ){ [create(:custom_field, workgroup: workgroup), create(:custom_field, code: :energy, workgroup: workgroup)] }
let!( :instance_fields ){
{
fields[0].code => fields[0].slice(:code, :name, :field_type, :options).update(value: nil),
"energy" => fields[1].slice(:code, :name, :field_type, :options).update(value: 99)
}
}
- it { expect(Chouette::VehicleJourney.custom_fields).to eq(fields) }
+ it { expect(Chouette::VehicleJourney.custom_fields(workgroup)).to eq(fields) }
it {
instance_fields.each do |code, cf|
cf.each do |k, v|
@@ -37,14 +42,36 @@ RSpec.describe CustomField, type: :model do
context "custom field_values for a resource" do
before do
- create :custom_field, field_type: :integer, code: :energy, name: :energy
+ create :custom_field, field_type: :integer, code: :energy, name: :energy, workgroup: workgroup
end
it { expect(vj.custom_field_value("energy")).to eq(99) }
+
+ context "given different workgroups" do
+ let(:ref1){ create :workbench_referential }
+ let(:ref2){ create :workbench_referential }
+ before do
+ create :custom_field, field_type: :integer, code: :ref1_energy, name: :energy, workgroup: ref1.workgroup, options: {default: 12}
+ create :custom_field, field_type: :integer, code: :ref1_energy, name: :energy, workgroup: ref1.workgroup, options: {default: 12}, resource_type: "Company"
+ create :custom_field, field_type: :integer, code: :ref2_energy, name: :energy, workgroup: ref2.workgroup
+ end
+ it "should only initialize fields from the right workgroup" do
+ ref1.switch
+ expect(Chouette::VehicleJourney.new.custom_fields.keys).to eq ["ref1_energy"]
+ expect(Chouette::VehicleJourney.new.custom_field_values["ref1_energy"]).to eq 12
+ expect(Chouette::VehicleJourney.new(custom_field_values: {ref1_energy: 13}).custom_field_values["ref1_energy"]).to eq 13
+ line_referential = create(:line_referential, workgroup: ref1.workgroup)
+ expect(line_referential.companies.build(custom_field_values: {ref1_energy: "13"}).custom_field_values["ref1_energy"]).to eq 13
+
+ ref2.switch
+ expect(Chouette::VehicleJourney.new.custom_fields.keys).to eq ["ref2_energy"]
+ expect(Chouette::VehicleJourney.new.custom_field_values).to_not have_key "ref1_energy"
+ end
+ end
end
context "with a 'list' field_type" do
- let!(:field){ [create(:custom_field, code: :energy, field_type: 'list', options: {list_values: %w(foo bar baz)})] }
+ let!(:field){ [create(:custom_field, code: :energy, field_type: 'list', options: {list_values: %w(foo bar baz)}, workgroup: workgroup)] }
let!( :vj ){ create :vehicle_journey, custom_field_values: {energy: "1"} }
it "should cast the value" do
expect(vj.custom_fields[:energy].value).to eq 1
@@ -75,7 +102,7 @@ RSpec.describe CustomField, type: :model do
end
context "with an 'integer' field_type" do
- let!(:field){ [create(:custom_field, code: :energy, field_type: 'integer')] }
+ let!(:field){ [create(:custom_field, code: :energy, field_type: 'integer', workgroup: workgroup)] }
let!( :vj ){ create :vehicle_journey, custom_field_values: {energy: "99"} }
it "should cast the value" do
expect(vj.custom_fields[:energy].value).to eq 99
@@ -101,7 +128,7 @@ RSpec.describe CustomField, type: :model do
end
context "with a 'string' field_type" do
- let!(:field){ [create(:custom_field, code: :energy, field_type: 'string')] }
+ let!(:field){ [create(:custom_field, code: :energy, field_type: 'string', workgroup: workgroup)] }
let!( :vj ){ create :vehicle_journey, custom_field_values: {energy: 99} }
it "should cast the value" do
expect(vj.custom_fields[:energy].value).to eq '99'
@@ -109,7 +136,7 @@ RSpec.describe CustomField, type: :model do
end
context "with a 'attachment' field_type" do
- let!(:field){ [create(:custom_field, code: :energy, field_type: 'attachment')] }
+ let!(:field){ [create(:custom_field, code: :energy, field_type: 'attachment', workgroup: workgroup)] }
let( :vj ){ create :vehicle_journey, custom_field_values: {energy: File.open(Rails.root.join('spec', 'fixtures', 'users.json'))} }
it "should cast the value" do
expect(vj.custom_fields[:energy].value.class).to be CustomFieldAttachmentUploader
@@ -129,7 +156,7 @@ RSpec.describe CustomField, type: :model do
end
context "with a whitelist" do
- let!(:field){ [create(:custom_field, code: :energy, field_type: 'attachment', options: {extension_whitelist: %w(zip)})] }
+ let!(:field){ [create(:custom_field, code: :energy, field_type: 'attachment', options: {extension_whitelist: %w(zip)}, workgroup: workgroup)] }
it "should validate extension" do
expect(build(:vehicle_journey, custom_field_values: {energy: File.open(Rails.root.join('spec', 'fixtures', 'users.json'))})).to_not be_valid
expect(build(:vehicle_journey, custom_field_values: {energy: File.open(Rails.root.join('spec', 'fixtures', 'nozip.zip'))})).to be_valid
diff --git a/spec/models/referential_metadata_spec.rb b/spec/models/referential_metadata_spec.rb
index 88a12b2bb..210f95e14 100644
--- a/spec/models/referential_metadata_spec.rb
+++ b/spec/models/referential_metadata_spec.rb
@@ -100,7 +100,7 @@ RSpec.describe ReferentialMetadata, :type => :model do
it "should validate that end is greather than or equlals to begin" do
expect(period(begin: "2016-11-21", end: "2016-11-22")).to be_valid
- expect(period(begin: "2016-11-21", end: "2016-11-21")).to_not be_valid
+ expect(period(begin: "2016-11-21", end: "2016-11-21")).to be_valid
expect(period(begin: "2016-11-22", end: "2016-11-21")).to_not be_valid
end