aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/route_spec.rb
blob: dbf00ab0b5d44c65b7c3c626732672987cb59c9f (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
require 'spec_helper'

RSpec.describe Chouette::Route, :type => :model do
  subject(:route){ create :route }
  context "the checksum" do
    it "should change when a stop is removed" do
      expect{route.stop_points.last.destroy}.to change {route.reload.checksum}
    end
  end
  context "metadatas" do
    it "should be empty at first" do
      expect(Chouette::Route.has_metadata?).to be_truthy
      expect(route.has_metadata?).to be_truthy
      expect(route.metadata.creator_username).to be_nil
      expect(route.metadata.modifier_username).to be_nil
    end

    context "once set" do
      it "should set the correct values" do
        Timecop.freeze(Time.now) do
          route.metadata.creator_username = "john.doe"
          route.save!
          id = route.id
          route = Chouette::Route.find id
          expect(route.metadata.creator_username).to eq "john.doe"
          expect(route.metadata.creator_username_updated_at.strftime('%Y-%m-%d %H:%M:%S.%3N')).to eq Time.now.strftime('%Y-%m-%d %H:%M:%S.%3N')
        end
      end
    end

    describe "#merge_metadata_from" do
      let(:source){ create :route }
      let(:metadata){ target.merge_metadata_from(source).metadata }
      let(:target){ create :route }
      before do
        target.metadata.creator_username = "john"
        target.metadata.modifier_username = "john"
      end
      context "when the source has no metadata" do
        it "should do nothing" do
          expect(metadata.creator_username).to eq "john"
          expect(metadata.modifier_username).to eq "john"
        end
      end

      context "when the source has older metadata" do
        before do
          source.metadata.creator_username = "jane"
          source.metadata.modifier_username = "jane"
          source.metadata.creator_username_updated_at = 1.month.ago
          source.metadata.modifier_username_updated_at = 1.month.ago
        end
        it "should do nothing" do
          expect(metadata.creator_username).to eq "john"
          expect(metadata.modifier_username).to eq "john"
        end
      end

      context "when the source has new metadata" do
        before do
          source.metadata.creator_username = "jane"
          source.metadata.modifier_username = "jane"
          target.metadata.creator_username_updated_at = 1.month.ago
          target.metadata.modifier_username_updated_at = 1.month.ago
        end
        it "should update metadata" do
          expect(metadata.creator_username).to eq "jane"
          expect(metadata.modifier_username).to eq "jane"
        end
      end
    end
  end

  context "when creating stop_points" do
    # Here we tests that acts_as_list does not mess with the positions
    let(:stop_areas){
      4.times.map{create :stop_area}
    }

    it "should set a correct order to the stop_points" do

      order = [0, 3, 2, 1]
      new = Referential.new
      new.name = "mkmkm"
      new.organisation = create(:organisation)
      new.line_referential = create(:line_referential)
      create(:line, line_referential: new.line_referential)
      new.stop_area_referential = create(:stop_area_referential)
      new.objectid_format = :netex
      new.save!
      new.switch
      route = new.routes.new

      route.published_name = route.name = "Route"
      route.line = new.line_referential.lines.last
      order.each_with_index do |position, i|
        _attributes = {
          stop_area: stop_areas[i],
          position: position
        }
        route.stop_points.build _attributes
      end
      route.save
      expect(route).to be_valid
      expect{route.run_callbacks(:commit)}.to_not raise_error
    end
  end
end