OSDN Git Service

miss edit importer
authoryasushiito <yas@pen-chan.jp>
Thu, 21 Jun 2012 09:21:57 +0000 (18:21 +0900)
committeryasushiito <yas@pen-chan.jp>
Thu, 21 Jun 2012 09:21:57 +0000 (18:21 +0900)
app/models/license_group.rb
db/schema.rb
spec/factories.rb
spec/json/license_group.json [new file with mode: 0644]
spec/models/license_group_spec.rb
vendor/plugins/pettan_importer/lib/pettan_importer.rb
vendor/plugins/pettan_importer/test/import.json
vendor/plugins/pettan_importer/test/import_spec.rb

index d59ba4e..a1ab14c 100644 (file)
@@ -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
index 6b827bd..a9668bc 100644 (file)
@@ -12,6 +12,7 @@
 # It's strongly recommended to check this file into your version control system.\r
 \r
 ActiveRecord::Schema.define(:version => 20120617061753) do\r
+\r
   create_table "admins", :force => true do |t|\r
     t.string   "email",                                 :default => "", :null => false\r
     t.string   "encrypted_password",     :limit => 128, :default => "", :null => false\r
@@ -111,13 +112,6 @@ ActiveRecord::Schema.define(:version => 20120617061753) do
     t.datetime "updated_at"\r
   end\r
 \r
-  create_table "imports", :force => true do |t|\r
-    t.integer  "a"\r
-    t.string   "b",          :null => false\r
-    t.datetime "created_at"\r
-    t.datetime "updated_at"\r
-  end\r
-\r
   create_table "licenses", :force => true do |t|\r
     t.integer  "license_group_id",               :default => 0,         :null => false\r
     t.string   "name",             :limit => 50, :default => "Default", :null => false\r
index 2f449d2..84ff1ee 100644 (file)
@@ -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 (file)
index 0000000..f289081
--- /dev/null
@@ -0,0 +1,8 @@
+{\r
+  "PublicDomain": {\r
+    "name": "PublicDomain", \r
+    "classname": "PublicDomain", \r
+    "caption": "Public Domain", \r
+    "url": "http://test.com/"\r
+  }\r
+}\r
index fddc68a..ebf426e 100644 (file)
@@ -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
index 2a3e378..e546d72 100644 (file)
@@ -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
index 4ca5057..bdd519e 100644 (file)
@@ -1,3 +1,6 @@
 {\r
-  "a": 1\r
+  "1": {\r
+    "a": 1,\r
+    "b": "x"\r
+  }\r
 }\r
index b370075..2432a1d 100644 (file)
@@ -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