From: yasushiito Date: Tue, 26 Jun 2012 07:33:36 +0000 (+0900) Subject: pass model import test X-Git-Url: http://git.osdn.net/view?p=pettanr%2Fpettanr.git;a=commitdiff_plain;h=f207b70a40512fa3727ba924da27f19b928d680a pass model import test --- f207b70a40512fa3727ba924da27f19b928d680a diff --cc app/models/license_group.rb index a1ab14c0,00000000..34fbf32b mode 100644,000000..100644 --- a/app/models/license_group.rb +++ b/app/models/license_group.rb @@@ -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 diff --cc spec/json/invalid_license_groups.json index 00000000,00000000..afed9923 new file mode 100644 --- /dev/null +++ b/spec/json/invalid_license_groups.json @@@ -1,0 -1,0 +1,15 @@@ ++{ ++ "PublicDomain": { ++ "classname": "PublicDomain", ++ "caption": "Public Domain", ++ "url": "http://test.com/" ++ }, ++ "UnknownUrl": { ++ "classname": "Unknown", ++ "caption": "Unknown owner" ++ }, ++ "UnknownClassname": { ++ "caption": "Unknown owner", ++ "url": "http://test.uk/" ++ } ++} diff --cc spec/json/license_groups.json index 00000000,00000000..afb7d37d new file mode 100644 --- /dev/null +++ b/spec/json/license_groups.json @@@ -1,0 -1,0 +1,12 @@@ ++{ ++ "PublicDomain": { ++ "classname": "PublicDomain", ++ "caption": "Public Domain", ++ "url": "http://test.com/" ++ }, ++ "Unknown": { ++ "classname": "Unknown", ++ "caption": "Unknown owner", ++ "url": "http://test.uk/" ++ } ++} diff --cc spec/models/license_group_spec.rb index fbf9f0fc,00000000..96431fa1 mode 100644,000000..100644 --- a/spec/models/license_group_spec.rb +++ b/spec/models/license_group_spec.rb @@@ -1,192 -1,0 +1,221 @@@ +# -*- 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 - it 'テキストインポートを依頼する' do - CommonLicense.should_receive(:parse).with(any_args).exactly(1) - CommonLicense.stub(:parse).with(any_args).and_return(@j) - 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.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 - 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 - CommonLicense.import(@ts).should eq [] ++ r = LicenseGroup.import(@fs) ++ r.should eq [] + end + end - context 'コモンライセンス作成に失敗したとき' do ++ context 'ライセンスグループ作成に失敗したとき' 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 'コモンライセンスの数に変化がない' do ++ it 'ライセンスグループの数に変化がない' 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 'コモンライセンスオブジェクトが入っている' do - r = CommonLicense.import(@t) - r.first.is_a?(CommonLicense).should be_true ++ it 'ライセンスグループオブジェクトが入っている' do ++ r = LicenseGroup.import(@f) ++ r.first.is_a?(LicenseGroup).should be_true + end + end - context '複数のコモンライセンス作成に失敗したとき' do ++ context '複数のライセンスグループ作成に失敗したとき' do + #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する - it 'コモンライセンスの数に変化がない' do ++ it 'ライセンスグループの数に変化がない' 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 '配列の中身は失敗したコモンライセンスオブジェクトが入っている' 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 '配列の中身は失敗したライセンスグループオブジェクトが入っている' 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