Commit 10f896ee by fistfvck

Merge remote branch 'smtlaissezfaire/master' into restart

Conflicts: lib/annotate/annotate_models.rb spec/annotate/annotate_models_spec.rb
parents a153002b 1dff6966
require 'rubygems'
require 'rake' require 'rake'
require 'lib/annotate' require 'lib/annotate'
......
...@@ -58,7 +58,7 @@ module AnnotateModels ...@@ -58,7 +58,7 @@ module AnnotateModels
attrs = [] attrs = []
attrs << "default(#{quote(col.default)})" unless col.default.nil? attrs << "default(#{quote(col.default)})" unless col.default.nil?
attrs << "not null" unless col.null attrs << "not null" unless col.null
attrs << "primary key" if col.name == klass.primary_key attrs << "primary key" if col.name.to_sym == klass.primary_key.to_sym
col_type = col.type.to_s col_type = col.type.to_s
if col_type == "decimal" if col_type == "decimal"
......
#encoding: utf-8 #encoding: utf-8
require File.dirname(__FILE__) + '/../spec_helper.rb' require File.dirname(__FILE__) + '/../spec_helper.rb'
require 'annotate/annotate_models' require 'annotate/annotate_models'
require 'rubygems'
require 'active_support' require 'active_support'
require 'fakefs/spec_helpers'
describe AnnotateModels do describe AnnotateModels do
include FakeFS::SpecHelpers
before(:all) do before(:all) do
require "tmpdir" require "tmpdir"
...@@ -25,11 +26,29 @@ describe AnnotateModels do ...@@ -25,11 +26,29 @@ describe AnnotateModels do
@dir + '/' + file @dir + '/' + file
end end
def mock_klass(stubs={}) def mock_class(table_name, primary_key, columns)
mock("Klass", stubs) options = {
:connection => mock("Conn", :indexes => []),
:table_name => table_name,
:primary_key => primary_key.to_s,
:column_names => columns.map { |col| col.name.to_s },
:columns => columns
}
mock("An ActiveRecord class", options)
end end
def mock_column(stubs={}) def mock_column(name, type, options={})
default_options = {
:limit => nil,
:null => false,
:default => nil
}
stubs = default_options.dup
stubs.merge!(options)
stubs.merge!(:name => name, :type => type)
mock("Column", stubs) mock("Column", stubs)
end end
...@@ -40,66 +59,13 @@ describe AnnotateModels do ...@@ -40,66 +59,13 @@ describe AnnotateModels do
it { AnnotateModels.quote(25.6).should eql("25.6") } it { AnnotateModels.quote(25.6).should eql("25.6") }
it { AnnotateModels.quote(1e-20).should eql("1.0e-20") } it { AnnotateModels.quote(1e-20).should eql("1.0e-20") }
describe "schema info" do
before(:each) do
@schema_info = <<-EOS
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# name :string(50) not null
#
EOS
@user_file = create('user.rb', <<-EOS)
class User < ActiveRecord::Base
end
EOS
@mock = mock_klass(
:connection => mock("Conn", :indexes => []),
:table_name => "users",
:primary_key => "id",
:column_names => ["id","name"],
:columns => [
mock_column(:type => "integer", :default => nil, :null => false, :name => "id", :limit => nil),
mock_column(:type => "string", :default => nil, :null => false, :name => "name", :limit => 50)
])
end
it "should get schema info" do it "should get schema info" do
AnnotateModels.get_schema_info(@mock , "Schema Info").should eql(@schema_info) klass = mock_class(:users, :id, [
end mock_column(:id, :integer),
mock_column(:name, :string, :limit => 50)
it "should write the schema before (default)" do ])
ARGV.stub!(:dup).and_return []
AnnotateModels.stub!(:get_schema_info).and_return @schema_info
AnnotateModels.do_annotations
File.read(@user_file).should eql(<<-EOF)
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# name :string(50) not null
#
class User < ActiveRecord::Base
end
EOF
end
it "should write the schema after" do
ARGV.stub!(:dup).and_return []
AnnotateModels.stub!(:get_schema_info).and_return @schema_info
AnnotateModels.do_annotations(:position => :after)
File.read(@user_file).should eql(<<-EOF)
class User < ActiveRecord::Base
end
AnnotateModels.get_schema_info(klass, "Schema Info").should eql(<<-EOS)
# Schema Info # Schema Info
# #
# Table name: users # Table name: users
...@@ -108,9 +74,7 @@ end ...@@ -108,9 +74,7 @@ end
# name :string(50) not null # name :string(50) not null
# #
EOF EOS
end
end end
describe "#get_model_class" do describe "#get_model_class" do
...@@ -151,18 +115,16 @@ EOF ...@@ -151,18 +115,16 @@ EOF
describe "#remove_annotation_of_file" do describe "#remove_annotation_of_file" do
def create(file, body="hi") def create(file, body="hi")
File.open(@dir + '/' + file, "w") do |f| File.open(file, "w") do |f|
f.puts(body) f.puts(body)
end end
end end
def content(file) def content(file)
File.read(@dir + '/' + file) File.read(file)
end end
before :all do it "should remove before annotate" do
require "tmpdir"
@dir = Dir.tmpdir + "/#{Time.now.to_i}" + "/annotate_models"
FileUtils.mkdir_p(@dir)
create("before.rb", <<-EOS) create("before.rb", <<-EOS)
# == Schema Information # == Schema Information
# #
...@@ -176,6 +138,16 @@ EOF ...@@ -176,6 +138,16 @@ EOF
class Foo < ActiveRecord::Base class Foo < ActiveRecord::Base
end end
EOS EOS
AnnotateModels.remove_annotation_of_file("before.rb")
content("before.rb").should == <<-EOS
class Foo < ActiveRecord::Base
end
EOS
end
it "should remove after annotate" do
create("after.rb", <<-EOS) create("after.rb", <<-EOS)
class Foo < ActiveRecord::Base class Foo < ActiveRecord::Base
end end
...@@ -190,16 +162,9 @@ end ...@@ -190,16 +162,9 @@ end
# #
EOS EOS
end
it "should remove before annotate" do AnnotateModels.remove_annotation_of_file("after.rb")
AnnotateModels.remove_annotation_of_file(@dir + '/' + "before.rb")
content("before.rb").should == <<-EOS
class Foo < ActiveRecord::Base
end
EOS
end
it "should remove after annotate" do
AnnotateModels.remove_annotation_of_file(@dir + '/' + "after.rb")
content("after.rb").should == <<-EOS content("after.rb").should == <<-EOS
class Foo < ActiveRecord::Base class Foo < ActiveRecord::Base
end end
...@@ -207,4 +172,38 @@ end ...@@ -207,4 +172,38 @@ end
end end
end end
describe "annotating a file" do
before do
@file_name = "user.rb"
@file_content = "class User < ActiveRecord::Base; end"
@klass = mock_class(:users, :id, [
mock_column(:id, :integer),
mock_column(:name, :string, :limit => 50)
])
end
def write_file
File.open("user.rb", "w") do |f|
f << @file_content
end
end
it "should annotate the file before the model if position == 'before'" do
write_file
schema_info = AnnotateModels.get_schema_info(@klass, "Schema Info")
AnnotateModels.annotate_one_file("user.rb", schema_info, :position => "before")
File.read("user.rb").should == "#{schema_info}#{@file_content}\n"
end
it "should annotate before if given :position => :before" do
write_file
schema_info = AnnotateModels.get_schema_info(@klass, "Schema Info")
AnnotateModels.annotate_one_file(@file_name, schema_info, :position => :before)
File.read("user.rb").should == "#{schema_info}#{@file_content}\n"
end
end
end end
begin require 'rubygems'
require 'spec' require 'spec'
rescue LoadError
require 'rubygems'
gem 'rspec'
require 'spec'
end
$:.unshift(File.dirname(__FILE__) + '/../lib') $:.unshift(File.dirname(__FILE__) + '/../lib')
require 'annotate' require 'annotate'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment