--- /dev/null
+# -*- encoding: utf-8 -*-
+require 'spec_helper'
+#用紙
+
+describe Sheet do
+ before do
+ SpeechBalloonTemplate.delete_all
+ @admin = FactoryGirl.create :admin
+ @demand_user = FactoryGirl.create :demand_user
+ @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
+ @other_user = FactoryGirl.create( :user_yas)
+ @other_author = FactoryGirl.create :author, :user_id => @other_user.id
+ @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
+ end
+
+ describe '検証に於いて' do
+ before do
+ @sheet = FactoryGirl.build :sheet, :author_id => @author.id
+ end
+
+ context 'オーソドックスなデータのとき' do
+ it '下限データが通る' do
+ @sheet.caption = 'a'
+ @sheet.width = 1
+ @sheet.height = 1
+ @sheet.visible = 0
+ @sheet.should be_valid
+ end
+ it '上限データが通る' do
+ @sheet.caption = 'a'*100
+ @sheet.width = 99999
+ @sheet.height = 99999
+ @sheet.visible = 1
+ @sheet.should be_valid
+ end
+ end
+
+ context 'captionを検証するとき' do
+ it 'nullなら失敗する' do
+ @sheet.caption = nil
+ @sheet.should_not be_valid
+ end
+ it '100文字以上なら失敗する' do
+ @sheet.caption = 'a'*101
+ @sheet.should_not be_valid
+ end
+ end
+ context 'widthを検証するとき' do
+ it 'nullなら失敗する' do
+ @sheet.width = nil
+ @sheet.should_not be_valid
+ end
+ it '数値でなければ失敗する' do
+ @sheet.width = 'a'
+ @sheet.should_not be_valid
+ end
+ it '0なら失敗する' do
+ @sheet.width = '0'
+ @sheet.should_not be_valid
+ end
+ it '負でも失敗する' do
+ @sheet.width = -1
+ @sheet.should_not be_valid
+ end
+ end
+ context 'heightを検証するとき' do
+ it 'nullなら失敗する' do
+ @sheet.height = nil
+ @sheet.should_not be_valid
+ end
+ it '数値でなければ失敗する' do
+ @sheet.height = 'a'
+ @sheet.should_not be_valid
+ end
+ it '0なら失敗する' do
+ @sheet.height = '0'
+ @sheet.should_not be_valid
+ end
+ it '負でも失敗する' do
+ @sheet.height = -1
+ @sheet.should_not be_valid
+ end
+ end
+ context 'visibleを検証するとき' do
+ it 'nullなら失敗する' do
+ @sheet.visible = nil
+ @sheet.should_not be_valid
+ end
+ it '負なら失敗する' do
+ @sheet.visible = -1
+ @sheet.should_not be_valid
+ end
+ it '2以上なら失敗する' do
+ @sheet.visible = 2
+ @sheet.should_not be_valid
+ end
+ end
+ end
+
+ describe '文字コード検証に於いて' do
+ before do
+ @sheet = FactoryGirl.build :sheet, :author_id => @author.id
+ end
+
+ context 'captionを検証するとき' do
+ it 'Shift JISなら失敗する' do
+ @sheet.caption = "\x83G\x83r\x83]\x83D"
+ lambda{
+ @sheet.valid_encode
+ }.should raise_error(Pettanr::BadRequest)
+ end
+ end
+
+ end
+
+ describe 'デフォルト値補充に於いて' do
+ it 'visibleが0になっている' do
+ @sheet = FactoryGirl.build :sheet, :visible => nil
+ @sheet.supply_default
+ @sheet.visible.should eq 0
+ end
+ end
+
+ describe '上書き補充に於いて' do
+ it '作家idが設定されている' do
+ @sheet = FactoryGirl.build :sheet, :author_id => nil
+ @sheet.overwrite @author
+ @sheet.author_id.should eq @author.id
+ end
+ end
+
+ describe '所持判定に於いて' do
+ before do
+ @sheet = FactoryGirl.build :sheet, :author_id => @author.id
+ end
+ context '事前チェックする' do
+ it '自身にロールリストからの作家取得を依頼している' do
+ Sheet.should_receive(:get_author_from_roles).with(any_args).exactly(1)
+ r = @sheet.own?([@author])
+ end
+ end
+ context 'ロール内作家が取得できるとき' do
+ before do
+ end
+ it 'ロール内作家のidが自身の作家idと一致するなら許可する' do
+ Sheet.stub(:get_author_from_roles).with(any_args).and_return(@author)
+ r = @sheet.own?([@author])
+ r.should be_true
+ end
+ it 'ロール内作家のidが自身の作家idと一致しないならno' do
+ Sheet.stub(:get_author_from_roles).with(any_args).and_return(@other_author)
+ @sheet.own?(@other_author).should be_false
+ end
+ end
+ context 'ロール内作家が取得できないとき' do
+ before do
+ Sheet.stub(:get_author_from_roles).with(any_args).and_return(nil)
+ end
+ it 'Falseを返す' do
+ r = @sheet.own?([@author])
+ r.should be_false
+ end
+ end
+ end
+
+ describe '閲覧許可に於いて' do
+ before do
+ @sheet = FactoryGirl.build :sheet, :author_id => @author.id
+ end
+ context 'オープンモードのとき' do
+ before do
+ MagicNumber['run_mode'] = 0
+ end
+ it '自身にゲスト用ロールチェックを問い合わせしている' do
+ Sheet.any_instance.stub(:guest_role_check).and_return(true)
+ Sheet.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
+ r = @sheet.visible?([@author])
+ end
+ it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
+ Sheet.any_instance.stub(:guest_role_check).and_return(false)
+ r = @sheet.visible?([@author])
+ r.should be_false
+ end
+ end
+ context 'クローズドモードのとき' do
+ before do
+ MagicNumber['run_mode'] = 1
+ end
+ it '自身に読者用ロールチェックを問い合わせしている' do
+ Sheet.any_instance.stub(:reader_role_check).and_return(true)
+ Sheet.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
+ r = @sheet.visible?([@author])
+ end
+ it '読者用ロールチェックが失敗したとき、falseを返す' do
+ Sheet.any_instance.stub(:reader_role_check).and_return(false)
+ r = @sheet.visible?([@author])
+ r.should be_false
+ end
+ end
+ context '事前チェックする' do
+ before do
+ MagicNumber['run_mode'] = 1
+ Sheet.any_instance.stub(:reader_role_check).and_return(true)
+ Sheet.any_instance.stub(:own?).and_return(true)
+ end
+ it '自身に所持判定を問い合わせしている' do
+ Sheet.any_instance.should_receive(:own?).with(any_args).exactly(1)
+ r = @sheet.visible?([@author])
+ end
+ end
+ context 'つつがなく終わるとき' do
+ before do
+ MagicNumber['run_mode'] = 1
+ Sheet.any_instance.stub(:reader_role_check).and_return(true)
+ end
+ it '自分の用紙なら許可する' do
+ Sheet.any_instance.stub(:own?).and_return(true)
+ Sheet.any_instance.stub(:visible).and_return(0)
+ r = @sheet.visible?([@author])
+ r.should be_true
+ end
+ it '他人の非公開用紙なら許可しない' do
+ Sheet.any_instance.stub(:own?).and_return(false)
+ Sheet.any_instance.stub(:visible).and_return(0)
+ r = @sheet.visible?([@author])
+ r.should be_false
+ end
+ it '他人の用紙でも公開なら許可する' do
+ Sheet.any_instance.stub(:own?).and_return(false)
+ Sheet.any_instance.stub(:visible).and_return(1)
+ r = @sheet.visible?([@author])
+ r.should be_true
+ end
+ end
+ end
+
+ describe '一覧取得に於いて' do
+ before do
+ @sheet = FactoryGirl.create :sheet, :author_id => @author.id
+ end
+ context 'page補正について' do
+ it '文字列から数値に変換される' do
+ Sheet.page('8').should eq 8
+ end
+ it 'nilの場合は1になる' do
+ Sheet.page().should eq 1
+ end
+ it '0以下の場合は1になる' do
+ Sheet.page('0').should eq 1
+ end
+ end
+ context 'page_size補正について' do
+ it '文字列から数値に変換される' do
+ Sheet.page_size('7').should eq 7
+ end
+ it 'nilの場合はSheet.default_page_sizeになる' do
+ Sheet.page_size().should eq Sheet.default_page_size
+ end
+ it '0以下の場合はSheet.default_page_sizeになる' do
+ Sheet.page_size('0').should eq Sheet.default_page_size
+ end
+ it 'Sheet.max_page_sizeを超えた場合はSheet.max_page_sizeになる' do
+ Sheet.page_size('1000').should eq Sheet.max_page_size
+ end
+ end
+ context 'つつがなく終わるとき' do
+ it '一覧取得オプションを利用している' do
+ Sheet.stub(:list_opt).with(any_args).and_return({})
+ Sheet.should_receive(:list_opt).with(any_args).exactly(1)
+ r = Sheet.list
+ end
+ end
+ it 'リストを返す' do
+ c = Sheet.list
+ c.should eq [@sheet]
+ end
+ it '非公開用紙は(自分の用紙であっても)含んでいない' do
+ FactoryGirl.create :sheet, :author_id => @author.id, :visible => 0
+ c = Sheet.list
+ c.should eq [@sheet]
+ end
+ it '時系列で並んでいる' do
+ #公開用紙は(他人の用紙であっても)含んでいる
+ v = FactoryGirl.create :sheet, :author_id => @other_author.id, :updated_at => Time.now + 100
+ c = Sheet.list
+ c.should eq [v, @sheet]
+ end
+ context 'DBに5件あって1ページの件数を2件に変えたとして' do
+ before do
+ @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 100
+ @sheet3 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 200
+ @sheet4 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 300
+ @sheet5 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 400
+ Sheet.stub(:default_page_size).and_return(2)
+ end
+ it '通常は2件を返す' do
+ c = Sheet.list
+ c.should have(2).items
+ end
+ it 'page=1なら末尾2件を返す' do
+ #時系列で並んでいる
+ c = Sheet.list(1)
+ c.should eq [@sheet5, @sheet4]
+ end
+ it 'page=2なら中間2件を返す' do
+ c = Sheet.list(2)
+ c.should eq [@sheet3, @sheet2]
+ end
+ it 'page=3なら先頭1件を返す' do
+ c = Sheet.list(3)
+ c.should eq [@sheet]
+ end
+ end
+ end
+
+ describe '自分の用紙一覧取得に於いて' do
+ before do
+ @sheet = FactoryGirl.create :sheet, :author_id => @author.id
+ end
+ context 'つつがなく終わるとき' do
+ it '一覧取得オプションを利用している' do
+ Sheet.stub(:list_opt).with(any_args).and_return({})
+ Sheet.should_receive(:list_opt).with(any_args).exactly(1)
+ r = Sheet.mylist @author
+ end
+ end
+ it 'リストを返す' do
+ c = Sheet.mylist @author
+ c.should eq [@sheet]
+ end
+ it '時系列で並んでいる' do
+ nc = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 100
+ cl = Sheet.mylist @author
+ cl.should eq [nc, @sheet]
+ end
+ it '他人の用紙は公開でも含まない' do
+ nc = FactoryGirl.create :sheet, :author_id => @other_author.id, :visible => 1
+ cl = Sheet.mylist @author
+ cl.should eq [@sheet]
+ end
+ it '自分の用紙は非公開でも含んでいる' do
+ nc = FactoryGirl.create :sheet, :author_id => @author.id, :visible => 0, :updated_at => Time.now + 100
+ cl = Sheet.mylist @author
+ cl.should eq [nc, @sheet]
+ end
+ context 'DBに5件あって1ページの件数を2件に変えたとして' do
+ before do
+ @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 100
+ @sheet3 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 200
+ @sheet4 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 300
+ @sheet5 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 400
+ end
+ it '通常は2件を返す' do
+ c = Sheet.mylist @author, 1, 2
+ c.should have(2).items
+ end
+ it 'page=1なら末尾2件を返す' do
+ #時系列で並んでいる
+ c = Sheet.mylist(@author, 1, 2)
+ c.should eq [@sheet5, @sheet4]
+ end
+ it 'page=2なら中間2件を返す' do
+ c = Sheet.mylist(@author, 2, 2)
+ c.should eq [@sheet3, @sheet2]
+ end
+ it 'page=3なら先頭1件を返す' do
+ c = Sheet.mylist(@author, 3, 2)
+ c.should eq [@sheet]
+ end
+ end
+ end
+
+ describe '他作家の用紙一覧取得に於いて' do
+ before do
+ @sheet = FactoryGirl.create :sheet, :author_id => @author.id
+ @other_sheet = FactoryGirl.create :sheet, :author_id => @other_author.id, :visible => 1
+ end
+ context 'つつがなく終わるとき' do
+ it '一覧取得オプションを利用している' do
+ Sheet.stub(:list_opt).with(any_args).and_return({})
+ Sheet.should_receive(:list_opt).with(any_args).exactly(1)
+ r = Sheet.himlist @other_author
+ end
+ end
+ it '指定した作家のリストを返す' do
+ r = Sheet.himlist @other_author
+ r.should eq [@other_sheet]
+ end
+ it '時系列で並んでいる' do
+ nc = FactoryGirl.create :sheet, :author_id => @other_author.id, :updated_at => Time.now + 100
+ r = Sheet.himlist @other_author
+ r.should eq [nc, @other_sheet]
+ end
+ it '公開用紙に限る ' do
+ hidden = FactoryGirl.create :sheet, :author_id => @other_author.id, :visible => 0
+ r = Sheet.himlist @other_author
+ r.should eq [@other_sheet]
+ end
+ context 'DBに5件あって1ページの件数を2件に変えたとして' do
+ before do
+ @other_sheet2 = FactoryGirl.create :sheet, :author_id => @other_author.id, :updated_at => Time.now + 100
+ @other_sheet3 = FactoryGirl.create :sheet, :author_id => @other_author.id, :updated_at => Time.now + 200
+ @other_sheet4 = FactoryGirl.create :sheet, :author_id => @other_author.id, :updated_at => Time.now + 300
+ @other_sheet5 = FactoryGirl.create :sheet, :author_id => @other_author.id, :updated_at => Time.now + 400
+ end
+ it '通常は2件を返す' do
+ c = Sheet.himlist @other_author, 1, 2
+ c.should have(2).items
+ end
+ it 'page=1なら末尾2件を返す' do
+ #時系列で並んでいる
+ c = Sheet.himlist(@other_author, 1, 2)
+ c.should eq [@other_sheet5, @other_sheet4]
+ end
+ it 'page=2なら中間2件を返す' do
+ c = Sheet.himlist(@other_author, 2, 2)
+ c.should eq [@other_sheet3, @other_sheet2]
+ end
+ it 'page=3なら先頭1件を返す' do
+ c = Sheet.himlist(@other_author, 3, 2)
+ c.should eq [@other_sheet]
+ end
+ end
+ end
+
+ describe '用紙一覧ページ制御に於いて' do
+ before do
+ Sheet.stub(:count).with(any_args).and_return(100)
+ end
+ it 'ページ制御を返す' do
+ r = Sheet.list_paginate
+ r.is_a?(Kaminari::PaginatableArray).should be_true
+ end
+ it '用紙一覧の取得条件を利用している' do
+ Sheet.stub(:list_where).with(any_args).and_return('')
+ Sheet.should_receive(:list_where).with(any_args).exactly(1)
+ r = Sheet.list_paginate
+ end
+ it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
+ r = Sheet.list_paginate 3, 10
+ r.limit_value.should eq 10
+ r.offset_value.should eq 20
+ end
+ end
+
+ describe '自分の用紙一覧ページ制御に於いて' do
+ before do
+ Sheet.stub(:count).with(any_args).and_return(100)
+ end
+ it 'ページ制御を返す' do
+ r = Sheet.mylist_paginate @author
+ r.is_a?(Kaminari::PaginatableArray).should be_true
+ end
+ it '自分の用紙一覧の取得条件を利用している' do
+ Sheet.stub(:mylist_where).with(any_args).and_return('')
+ Sheet.should_receive(:mylist_where).with(any_args).exactly(1)
+ r = Sheet.mylist_paginate @author
+ end
+ it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
+ r = Sheet.mylist_paginate @author, 3, 10
+ r.limit_value.should eq 10
+ r.offset_value.should eq 20
+ end
+ end
+
+ describe '他作家の用紙一覧ページ制御に於いて' do
+ before do
+ Sheet.stub(:count).with(any_args).and_return(100)
+ end
+ it 'ページ制御を返す' do
+ r = Sheet.himlist_paginate @other_author
+ r.is_a?(Kaminari::PaginatableArray).should be_true
+ end
+ it '他作家の用紙一覧の取得条件を利用している' do
+ Sheet.stub(:himlist_where).with(any_args).and_return('')
+ Sheet.should_receive(:himlist_where).with(any_args).exactly(1)
+ r = Sheet.himlist_paginate @other_author
+ end
+ it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
+ r = Sheet.himlist_paginate @other_author, 3, 10
+ r.limit_value.should eq 10
+ r.offset_value.should eq 20
+ end
+ end
+
+ describe '一覧取得オプションに於いて' do
+ it '2つの項目を含んでいる' do
+ r = Sheet.list_opt
+ r.should have(2).items
+ end
+ it 'スクコマを含んでいる' do
+ r = Sheet.list_opt
+ r.has_key?(:sheet_panels).should be_true
+ end
+ it 'スクコマはコマを含んでいる' do
+ r = Sheet.list_opt
+ r[:sheet_panels].has_key?(:panel).should be_true
+ end
+ it '作家を含んでいる' do
+ r = Sheet.list_opt
+ r.has_key?(:author).should be_true
+ end
+ end
+ describe 'json一覧出力オプションに於いて' do
+ before do
+ @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
+ @sheet = FactoryGirl.create :sheet, :author_id => @author.id, :visible => 1
+ @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
+ @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
+ end
+ it 'スクコマを含んでいる' do
+ r = Sheet.list.to_json Sheet.list_json_opt
+ j = JSON.parse r
+ i = j.first
+ i.has_key?('sheet_panels').should be_true
+ end
+ it 'スクコマはコマを含んでいる' do
+ r = Sheet.list.to_json Sheet.list_json_opt
+ j = JSON.parse r
+ i = j.first
+ s = i['sheet_panels'].first
+ s.has_key?('panel').should be_true
+ end
+ it '作家を含んでいる' do
+ r = Sheet.list.to_json Sheet.list_json_opt
+ j = JSON.parse r
+ i = j.first
+ i.has_key?('author').should be_true
+ end
+ end
+
+ describe '単体取得に於いて' do
+ before do
+ @sheet = FactoryGirl.create :sheet, :author_id => @author.id
+ end
+ context 'つつがなく終わるとき' do
+ it '単体取得オプションを利用している' do
+ Sheet.stub(:show_opt).with(any_args).and_return({})
+ Sheet.should_receive(:show_opt).with(any_args).exactly(1)
+ r = Sheet.show @sheet.id, @author
+ end
+ it '閲覧許可を問い合わせている' do
+ Sheet.any_instance.stub(:visible?).with(any_args).and_return(true)
+ Sheet.any_instance.should_receive(:visible?).with(any_args).exactly(1)
+ r = Sheet.show @sheet.id, @author
+ end
+ end
+ it '指定の用紙を返す' do
+ c = Sheet.show @sheet.id, @author
+ c.should eq @sheet
+ end
+ context '閲覧許可が出なかったとき' do
+ it '403Forbidden例外を返す' do
+ Sheet.any_instance.stub(:visible?).and_return(false)
+ lambda{
+ Sheet.show @sheet.id, @author
+ }.should raise_error(ActiveRecord::Forbidden)
+ end
+ end
+ context '存在しない用紙を開こうとしたとき' do
+ it '404RecordNotFound例外を返す' do
+ lambda{
+ Sheet.show 110, @author
+ }.should raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
+ end
+
+ describe '編集取得に於いて' do
+ before do
+ @sheet = FactoryGirl.create :sheet, :author_id => @author.id
+ end
+ context 'つつがなく終わるとき' do
+ it '単体取得オプションを利用している' do
+ Sheet.stub(:show_opt).with(any_args).and_return({})
+ Sheet.should_receive(:show_opt).with(any_args).exactly(1)
+ r = Sheet.edit @sheet.id, @author
+ end
+ it '所持判定を問い合わせている' do
+ Sheet.any_instance.stub(:own?).with(any_args).and_return(true)
+ Sheet.any_instance.should_receive(:own?).with(any_args).exactly(1)
+ r = Sheet.edit @sheet.id, @author
+ end
+ end
+ it '指定の用紙を返す' do
+ Sheet.any_instance.stub(:own?).and_return(true)
+ c = Sheet.edit @sheet.id, @author.id
+ c.should eq @sheet
+ end
+ context '他人の用紙を開こうとしたとき' do
+ it '403Forbidden例外を返す' do
+ Sheet.any_instance.stub(:own?).and_return(false)
+ lambda{
+ Sheet.edit @sheet.id, @author
+ }.should raise_error(ActiveRecord::Forbidden)
+ end
+ end
+ context '存在しない用紙を開こうとしたとき' do
+ it '404RecordNotFound例外を返す' do
+ lambda{
+ Sheet.edit 110, @author
+ }.should raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
+ end
+ describe '単体取得オプションに於いて' do
+ it 'includeキーを含んでいる' do
+ r = Sheet.show_opt
+ r.has_key?(:include).should be_true
+ end
+ it '2つの項目を含んでいる' do
+ r = Sheet.show_opt[:include]
+ r.should have(2).items
+ end
+ it '作家を含んでいる' do
+ r = Sheet.show_opt[:include]
+ r.has_key?(:author).should be_true
+ end
+ it 'スクコマを含んでいる' do
+ r = Sheet.show_opt[:include]
+ r.has_key?(:sheet_panels).should be_true
+ end
+ it 'スクコマはコマを含んでいる' do
+ r = Sheet.show_opt[:include]
+ r[:sheet_panels].has_key?(:panel).should be_true
+ end
+ end
+ describe 'json単体出力オプションに於いて' do
+ before do
+ @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
+ @sheet = FactoryGirl.create :sheet, :author_id => @author.id, :visible => 1
+ @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
+ @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
+ end
+ it 'スクコマを含んでいる' do
+ r = Sheet.show(@sheet.id, @author).to_json Sheet.show_json_opt
+ j = JSON.parse r
+ i = j
+ i.has_key?('sheet_panels').should be_true
+ end
+ it 'スクコマはコマを含んでいる' do
+ r = Sheet.show(@sheet.id, @author).to_json Sheet.show_json_opt
+ j = JSON.parse r
+ i = j
+ s = i['sheet_panels'].first
+ s.has_key?('panel').should be_true
+ end
+ it '作家を含んでいる' do
+ r = Sheet.show(@sheet.id, @author).to_json Sheet.show_json_opt
+ j = JSON.parse r
+ i = j
+ i.has_key?('author').should be_true
+ end
+ end
+
+ describe '検証値収集に於いて' do
+ context 'つつがなく終わるとき' do
+ it '第一パラメータで指定された配列中から第二引数のカラム値を収集している' do
+ elements = [[{:sheet_id => 1, :a => 'a'}, {:sheet_id => 2, :a => 'a'}],
+ [{:sheet_id => 3, :a => 'a'}, {:sheet_id => 4, :a => 'a'}]]
+ r = Sheet.collect_element_value elements, :sheet_id
+ r.should eq [1, 2, 3, 4]
+ end
+ it '第一パラメータで指定された配列中から第二引数のカラム値を収集している' do
+ elements = [[{:t => 1, :a => 'a'}, {:t => 2, :a => 'a'}],
+ [{:t => 3, :a => 'a'}, {:t => 0, :a => 'a'}]]
+ r = Sheet.collect_element_value elements, :t
+ r.should eq [1, 2, 3, 0]
+ end
+ end
+ end
+ describe 'シリアライズチェックに於いて' do
+ context 'つつがなく終わるとき' do
+ it '0からシリアライズされているならTrueを返す' do
+ r = Sheet.validate_serial [0, 1, 2]
+ r.should be_true
+ end
+ it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
+ r = Sheet.validate_serial [0, 2, 1]
+ r.should be_true
+ end
+ it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
+ r = Sheet.validate_serial [ 2, 1, 4, 3, 0]
+ r.should be_true
+ end
+ end
+ context 'オフセットが1のとき' do
+ it '0からシリアライズされているならFalseを返す' do
+ r = Sheet.validate_serial [0, 1, 2], 1
+ r.should be_false
+ end
+ it '1からシリアライズされているならTrueを返す' do
+ r = Sheet.validate_serial [1, 2, 3], 1
+ r.should be_true
+ end
+ end
+ context '異常なとき' do
+ it '0から始まらないならFalseを返す' do
+ r = Sheet.validate_serial [1, 2, 3]
+ r.should be_false
+ end
+ it '連続していないならFalseを返す' do
+ r = Sheet.validate_serial [0, 1, 2, 4]
+ r.should be_false
+ end
+ it '連続していないならFalseを返す' do
+ r = Sheet.validate_serial [0, 1, 2, 4, 5]
+ r.should be_false
+ end
+ end
+ end
+ describe 'シリアライズチェック単体に於いて' do
+ before do
+ end
+ context 'つつがなく終わるとき' do
+ it '検証値収集を依頼している' do
+ Sheet.should_receive(:collect_element_value).with(any_args).exactly(1)
+ Sheet.stub(:collect_element_value).with(any_args).and_return([])
+ Sheet.stub(:validate_serial).with(any_args).and_return(true)
+ r = Sheet.validate_element_serial [], :t
+ end
+ it 'シリアライズチェック依頼している' do
+ Sheet.stub(:collect_element_value).with(any_args).and_return([])
+ Sheet.should_receive(:validate_serial).with(any_args).exactly(1)
+ Sheet.stub(:validate_serial).with(any_args).and_return(true)
+ r = Sheet.validate_element_serial [], :t
+ end
+ end
+ end
+ describe '従属データの検証に於いて' do
+ context 'つつがなく終わるとき' do
+ it 'trueを返している' do
+ @sheet = FactoryGirl.create :sheet, :author_id => @author.id
+ @panel = FactoryGirl.create :panel, :author_id => @author.id
+ @sheet.sheet_panels.create(
+ FactoryGirl.attributes_for(:sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 0, :z => 0+1)
+ )
+ @sheet.sheet_panels.create(
+ FactoryGirl.attributes_for(:sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 1, :z => 1+1)
+ )
+ r = @sheet.validate_child
+ r.should be_true
+ end
+ end
+ context 'tシリアライズされていないとき' do
+ it 'falseを返している' do
+ @sheet = FactoryGirl.create :sheet, :author_id => @author.id
+ @panel = FactoryGirl.create :panel, :author_id => @author.id
+ @sheet.sheet_panels.create(
+ FactoryGirl.attributes_for(:sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 2, :z => 0+1)
+ )
+ @sheet.sheet_panels.create(
+ FactoryGirl.attributes_for(:sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 1, :z => 1+1)
+ )
+ r = @sheet.validate_child
+ r.should be_false
+ end
+ end
+ context 'zシリアライズされていないとき' do
+ it 'falseを返している' do
+ @sheet = FactoryGirl.create :sheet, :author_id => @author.id
+ @panel = FactoryGirl.create :panel, :author_id => @author.id
+ @sheet.sheet_panels.create(
+ FactoryGirl.attributes_for(:sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 0, :z => 0+2)
+ )
+ @sheet.sheet_panels.create(
+ FactoryGirl.attributes_for(:sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 1, :z => 1+1)
+ )
+ r = @sheet.validate_child
+ r.should be_false
+ end
+ end
+ end
+ describe '保存に於いて' do
+ before do
+ @attr = FactoryGirl.attributes_for :sheet
+ @sheet = Sheet.new
+ @sheet.supply_default
+ end
+ context 'つつがなく終わるとき' do
+ before do
+ Sheet.any_instance.stub(:validate_child).with(any_args).and_return(true)
+ Sheet.any_instance.stub(:save).with(any_args).and_return(true)
+ end
+ it 'コマモデルに上書き補充を依頼している' do
+ Sheet.any_instance.should_receive(:overwrite).exactly(1)
+ r = @sheet.store @attr, @author
+ end
+ it '保存を依頼している' do
+ Sheet.any_instance.should_receive(:save).with(any_args).exactly(1)
+ @sheet = FactoryGirl.build :sheet, :author_id => @author.id
+ r = @sheet.store @attr, @author
+ end
+ it '従属データの検証を依頼している' do
+ Sheet.any_instance.should_receive(:validate_child).with(any_args).exactly(1)
+ r = @sheet.store @attr, @author
+ end
+ end
+ context 'つつがなく終わるとき' do
+ before do
+ Sheet.any_instance.stub(:validate_child).with(any_args).and_return(true)
+ end
+ it 'Trueを返す' do
+ r = @sheet.store @attr, @author
+ r.should be_true
+ end
+ it '行が追加されている' do
+ Sheet.any_instance.stub(:validate_child).with(any_args).and_return(true)
+ lambda {
+ r = @sheet.store @attr, @author
+ }.should change(Sheet, :count)
+ end
+ end
+ context '不正なjsonデータのとき' do
+ before do
+ end
+ it 'エラーメッセージがセットされている' do
+ r = @sheet.store false, @author
+ @sheet.errors[:base].should_not be_blank
+ end
+ end
+ context '従属データの検証に失敗したとき' do
+ before do
+ Sheet.any_instance.stub(:save).with(any_args).and_return(true)
+ Sheet.any_instance.stub(:validate_child).with(any_args).and_return(false)
+ end
+ it 'エラーメッセージがセットされている' do
+ r = @sheet.store @attr, @author
+ @sheet.errors[:base].should_not be_blank
+ end
+ end
+ context 'カラム値がFalseしたとき' do
+ before do
+ Sheet.any_instance.stub(:validate_child).with(any_args).and_return(true)
+ end
+ it 'エラーメッセージがセットされている' do
+ r = @sheet.store false, @author
+ @sheet.errors.should_not be_empty
+ end
+ end
+ end
+
+ describe '削除に於いて' do
+ before do
+ @sheet = FactoryGirl.create :sheet, :author_id => @author.id
+ @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
+ @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
+ @other_sheet = FactoryGirl.create :sheet, :author_id => @author.id
+ @other_sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @other_sheet.id, :panel_id => @panel.id
+ end
+ context 'つつがなく終わるとき' do
+ it '自身を削除する' do
+ lambda {
+ r = @sheet.destroy_with_sheet_panel
+ }.should change(Sheet, :count).by(-1)
+ lambda {
+ r = Sheet.find @sheet.id
+ }.should raise_error
+ end
+ it '自身にリンクしているスクコマをすべて削除する' do
+ lambda {
+ r = @sheet.destroy_with_sheet_panel
+ }.should change(SheetPanel, :count).by(-1)
+ lambda {
+ r = SheetPanel.find @sheet_panel.id
+ }.should raise_error
+ end
+ it 'Trueを返す' do
+ r = @sheet.destroy_with_sheet_panel
+ r.should be_true
+ end
+ end
+ context '削除に失敗したとき' do
+ before do
+ SheetPanel.any_instance.stub(:destroy).with(any_args).and_return(false)
+ end
+ it 'Falseを返す' do
+ r = @sheet.destroy_with_sheet_panel
+ r.should be_false
+ end
+ it 'ロールバックしている' do
+ lambda {
+ r = @sheet.destroy_with_sheet_panel
+ }.should_not change(Sheet, :count)
+ lambda {
+ r = @sheet.destroy_with_sheet_panel
+ }.should_not change(SheetPanel, :count)
+ end
+ end
+ end
+end