OSDN Git Service

t#32046:
[pettanr/pettanr.git] / spec / models / speech_balloon_template_spec.rb
index 3540bec..6797ae5 100644 (file)
@@ -3,6 +3,7 @@ require 'spec_helper'
 #フキダシテンプレート
 describe SpeechBalloonTemplate do
   before do
+    SpeechBalloonTemplate.delete_all
     #テストデータを用意してね
     @f = Rails.root + 'spec/json/speech_balloon_template.json'
     @t = File.open(@f, 'r').read
@@ -14,27 +15,42 @@ describe SpeechBalloonTemplate do
     @tes = File.open(@fes, 'r').read
     @jes = JSON.parse @tes
     
-    @admin = Factory :admin
-    @license = Factory :license
+    @admin = FactoryGirl.create :admin
+    @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
+    @sp = FactoryGirl.create :system_picture
   end
   
   describe '検証に於いて' do
     before do
+      @st = FactoryGirl.build :speech_balloon_template
     end
     
-    it 'オーソドックスなデータなら通る' do
-      @st = Factory.build :speech_balloon_template
-      @st.should be_valid
-    end
-    
-    context 'nameを検証するとき' do
-      before do
-        @st = Factory.build :speech_balloon_template
+    context 'オーソドックスなデータのとき' do
+      it '下限データが通る' do
+        @st.name = 'a'
+        @st.classname = 'a'
+        @st.caption = 'a'
+        @st.t = 0
+        @st.settings = '{}'
+        @st.should be_valid
       end
-      it 'テストデータの確認' do
-        @st.name = 'abcdefghi0abcdefghi0abcdefghi0abcdefghi0abcdefghi0'
+      it '上限データが通る' do
+        @st.name = 'a'*50
+        @st.classname = 'a'*50
+        @st.caption = 'a'*30
+        @st.t = 99999
+        @st.settings = '{' + 'a'*99999 + '}'
         @st.should be_valid
       end
+    end
+    
+    
+    context 'nameを検証するとき' do
       it 'nullなら失敗する' do
         @st.name = ''
         @st.should_not be_valid
@@ -45,13 +61,6 @@ describe SpeechBalloonTemplate do
       end
     end
     context 'classnameを検証するとき' do
-      before do
-        @st = Factory.build :speech_balloon_template
-      end
-      it 'テストデータの確認' do
-        @st.classname = 'abcdefghi0abcdefghi0abcdefghi0abcdefghi0abcdefghi0'
-        @st.should be_valid
-      end
       it 'nullなら失敗する' do
         @st.classname = ''
         @st.should_not be_valid
@@ -62,13 +71,6 @@ describe SpeechBalloonTemplate do
       end
     end
     context 'captionを検証するとき' do
-      before do
-        @st = Factory.build :speech_balloon_template
-      end
-      it 'テストデータの確認' do
-        @st.caption = 'abcdefghi0abcdefghi0abcdefghi0'
-        @st.should be_valid
-      end
       it 'nullなら失敗する' do
         @st.caption = ''
         @st.should_not be_valid
@@ -79,13 +81,6 @@ describe SpeechBalloonTemplate do
       end
     end
     context 'tを検証するとき' do
-      before do
-        @st = Factory.build :speech_balloon_template
-      end
-      it 'テストデータの確認' do
-        @st.t = 0
-        @st.should be_valid
-      end
       it 'nullなら失敗する' do
         @st.t = nil
         @st.should_not be_valid
@@ -99,14 +94,21 @@ describe SpeechBalloonTemplate do
         @st.should_not be_valid
       end
     end
-    context 'settingsを検証するとき' do
-      before do
-        @st = Factory.build :speech_balloon_template
+    context 'system_picture_idを検証するとき' do
+      it 'nullなら失敗する' do
+        @st.system_picture_id = nil
+        @st.should_not be_valid
       end
-      it 'テストデータの確認' do
-        @st.settings = '{}'
-        @st.should be_valid
+      it '数値でなければ失敗する' do
+        @st.system_picture_id = 'a'
+        @st.should_not be_valid
       end
+      it '存在するシステム画像でなければ失敗する' do
+        @st.system_picture_id = 0
+        @st.should_not be_valid
+      end
+    end
+    context 'settingsを検証するとき' do
       it 'nullなら失敗する' do
         @st.settings = nil
         @st.should_not be_valid
@@ -114,290 +116,229 @@ describe SpeechBalloonTemplate do
     end
   end
   
-  describe 'データ補充に於いて' do
+  describe '文字コード検証に於いて' do
     before do
+      @st = FactoryGirl.build :speech_balloon_template
     end
     
-  end
-  
-  describe 'Json解析に於いて' do
-    before do
+    context 'nameを検証するとき' do
+      it 'Shift JISなら失敗する' do
+        @st.name = "\x83G\x83r\x83]\x83D"
+        lambda{
+          @st.valid_encode
+        }.should raise_error(Pettanr::BadRequest)
+      end
     end
-    context 'テキストを渡されたとき' do
-      it 'Json解析している' do
-        JSON.should_receive(:parse).with(@t).exactly(1)
-        r = SpeechBalloonTemplate.parse @t
+    
+    context 'classnameを検証するとき' do
+      it 'Shift JISなら失敗する' do
+        @st.classname = "\x83G\x83r\x83]\x83D"
+        lambda{
+          @st.valid_encode
+        }.should raise_error(Pettanr::BadRequest)
       end
-      it 'Hashで返す' do
-        r = SpeechBalloonTemplate.parse @t
-        r.is_a?(Hash).should be_true
+    end
+    
+    context 'captionを検証するとき' do
+      it 'Shift JISなら失敗する' do
+        @st.caption = "\x83G\x83r\x83]\x83D"
+        lambda{
+          @st.valid_encode
+        }.should raise_error(Pettanr::BadRequest)
       end
     end
-    context 'パース失敗したとき' do
-      it 'Falseを返す' do
-        JSON.should_receive(:parse).with(any_args).and_raise('StandardError')
-        r = SpeechBalloonTemplate.parse @t
-        r.should be_false
+    
+    context 'settingsを検証するとき' do
+      it 'Shift JISなら失敗する' do
+        @st.settings = "\x83G\x83r\x83]\x83D"
+        lambda{
+          @st.valid_encode
+        }.should raise_error(Pettanr::BadRequest)
       end
     end
   end
   
-  describe '対象フキダシテンプレートの取得に於いて' do
+  describe 'デフォルト値補充に於いて' do
+    it 'defined' do
+      @st = FactoryGirl.build :speech_balloon_template
+      @st.supply_default
+    end
+  end
+  
+  describe '上書き補充に於いて' do
     before do
-      @st = Factory :speech_balloon_template
+      @st = FactoryGirl.build :speech_balloon_template
     end
-    context 'nameが一致するフキダシテンプレートがないとき' do
-      it '新規フキダシテンプレートを準備して返す' do
-        r = SpeechBalloonTemplate.update_speech_balloon_template 'newtemplate', {:classname => 'New'}
-        r.should be_a_new SpeechBalloonTemplate
-        r[:classname].should eq 'New'
-      end
+    it 'テーブルが空のときは0を返す' do
+      @st.overwrite
+      @st.t.should eq 0
     end
-    context 'nameが一致するフキダシテンプレートがあるとき' do
-      it '該当フキダシテンプレートを値設定して返す' do
-        r = SpeechBalloonTemplate.update_speech_balloon_template @st.name, {:classname => 'Update'}
-        r.is_a?(SpeechBalloonTemplate).should be_true
-        r.should_not be_a_new SpeechBalloonTemplate
-        r[:classname].should eq 'Update'
+    it 'テーブルが1行あるときは1を返す' do
+      FactoryGirl.create :speech_balloon_template
+      @st.overwrite
+      @st.t.should eq 1
+    end
+    it 'テーブルが2行あるときは2を返す' do
+      FactoryGirl.create :speech_balloon_template
+      FactoryGirl.create :speech_balloon_template, :name => "5", :t => 1
+      @st.overwrite
+      @st.t.should eq 2
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        @st = FactoryGirl.create :speech_balloon_template, :t => 0
+      end
+      it '更新のときは補充しない' do
+        @st.overwrite
+        @st.t.should eq 0
       end
     end
   end
   
-  describe 'フキダシテンプレート保存に於いて' do
+  describe '閲覧許可に於いて' do
     before do
-      @st = Factory.build :speech_balloon_template
+      @st = FactoryGirl.build :speech_balloon_template
     end
-    context 'つつがなく終わるとき' do
-      it '保存を問い合わせている' do
-        SpeechBalloonTemplate.any_instance.should_receive(:save).exactly(1)
-        @st.store
-      end
-      it 'フキダシテンプレートが作成されている' do
-        lambda {
-          @st.store
-        }.should change SpeechBalloonTemplate, :count
-      end
+    it '許可する' do
+      @st.visible?(@author).should == true
     end
   end
   
-  describe 'フキダシテンプレート更新に於いて' do
+  describe '一覧取得に於いて' do
     before do
-      @st = Factory :speech_balloon_template
-      @attr = {:name => @st.name, :classname => 'Update', :t => 0, :caption => 'yp', :settings => '{}'}
-      @newattr = {:name => @st.name, :classname => 'New', :t => 1, :caption => 'nw', :settings => '{}'}
+      @st = FactoryGirl.create :speech_balloon_template
     end
     context 'つつがなく終わるとき' do
-      it '対象フキダシテンプレートを問い合わせている' do
-        SpeechBalloonTemplate.stub(:update_speech_balloon_template).with(any_args).and_return(SpeechBalloonTemplate.new(@attr))
-        SpeechBalloonTemplate.should_receive(:update_speech_balloon_template).exactly(1)
-        SpeechBalloonTemplate.store @st.name, @attr
-      end
-      context '新規のとき' do
-        it 'フキダシテンプレートを保存しようとしている' do
-          SpeechBalloonTemplate.any_instance.should_receive(:save).exactly(1)
-          SpeechBalloonTemplate.store 'newtemplate', @newattr
-        end
-        it 'フキダシテンプレートが作成されている' do
-          lambda {
-            SpeechBalloonTemplate.store 'newtemplate', @newattr
-          }.should change SpeechBalloonTemplate, :count
-        end
-      end
-      context '更新のとき' do
-        it 'フキダシテンプレートを保存しようとしている' do
-          SpeechBalloonTemplate.any_instance.should_receive(:save).exactly(1)
-          SpeechBalloonTemplate.store @st.name, @attr
-        end
-        it 'フキダシテンプレートの数に変化がない' do
-          lambda {
-            SpeechBalloonTemplate.store @st.name, @attr
-          }.should_not change SpeechBalloonTemplate, :count
-        end
-      end
-      it '属性が一致している' do
-        r = SpeechBalloonTemplate.store 'newtemplate', @newattr
-        r.classname.should eq 'New'
-      end
-      it '保存されたSpeechBalloonTemplateオブジェクトを返す' do
-        r = SpeechBalloonTemplate.store 'newtemplate', @newattr
-p r.errors.full_messages
-        r.should_not be_a_new SpeechBalloonTemplate
-      end
-    end
-    context 'フキダシテンプレートの作成に失敗するとき' do
-      before do
-        SpeechBalloonTemplate.any_instance.stub(:save).with(any_args).and_return(false)
-      end
-      context '新規のとき' do
-        it 'フキダシテンプレートに変化がない' do
-          lambda {
-            r = SpeechBalloonTemplate.store 'newtemplate', @newattr
-          }.should_not change License, :count
-        end
-      end
-      context '更新のとき' do
-        it 'フキダシテンプレート属性に変化がない' do
-          lambda {
-            r = SpeechBalloonTemplate.store @st.name, @attr
-          }.should_not change License.find(@st.id), :classname
-        end
+      it '一覧取得オプションを利用している' do
+        SpeechBalloonTemplate.stub(:list_opt).with(any_args).and_return({})
+        SpeechBalloonTemplate.should_receive(:list_opt).with(any_args).exactly(1)
+        r = SpeechBalloonTemplate.list
       end
     end
