From 06f8a6f308fe7bc0377e956784192e87c74c5808 Mon Sep 17 00:00:00 2001 From: yasushiito Date: Thu, 21 Jun 2012 18:21:57 +0900 Subject: [PATCH 1/1] miss edit importer --- app/models/license_group.rb | 4 + db/schema.rb | 8 +- spec/factories.rb | 7 ++ spec/json/license_group.json | 8 ++ spec/models/license_group_spec.rb | 96 +++++++++++++++++++++- .../plugins/pettan_importer/lib/pettan_importer.rb | 4 +- vendor/plugins/pettan_importer/test/import.json | 5 +- vendor/plugins/pettan_importer/test/import_spec.rb | 6 ++ 8 files changed, 127 insertions(+), 11 deletions(-) create mode 100644 spec/json/license_group.json diff --git a/app/models/license_group.rb b/app/models/license_group.rb index d59ba4e1..a1ab14c0 100644 --- a/app/models/license_group.rb +++ b/app/models/license_group.rb @@ -1,2 +1,6 @@ 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 end diff --git a/db/schema.rb b/db/schema.rb index 6b827bd0..a9668bc9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -12,6 +12,7 @@ # It's strongly recommended to check this file into your version control system. ActiveRecord::Schema.define(:version => 20120617061753) do + create_table "admins", :force => true do |t| t.string "email", :default => "", :null => false t.string "encrypted_password", :limit => 128, :default => "", :null => false @@ -111,13 +112,6 @@ ActiveRecord::Schema.define(:version => 20120617061753) do t.datetime "updated_at" end - create_table "imports", :force => true do |t| - t.integer "a" - t.string "b", :null => false - t.datetime "created_at" - t.datetime "updated_at" - end - create_table "licenses", :force => true do |t| t.integer "license_group_id", :default => 0, :null => false t.string "name", :limit => 50, :default => "Default", :null => false diff --git a/spec/factories.rb b/spec/factories.rb index 2f449d2d..84ff1eed 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -36,6 +36,13 @@ Factory.define :artist_yas, :class => Artist do |artist| # artist.association :author_yas end +Factory.define :license_group, :class => LicenseGroup do |license_group| + license_group.name 'pettan_public_01' + license_group.classname 'PettanPublicLicense' + license_group.caption 'pettan public 0.1' + license_group.url 'http://test.lc/' +end + Factory.define :license, :class => License do |license| license.name 'peta2.5' license.url 'http://test.lc/' diff --git a/spec/json/license_group.json b/spec/json/license_group.json new file mode 100644 index 00000000..f289081d --- /dev/null +++ b/spec/json/license_group.json @@ -0,0 +1,8 @@ +{ + "PublicDomain": { + "name": "PublicDomain", + "classname": "PublicDomain", + "caption": "Public Domain", + "url": "http://test.com/" + } +} diff --git a/spec/models/license_group_spec.rb b/spec/models/license_group_spec.rb index fddc68a7..ebf426ec 100644 --- a/spec/models/license_group_spec.rb +++ b/spec/models/license_group_spec.rb @@ -1,5 +1,99 @@ +# -*- encoding: utf-8 -*- +#ライセンスグループ require 'spec_helper' describe LicenseGroup do - pending "add some examples to (or delete) #{__FILE__}" + before do + #テストデータを用意してね + @f = Rails.root + 'spec/json/license_group.json' + @t = File.open(@f, 'r').read + @j = JSON.parse @t + 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 + end diff --git a/vendor/plugins/pettan_importer/lib/pettan_importer.rb b/vendor/plugins/pettan_importer/lib/pettan_importer.rb index 2a3e378d..e546d72b 100644 --- a/vendor/plugins/pettan_importer/lib/pettan_importer.rb +++ b/vendor/plugins/pettan_importer/lib/pettan_importer.rb @@ -30,14 +30,14 @@ module ActiveRecord res end - def import_file(filename) + def import_file(filename, &blk) t = nil begin t = File.open(filename, 'r').read rescue return false end - self.import t + self.import t, blk end end diff --git a/vendor/plugins/pettan_importer/test/import.json b/vendor/plugins/pettan_importer/test/import.json index 4ca50573..bdd519e3 100644 --- a/vendor/plugins/pettan_importer/test/import.json +++ b/vendor/plugins/pettan_importer/test/import.json @@ -1,3 +1,6 @@ { - "a": 1 + "1": { + "a": 1, + "b": "x" + } } diff --git a/vendor/plugins/pettan_importer/test/import_spec.rb b/vendor/plugins/pettan_importer/test/import_spec.rb index b3700754..2432a1df 100644 --- a/vendor/plugins/pettan_importer/test/import_spec.rb +++ b/vendor/plugins/pettan_importer/test/import_spec.rb @@ -155,6 +155,12 @@ describe Import do Import.should_receive(:import).with(any_args).exactly(1) Import.import_file(@f) end + it 'ブロックがあるとき更新を一回依頼する' do + Import.any_instance.stub(:valid?).with(any_args).and_return(true) + #newでスタブを作るとインスタンス生成ができないので、テスト用スタブで + Import.should_receive(:etest).with(any_args).exactly(1) + Import.import_file(@f) {|name, attr| Import.etest ; Import.new(attr) } + end #テキスト取り込み成功でTrueが返る it 'Trueを返す' do Import.import_file(@f).should be_true -- 2.11.0