aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/chouette/line_spec.rb
blob: f0a2453b576e88d35da9b5625aa0ae2889ca81f1 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
require 'spec_helper'

describe Chouette::Line, :type => :model do

  subject { create(:line) }

  it { is_expected.to validate_presence_of :network }
  it { is_expected.to validate_presence_of :company }

  it { is_expected.to validate_presence_of :name }

  # it { should validate_presence_of :objectid }
  it { is_expected.to validate_uniqueness_of :objectid }

  describe '#objectid' do
    subject { super().objectid }
    it { is_expected.to be_kind_of(Chouette::ObjectId) }
  end

  # it { should validate_numericality_of :objectversion }

  describe ".last_stop_areas_parents" do

    it "should return stop areas if no parents" do
      line = create(:line_with_stop_areas)
      expect(line.stop_areas_last_parents).to eq(line.stop_areas)
    end

    it "should return stop areas parents if parents" do
      line = create(:line_with_stop_areas)
      route = create(:route, :line => line)
      parent = create(:stop_area)
      stop_areas = [ create(:stop_area),  create(:stop_area), create(:stop_area, :parent_id => parent.id) ]
      stop_areas.each do |stop_area|
        create(:stop_point, :stop_area => stop_area, :route => route)
      end

      expect(line.stop_areas_last_parents).to match(line.stop_areas[0..(line.stop_areas.size - 2)].push(parent))
    end

  end

  describe "#stop_areas" do
    let!(:route){create(:route, :line => subject)}
    it "should retreive route's stop_areas" do
      expect(subject.stop_areas.count).to eq(route.stop_points.count)
    end
  end

  describe "#transport_mode" do

    def self.legacy_transport_mode_names
      %w{Air Train LongDistanceTrain LocalTrain RapidTransit Metro Tramway Coach Bus Ferry Waterborne PrivateVehicle Walk Trolleybus Bicycle Shuttle Taxi VAL Other}
    end

    legacy_transport_mode_names.each do |transport_mode_name|
      context "when transport_mode_name is #{transport_mode_name}" do
        transport_mode = Chouette::TransportMode.new(transport_mode_name.underscore)
        it "should be #{transport_mode}" do
          subject.transport_mode_name = transport_mode_name
          expect(subject.transport_mode).to eq(transport_mode)
        end
      end
    end
    context "when transport_mode_name is nil" do
      it "should be nil" do
        subject.transport_mode_name = nil
        expect(subject.transport_mode).to be_nil
      end
    end

  end

  describe "#transport_mode=" do

    it "should change transport_mode_name with TransportMode#name" do
      subject.transport_mode = "Test"
      expect(subject.transport_mode_name).to eq("Test")
    end

  end

  describe ".transport_modes" do

    it "should not include unknown transport_mode" do
      expect(Chouette::Line.transport_modes).not_to include(Chouette::TransportMode.new("unknown"))
    end

    it "should not include interchange transport_mode" do
      expect(Chouette::Line.transport_modes).not_to include(Chouette::TransportMode.new("interchange"))
    end

  end

  context "#group_of_line_tokens=" do
    let!(:group_of_line1){create(:group_of_line)}
    let!(:group_of_line2){create(:group_of_line)}

    it "should return associated group_of_line ids" do
      subject.update_attributes :group_of_line_tokens => [group_of_line1.id, group_of_line2.id].join(',')
      expect(subject.group_of_lines).to include( group_of_line1)
      expect(subject.group_of_lines).to include( group_of_line2)
    end
  end

  describe "#update_attributes footnotes_attributes" do
    context "instanciate 2 footnotes without line" do
      let!( :footnote_first) {build( :footnote, :line_id => nil)}
      let!( :footnote_second) {build( :footnote, :line_id => nil)}
      it "should add 2 footnotes to the line" do
        subject.update_attributes :footnotes_attributes =>
          { Time.now.to_i => footnote_first.attributes,
            (Time.now.to_i-5) => footnote_second.attributes}
        expect(Chouette::Line.find( subject.id ).footnotes.size).to eq(2)
      end
    end
  end


end