Commit 57a04a40 by Scott Taylor

Fix specs. Cleanup spec helpers

parent 9c68c8c9
......@@ -50,7 +50,7 @@ module AnnotateModels
attrs = []
attrs << "default(#{quote(col.default)})" unless col.default.nil?
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
if col_type == "decimal"
......@@ -163,12 +163,12 @@ module AnnotateModels
if annotate_one_file(model_file_name, info, options_with_position(options, :position_in_class))
annotated = true
end
unless ENV['exclude_tests']
[
File.join(UNIT_TEST_DIR, "#{model_name}_test.rb"), # test
File.join(SPEC_MODEL_DIR, "#{model_name}_spec.rb"), # spec
].each do |file|
].each do |file|
# todo: add an option "position_in_test" -- or maybe just ask if anyone ever wants different positions for model vs. test vs. fixture
annotate_one_file(file, info, options_with_position(options, :position_in_fixture))
end
......@@ -179,21 +179,21 @@ module AnnotateModels
File.join(EXEMPLARS_TEST_DIR, "#{model_name}_exemplar.rb"), # Object Daddy
File.join(EXEMPLARS_SPEC_DIR, "#{model_name}_exemplar.rb"), # Object Daddy
File.join(BLUEPRINTS_DIR, "#{model_name}_blueprint.rb"), # Machinist Blueprints
].each do |file|
].each do |file|
annotate_one_file(file, info, options_with_position(options, :position_in_fixture))
end
FIXTURE_DIRS.each do |dir|
fixture_file_name = File.join(dir,klass.table_name + ".yml")
if File.exist?(fixture_file_name)
annotate_one_file(fixture_file_name, info, options_with_position(options, :position_in_fixture))
annotate_one_file(fixture_file_name, info, options_with_position(options, :position_in_fixture))
end
end
end
annotated
end
# position = :position_in_fixture or :position_in_class
def options_with_position(options, position_in)
options.merge(:position=>(options[position_in] || options[:position]))
......@@ -274,7 +274,7 @@ module AnnotateModels
puts "Unable to annotate #{file}: #{e.inspect}"
puts ""
# todo: check if all backtrace lines are in "gems" -- if so, it's an annotate bug, so print the whole stack trace.
# puts e.backtrace.join("\n\t")
# puts e.backtrace.join("\n\t")
end
end
if annotated.empty?
......@@ -303,12 +303,12 @@ module AnnotateModels
fixture_file_name = File.join(dir,klass.table_name + ".yml")
remove_annotation_of_file(fixture_file_name) if File.exist?(fixture_file_name)
end
[ File.join(UNIT_TEST_DIR, "#{klass.name.underscore}_test.rb"),
File.join(SPEC_MODEL_DIR,"#{klass.name.underscore}_spec.rb")].each do |file|
remove_annotation_of_file(file) if File.exist?(file)
end
end
rescue Exception => e
puts "Unable to annotate #{file}: #{e.message}"
......
......@@ -4,13 +4,30 @@ require 'rubygems'
require 'activesupport'
describe AnnotateModels do
def mock_class(table_name, primary_key, columns)
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
}
def mock_klass(stubs={})
@mock_file ||= mock("Klass", stubs)
mock("An ActiveRecord class", options)
end
def mock_column(stubs={})
@mock_column ||= 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)
end
it { AnnotateModels.quote(nil).should eql("NULL") }
......@@ -21,22 +38,18 @@ describe AnnotateModels do
it { AnnotateModels.quote(1e-20).should eql("1.0e-20") }
it "should get schema info" do
klass = mock_class(:users, :id, [
mock_column(:id, :integer),
mock_column(:name, :string, :limit => 50)
])
AnnotateModels.get_schema_info(mock_klass(
:connection => mock("Conn", :indexes => []),
:table_name => "users",
:primary_key => "id",
:column_names => ["id","login"],
: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)
]), "Schema Info").should eql(<<-EOS)
AnnotateModels.get_schema_info(klass, "Schema Info").should eql(<<-EOS)
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# id :integer not null, primary key
# id :integer not null, primary key
# name :string(50) not null
#
EOS
......
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