OSDN Git Service

pass model import test
authoryasushiito <yas@pen-chan.jp>
Tue, 26 Jun 2012 07:33:36 +0000 (16:33 +0900)
committeryasushiito <yas@pen-chan.jp>
Tue, 26 Jun 2012 07:33:36 +0000 (16:33 +0900)
1  2 
app/models/license_group.rb
spec/json/invalid_license_groups.json
spec/json/license_groups.json
spec/models/license_group_spec.rb

index a1ab14c,0000000..34fbf32
mode 100644,000000..100644
--- /dev/null
@@@ -1,6 -1,0 +1,17 @@@
 +class LicenseGroup < ActiveRecord::Base
 +  validates :name, :presence => true, :length => {:maximum => 50}, :uniqueness => true
 +  validates :classname, :presence => true, :length => {:maximum => 50}
 +  validates :caption, :presence => true, :length => {:maximum => 30}
 +  validates :url, :presence => true, :length => {:maximum => 200}, :url => true
++  
++  def self.store name, attr
++    r = LicenseGroup.modify_object name, attr
++    r.save
++    r
++  end
++  
++  def self.import filename
++    LicenseGroup.import_file(filename) {|name, attr| LicenseGroup.store(name, attr)}
++  end
++  
 +end
index 0000000,0000000..afed992
new file mode 100644 (file)
--- /dev/null
--- /dev/null
@@@ -1,0 -1,0 +1,15 @@@
++{\r
++  "PublicDomain": {\r
++    "classname": "PublicDomain", \r
++    "caption": "Public Domain", \r
++    "url": "http://test.com/"\r
++  },\r
++  "UnknownUrl": {\r
++    "classname": "Unknown", \r
++    "caption": "Unknown owner"\r
++  },\r
++  "UnknownClassname": {\r
++    "caption": "Unknown owner", \r
++    "url": "http://test.uk/"\r
++  }\r
++}\r
index 0000000,0000000..afb7d37
new file mode 100644 (file)
--- /dev/null
--- /dev/null
@@@ -1,0 -1,0 +1,12 @@@
++{\r
++  "PublicDomain": {\r
++    "classname": "PublicDomain", \r
++    "caption": "Public Domain", \r
++    "url": "http://test.com/"\r
++  },\r
++  "Unknown": {\r
++    "classname": "Unknown", \r
++    "caption": "Unknown owner", \r
++    "url": "http://test.uk/"\r
++  }\r
++}\r
index fbf9f0f,0000000..96431fa
mode 100644,000000..100644
--- /dev/null
@@@ -1,192 -1,0 +1,221 @@@
-       it 'テキストインポートを依頼する' do
-         CommonLicense.should_receive(:parse).with(any_args).exactly(1)
-         CommonLicense.stub(:parse).with(any_args).and_return(@j)
-         CommonLicense.import(@t)
 +# -*- encoding: utf-8 -*-
 +#ライセンスグループ
 +require 'spec_helper'
 +
 +describe LicenseGroup do
 +  before do
 +    #テストデータを用意してね
 +    @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
 +    end
 +    
 +    it 'オーソドックスなデータなら通る' do
 +      @lg = Factory.build :license_group
 +      @lg.should be_valid
 +    end
 +    
 +    context 'nameを検証するとき' do
 +      before do
 +        @lg = Factory.build :license_group
 +      end
 +      it 'テストデータの確認' do
 +        @lg.name = 'a'*50
 +        @lg.should be_valid
 +      end
 +      it 'nullなら失敗する' do
 +        @lg.name = ''
 +        @lg.should_not be_valid
 +      end
 +      it '51文字以上なら失敗する' do
 +        @lg.name = 'a'*51
 +        @lg.should_not be_valid
 +      end
 +      it '重複していたら失敗する' do
 +        l = Factory :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
 +      it '51文字以上なら失敗する' do
 +        @lg.classname = 'a'*51
 +        @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
 +      it '51文字以上なら失敗する' do
 +        @lg.caption = 'a'*51
 +        @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
 +      it '201文字以上なら失敗する' do
 +        @lg.url = 'a'*201
 +        @lg.should_not be_valid
 +      end
 +      it 'url形式でないなら失敗する' do
 +        @lg.url = 'aaaaaaa'
 +        @lg.should_not be_valid
 +      end
 +    end
 +  end
 +  
++  describe '更新に於いて' do
++    before do
++      @n = @j.keys.first
++      @a = @j.values.first
++    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)
++        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)
++        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
++    end
++  end
++  
 +  describe 'インポートに於いて' do
 +    before do
 +    end
 +    context 'つつがなく終わるとき' 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)
++      it 'ファイルインポートを依頼する' 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.import(@t)
-         }.should change CommonLicense, :count
++        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 eq []
++          LicenseGroup.import(@f)
++        }.should change LicenseGroup, :count
 +      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(2)
-         CommonLicense.import(@ts)
++        r = LicenseGroup.import(@f)
++        r.should eq []
 +      end
 +    end
 +    context '複数データがつつがなく終わるとき' do
 +      it 'ライセンスグループ更新を二回依頼する' do
-           CommonLicense.import(@ts)
-         }.should change(CommonLicense, :count).by 2
++        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 eq []
++          r = LicenseGroup.import(@fs)
++        }.should change(LicenseGroup, :count).by 2
 +      end
 +      it '[]を返す' do
-     context 'コモンライセンス作成に失敗したとき' do
++        r = LicenseGroup.import(@fs)
++        r.should eq []
 +      end
 +    end
-         CommonLicense.any_instance.stub(:save).with(any_args).and_return(false)
-         CommonLicense.any_instance.stub(:valid?).with(any_args).and_return(false)
++    context 'ライセンスグループ作成に失敗したとき' do
 +      before do
-       it 'コモンライセンスの数に変化がない' do
++        LicenseGroup.any_instance.stub(:save).with(any_args).and_return(false)
++        LicenseGroup.any_instance.stub(:valid?).with(any_args).and_return(false)
 +      end
-           CommonLicense.import(@t)
-         }.should_not change CommonLicense, :count
++      it 'ライセンスグループの数に変化がない' do
 +        lambda {
-         r = CommonLicense.import(@t)
++          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
-       it 'コモンライセンスオブジェクトが入っている' do
-         r = CommonLicense.import(@t)
-         r.first.is_a?(CommonLicense).should be_true
++        r = LicenseGroup.import(@f)
 +        r.should have(1).items
 +      end
-     context '複数のコモンライセンス作成に失敗したとき' do
++      it 'ライセンスグループオブジェクトが入っている' do
++        r = LicenseGroup.import(@f)
++        r.first.is_a?(LicenseGroup).should be_true
 +      end
 +    end
-       it 'コモンライセンスの数に変化がない' do
++    context '複数のライセンスグループ作成に失敗したとき' do
 +      #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
-           CommonLicense.import(@tes)
-         }.should_not change CommonLicense, :count
++      it 'ライセンスグループの数に変化がない' do
 +        lambda {
-         CommonLicense.stub(:store).with(any_args).and_return(CommonLicense.new)
-         CommonLicense.should_receive(:store).with(any_args).exactly(3)
-         CommonLicense.import(@tes)
++          LicenseGroup.import(@fes)
++        }.should_not change LicenseGroup, :count
 +      end
 +      it '途中で保存に失敗しても全件更新依頼する' do
-         r = 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
-       it '配列の中身は失敗したコモンライセンスオブジェクトが入っている' 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'
++        r = LicenseGroup.import(@fes)
 +        r.should have(2).items
 +      end
++      it '配列の中身は失敗したライセンスグループオブジェクトが入っている' 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
 +  
 +end