+    it 'リストを返す' do
+      l = SpeechBalloonTemplate.list
+      l.should eq [@st]
+    end
+    it 't順で並んでいる' do
+      @st2 = FactoryGirl.create :speech_balloon_template, :name => "5", :t => 1
+      l = SpeechBalloonTemplate.list
+      l.should eq [@st, @st2]
+    end
+  end
+  describe '一覧取得オプションに於いて' do
+    it '空のHashを返す' do
+      r = SpeechBalloonTemplate.list_opt
+      r.is_a?(Hash).should be_true
+      r.should be_empty
+    end
+  end
+  describe 'json一覧出力オプションに於いて' do
+    it '空のHashを返す' do
+      r = SpeechBalloonTemplate.list_json_opt
+      r.is_a?(Hash).should be_true
+      r.should be_empty
+    end
   end
   
-  describe 'テキスト取り込みに於いて' do
-    #成功でTrue、パース失敗でFalse、失敗は保存エラーのモデルを配列で返す
+  describe '単体取得に於いて' do
     before do
+      @st = FactoryGirl.create :speech_balloon_template
     end
     context 'つつがなく終わるとき' do
-      it 'Json解析を依頼する' do
-        SpeechBalloonTemplate.should_receive(:parse).with(any_args).exactly(1)
-        SpeechBalloonTemplate.stub(:parse).with(any_args).and_return(@j)
-        SpeechBalloonTemplate.import(@t)
-      end
-      it 'フキダシテンプレート更新を一回依頼する' do
-        SpeechBalloonTemplate.stub(:store).with(any_args).and_return(SpeechBalloonTemplate.new)
-        SpeechBalloonTemplate.any_instance.stub(:valid?).with(any_args).and_return(true)
-        SpeechBalloonTemplate.should_receive(:store).with(any_args).exactly(1)
-        SpeechBalloonTemplate.import(@t)
-      end
-      it 'フキダシテンプレートが追加される' do
-        lambda {
-          SpeechBalloonTemplate.import(@t)
-        }.should change SpeechBalloonTemplate, :count
-      end
-      it '[]を返す' do
-        SpeechBalloonTemplate.import(@t).should eq []
+      it '単体取得オプションを利用している' do
+        SpeechBalloonTemplate.stub(:show_opt).with(any_args).and_return({})
+        SpeechBalloonTemplate.should_receive(:show_opt).with(any_args).exactly(1)
+        r = SpeechBalloonTemplate.show @st.id, @author
       end
     end
-    context '複数データがつつがなく終わるとき' do
-      it 'フキダシテンプレート更新を二回依頼する' do
-        SpeechBalloonTemplate.stub(:store).with(any_args).and_return(SpeechBalloonTemplate.new)
-        SpeechBalloonTemplate.any_instance.stub(:valid?).with(any_args).and_return(true)
-        SpeechBalloonTemplate.should_receive(:store).with(any_args).exactly(2)
-        SpeechBalloonTemplate.import(@ts)
-      end
-      it 'フキダシテンプレートが二個追加される' do
-        lambda {
-          SpeechBalloonTemplate.import(@ts)
-        }.should change(SpeechBalloonTemplate, :count).by 2
-      end
-      it '[]を返す' do
-        SpeechBalloonTemplate.import(@ts).should eq []
-      end
+    it '指定のフキダシテンプレートを返す' do
+      l = SpeechBalloonTemplate.show @st.id, @author
+      l.should eq @st
     end
