diff options
| author | teddywing | 2018-03-01 17:12:19 +0100 | 
|---|---|---|
| committer | GitHub | 2018-03-01 17:12:19 +0100 | 
| commit | 3d5e2bc56650471954ba1c51c7f53cbd84013d47 (patch) | |
| tree | f82a0b7fbf81d4e4ad60492f6c51268356b1e256 /spec | |
| parent | 9ae8c7e2f7efd8441b2c403c35eca3cbccdfd6cc (diff) | |
| parent | b9c3c0a47db7d733a80001f2b80ddcb7d69ec852 (diff) | |
| download | chouette-core-3d5e2bc56650471954ba1c51c7f53cbd84013d47.tar.bz2 | |
Merge pull request #237 from af83/proposed-custom-fields-refactor
Proposed custom fields refactor
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/factories/custom_fields.rb | 2 | ||||
| -rw-r--r-- | spec/models/chouette/vehicle_journey_spec.rb | 5 | ||||
| -rw-r--r-- | spec/models/custom_field_spec.rb | 42 | 
3 files changed, 43 insertions, 6 deletions
diff --git a/spec/factories/custom_fields.rb b/spec/factories/custom_fields.rb index 2f5fae555..7c43a6147 100644 --- a/spec/factories/custom_fields.rb +++ b/spec/factories/custom_fields.rb @@ -4,6 +4,6 @@ FactoryGirl.define do      resource_type "VehicleJourney"      sequence(:name){|n| "custom field ##{n}"}      field_type "list" -    options( { "capacity" => "0" } ) +    options( { capacity: "0" } )    end  end diff --git a/spec/models/chouette/vehicle_journey_spec.rb b/spec/models/chouette/vehicle_journey_spec.rb index 76e73d9cf..c69655bd4 100644 --- a/spec/models/chouette/vehicle_journey_spec.rb +++ b/spec/models/chouette/vehicle_journey_spec.rb @@ -260,7 +260,7 @@ describe Chouette::VehicleJourney, :type => :model do          item['purchase_windows']         = []          item['footnotes']                = []          item['purchase_windows']         = [] -        item['custom_fields']            = vj.custom_fields +        item['custom_fields']            = vj.custom_fields.to_hash          vj.vehicle_journey_at_stops.each do |vjas|            item['vehicle_journey_at_stops'] << vehicle_journey_at_stop_to_state(vjas) @@ -282,7 +282,6 @@ describe Chouette::VehicleJourney, :type => :model do          Chouette::VehicleJourney.state_update(route, collection)        }.to change {Chouette::VehicleJourney.count}.by(1) -        obj = Chouette::VehicleJourney.last        expect(obj).to receive(:after_commit_objectid).and_call_original @@ -292,7 +291,7 @@ describe Chouette::VehicleJourney, :type => :model do        expect(collection.last['objectid']).to eq obj.objectid        expect(obj.published_journey_name).to eq 'dummy' -      expect(obj.custom_fields["energy"]["value"]).to eq 99 +      expect(obj.custom_fields["energy"].value).to eq 99      end      it 'should expect local times' do diff --git a/spec/models/custom_field_spec.rb b/spec/models/custom_field_spec.rb index 51128b0a2..b92bcfbdb 100644 --- a/spec/models/custom_field_spec.rb +++ b/spec/models/custom_field_spec.rb @@ -16,7 +16,6 @@ RSpec.describe CustomField, type: :model do      end    end -    context "custom fields for a resource" do      let!( :fields ){ [create(:custom_field), create(:custom_field, code: :energy)] }      let!( :instance_fields ){ @@ -26,10 +25,49 @@ RSpec.describe CustomField, type: :model do        }      }      it { expect(Chouette::VehicleJourney.custom_fields).to eq(fields) } -    it { expect(vj.custom_fields).to eq(instance_fields) } +    it { +      instance_fields.each do |code, cf| +        cf.each do |k, v| +          expect(vj.custom_fields[code].send(k)).to eq(v) +        end +      end +    }    end    context "custom field_values for a resource" do      it { expect(vj.custom_field_value("energy")).to eq(99) }    end + +  context "with an 'integer' field_type" do +    let!(:field){ [create(:custom_field, code: :energy, options: {field_type: 'integer'})] } +    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 +    end + +    it "should validate the value" do +      { +        "99" => true, +        "azerty" => false, +        "91a" => false, +        "a91" => false +      }.each do |val, valid| +        vj = build :vehicle_journey, custom_field_values: {energy: val} +        if valid +          expect(vj.validate).to be_truthy +        else +          expect(vj.validate).to be_falsy +          expect(vj.errors.messages[:"custom_fields.energy"]).to be_present +        end +      end +    end +  end + +  context "with a 'string' field_type" do +    let!(:field){ [create(:custom_field, code: :energy, options: {field_type: 'string'})] } +    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' +    end +  end  end  | 
