OSDN Git Service

speech balloon template changed
[pettanr/pettanr.git] / spec / models / speech_balloon_template_spec.rb
index 1445421..3540bec 100644 (file)
@@ -1,5 +1,405 @@
+# -*- encoding: utf-8 -*-
 require 'spec_helper'
-
+#フキダシテンプレート
 describe SpeechBalloonTemplate do
-  pending "add some examples to (or delete) #{__FILE__}"
+  before do
+    #テストデータを用意してね
+    @f = Rails.root + 'spec/json/speech_balloon_template.json'
+    @t = File.open(@f, 'r').read
+    @j = JSON.parse @t
+    @fs = Rails.root + 'spec/json/speech_balloon_templates.json'
+    @ts = File.open(@fs, 'r').read
+    @js = JSON.parse @ts
+    @fes = Rails.root + 'spec/json/invalid_speech_balloon_templates.json'
+    @tes = File.open(@fes, 'r').read
+    @jes = JSON.parse @tes
+    
+    @admin = Factory :admin
+    @license = Factory :license
+  end
+  
+  describe '検証に於いて' do
+    before do
+    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
+      end
+      it 'テストデータの確認' do
+        @st.name = 'abcdefghi0abcdefghi0abcdefghi0abcdefghi0abcdefghi0'
+        @st.should be_valid
+      end
+      it 'nullなら失敗する' do
+        @st.name = ''
+        @st.should_not be_valid
+      end
+      it '51文字以上なら失敗する' do
+        @st.name = 'a'*51
+        @st.should_not be_valid
+      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
+      end
+      it '51文字以上なら失敗する' do
+        @st.classname = 'a'*51
+        @st.should_not be_valid
+      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
+      end
+      it '31文字以上なら失敗する' do
+        @st.caption = 'a'*31
+        @st.should_not be_valid
+      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
+      end
+      it '数値でなければ失敗する' do
+        @st.t = 'a'
+        @st.should_not be_valid
+      end
+      it '負なら失敗する' do
+        @st.t = -1
+        @st.should_not be_valid
+      end
+    end
+    context 'settingsを検証するとき' do
+      before do
+        @st = Factory.build :speech_balloon_template
+      end
+      it 'テストデータの確認' do
+        @st.settings = '{}'
+        @st.should be_valid
+      end
+      it 'nullなら失敗する' do
+        @st.settings = nil
+        @st.should_not be_valid
+      end
+    end
+  end
+  
+  describe 'データ補充に於いて' do
+    before do
+    end
+    
+  end
+  
+  describe 'Json解析に於いて' do
+    before do
+    end
+    context 'テキストを渡されたとき' do
+      it 'Json解析している' do
+        JSON.should_receive(:parse).with(@t).exactly(1)
+        r = SpeechBalloonTemplate.parse @t
+      end
+      it 'Hashで返す' do
+        r = SpeechBalloonTemplate.parse @t
+        r.is_a?(Hash).should be_true
+      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
+      end
+    end
+  end
+  
+  describe '対象フキダシテンプレートの取得に於いて' do
+    before do
+      @st = Factory :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
+    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'
+      end
+    end
+  end
+  
+  describe 'フキダシテンプレート保存に於いて' do
+    before do
+      @st = Factory.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
+    end
+  end
+  
+  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 => '{}'}
+    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
+      end
+    end
+  end
+  
+  describe 'テキスト取り込みに於いて' do
+    #成功でTrue、パース失敗でFalse、失敗は保存エラーのモデルを配列で返す
+    before do
+    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 []
+      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
+    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
+      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
+      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
+  end
+  
+  describe 'インポートエラーの表示に於いて' do
+  end
+  
+  describe 'ファイル取り込みに於いて' do
+    before do
+      SpeechBalloonTemplate.stub(:import).with(any_args).and_return(true)
+    end
+    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)
+      end
+      it 'テキスト取り込みを依頼する' do
+        SpeechBalloonTemplate.should_receive(:import).with(any_args).exactly(1)
+        SpeechBalloonTemplate.import_file(@f)
+      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')
+      end
+      it 'ファイルエラーのメッセージを出力する' do
+        pending
+      end
+      it 'Falseを返す' do
+        SpeechBalloonTemplate.import_file(@f).should be_false
+      end
+    end
+    #失敗したときは、失敗したフキダシテンプレートが配列で返る
+    context 'テキスト取り込みが失敗したとき' do
+      before do
+        SpeechBalloonTemplate.stub(:import).with(any_args).and_return(false)
+      end
+      it '各フキダシテンプレートのエラーメッセージを出力する' do
+        pending
+      end
+      it 'Falseを返す' do
+        SpeechBalloonTemplate.import_file(@f).should be_false
+      end
+    end
+  end
+  
 end