mirror of
https://github.com/awesome-print/awesome_print
synced 2023-03-27 23:22:34 -04:00
Made specs pass with ActiveRecord 3.1
This commit is contained in:
parent
fc23c998bb
commit
c6d27131df
1 changed files with 186 additions and 73 deletions
|
@ -3,54 +3,103 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
|||
begin
|
||||
require 'active_record'
|
||||
require 'awesome_print/ext/active_record'
|
||||
return if !defined?(ActiveRecord::VERSION::MAJOR) || ActiveRecord::VERSION::MAJOR < 2
|
||||
|
||||
# Create tableless ActiveRecord model.
|
||||
#------------------------------------------------------------------------------
|
||||
class User < ActiveRecord::Base
|
||||
def self.columns()
|
||||
@columns ||= []
|
||||
end
|
||||
|
||||
def self.column(name, sql_type = nil, default = nil, null = true)
|
||||
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
|
||||
end
|
||||
|
||||
column :id, :integer
|
||||
column :name, :string
|
||||
column :rank, :integer
|
||||
column :admin, :boolean
|
||||
column :created_at, :datetime
|
||||
|
||||
def self.table_exists?
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
class SubUser < User
|
||||
def self.columns
|
||||
User.columns
|
||||
end
|
||||
end
|
||||
|
||||
describe "AwesomePrint/ActiveRecord" do
|
||||
before do
|
||||
stub_dotfile!
|
||||
end
|
||||
if defined?(ActiveRecord::VERSION::MAJOR) && ActiveRecord::VERSION::MAJOR >= 2
|
||||
|
||||
# Create tableless ActiveRecord model.
|
||||
#------------------------------------------------------------------------------
|
||||
describe "ActiveRecord instance" do
|
||||
before do
|
||||
ActiveRecord::Base.default_timezone = :utc
|
||||
@diana = User.new(:name => "Diana", :rank => 1, :admin => false, :created_at => "1992-10-10 12:30:00")
|
||||
@laura = User.new(:name => "Laura", :rank => 2, :admin => true, :created_at => "2003-05-26 14:15:00")
|
||||
@ap = AwesomePrint::Inspector.new(:plain => true, :sort_keys => true)
|
||||
class User < ActiveRecord::Base
|
||||
def self.columns()
|
||||
@columns ||= []
|
||||
end
|
||||
|
||||
it "display single record" do
|
||||
out = @ap.send(:awesome, @diana)
|
||||
if ActiveRecord::VERSION::MAJOR == 3
|
||||
str = <<-EOS.strip
|
||||
# Tableless model support for ActiveRecord 3.1 mess.
|
||||
if self.respond_to?(:column_defaults)
|
||||
def self.primary_key
|
||||
:id
|
||||
end
|
||||
|
||||
def self.column_defaults
|
||||
@column_defaults ||= Hash[columns.map { |column| [column.name, nil] }]
|
||||
end
|
||||
|
||||
def self.columns_hash
|
||||
@columns_hash ||= Hash[columns.map { |column| [column.name, column] }]
|
||||
end
|
||||
end
|
||||
|
||||
def self.column(name, sql_type = nil, default = nil, null = true)
|
||||
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
|
||||
end
|
||||
|
||||
column :id, :integer
|
||||
column :name, :string
|
||||
column :rank, :integer
|
||||
column :admin, :boolean
|
||||
column :created_at, :datetime
|
||||
|
||||
def self.table_exists?
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
class SubUser < User
|
||||
def self.columns
|
||||
User.columns
|
||||
end
|
||||
end
|
||||
|
||||
describe "AwesomePrint/ActiveRecord" do
|
||||
before do
|
||||
stub_dotfile!
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
describe "ActiveRecord instance" do
|
||||
before do
|
||||
ActiveRecord::Base.default_timezone = :utc
|
||||
@diana = User.new(:name => "Diana", :rank => 1, :admin => false, :created_at => "1992-10-10 12:30:00")
|
||||
@laura = User.new(:name => "Laura", :rank => 2, :admin => true, :created_at => "2003-05-26 14:15:00")
|
||||
@ap = AwesomePrint::Inspector.new(:plain => true, :sort_keys => true)
|
||||
end
|
||||
|
||||
it "display single record" do
|
||||
out = @ap.send(:awesome, @diana)
|
||||
|
||||
# ActiveRecord 3.1
|
||||
#--------------------------------------------------------------------------
|
||||
if ActiveRecord::VERSION::STRING.start_with?('3.1')
|
||||
str = <<-EOS.strip
|
||||
#<User:0x01234567
|
||||
@aggregation_cache = {},
|
||||
@attributes_cache = {},
|
||||
@destroyed = false,
|
||||
@marked_for_destruction = false,
|
||||
@new_record = true,
|
||||
@previously_changed = {},
|
||||
@readonly = false,
|
||||
@relation = nil,
|
||||
attr_accessor :attributes = {
|
||||
"admin" => false,
|
||||
"created_at" => "1992-10-10 12:30:00",
|
||||
"id" => nil,
|
||||
"name" => "Diana",
|
||||
"rank" => 1
|
||||
},
|
||||
attr_reader :association_cache = {},
|
||||
attr_reader :changed_attributes = {
|
||||
"admin" => nil,
|
||||
"created_at" => nil,
|
||||
"name" => nil,
|
||||
"rank" => nil
|
||||
},
|
||||
attr_reader :mass_assignment_options = nil
|
||||
>
|
||||
EOS
|
||||
# ActiveRecord 3.0.x
|
||||
#--------------------------------------------------------------------------
|
||||
elsif ActiveRecord::VERSION::STRING.start_with?('3.0')
|
||||
str = <<-EOS.strip
|
||||
#<User:0x01234567
|
||||
@attributes_cache = {},
|
||||
@destroyed = false,
|
||||
|
@ -72,8 +121,10 @@ begin
|
|||
}
|
||||
>
|
||||
EOS
|
||||
else # ActiveRecord 2.x
|
||||
str = <<-EOS.strip
|
||||
# ActiveRecord 2.x
|
||||
#--------------------------------------------------------------------------
|
||||
else
|
||||
str = <<-EOS.strip
|
||||
#<User:0x01234567
|
||||
@attributes_cache = {},
|
||||
@changed_attributes = {
|
||||
|
@ -91,15 +142,75 @@ EOS
|
|||
}
|
||||
>
|
||||
EOS
|
||||
end
|
||||
str.sub!('?', '1992-10-10 12:30:00')
|
||||
out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str
|
||||
end
|
||||
str.sub!('?', '1992-10-10 12:30:00')
|
||||
out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str
|
||||
end
|
||||
|
||||
it "display multiple records" do
|
||||
out = @ap.send(:awesome, [ @diana, @laura ])
|
||||
if ActiveRecord::VERSION::MAJOR == 3
|
||||
str = <<-EOS.strip
|
||||
it "display multiple records" do
|
||||
out = @ap.send(:awesome, [ @diana, @laura ])
|
||||
|
||||
# ActiveRecord 3.1
|
||||
#--------------------------------------------------------------------------
|
||||
if ActiveRecord::VERSION::STRING.start_with?('3.1')
|
||||
str = <<-EOS.strip
|
||||
[
|
||||
[0] #<User:0x01234567
|
||||
@aggregation_cache = {},
|
||||
@attributes_cache = {},
|
||||
@destroyed = false,
|
||||
@marked_for_destruction = false,
|
||||
@new_record = true,
|
||||
@previously_changed = {},
|
||||
@readonly = false,
|
||||
@relation = nil,
|
||||
attr_accessor :attributes = {
|
||||
"admin" => false,
|
||||
"created_at" => "1992-10-10 12:30:00",
|
||||
"id" => nil,
|
||||
"name" => "Diana",
|
||||
"rank" => 1
|
||||
},
|
||||
attr_reader :association_cache = {},
|
||||
attr_reader :changed_attributes = {
|
||||
"admin" => nil,
|
||||
"created_at" => nil,
|
||||
"name" => nil,
|
||||
"rank" => nil
|
||||
},
|
||||
attr_reader :mass_assignment_options = nil
|
||||
>,
|
||||
[1] #<User:0x01234567
|
||||
@aggregation_cache = {},
|
||||
@attributes_cache = {},
|
||||
@destroyed = false,
|
||||
@marked_for_destruction = false,
|
||||
@new_record = true,
|
||||
@previously_changed = {},
|
||||
@readonly = false,
|
||||
@relation = nil,
|
||||
attr_accessor :attributes = {
|
||||
"admin" => true,
|
||||
"created_at" => "2003-05-26 14:15:00",
|
||||
"id" => nil,
|
||||
"name" => "Laura",
|
||||
"rank" => 2
|
||||
},
|
||||
attr_reader :association_cache = {},
|
||||
attr_reader :changed_attributes = {
|
||||
"admin" => nil,
|
||||
"created_at" => nil,
|
||||
"name" => nil,
|
||||
"rank" => nil
|
||||
},
|
||||
attr_reader :mass_assignment_options = nil
|
||||
>
|
||||
]
|
||||
EOS
|
||||
# ActiveRecord 3.0.x
|
||||
#--------------------------------------------------------------------------
|
||||
elsif ActiveRecord::VERSION::STRING.start_with?('3.0')
|
||||
str = <<-EOS.strip
|
||||
[
|
||||
[0] #<User:0x01234567
|
||||
@attributes_cache = {},
|
||||
|
@ -143,8 +254,10 @@ EOS
|
|||
>
|
||||
]
|
||||
EOS
|
||||
else # ActiveRecord 2.x
|
||||
str = <<-EOS.strip
|
||||
# ActiveRecord 2.0.x
|
||||
#--------------------------------------------------------------------------
|
||||
else
|
||||
str = <<-EOS.strip
|
||||
[
|
||||
[0] #<User:0x01234567
|
||||
@attributes_cache = {},
|
||||
|
@ -180,21 +293,21 @@ EOS
|
|||
>
|
||||
]
|
||||
EOS
|
||||
end
|
||||
str.sub!('?', '1992-10-10 12:30:00')
|
||||
str.sub!('?', '2003-05-26 14:15:00')
|
||||
out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str
|
||||
end
|
||||
str.sub!('?', '1992-10-10 12:30:00')
|
||||
str.sub!('?', '2003-05-26 14:15:00')
|
||||
out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str
|
||||
end
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
describe "ActiveRecord class" do
|
||||
before do
|
||||
@ap = AwesomePrint::Inspector.new(:plain => true)
|
||||
end
|
||||
|
||||
it "should print the class" do
|
||||
@ap.send(:awesome, User).should == <<-EOS.strip
|
||||
#------------------------------------------------------------------------------
|
||||
describe "ActiveRecord class" do
|
||||
before do
|
||||
@ap = AwesomePrint::Inspector.new(:plain => true)
|
||||
end
|
||||
|
||||
it "should print the class" do
|
||||
@ap.send(:awesome, User).should == <<-EOS.strip
|
||||
class User < ActiveRecord::Base {
|
||||
:id => :integer,
|
||||
:name => :string,
|
||||
|
@ -203,10 +316,10 @@ class User < ActiveRecord::Base {
|
|||
:created_at => :datetime
|
||||
}
|
||||
EOS
|
||||
end
|
||||
end
|
||||
|
||||
it "should print the class for non-direct subclasses of ActiveRecord::Base" do
|
||||
@ap.send(:awesome, SubUser).should == <<-EOS.strip
|
||||
it "should print the class for non-direct subclasses of ActiveRecord::Base" do
|
||||
@ap.send(:awesome, SubUser).should == <<-EOS.strip
|
||||
class SubUser < User {
|
||||
:id => :integer,
|
||||
:name => :string,
|
||||
|
@ -215,14 +328,14 @@ class SubUser < User {
|
|||
:created_at => :datetime
|
||||
}
|
||||
EOS
|
||||
end
|
||||
end
|
||||
|
||||
it "should print ActiveRecord::Base objects (ex. ancestors)" do
|
||||
lambda { @ap.send(:awesome, User.ancestors) }.should_not raise_error
|
||||
it "should print ActiveRecord::Base objects (ex. ancestors)" do
|
||||
lambda { @ap.send(:awesome, User.ancestors) }.should_not raise_error
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
rescue LoadError => error
|
||||
puts "Skipping ActiveRecord specs: #{error}"
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue