| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 | require 'rails_helper'
RSpec.describe CustomField, type: :model do
  let( :vj ){ create :vehicle_journey, custom_field_values: {energy: 99} }
  context "validates" do
    it { should validate_uniqueness_of(:name).scoped_to(:resource_type, :workgroup_id) }
    it { should validate_uniqueness_of(:code).scoped_to(:resource_type, :workgroup_id).case_insensitive }
  end
  context "field access" do
    let( :custom_field ){ build_stubbed :custom_field }
    it "option's values can be accessed by a key" do
      expect( custom_field.options['capacity'] ).to eq("0")
    end
  end
  context "custom fields for a resource" do
    let!( :fields ){ [create(:custom_field), create(:custom_field, code: :energy)] }
    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 {
      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
  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
 |