OSDN Git Service

miss edit
[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   end
12   describe '検証に於いて' do
13     before do
14     end
15     
16     it 'オーソドックスなデータなら通る' do
17       @lg = Factory.build :license_group
18       @lg.should be_valid
19     end
20     
21     context 'nameを検証するとき' do
22       before do
23         @lg = Factory.build :license_group
24       end
25       it 'テストデータの確認' do
26         @lg.name = 'a'*50
27         @lg.should be_valid
28       end
29       it 'nullなら失敗する' do
30         @lg.name = ''
31         @lg.should_not be_valid
32       end
33       it '51文字以上なら失敗する' do
34         @lg.name = 'a'*51
35         @lg.should_not be_valid
36       end
37       it '重複していたら失敗する' do
38         l = Factory :license_group
39         @lg.should_not be_valid
40       end
41     end
42     context 'classnameを検証するとき' do
43       before do
44         @lg = Factory.build :license_group
45       end
46       it 'テストデータの確認' do
47         @lg.classname = 'a'*50
48         @lg.should be_valid
49       end
50       it 'nullなら失敗する' do
51         @lg.classname = ''
52         @lg.should_not be_valid
53       end
54       it '51文字以上なら失敗する' do
55         @lg.classname = 'a'*51
56         @lg.should_not be_valid
57       end
58     end
59     context 'captionを検証するとき' do
60       before do
61         @lg = Factory.build :license_group
62       end
63       it 'テストデータの確認' do
64         @lg.caption = 'a'*30
65         @lg.should be_valid
66       end
67       it 'nullなら失敗する' do
68         @lg.caption = ''
69         @lg.should_not be_valid
70       end
71       it '51文字以上なら失敗する' do
72         @lg.caption = 'a'*51
73         @lg.should_not be_valid
74       end
75     end
76     context 'urlを検証するとき' do
77       before do
78         @lg = Factory.build :license_group
79       end
80       it 'テストデータの確認' do
81         @lg.url = 'http://test.com/'
82         @lg.should be_valid
83       end
84       it 'nullなら失敗する' do
85         @lg.url = ''
86         @lg.should_not be_valid
87       end
88       it '201文字以上なら失敗する' do
89         @lg.url = 'a'*201
90         @lg.should_not be_valid
91       end
92       it 'url形式でないなら失敗する' do
93         @lg.url = 'aaaaaaa'
94         @lg.should_not be_valid
95       end
96     end
97   end
98   
99   describe 'インポートに於いて' do
100     before do
101     end
102     context 'つつがなく終わるとき' do
103       it 'テキストインポートを依頼する' do
104         CommonLicense.should_receive(:parse).with(any_args).exactly(1)
105         CommonLicense.stub(:parse).with(any_args).and_return(@j)
106         CommonLicense.import(@t)
107       end
108       it 'ライセンスグループ更新を一回依頼する' do
109         CommonLicense.stub(:store).with(any_args).and_return(CommonLicense.new)
110         CommonLicense.any_instance.stub(:valid?).with(any_args).and_return(true)
111         CommonLicense.should_receive(:store).with(any_args).exactly(1)
112         CommonLicense.import(@t)
113       end
114       it 'ライセンスグループが追加される' do
115         lambda {
116           CommonLicense.import(@t)
117         }.should change CommonLicense, :count
118       end
119       it '[]を返す' do
120         CommonLicense.import(@t).should eq []
121       end
122     end
123     context '複数データがつつがなく終わるとき' do
124       it 'ライセンスグループ更新を二回依頼する' do
125         CommonLicense.stub(:store).with(any_args).and_return(CommonLicense.new)
126         CommonLicense.any_instance.stub(:valid?).with(any_args).and_return(true)
127         CommonLicense.should_receive(:store).with(any_args).exactly(2)
128         CommonLicense.import(@ts)
129       end
130       it 'ライセンスグループが二個追加される' do
131         lambda {
132           CommonLicense.import(@ts)
133         }.should change(CommonLicense, :count).by 2
134       end
135       it '[]を返す' do
136         CommonLicense.import(@ts).should eq []
137       end
138     end
139     context 'コモンライセンス作成に失敗したとき' do
140       before do
141         CommonLicense.any_instance.stub(:save).with(any_args).and_return(false)
142         CommonLicense.any_instance.stub(:valid?).with(any_args).and_return(false)
143       end
144       it 'コモンライセンスの数に変化がない' do
145         lambda {
146           CommonLicense.import(@t)
147         }.should_not change CommonLicense, :count
148       end
149       it '配列を返す' do
150         r = CommonLicense.import(@t)
151         r.is_a?(Array).should be_true
152       end
153       it '配列の中身は一件' do
154         r = CommonLicense.import(@t)
155         r.should have(1).items
156       end
157       it 'コモンライセンスオブジェクトが入っている' do
158         r = CommonLicense.import(@t)
159         r.first.is_a?(CommonLicense).should be_true
160       end
161     end
162     context '複数のコモンライセンス作成に失敗したとき' do
163       #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
164       it 'コモンライセンスの数に変化がない' do
165         lambda {
166           CommonLicense.import(@tes)
167         }.should_not change CommonLicense, :count
168       end
169       it '途中で保存に失敗しても全件更新依頼する' do
170         CommonLicense.stub(:store).with(any_args).and_return(CommonLicense.new)
171         CommonLicense.should_receive(:store).with(any_args).exactly(3)
172         CommonLicense.import(@tes)
173       end
174       it '配列を返す' do
175         r = CommonLicense.import(@tes)
176         r.is_a?(Array).should be_true
177       end
178       it '配列の中身は2件' do
179         r = CommonLicense.import(@tes)
180         r.should have(2).items
181       end
182       it '配列の中身は失敗したコモンライセンスオブジェクトが入っている' do
183         r = CommonLicense.import(@tes)
184         r[0].is_a?(CommonLicense).should be_true
185         r[0]["name"].should eq 'fail1'
186         r[1].is_a?(CommonLicense).should be_true
187         r[1]["name"].should eq 'fail2'
188       end
189     end
190   end
191   
192 end