-    #例外ケース
-    context 'Json解析に失敗したとき' do
-      before do
-        SpeechBalloonTemplate.stub(:parse).with(any_args).and_return(false)
-      end
-      it 'フキダシテンプレートの数に変化がない' do
-        lambda {
-          SpeechBalloonTemplate.import(@t)
-        }.should_not change SpeechBalloonTemplate, :count
-      end
-      it 'Falseを返す' do
-        SpeechBalloonTemplate.import(@t).should be_false
+    context '他人のフキダシテンプレートを開こうとしたとき' do
+      it '403Forbidden例外を返す' do
+        SpeechBalloonTemplate.any_instance.stub(:visible?).and_return(false)
+        lambda{
+          r = SpeechBalloonTemplate.show @st.id, @other_author
+        }.should raise_error(ActiveRecord::Forbidden)
       end
     end
-    context 'フキダシテンプレート作成に失敗したとき' do
-      before do
-        SpeechBalloonTemplate.any_instance.stub(:save).with(any_args).and_return(false)
-        SpeechBalloonTemplate.any_instance.stub(:valid?).with(any_args).and_return(false)
-      end
-      it 'フキダシテンプレートの数に変化がない' do
-        lambda {
-          SpeechBalloonTemplate.import(@t)
-        }.should_not change SpeechBalloonTemplate, :count
-      end
-      it '配列を返す' do
-        r = SpeechBalloonTemplate.import(@t)
-        r.is_a?(Array).should be_true
-      end
-      it '配列の中身は一件' do
-        r = SpeechBalloonTemplate.import(@t)
-        r.should have(1).items
-      end
-      it 'フキダシテンプレートオブジェクトが入っている' do
-        r = SpeechBalloonTemplate.import(@t)
-        r.first.is_a?(SpeechBalloonTemplate).should be_true
+    context '存在しないフキダシテンプレートを開こうとしたとき' do
+      it '404RecordNotFound例外を返す' do
+        lambda{
+          SpeechBalloonTemplate.show 110, @author
+        }.should raise_error(ActiveRecord::RecordNotFound)
       end
     end
-    context '複数のフキダシテンプレート作成に失敗したとき' do
-      #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
-      it 'フキダシテンプレートの数に変化がない' do
-        lambda {
-          SpeechBalloonTemplate.import(@tes)
-        }.should_not change SpeechBalloonTemplate, :count
-      end
-      it '途中で保存に失敗しても全件更新依頼する' do
-        SpeechBalloonTemplate.stub(:store).with(any_args).and_return(SpeechBalloonTemplate.new)
-        SpeechBalloonTemplate.should_receive(:store).with(any_args).exactly(3)
-        SpeechBalloonTemplate.import(@tes)
-      end
-      it '配列を返す' do
-        r = SpeechBalloonTemplate.import(@tes)
-        r.is_a?(Array).should be_true
-      end
-      it '配列の中身は2件' do
-        r = SpeechBalloonTemplate.import(@tes)
-        r.should have(2).items
-      end
-      it '配列の中身は失敗したフキダシテンプレートオブジェクトが入っている' do
-        r = SpeechBalloonTemplate.import(@tes)
-        r[0].is_a?(SpeechBalloonTemplate).should be_true
-        r[0]["name"].should eq 'fail1'
-        r[1].is_a?(SpeechBalloonTemplate).should be_true
-        r[1]["name"].should eq 'fail2'
-      end
+  end
+  describe '単体出力オプションに於いて' do
+    it '空のHashを返す' do
+      r = SpeechBalloonTemplate.show_opt
+      r.is_a?(Hash).should be_true
+      r.should be_empty
     end
   end
-  
-  describe 'インポートエラーの表示に於いて' do
+  describe 'json単体出力オプションに於いて' do
+    it '空のHashを返す' do
+      r = SpeechBalloonTemplate.show_json_opt
+      r.is_a?(Hash).should be_true
+      r.should be_empty
+    end
   end
   
-  describe 'ファイル取り込みに於いて' do
+  describe '更新に於いて' do
+    #置換まわりが破壊的でテストしにくいので、外部と連携したテストが多い。
+    #特にシステム画像データはidを直接記述して置換させないようにテストしている。
     before do
