aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/concerns/objectid_support_spec.rb
blob: fabeeec113ac4884e153e4faf2dac7b5f728dbbc (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
RSpec.describe ObjectidSupport do 

  context 'when referential has an objectid format of stif_netex' do
    let(:object) { create(:time_table, objectid: nil) }

    context "#objectid_format" do
      it "should be stif_netex" do
        expect(object.referential.objectid_format).to eq('stif_netex')
      end
    end

    it 'should fill __pending_id__' do
      expect(object.objectid.include?('__pending_id__')).to be_truthy
    end

    context "#get_objectid" do

      before(:each) do
        object.run_callbacks(:commit)
      end

      it "should be valid" do
        expect(object.get_objectid).to be_valid
      end

      it "should have the same local id than the object" do
        expect(object.get_objectid.local_id).to eq(object.local_id)
      end

      it "should be a Chouette::Objectid::StifNetex" do
        expect(object.get_objectid).to be_kind_of(Chouette::Objectid::StifNetex)
      end

      context "#to_s" do
        it "should return a string" do
          expect(object.get_objectid.to_s).to be_kind_of(String)
        end

        it "should be the same as the db attribute" do 
          expect(object.get_objectid.to_s).to eq(object.read_attribute(:objectid))
          expect(object.get_objectid.to_s).to eq(object.objectid)
        end
    
    context "#objectid" do

      it 'should build objectid on create' do
        object.save
        object.run_callbacks(:commit)
        objectid = object.get_objectid
        id = "#{objectid.provider_id}:#{objectid.object_type}:#{objectid.local_id}:#{objectid.creation_id}"
        expect(object.read_attribute(:objectid)).to eq(id)
      end

      it 'should not build new objectid is already set' do
        id = "first:TimeTable:1-1:LOC"
        object.attributes = {objectid: id}
        object.save
        expect(object.objectid).to eq(id)
      end

      it 'should create a new objectid when cleared' do
        object.save
        object.attributes = { objectid: nil}
        object.save
        expect(object.objectid).to be_truthy
      end
    end
      end
    end
  end

  context 'when referential has an objectid format of netex' do
    before(:all) do
      Referential.first.update(objectid_format: 'netex')
    end

    let(:object) { create(:time_table, objectid: nil) }


    context "#objectid_format" do 
      it "should be netex" do
        expect(object.referential.objectid_format).to eq('netex')
      end
    end

    context "#get_objectid" do

      it "should be valid" do
        expect(object.get_objectid).to be_valid
      end

      it "should have the same local id than the object" do
        expect(object.get_objectid.local_id).to match(/\w+-\w+-\w+-\w+-\w+/)
      end

      it "should be a Chouette::Objectid::StifNetex" do
        expect(object.get_objectid).to be_kind_of(Chouette::Objectid::Netex)
      end

      context "#to_s" do
        it "should return a string" do
          expect(object.get_objectid.to_s).to be_kind_of(String)
        end

        it "should be the same as the db attribute" do 
          expect(object.get_objectid.to_s).to eq(object.read_attribute(:objectid))
          expect(object.get_objectid.to_s).to eq(object.objectid)
        end
    
    context "#objectid" do

      it 'should build objectid on create' do
        object.save
        object.run_callbacks(:commit)
        objectid = object.get_objectid
        id = "#{objectid.provider_id}:#{objectid.object_type}:#{objectid.local_id}:#{objectid.creation_id}"
        expect(object.read_attribute(:objectid)).to eq(id)
      end

      it 'should not build new objectid is already set' do
        id = "first:TimeTable:1-1:LOC"
        object.attributes = {objectid: id}
        object.save
        expect(object.objectid).to eq(id)
      end

      it 'should create a new objectid when cleared' do
        object.save
        object.attributes = { objectid: nil}
        object.save
        expect(object.objectid).to be_truthy
      end
    end
      end
    end
  end
  
end