OSDN Git Service

pass model import test
[pettanr/pettanr.git] / spec / models / license_group_spec.rb
1 # -*- encoding: utf-8 -*-
2 #ライセンスグループ
3 require 'spec_helper'
4
5 describe LicenseGroup do
6   before do
7     #テストデータを用意してね
8     @f = Rails.root + 'spec/json/license_group.json'
9     @t = File.open(@f, 'r').read
10     @j = JSON.parse @t
11     @fs = Rails.root + 'spec/json/license_groups.json'
12     @fes = Rails.root + 'spec/json/invalid_license_groups.json'
13   end
14   describe '検証に於いて' do
15     before do
16     end
17     
18     it 'オーソドックスなデータなら通る' do
19       @lg = Factory.build :license_group
20       @lg.should be_valid
21     end
22     
23     context 'nameを検証するとき' do
24       before do
25         @lg = Factory.build :license_group
26       end
27       it 'テストデータの確認' do
28         @lg.name = 'a'*50
29         @lg.should be_valid
30       end
31       it 'nullなら失敗する' do
32         @lg.name = ''
33         @lg.should_not be_valid
34       end
35       it '51文字以上なら失敗する' do
36         @lg.name = 'a'*51
37         @lg.should_not be_valid
38       end
39       it '重複していたら失敗する' do
40         l = Factory :license_group
41         @lg.should_not be_valid
42       end
43     end
44     context 'classnameを検証するとき' do
45       before do
46         @lg = Factory.build :license_group
47       end
48       it 'テストデータの確認' do
49         @lg.classname = 'a'*50
50         @lg.should be_valid
51       end
52       it 'nullなら失敗する' do
53         @lg.classname = ''
54         @lg.should_not be_valid
55       end
56       it '51文字以上なら失敗する' do
57         @lg.classname = 'a'*51
58         @lg.should_not be_valid
59       end
60     end
61     context 'captionを検証するとき' do
62       before do
63         @lg = Factory.build :license_group
64       end
65       it 'テストデータの確認' do
66         @lg.caption = 'a'*30
67         @lg.should be_valid
68       end
69       it 'nullなら失敗する' do
70         @lg.caption = ''
71         @lg.should_not be_valid
72       end
73       it '51文字以上なら失敗する' do
74         @lg.caption = 'a'*51
75         @lg.should_not be_valid
76       end
77     end
78     context 'urlを検証するとき' do
79       before do
80         @lg = Factory.build :license_group
81       end
82       it 'テストデータの確認' do
83         @lg.url = 'http://test.com/'
84         @lg.should be_valid
85       end
86       it 'nullなら失敗する' do
87         @lg.url = ''
88         @lg.should_not be_valid
89       end
90       it '201文字以上なら失敗する' do
91         @lg.url = 'a'*201
92         @lg.should_not be_valid
93       end
94       it 'url形式でないなら失敗する' do
95         @lg.url = 'aaaaaaa'
96         @lg.should_not be_valid
97       end
98     end
99   end
100   
101   describe '更新に於いて' do
102     before do
103       @n = @j.keys.first
104       @a = @j.values.first
105     end
106     context 'つつがなく終わるとき' do
107       it 'データ更新準備を依頼する' do
108         LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
109         LicenseGroup.should_receive(:modify_object).with(any_args).exactly(1)
110         LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
111         r = LicenseGroup.store(@n, @a)
112       end
113       it '保存を依頼する' do
114         LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
115         LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
116         LicenseGroup.any_instance.should_receive(:save).with(any_args).exactly(1)
117         r = LicenseGroup.store(@n, @a)
118       end
119       it 'オブジェクトを返す' do
120         r = LicenseGroup.store(@n, @a)
121         r.is_a?(LicenseGroup).should be_true
122         r.name.should eq @n
123         r.url.should eq @a["url"]
124       end
125     end
126   end
127   
128   describe 'インポートに於いて' do
129     before do
130     end
131     context 'つつがなく終わるとき' do
132       it 'ファイルインポートを依頼する' do
133         LicenseGroup.should_receive(:import_file).with(any_args).exactly(1)
134         LicenseGroup.stub(:import_file).with(any_args).and_return([])
135         LicenseGroup.import(@f)
136       end
137       it 'ライセンスグループ更新を一回依頼する' do
138         LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
139         LicenseGroup.should_receive(:store).with(any_args).exactly(1)
140         LicenseGroup.import(@f)
141       end
142       it 'ライセンスグループが追加される' do
143         lambda {
144           LicenseGroup.import(@f)
145         }.should change LicenseGroup, :count
146       end
147       it '[]を返す' do
148         r = LicenseGroup.import(@f)
149         r.should eq []
150       end
151     end
152     context '複数データがつつがなく終わるとき' do
153       it 'ライセンスグループ更新を二回依頼する' do
154         LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
155         LicenseGroup.should_receive(:store).with(any_args).exactly(2)
156         LicenseGroup.import(@fs)
157       end
158       it 'ライセンスグループが二個追加される' do
159         lambda {
160           r = LicenseGroup.import(@fs)
161         }.should change(LicenseGroup, :count).by 2
162       end
163       it '[]を返す' do
164         r = LicenseGroup.import(@fs)
165         r.should eq []
166       end
167     end
168     context 'ライセンスグループ作成に失敗したとき' do
169       before do
170         LicenseGroup.any_instance.stub(:save).with(any_args).and_return(false)
171         LicenseGroup.any_instance.stub(:valid?).with(any_args).and_return(false)
172       end
173       it 'ライセンスグループの数に変化がない' do
174         lambda {
175           LicenseGroup.import(@f)
176         }.should_not change LicenseGroup, :count
177       end
178       it '配列を返す' do
179         r = LicenseGroup.import(@f)
180         r.is_a?(Array).should be_true
181       end
182       it '配列の中身は一件' do
183         r = LicenseGroup.import(@f)
184         r.should have(1).items
185       end
186       it 'ライセンスグループオブジェクトが入っている' do
187         r = LicenseGroup.import(@f)
188         r.first.is_a?(LicenseGroup).should be_true
189       end
190     end
191     context '複数のライセンスグループ作成に失敗したとき' do
192       #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
193       it 'ライセンスグループの数に変化がない' do
194         lambda {
195           LicenseGroup.import(@fes)
196         }.should_not change LicenseGroup, :count
197       end
198       it '途中で保存に失敗しても全件更新依頼する' do
199         LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
200         LicenseGroup.should_receive(:store).with(any_args).exactly(3)
201         LicenseGroup.import(@fes)
202       end
203       it '配列を返す' do
204         r = LicenseGroup.import(@fes)
205         r.is_a?(Array).should be_true
206       end
207       it '配列の中身は2件' do
208         r = LicenseGroup.import(@fes)
209         r.should have(2).items
210       end
211       it '配列の中身は失敗したライセンスグループオブジェクトが入っている' do
212         r = LicenseGroup.import(@fes)
213         r[0].is_a?(LicenseGroup).should be_true
214         r[0]["name"].should eq 'UnknownUrl'
215         r[1].is_a?(LicenseGroup).should be_true
216         r[1]["name"].should eq 'UnknownClassname'
217       end
218     end
219   end
220   
221 end