2014-12-31 15:59:32 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
require 'active_record_helper'
|
2011-05-13 19:37:24 -04:00
|
|
|
|
2016-11-08 21:39:25 -05:00
|
|
|
RSpec.describe 'AwesomePrint/ActiveRecord', skip: -> { !ExtVerifier.has_rails? }.call do
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'ActiveRecord instance, attributes only (default)' do
|
2014-12-31 15:59:32 -05:00
|
|
|
before do
|
|
|
|
ActiveRecord::Base.default_timezone = :utc
|
2016-11-08 09:53:17 -05:00
|
|
|
@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)
|
2014-12-31 15:59:32 -05:00
|
|
|
end
|
2011-11-24 03:16:43 -05:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'display single record' do
|
2016-05-09 10:04:42 -04:00
|
|
|
out = @ap.awesome(@diana)
|
2014-12-31 15:59:32 -05:00
|
|
|
str = <<-EOS.strip
|
2016-05-09 10:04:42 -04:00
|
|
|
#<User:placeholder_id> {
|
2011-12-18 18:49:43 -05:00
|
|
|
:admin => false,
|
2011-12-20 01:30:06 -05:00
|
|
|
:created_at => ?,
|
2011-12-18 18:49:43 -05:00
|
|
|
:id => nil,
|
|
|
|
:name => "Diana",
|
|
|
|
:rank => 1
|
|
|
|
}
|
2014-12-31 15:59:32 -05:00
|
|
|
EOS
|
|
|
|
if RUBY_VERSION < '1.9'
|
|
|
|
str.sub!('?', 'Sat Oct 10 12:30:00 UTC 1992')
|
|
|
|
else
|
|
|
|
str.sub!('?', '1992-10-10 12:30:00 UTC')
|
|
|
|
end
|
2016-05-09 10:04:42 -04:00
|
|
|
expect(out).to be_similar_to(str)
|
2014-12-31 15:59:32 -05:00
|
|
|
end
|
2011-12-18 18:49:43 -05:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'display multiple records' do
|
2016-11-08 21:39:25 -05:00
|
|
|
out = @ap.awesome([@diana, @laura])
|
2014-12-31 15:59:32 -05:00
|
|
|
str = <<-EOS.strip
|
2011-12-18 18:49:43 -05:00
|
|
|
[
|
2016-05-09 10:04:42 -04:00
|
|
|
[0] #<User:placeholder_id> {
|
2011-12-18 18:49:43 -05:00
|
|
|
:admin => false,
|
2011-12-20 01:30:06 -05:00
|
|
|
:created_at => ??,
|
2011-12-18 18:49:43 -05:00
|
|
|
:id => nil,
|
|
|
|
:name => "Diana",
|
|
|
|
:rank => 1
|
|
|
|
},
|
2016-05-09 10:04:42 -04:00
|
|
|
[1] #<User:placeholder_id> {
|
2011-12-18 18:49:43 -05:00
|
|
|
:admin => true,
|
2011-12-20 01:30:06 -05:00
|
|
|
:created_at => ?!,
|
2011-12-18 18:49:43 -05:00
|
|
|
:id => nil,
|
|
|
|
:name => "Laura",
|
|
|
|
:rank => 2
|
|
|
|
}
|
2015-02-11 16:52:11 -05:00
|
|
|
]
|
|
|
|
EOS
|
|
|
|
if RUBY_VERSION < '1.9'
|
|
|
|
str.sub!('??', 'Sat Oct 10 12:30:00 UTC 1992')
|
|
|
|
str.sub!('?!', 'Mon May 26 14:15:00 UTC 2003')
|
|
|
|
else
|
|
|
|
str.sub!('??', '1992-10-10 12:30:00 UTC')
|
|
|
|
str.sub!('?!', '2003-05-26 14:15:00 UTC')
|
|
|
|
end
|
2016-05-09 10:04:42 -04:00
|
|
|
expect(out).to be_similar_to(str)
|
2015-02-11 16:52:11 -05:00
|
|
|
end
|
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'display multiple records on a relation' do
|
2015-02-11 16:52:11 -05:00
|
|
|
@diana.save
|
|
|
|
@laura.save
|
2016-05-09 10:04:42 -04:00
|
|
|
out = @ap.awesome(User.all)
|
2015-02-11 16:52:11 -05:00
|
|
|
str = <<-EOS.strip
|
|
|
|
[
|
2016-05-09 10:04:42 -04:00
|
|
|
[0] #<User:placeholder_id> {
|
2015-02-11 16:52:11 -05:00
|
|
|
:admin => false,
|
|
|
|
:created_at => ??,
|
|
|
|
:id => 1,
|
|
|
|
:name => "Diana",
|
|
|
|
:rank => 1
|
|
|
|
},
|
2016-05-09 10:04:42 -04:00
|
|
|
[1] #<User:placeholder_id> {
|
2015-02-11 16:52:11 -05:00
|
|
|
:admin => true,
|
|
|
|
:created_at => ?!,
|
|
|
|
:id => 2,
|
|
|
|
:name => "Laura",
|
|
|
|
:rank => 2
|
|
|
|
}
|
2011-12-18 18:49:43 -05:00
|
|
|
]
|
2014-12-31 15:59:32 -05:00
|
|
|
EOS
|
|
|
|
if RUBY_VERSION < '1.9'
|
|
|
|
str.sub!('??', 'Sat Oct 10 12:30:00 UTC 1992')
|
|
|
|
str.sub!('?!', 'Mon May 26 14:15:00 UTC 2003')
|
|
|
|
else
|
|
|
|
str.sub!('??', '1992-10-10 12:30:00 UTC')
|
|
|
|
str.sub!('?!', '2003-05-26 14:15:00 UTC')
|
2011-12-18 18:49:43 -05:00
|
|
|
end
|
2016-05-09 10:04:42 -04:00
|
|
|
expect(out).to be_similar_to(str)
|
2014-12-31 15:59:32 -05:00
|
|
|
end
|
|
|
|
end
|
2011-12-18 18:49:43 -05:00
|
|
|
|
2015-12-01 17:38:15 -05:00
|
|
|
describe 'Linked records (joins)' do
|
|
|
|
before do
|
2016-11-08 09:53:17 -05:00
|
|
|
@ap = AwesomePrint::Inspector.new(plain: true)
|
2015-12-01 17:38:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should show the entire record' do
|
2016-11-08 09:53:17 -05:00
|
|
|
e = Email.create(email_address: 'foo@bar.com')
|
2015-12-01 17:38:15 -05:00
|
|
|
u = User.last
|
|
|
|
u.emails << e
|
|
|
|
email_record = User.joins(:emails).select('users.id, emails.email_address').last
|
|
|
|
out = @ap.awesome(email_record)
|
|
|
|
raw_object_string = <<-EOS.strip
|
|
|
|
#<User:placeholder_id> {
|
|
|
|
"id" => #{u.id},
|
|
|
|
"email_address" => "#{e.email_address}"
|
|
|
|
}
|
|
|
|
EOS
|
|
|
|
expect(out).to be_similar_to(raw_object_string)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-31 15:59:32 -05:00
|
|
|
#------------------------------------------------------------------------------
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'ActiveRecord instance (raw)' do
|
2014-12-31 15:59:32 -05:00
|
|
|
before do
|
|
|
|
ActiveRecord::Base.default_timezone = :utc
|
2016-11-08 09:53:17 -05:00
|
|
|
@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, raw: true)
|
2014-12-31 15:59:32 -05:00
|
|
|
end
|
2011-12-18 18:49:43 -05:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'display single record' do
|
2016-05-09 10:04:42 -04:00
|
|
|
out = @ap.awesome(@diana)
|
2011-11-24 03:16:43 -05:00
|
|
|
|
2016-05-12 16:29:49 -04:00
|
|
|
raw_object_string =
|
2018-05-17 09:14:34 -04:00
|
|
|
if activerecord_5_2?
|
|
|
|
ActiveRecordData.raw_5_2_diana
|
|
|
|
elsif activerecord_5_1?
|
|
|
|
ActiveRecordData.raw_5_1_diana
|
|
|
|
elsif activerecord_5_0?
|
2016-05-12 17:47:42 -04:00
|
|
|
ActiveRecordData.raw_5_0_diana
|
|
|
|
elsif activerecord_4_2?
|
2016-05-12 16:29:49 -04:00
|
|
|
if RUBY_VERSION > '1.9.3'
|
|
|
|
ActiveRecordData.raw_4_2_diana
|
|
|
|
else
|
|
|
|
ActiveRecordData.raw_4_2_diana_legacy
|
|
|
|
end
|
|
|
|
elsif activerecord_4_1?
|
|
|
|
ActiveRecordData.raw_4_1_diana
|
|
|
|
elsif activerecord_4_0?
|
|
|
|
ActiveRecordData.raw_4_0_diana
|
|
|
|
elsif activerecord_3_2?
|
|
|
|
if RUBY_VERSION > '1.9.3'
|
|
|
|
ActiveRecordData.raw_3_2_diana
|
|
|
|
else
|
|
|
|
ActiveRecordData.raw_3_2_diana_legacy
|
|
|
|
end
|
2015-01-20 11:23:20 -05:00
|
|
|
end
|
2016-05-12 16:29:49 -04:00
|
|
|
raw_object_string.sub!('?', '1992-10-10 12:30:00')
|
|
|
|
expect(out).to be_similar_to(raw_object_string)
|
2014-12-31 15:59:32 -05:00
|
|
|
end
|
2011-05-13 19:37:24 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'display multiple records' do
|
2016-11-08 21:39:25 -05:00
|
|
|
out = @ap.awesome([@diana, @laura])
|
2011-11-24 03:16:43 -05:00
|
|
|
|
2016-05-12 16:29:49 -04:00
|
|
|
raw_object_string =
|
2018-05-17 09:14:34 -04:00
|
|
|
if activerecord_5_2?
|
|
|
|
ActiveRecordData.raw_5_2_multi
|
|
|
|
elsif activerecord_5_1?
|
|
|
|
ActiveRecordData.raw_5_1_multi
|
|
|
|
elsif activerecord_5_0?
|
2016-05-12 17:47:42 -04:00
|
|
|
ActiveRecordData.raw_5_0_multi
|
|
|
|
elsif activerecord_4_2?
|
2016-05-12 16:29:49 -04:00
|
|
|
if RUBY_VERSION > '1.9.3'
|
|
|
|
ActiveRecordData.raw_4_2_multi
|
|
|
|
else
|
|
|
|
ActiveRecordData.raw_4_2_multi_legacy
|
|
|
|
end
|
|
|
|
elsif activerecord_4_1?
|
|
|
|
ActiveRecordData.raw_4_1_multi
|
|
|
|
elsif activerecord_4_0?
|
|
|
|
ActiveRecordData.raw_4_0_multi
|
|
|
|
elsif activerecord_3_2?
|
|
|
|
if RUBY_VERSION > '1.9.3'
|
|
|
|
ActiveRecordData.raw_3_2_multi
|
|
|
|
else
|
|
|
|
ActiveRecordData.raw_3_2_multi_legacy
|
|
|
|
end
|
2015-01-20 11:23:20 -05:00
|
|
|
end
|
2016-05-12 16:29:49 -04:00
|
|
|
raw_object_string.sub!('?', '1992-10-10 12:30:00')
|
|
|
|
raw_object_string.sub!('?', '2003-05-26 14:15:00')
|
|
|
|
expect(out).to be_similar_to(raw_object_string)
|
2014-12-31 15:59:32 -05:00
|
|
|
end
|
|
|
|
end
|
2011-05-13 19:37:24 -04:00
|
|
|
|
2014-12-31 15:59:32 -05:00
|
|
|
#------------------------------------------------------------------------------
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'ActiveRecord class' do
|
2014-12-31 15:59:32 -05:00
|
|
|
before do
|
2016-11-08 09:53:17 -05:00
|
|
|
@ap = AwesomePrint::Inspector.new(plain: true)
|
2014-12-31 15:59:32 -05:00
|
|
|
end
|
2011-11-08 14:05:16 -05:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'should print the class' do
|
2016-05-09 10:04:42 -04:00
|
|
|
expect(@ap.awesome(User)).to eq <<-EOS.strip
|
2011-05-13 19:37:24 -04:00
|
|
|
class User < ActiveRecord::Base {
|
|
|
|
:id => :integer,
|
|
|
|
:name => :string,
|
|
|
|
:rank => :integer,
|
|
|
|
:admin => :boolean,
|
|
|
|
:created_at => :datetime
|
|
|
|
}
|
2014-12-31 15:59:32 -05:00
|
|
|
EOS
|
|
|
|
end
|
2011-05-13 19:37:24 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'should print the class for non-direct subclasses of ActiveRecord::Base' do
|
2016-05-09 10:04:42 -04:00
|
|
|
out = @ap.awesome(SubUser)
|
2014-12-31 15:59:32 -05:00
|
|
|
expect(out).to eq <<-EOS.strip
|
2011-05-13 19:37:24 -04:00
|
|
|
class SubUser < User {
|
|
|
|
:id => :integer,
|
|
|
|
:name => :string,
|
|
|
|
:rank => :integer,
|
|
|
|
:admin => :boolean,
|
|
|
|
:created_at => :datetime
|
|
|
|
}
|
2014-12-31 15:59:32 -05:00
|
|
|
EOS
|
|
|
|
end
|
2011-12-20 01:30:06 -05:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'should print ActiveRecord::Base objects (ex. ancestors)' do
|
2016-05-09 10:04:42 -04:00
|
|
|
expect { @ap.awesome(User.ancestors) }.not_to raise_error
|
2014-12-31 15:59:32 -05:00
|
|
|
end
|
|
|
|
end
|
2011-12-20 01:30:06 -05:00
|
|
|
|
2014-12-31 15:59:32 -05:00
|
|
|
#------------------------------------------------------------------------------
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'ActiveRecord methods formatting' do
|
2014-12-31 15:59:32 -05:00
|
|
|
before do
|
2016-11-08 09:53:17 -05:00
|
|
|
@ap = AwesomePrint::Inspector.new(plain: true)
|
2014-12-31 15:59:32 -05:00
|
|
|
end
|
2012-09-05 21:40:30 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'should format class methods properly' do
|
2014-12-31 15:59:32 -05:00
|
|
|
# spec 1
|
2016-05-09 10:04:42 -04:00
|
|
|
out = @ap.awesome(User.methods.grep(/first/))
|
2011-12-20 01:30:06 -05:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
if ActiveRecord::VERSION::STRING >= '3.2'
|
|
|
|
if RUBY_VERSION >= '1.9'
|
2014-12-31 15:59:32 -05:00
|
|
|
expect(out).to match(/\sfirst\(\*args,\s&block\)\s+Class \(ActiveRecord::Querying\)/)
|
|
|
|
else
|
|
|
|
expect(out).to match(/\sfirst\(\*arg1\)\s+Class \(ActiveRecord::Querying\)/)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
expect(out).to match(/\sfirst\(\*arg.*?\)\s+User \(ActiveRecord::Base\)/)
|
|
|
|
end
|
2011-12-20 01:30:06 -05:00
|
|
|
|
2014-12-31 15:59:32 -05:00
|
|
|
# spec 2
|
2016-05-09 10:04:42 -04:00
|
|
|
out = @ap.awesome(User.methods.grep(/primary_key/))
|
2014-12-31 15:59:32 -05:00
|
|
|
expect(out).to match(/\sprimary_key\(.*?\)\s+Class \(ActiveRecord::AttributeMethods::PrimaryKey::ClassMethods\)/)
|
2014-08-07 21:56:23 -04:00
|
|
|
|
2014-12-31 15:59:32 -05:00
|
|
|
# spec 3
|
2016-05-09 10:04:42 -04:00
|
|
|
out = @ap.awesome(User.methods.grep(/validate/))
|
2014-08-07 21:56:23 -04:00
|
|
|
|
2014-12-31 15:59:32 -05:00
|
|
|
if ActiveRecord::VERSION::MAJOR < 3
|
|
|
|
expect(out).to match(/\svalidate\(\*arg.*?\)\s+User \(ActiveRecord::Base\)/)
|
|
|
|
else
|
|
|
|
expect(out).to match(/\svalidate\(\*arg.*?\)\s+Class \(ActiveModel::Validations::ClassMethods\)/)
|
2011-12-20 01:30:06 -05:00
|
|
|
end
|
2014-12-31 15:59:32 -05:00
|
|
|
|
2011-05-13 19:37:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|