OSDN Git Service

t#31223:add them contents list for author and artist
[pettanr/pettanr.git] / spec / models / provider_artist_spec.rb
1 # -*- encoding: utf-8 -*-
2 #絵師対照表
3 require 'spec_helper'
4
5 describe ProviderArtist do
6   before do
7     @admin = FactoryGirl.create :admin
8     @sp = FactoryGirl.create :system_picture
9     @lg = FactoryGirl.create :license_group
10     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
11     @user = FactoryGirl.create :user_yas
12     @author = FactoryGirl.create :author, :user_id => @user.id
13     @artist = FactoryGirl.create :artist, :author_id => @author.id
14     @other_user = FactoryGirl.create :user_yas
15     @other_author = FactoryGirl.create :author, :user_id => @other_user.id
16     @provider_status = FactoryGirl.create :provider_status
17     @provider = FactoryGirl.create :provider, :provider_status_id => @provider_status.id
18   end
19   describe '検証に於いて' do
20     before do
21       @pa = FactoryGirl.build :provider_artist, :provider_id => @provider.id, :providers_artist_id => 2, :demanders_artist_id => @artist.id
22     end
23     
24     context 'オーソドックスなデータのとき' do
25       it '下限データが通る' do
26         @pa.should be_valid
27       end
28       it '上限データが通る' do
29         @pa.should be_valid
30       end
31     end
32     
33     context 'provider_idを検証するとき' do
34       it 'nullなら失敗する' do
35         @pa.provider_id = nil
36         @pa.should_not be_valid
37       end
38       it '数値でなければ失敗する' do
39         @pa.provider_id = 'a'
40         @pa.should_not be_valid
41       end
42       it '存在する貸手でなければ失敗する' do
43         @pa.provider_id = 0
44         @pa.should_not be_valid
45       end
46     end
47     context 'providers_artist_idを検証するとき' do
48       it 'nullなら失敗する' do
49         @pa.providers_artist_id = nil
50         @pa.should_not be_valid
51       end
52       it '数値でなければ失敗する' do
53         @pa.providers_artist_id = 'a'
54         @pa.should_not be_valid
55       end
56     end
57     context 'demanders_artist_idを検証するとき' do
58       it 'nullなら失敗する' do
59         @pa.demanders_artist_id = nil
60         @pa.should_not be_valid
61       end
62       it '数値でなければ失敗する' do
63         @pa.demanders_artist_id = 'a'
64         @pa.should_not be_valid
65       end
66     end
67   end
68   
69   describe '対照表取得に於いて' do
70     before do
71       @pa = FactoryGirl.create :provider_artist, :provider_id => @provider.id, :providers_artist_id => 2, :demanders_artist_id => @artist.id
72     end
73     it '取得した対照表を返す' do
74       r = ProviderArtist.get_one @provider.id, 2
75       r.should be_true
76     end
77     it '該当する対照表が存在しなかったら、新規に興した対照表オブジェクトを返す' do
78       r = ProviderArtist.get_one @provider.id, 1
79       r.should be_a_new ProviderArtist
80       r = ProviderArtist.get_one 0, 2
81       r.should be_a_new ProviderArtist
82     end
83   end
84   
85   describe '貸手側絵師同期に於いて' do
86     before do
87       @pa = FactoryGirl.create :provider_artist, :provider_id => @provider.id, :providers_artist_id => 2, :demanders_artist_id => @artist.id
88       @newpa = ProviderArtist.new
89     end
90     context '自身に絵師がリンクしてないとき' do
91       before do
92       end
93       it '絵師オブジェクトを新規に興す' do
94         lambda {
95           r = @newpa.modify_artist @artist.attributes
96         }.should change Artist, :count
97       end
98     end
99     context '自身に絵師がリンクしているとき' do
100       it '自身から絵師を取得する' do
101         lambda {
102           r = @pa.modify_artist @artist.attributes
103         }.should_not change Artist, :count
104       end
105     end
106     it '貸手側絵師カラム値から絵師名をセットする 絵師オブジェクトを保存する' do
107       r = @pa.modify_artist :name => 'artist2'
108       @artist.reload
109       @artist.name.should eq 'artist2'
110     end
111     it '作家idをクリアする。(外絵師の証明)' do
112       r = @pa.modify_artist :name => 'artist2'
113       @artist.reload
114       @artist.author_id.should be_blank
115     end
116     it '絵師オブジェクトを返す' do
117       r = @pa.modify_artist @artist.attributes
118       r.should eq @artist
119     end
120   end
121
122   describe 'インポートに於いて' do
123     before do
124       @artist2 = FactoryGirl.create :artist, :author_id => @other_author.id, :name => 'artist2'
125     end
126     context '事前チェックする' do
127       before do
128         ProviderArtist.stub(:get_one).with(any_args).and_return(ProviderArtist.new)
129         ProviderArtist.any_instance.stub(:modify_artist).with(any_args).and_return(@artist)
130         ProviderArtist.any_instance.stub(:save).with(any_args).and_return(true)
131       end
132       it '対照表取得を問い合わせている' do
133         ProviderArtist.should_receive(:get_one).with(any_args).exactly(1)
134         r = ProviderArtist.import @provider.id, [@artist.attributes]
135       end
136       it '対照表に絵師同期を依頼してしている' do
137         ProviderArtist.any_instance.should_receive(:modify_artist).with(any_args).exactly(1)
138         r = ProviderArtist.import @provider.id, [@artist.attributes]
139       end
140       it '対照表オブジェクトを保存している' do
141         ProviderArtist.any_instance.should_receive(:save).with(any_args).exactly(1)
142         r = ProviderArtist.import @provider.id, [@artist.attributes]
143       end
144     end
145     context 'つつがなく終わるとき' do
146       it '空っぽの配列を返す' do
147         r = ProviderArtist.import @provider.id, [@artist.attributes]
148         r.should be_empty
149       end
150       it '対照表が追加される' do
151         lambda {
152           r = ProviderArtist.import @provider.id, [@artist.attributes]
153         }.should change ProviderArtist, :count
154       end
155     end
156     context '複数インポートのとき' do
157       it '空っぽの配列を返す' do
158         r = ProviderArtist.import @provider.id, [@artist.attributes, @artist2.attributes]
159         r.should be_empty
160       end
161       it '対照表が追加される' do
162         lambda {
163           r = ProviderArtist.import @provider.id, [@artist.attributes, @artist2.attributes]
164         }.should change(ProviderArtist, :count).by(2)
165       end
166     end
167     #警告ケース
168     context '対照表オブジェクトの保存に失敗したとき' do
169       before do
170         ProviderArtist.any_instance.stub(:save).with(any_args).and_return(false)
171       end
172      it '結果に貸手側絵師のカラム値を追加している' do
173         r = ProviderArtist.import @provider.id, [@artist.attributes]
174         r.should_not be_empty
175       end
176     end
177     context '絵師オブジェクトの保存に失敗したとき' do
178       before do
179         Artist.any_instance.stub(:save).with(any_args).and_return(false)
180       end
181       it '結果に貸手側絵師のカラム値を追加している' do
182         r = ProviderArtist.import @provider.id, [@artist.attributes]
183         r.should_not be_empty
184       end
185     end
186   end
187 end