describe LicenseGroup do
before do
+ SpeechBalloonTemplate.delete_all
#テストデータを用意してね
@f = Rails.root + 'spec/json/license_group.json'
@t = File.open(@f, 'r').read
@j = JSON.parse @t
+ @fs = Rails.root + 'spec/json/license_groups.json'
+ @fes = Rails.root + 'spec/json/invalid_license_groups.json'
end
describe '検証に於いて' do
before do
+ @lg = FactoryGirl.build :license_group
end
- it 'オーソドックスなデータなら通る' do
- @lg = Factory.build :license_group
- @lg.should be_valid
- end
-
- context 'nameを検証するとき' do
- before do
- @lg = Factory.build :license_group
+ context 'オーソドックスなデータのとき' do
+ it '下限データが通る' do
+ @lg.name = 'a'
+ @lg.classname = 'a'
+ @lg.caption = 'a'
+ @lg.url = 'http://test.jp/'
+ @lg.should be_valid
end
- it 'テストデータの確認' do
+ it '上限データが通る' do
@lg.name = 'a'*50
+ @lg.classname = 'a'*50
+ @lg.caption = 'a'*30
+ @lg.url = 'http://test.jp/aaaaa' + 'a' * 180
@lg.should be_valid
end
+ end
+
+ context 'nameを検証するとき' do
it 'nullなら失敗する' do
@lg.name = ''
@lg.should_not be_valid
@lg.should_not be_valid
end
it '重複していたら失敗する' do
- l = Factory :license_group
+ l = FactoryGirl.create :license_group
@lg.should_not be_valid
end
end
context 'classnameを検証するとき' do
- before do
- @lg = Factory.build :license_group
- end
- it 'テストデータの確認' do
- @lg.classname = 'a'*50
- @lg.should be_valid
- end
it 'nullなら失敗する' do
@lg.classname = ''
@lg.should_not be_valid
end
end
context 'captionを検証するとき' do
- before do
- @lg = Factory.build :license_group
- end
- it 'テストデータの確認' do
- @lg.caption = 'a'*30
- @lg.should be_valid
- end
it 'nullなら失敗する' do
@lg.caption = ''
@lg.should_not be_valid
end
end
context 'urlを検証するとき' do
- before do
- @lg = Factory.build :license_group
- end
- it 'テストデータの確認' do
- @lg.url = 'http://test.com/'
- @lg.should be_valid
- end
it 'nullなら失敗する' do
@lg.url = ''
@lg.should_not be_valid
end
end
+ describe '文字コード検証に於いて' do
+ before do
+ @lg = FactoryGirl.build :license_group
+ end
+
+ context 'nameを検証するとき' do
+ it 'Shift JISなら失敗する' do
+ @lg.name = "\x83G\x83r\x83]\x83D"
+ lambda{
+ @lg.valid_encode
+ }.should raise_error(Pettanr::BadRequest)
+ end
+ end
+
+ context 'classnameを検証するとき' do
+ it 'Shift JISなら失敗する' do
+ @lg.classname = "\x83G\x83r\x83]\x83D"
+ lambda{
+ @lg.valid_encode
+ }.should raise_error(Pettanr::BadRequest)
+ end
+ end
+
+ context 'captionを検証するとき' do
+ it 'Shift JISなら失敗する' do
+ @lg.caption = "\x83G\x83r\x83]\x83D"
+ lambda{
+ @lg.valid_encode
+ }.should raise_error(Pettanr::BadRequest)
+ end
+ end
+
+ context 'urlを検証するとき' do
+ it 'Shift JISなら失敗する' do
+ @lg.url = "\x83G\x83r\x83]\x83D"
+ lambda{
+ @lg.valid_encode
+ }.should raise_error(Pettanr::BadRequest)
+ end
+ end
+ end
+
+ describe 'デフォルト値補充に於いて' do
+ it 'defined' do
+ @lg = FactoryGirl.build :license_group, :name => "1"
+ @lg.supply_default
+ end
+ end
+
+ describe '上書き補充に於いて' do
+ it 'defined' do
+ @lg = FactoryGirl.build :license_group, :name => "1"
+ @lg.overwrite
+ end
+ end
+
+ describe '閲覧許可に於いて' do
+ #ライセンスグループは作家作成する前から存在するので、閲覧制限の意味がない
+ end
+
+ describe '一覧取得に於いて' do
+ before do
+ @lg = FactoryGirl.create :license_group, :name => "1"
+ end
+ context 'つつがなく終わるとき' do
+ it '一覧取得オプションを利用している' do
+ LicenseGroup.stub(:list_opt).with(any_args).and_return({})
+ LicenseGroup.should_receive(:list_opt).with(any_args).exactly(1)
+ r = LicenseGroup.list
+ end
+ end
+ it 'リストを返す' do
+ l = LicenseGroup.list
+ l.should eq [@lg]
+ end
+ it '名前順で並んでいる' do
+ @lg2 = FactoryGirl.create :license_group, :name => "5", :url => 'http://test.ptn/10'
+ l = LicenseGroup.list
+ l.should eq [@lg, @lg2]
+ end
+ end
+ describe '一覧取得オプションに於いて' do
+ it 'includeキーを含んでいる' do
+ r = LicenseGroup.list_opt
+ r.has_key?(:include).should be_true
+ end
+ it '1つの項目を含んでいる' do
+ r = LicenseGroup.list_opt[:include]
+ r.should have(1).items
+ end
+ it 'ライセンスを含んでいる' do
+ r = LicenseGroup.list_opt[:include]
+ r.has_key?(:licenses).should be_true
+ end
+ end
+ describe 'json一覧出力オプションに於いて' do
+ before do
+ @sp = FactoryGirl.create :system_picture
+ @lg = FactoryGirl.create :license_group
+ @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+ @user = FactoryGirl.create( :user_yas)
+ @author = FactoryGirl.create :author, :user_id => @user.id
+ @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
+ @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+ @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
+ @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
+ @sbt = FactoryGirl.create :speech_balloon_template
+ @scroll = FactoryGirl.create :scroll, :author_id => @author.id, :visible => 1
+ @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
+ @scroll_panel = FactoryGirl.create :scroll_panel, :author_id => @author.id, :scroll_id => @scroll.id, :panel_id => @panel.id
+ end
+ it 'ライセンスを含んでいる' do
+ r = LicenseGroup.list.to_json LicenseGroup.list_json_opt
+ j = JSON.parse r
+ i = j.first
+ i.has_key?('licenses').should be_true
+ end
+ end
+
+ describe '単体取得に於いて' do
+ before do
+ @lg = FactoryGirl.create :license_group
+ end
+ context 'つつがなく終わるとき' do
+ it '単体取得オプションを利用している' do
+ LicenseGroup.stub(:show_opt).with(any_args).and_return({})
+ LicenseGroup.should_receive(:show_opt).with(any_args).exactly(1)
+ r = LicenseGroup.show @lg.id
+ end
+ end
+ it '指定のコマを返す' do
+ l = LicenseGroup.show @lg.id
+ l.should eq @lg
+ end
+ context '存在しないライセンスグループを開こうとしたとき' do
+ it '404RecordNotFound例外を返す' do
+ lambda{
+ LicenseGroup.show 110
+ }.should raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
+ end
+ describe '単体取得オプションに於いて' do
+ it 'includeキーを含んでいる' do
+ r = LicenseGroup.show_opt
+ r.has_key?(:include).should be_true
+ end
+ it '1つの項目を含んでいる' do
+ r = LicenseGroup.show_opt[:include]
+ r.should have(1).items
+ end
+ it 'ライセンスを含んでいる' do
+ r = LicenseGroup.show_opt[:include]
+ r.has_key?(:licenses).should be_true
+ end
+ end
+ describe 'json単体出力オプションに於いて' do
+ before do
+ @sp = FactoryGirl.create :system_picture
+ @lg = FactoryGirl.create :license_group
+ @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+ @user = FactoryGirl.create( :user_yas)
+ @author = FactoryGirl.create :author, :user_id => @user.id
+ @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
+ @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+ @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
+ @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
+ @sbt = FactoryGirl.create :speech_balloon_template
+ @scroll = FactoryGirl.create :scroll, :author_id => @author.id, :visible => 1
+ @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
+ @scroll_panel = FactoryGirl.create :scroll_panel, :author_id => @author.id, :scroll_id => @scroll.id, :panel_id => @panel.id
+ end
+ it 'ライセンスを含んでいる' do
+ r = LicenseGroup.show(@lg.id).to_json LicenseGroup.show_json_opt
+ j = JSON.parse r
+ i = j
+ i.has_key?('licenses').should be_true
+ end
+ end
+
+ describe '更新に於いて' do
+ before do
+ @n = @j.keys.first
+ @a = @j.values.first
+ @imager = ImagerTest.load("abc\ndef\nghi")
+ PettanImager.stub(:load).with(any_args).and_return(@imager)
+ end
+ context 'つつがなく終わるとき' do
+ it 'データ更新準備を依頼する' do
+ LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
+ LicenseGroup.should_receive(:modify_object).with(any_args).exactly(1)
+ License.stub(:stores).with(any_args).and_return(0)
+ LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
+ r = LicenseGroup.store(@n, @a)
+ end
+ it 'ライセンスに複数の更新を依頼する' do
+ LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
+ License.stub(:stores).with(any_args).and_return(0)
+ License.should_receive(:stores).with(any_args).exactly(1)
+ LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
+ r = LicenseGroup.store(@n, @a)
+ end
+ it '保存を依頼する' do
+ LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
+ License.stub(:stores).with(any_args).and_return(0)
+ LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
+ LicenseGroup.any_instance.should_receive(:save).with(any_args).exactly(1)
+ r = LicenseGroup.store(@n, @a)
+ end
+ it 'オブジェクトを返す' do
+ r = LicenseGroup.store(@n, @a)
+ r.is_a?(LicenseGroup).should be_true
+ r.name.should eq @n
+ r.url.should eq @a["url"]
+ end
+ it 'カラム値からlicenses_attributesが削除されている' do
+ @a["licenses_attributes"].should_not be_nil
+ r = LicenseGroup.store(@n, @a)
+ @a["licenses_attributes"].should be_nil
+ end
+ end
+ context 'ライセンス複数更新が失敗するとき' do
+ before do
+ LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
+ License.stub(:stores).with(any_args).and_return(1)
+ LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
+ end
+ it '全体エラーメッセージがセットされている' do
+ r = LicenseGroup.store(@n, @a)
+ r.errors[:base].should_not be_blank
+ end
+ it 'ライセンスが作成されていない' do
+ lambda {
+ r = LicenseGroup.store(@n, @a)
+ }.should_not change License, :count
+ end
+ it 'ライセンスグループが作成されていない' do
+ lambda {
+ r = LicenseGroup.store(@n, @a)
+ }.should_not change LicenseGroup, :count
+ end
+ end
+ end
+
describe 'インポートに於いて' do
before do
end
context 'つつがなく終わるとき' do
- it 'ã\83\86ã\82ã\82¹ã\83\88インポートを依頼する' do
- CommonLicense.should_receive(:parse).with(any_args).exactly(1)
- CommonLicense.stub(:parse).with(any_args).and_return(@j)
- CommonLicense.import(@t)
+ it 'ã\83\95ã\82¡ã\82¤ã\83«インポートを依頼する' do
+ LicenseGroup.should_receive(:import_file).with(any_args).exactly(1)
+ LicenseGroup.stub(:import_file).with(any_args).and_return([])
+ LicenseGroup.import(@f)
end
it 'ライセンスグループ更新を一回依頼する' do
- CommonLicense.stub(:store).with(any_args).and_return(CommonLicense.new)
- CommonLicense.any_instance.stub(:valid?).with(any_args).and_return(true)
- CommonLicense.should_receive(:store).with(any_args).exactly(1)
- CommonLicense.import(@t)
+ LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
+ LicenseGroup.should_receive(:store).with(any_args).exactly(1)
+ LicenseGroup.import(@f)
end
it 'ライセンスグループが追加される' do
lambda {
- CommonLicense.import(@t)
- }.should change CommonLicense, :count
+ LicenseGroup.import(@f)
+ }.should change LicenseGroup, :count
+ end
+ it 'ライセンスが追加される' do
+ lambda {
+ LicenseGroup.import(@f)
+ }.should change License, :count
end
it '[]を返す' do
- CommonLicense.import(@t).should eq []
+ r = LicenseGroup.import(@f)
+ r.should eq []
end
end
context '複数データがつつがなく終わるとき' do
it 'ライセンスグループ更新を二回依頼する' do
- CommonLicense.stub(:store).with(any_args).and_return(CommonLicense.new)
- CommonLicense.any_instance.stub(:valid?).with(any_args).and_return(true)
- CommonLicense.should_receive(:store).with(any_args).exactly(2)
- CommonLicense.import(@ts)
+ LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
+ LicenseGroup.should_receive(:store).with(any_args).exactly(2)
+ LicenseGroup.import(@fs)
end
it 'ライセンスグループが二個追加される' do
lambda {
- CommonLicense.import(@ts)
- }.should change(CommonLicense, :count).by 2
+ r = LicenseGroup.import(@fs)
+ }.should change(LicenseGroup, :count).by 2
+ end
+ it 'ライセンスが追加される' do
+ lambda {
+ r = LicenseGroup.import(@fs)
+ }.should change(License, :count)
end
it '[]を返す' do
- CommonLicense.import(@ts).should eq []
+ r = LicenseGroup.import(@fs)
+ r.should eq []
end
end
- context 'ã\82³ã\83¢ã\83³ã\83©ã\82¤ã\82»ã\83³ã\82¹作成に失敗したとき' do
+ context 'ã\83©ã\82¤ã\82»ã\83³ã\82¹ã\82°ã\83«ã\83¼ã\83\97作成に失敗したとき' do
before do
- CommonLicense.any_instance.stub(:save).with(any_args).and_return(false)
- CommonLicense.any_instance.stub(:valid?).with(any_args).and_return(false)
+ LicenseGroup.any_instance.stub(:save).with(any_args).and_return(false)
+ LicenseGroup.any_instance.stub(:valid?).with(any_args).and_return(false)
end
- it 'ã\82³ã\83¢ã\83³ã\83©ã\82¤ã\82»ã\83³ã\82¹の数に変化がない' do
+ it 'ã\83©ã\82¤ã\82»ã\83³ã\82¹ã\82°ã\83«ã\83¼ã\83\97の数に変化がない' do
lambda {
- CommonLicense.import(@t)
- }.should_not change CommonLicense, :count
+ LicenseGroup.import(@f)
+ }.should_not change LicenseGroup, :count
end
it '配列を返す' do
- r = CommonLicense.import(@t)
+ r = LicenseGroup.import(@f)
r.is_a?(Array).should be_true
end
it '配列の中身は一件' do
- r = CommonLicense.import(@t)
+ r = LicenseGroup.import(@f)
r.should have(1).items
end
- it 'ã\82³ã\83¢ã\83³ã\83©ã\82¤ã\82»ã\83³ã\82¹オブジェクトが入っている' do
- r = CommonLicense.import(@t)
- r.first.is_a?(CommonLicense).should be_true
+ it 'ã\83©ã\82¤ã\82»ã\83³ã\82¹ã\82°ã\83«ã\83¼ã\83\97オブジェクトが入っている' do
+ r = LicenseGroup.import(@f)
+ r.first.is_a?(LicenseGroup).should be_true
end
end
- context 'è¤\87æ\95°ã\81®ã\82³ã\83¢ã\83³ã\83©ã\82¤ã\82»ã\83³ã\82¹作成に失敗したとき' do
+ context 'è¤\87æ\95°ã\81®ã\83©ã\82¤ã\82»ã\83³ã\82¹ã\82°ã\83«ã\83¼ã\83\97作成に失敗したとき' do
#三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
- it 'ã\82³ã\83¢ã\83³ã\83©ã\82¤ã\82»ã\83³ã\82¹の数に変化がない' do
+ it 'ã\83©ã\82¤ã\82»ã\83³ã\82¹ã\82°ã\83«ã\83¼ã\83\97の数に変化がない' do
lambda {
- CommonLicense.import(@tes)
- }.should_not change CommonLicense, :count
+ LicenseGroup.import(@fes)
+ }.should_not change LicenseGroup, :count
end
it '途中で保存に失敗しても全件更新依頼する' do
- CommonLicense.stub(:store).with(any_args).and_return(CommonLicense.new)
- CommonLicense.should_receive(:store).with(any_args).exactly(3)
- CommonLicense.import(@tes)
+ LicenseGroup.stub(:store).with(any_args).and_return(LicenseGroup.new)
+ LicenseGroup.should_receive(:store).with(any_args).exactly(3)
+ LicenseGroup.import(@fes)
end
it '配列を返す' do
- r = CommonLicense.import(@tes)
+ r = LicenseGroup.import(@fes)
r.is_a?(Array).should be_true
end
it '配列の中身は2件' do
- r = CommonLicense.import(@tes)
+ r = LicenseGroup.import(@fes)
r.should have(2).items
end
- it 'é\85\8då\88\97ã\81®ä¸èº«ã\81¯å¤±æ\95\97ã\81\97ã\81\9fã\82³ã\83¢ã\83³ã\83©ã\82¤ã\82»ã\83³ã\82¹オブジェクトが入っている' do
- r = CommonLicense.import(@tes)
- r[0].is_a?(CommonLicense).should be_true
- r[0]["name"].should eq 'fail1'
- r[1].is_a?(CommonLicense).should be_true
- r[1]["name"].should eq 'fail2'
+ it 'é\85\8då\88\97ã\81®ä¸èº«ã\81¯å¤±æ\95\97ã\81\97ã\81\9fã\83©ã\82¤ã\82»ã\83³ã\82¹ã\82°ã\83«ã\83¼ã\83\97オブジェクトが入っている' do
+ r = LicenseGroup.import(@fes)
+ r[0].is_a?(LicenseGroup).should be_true
+ r[0]["name"].should eq 'UnknownUrl'
+ r[1].is_a?(LicenseGroup).should be_true
+ r[1]["name"].should eq 'UnknownClassname'
end
end
end