-      SpeechBalloonTemplate.stub(:import).with(any_args).and_return(true)
+      @sp = FactoryGirl.create :system_picture
+      @st = FactoryGirl.create :speech_balloon_template
+      @settings = {
+        "speech_balloon" => {"default" => 1}, 
+        "one" => {"balloon" => {"system_picture_id" => @sp.id}}
+      }
+      @attr = {"classname" => 'store', "caption" => 'store test', "system_picture_id" => @sp.id, 
+        "settings" => @settings
+      }
     end
-    context 'つつがなく終わるとき' do
+    context '事前チェック' do
       before do
-        SpeechBalloonTemplate.stub(:import).with(any_args).and_return(true)
-      end
-      it 'ファイルを開いてテキストを読む' do
-        File.should_receive(:open).with(@f, 'r').exactly(1)
-        SpeechBalloonTemplate.import_file(@f)
+        SpeechBalloonTemplate.stub(:replace_system_picture).with(@attr).and_return(@attr)
+        SpeechBalloonTemplate.stub(:modify_object).with(any_args).and_return(@st)
+        SpeechBalloonTemplate.any_instance.stub(:overwrite).with(any_args).and_return(true)
+        SpeechBalloonTemplate.any_instance.stub(:save).with(any_args).and_return(true)
       end
-      it 'ã\83\86ã\82­ã\82¹ã\83\88å\8f\96ã\82\8aè¾¼ã\81¿ã\82\92ä¾\9dé ¼ã\81\99る' do
-        SpeechBalloonTemplate.should_receive(:import).with(any_args).exactly(1)
-        SpeechBalloonTemplate.import_file(@f)
+      it 'ã\82·ã\82¹ã\83\86ã\83 ç\94»å\83\8fç½®æ\8f\9bã\81«ä¾\9dé ¼ã\81\97ã\81¦ã\81\84る' do
+        SpeechBalloonTemplate.should_receive(:replace_system_picture).with(@attr).exactly(1)
+        r = SpeechBalloonTemplate.store 'circle_v01', @attr
       end
-      #テキスト取り込み成功でTrueが返る
-      it 'Trueを返す' do
-        SpeechBalloonTemplate.import_file(@f).should be_true
-      end
-    end
-    context 'ファイルが開けないとき' do
-      before do
-        File.stub(:open).with(any_args).and_raise('StandardError')
+      it 'インポート処理のデータ更新準備に依頼している' do
+        SpeechBalloonTemplate.should_receive(:modify_object).exactly(1)
+        r = SpeechBalloonTemplate.store 'circle_v01', @attr
       end
-      it 'ファイルエラーのメッセージを出力する' do
-        pending
+      it '上書き補充を依頼している' do
+        SpeechBalloonTemplate.any_instance.should_receive(:overwrite).exactly(1)
+        r = SpeechBalloonTemplate.store 'circle_v01', @attr
       end
-      it 'Falseを返す' do
-        SpeechBalloonTemplate.import_file(@f).should be_false
+      it 'オブジェクトを保存している' do
+        SpeechBalloonTemplate.any_instance.should_receive(:save).exactly(1)
+        r = SpeechBalloonTemplate.store 'circle_v01', @attr
       end
     end
-    #失敗したときは、失敗したフキダシテンプレートが配列で返る
-    context 'テキスト取り込みが失敗したとき' do
+    context 'つつがなく終わるとき' do
       before do
-        SpeechBalloonTemplate.stub(:import).with(any_args).and_return(false)
       end
-      it '各フキダシテンプレートのエラーメッセージを出力する' do
-        pending
+      it 'カラム値のsettingsをセットしている' do
+        r = SpeechBalloonTemplate.store 'circle_v01', @attr
+        r.settings.should eq @settings.to_json.to_s
+      end
+      it 'モデルが作成されている' do
+        lambda {
+          r = SpeechBalloonTemplate.store 'circle_v01', @attr
+        }.should change SpeechBalloonTemplate, :count
       end
-      it 'Falseを返す' do
-        SpeechBalloonTemplate.import_file(@f).should be_false
+      it 'オブジェクトを返している' do
+        r = SpeechBalloonTemplate.store 'circle_v01', @attr
+        r.is_a?(SpeechBalloonTemplate).should be_true
       end
     end
   end