2014-12-31 15:59:32 -05:00
|
|
|
require 'spec_helper'
|
2011-03-29 03:48:52 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
RSpec.describe 'AwesomePrint/MongoMapper', skip: ->{ !ExtVerifier.has_mongo_mapper? }.call do
|
2014-12-31 15:59:32 -05:00
|
|
|
if ExtVerifier.has_mongo_mapper?
|
2011-05-06 17:29:53 -04:00
|
|
|
before :all do
|
|
|
|
class MongoUser
|
|
|
|
include MongoMapper::Document
|
2011-03-29 03:48:52 -04:00
|
|
|
|
2011-05-06 17:29:53 -04:00
|
|
|
key :first_name, String
|
|
|
|
key :last_name, String
|
|
|
|
end
|
2011-03-29 03:48:52 -04:00
|
|
|
end
|
|
|
|
|
2012-09-06 19:34:11 -04:00
|
|
|
after :all do
|
|
|
|
Object.instance_eval{ remove_const :MongoUser }
|
|
|
|
Object.instance_eval{ remove_const :Chamelion }
|
|
|
|
end
|
2014-12-31 15:59:32 -05:00
|
|
|
end
|
2012-09-06 19:34:11 -04:00
|
|
|
|
2014-12-31 15:59:32 -05:00
|
|
|
before do
|
2016-11-08 09:53:17 -05:00
|
|
|
@ap = AwesomePrint::Inspector.new(plain: true, sort_keys: true)
|
2014-12-31 15:59:32 -05:00
|
|
|
end
|
2011-05-04 20:55:31 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'with the raw option set to true' do
|
2014-12-31 15:59:32 -05:00
|
|
|
# before { @ap.options[:raw] = true }
|
2016-11-08 09:53:17 -05:00
|
|
|
before { @ap = AwesomePrint::Inspector.new(plain: true, sort_keys: true, raw: true) }
|
2012-09-06 21:11:13 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'should print class instance' do
|
2016-11-08 09:53:17 -05:00
|
|
|
user = MongoUser.new(first_name: 'Al', last_name: 'Capone')
|
2014-08-07 21:56:23 -04:00
|
|
|
|
2014-12-31 15:59:32 -05:00
|
|
|
out = @ap.send(:awesome, user)
|
|
|
|
out.gsub!(/#\<Proc:.+?\>/, 'AWESOME_PRINT_PROC_STUB')
|
|
|
|
out.gsub!(/BSON::ObjectId\('[\da-f]+?'\)/, "BSON::ObjectId('123456789')")
|
2014-08-07 21:56:23 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
if MongoMapper::Version >= '0.13'
|
2014-12-31 15:59:32 -05:00
|
|
|
str = <<-EOS.strip
|
2016-05-09 10:04:42 -04:00
|
|
|
#<MongoUser:placeholder_id
|
2014-08-07 21:56:23 -04:00
|
|
|
@__mm_default_keys = [
|
2016-05-09 10:04:42 -04:00
|
|
|
[0] #<MongoMapper::Plugins::Keys::Key:placeholder_id
|
2014-08-07 21:56:23 -04:00
|
|
|
@dynamic = false,
|
|
|
|
@embeddable = false,
|
|
|
|
@has_default = true,
|
|
|
|
@is_id = true,
|
|
|
|
@typecast = nil,
|
|
|
|
attr_accessor :accessors = [],
|
|
|
|
attr_accessor :default = AWESOME_PRINT_PROC_STUB,
|
|
|
|
attr_accessor :ivar = :@_id,
|
|
|
|
attr_accessor :name = "_id",
|
|
|
|
attr_accessor :options = {
|
|
|
|
:default => AWESOME_PRINT_PROC_STUB
|
|
|
|
},
|
|
|
|
attr_accessor :type = ObjectId < Object
|
|
|
|
>
|
|
|
|
],
|
|
|
|
@__mm_keys = {
|
2016-05-09 10:04:42 -04:00
|
|
|
"_id" => #<MongoMapper::Plugins::Keys::Key:placeholder_id
|
2014-08-07 21:56:23 -04:00
|
|
|
@dynamic = false,
|
|
|
|
@embeddable = false,
|
|
|
|
@has_default = true,
|
|
|
|
@is_id = true,
|
|
|
|
@typecast = nil,
|
|
|
|
attr_accessor :accessors = [],
|
|
|
|
attr_accessor :default = AWESOME_PRINT_PROC_STUB,
|
|
|
|
attr_accessor :ivar = :@_id,
|
|
|
|
attr_accessor :name = "_id",
|
|
|
|
attr_accessor :options = {
|
|
|
|
:default => AWESOME_PRINT_PROC_STUB
|
|
|
|
},
|
|
|
|
attr_accessor :type = ObjectId < Object
|
|
|
|
>,
|
2016-05-09 10:04:42 -04:00
|
|
|
"first_name" => #<MongoMapper::Plugins::Keys::Key:placeholder_id
|
2014-08-07 21:56:23 -04:00
|
|
|
@dynamic = false,
|
|
|
|
@embeddable = false,
|
|
|
|
@has_default = false,
|
|
|
|
@is_id = false,
|
|
|
|
@typecast = nil,
|
|
|
|
attr_accessor :accessors = [],
|
|
|
|
attr_accessor :ivar = :@first_name,
|
|
|
|
attr_accessor :name = "first_name",
|
|
|
|
attr_accessor :options = {},
|
|
|
|
attr_accessor :type = String < Object
|
|
|
|
>,
|
2016-05-09 10:04:42 -04:00
|
|
|
"last_name" => #<MongoMapper::Plugins::Keys::Key:placeholder_id
|
2014-08-07 21:56:23 -04:00
|
|
|
@dynamic = false,
|
|
|
|
@embeddable = false,
|
|
|
|
@has_default = false,
|
|
|
|
@is_id = false,
|
|
|
|
@typecast = nil,
|
|
|
|
attr_accessor :accessors = [],
|
|
|
|
attr_accessor :ivar = :@last_name,
|
|
|
|
attr_accessor :name = "last_name",
|
|
|
|
attr_accessor :options = {},
|
|
|
|
attr_accessor :type = String < Object
|
|
|
|
>
|
|
|
|
},
|
|
|
|
@__mm_pre_cast = {
|
|
|
|
"first_name" => "Al",
|
|
|
|
"last_name" => "Capone"
|
|
|
|
},
|
|
|
|
@_dynamic_attributes = {},
|
|
|
|
@_new = true,
|
|
|
|
attr_accessor :_id = BSON::ObjectId('123456789'),
|
|
|
|
attr_accessor :attributes = nil,
|
|
|
|
attr_accessor :first_name = "Al",
|
|
|
|
attr_accessor :last_name = "Capone",
|
|
|
|
attr_reader :changed_attributes = {
|
|
|
|
"first_name" => nil,
|
|
|
|
"last_name" => nil
|
|
|
|
}
|
|
|
|
>
|
2014-12-31 15:59:32 -05:00
|
|
|
EOS
|
|
|
|
else
|
|
|
|
str = <<-EOS.strip
|
2016-05-09 10:04:42 -04:00
|
|
|
#<MongoUser:placeholder_id
|
2011-11-09 00:27:02 -05:00
|
|
|
@_new = true,
|
|
|
|
attr_accessor :first_name = "Al",
|
|
|
|
attr_accessor :last_name = "Capone",
|
|
|
|
attr_reader :changed_attributes = {
|
2011-05-14 00:07:34 -04:00
|
|
|
"first_name" => nil,
|
|
|
|
"last_name" => nil
|
|
|
|
},
|
|
|
|
attr_reader :first_name_before_type_cast = "Al",
|
2011-11-09 00:27:02 -05:00
|
|
|
attr_reader :last_name_before_type_cast = "Capone"
|
2011-05-14 00:07:34 -04:00
|
|
|
>
|
2014-12-31 15:59:32 -05:00
|
|
|
EOS
|
2012-09-06 21:11:13 -04: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
|
2011-03-29 03:48:52 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'should print the class' do
|
2014-12-31 15:59:32 -05:00
|
|
|
expect(@ap.send(:awesome, MongoUser)).to eq <<-EOS.strip
|
2011-05-06 17:29:53 -04:00
|
|
|
class MongoUser < Object {
|
2011-03-29 03:48:52 -04:00
|
|
|
"_id" => :object_id,
|
2011-05-06 15:29:06 -04:00
|
|
|
"first_name" => :string,
|
|
|
|
"last_name" => :string
|
2011-03-29 03:48:52 -04:00
|
|
|
}
|
2014-12-31 15:59:32 -05:00
|
|
|
EOS
|
|
|
|
end
|
2011-05-04 20:55:31 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'should print the class when type is undefined' do
|
2014-12-31 15:59:32 -05:00
|
|
|
class Chamelion
|
|
|
|
include MongoMapper::Document
|
|
|
|
key :last_attribute
|
|
|
|
end
|
2012-09-06 21:11:13 -04:00
|
|
|
|
2014-12-31 15:59:32 -05:00
|
|
|
expect(@ap.send(:awesome, Chamelion)).to eq <<-EOS.strip
|
2011-03-29 04:04:18 -04:00
|
|
|
class Chamelion < Object {
|
|
|
|
"_id" => :object_id,
|
|
|
|
"last_attribute" => :undefined
|
|
|
|
}
|
2014-12-31 15:59:32 -05:00
|
|
|
EOS
|
2012-09-06 21:11:13 -04:00
|
|
|
end
|
2014-12-31 15:59:32 -05:00
|
|
|
end
|
2012-09-06 21:11:13 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'with associations' do
|
2014-12-31 15:59:32 -05:00
|
|
|
|
|
|
|
if ExtVerifier.has_mongo_mapper?
|
2012-09-06 21:11:13 -04:00
|
|
|
before :all do
|
|
|
|
class Child
|
|
|
|
include MongoMapper::EmbeddedDocument
|
|
|
|
key :data
|
|
|
|
end
|
|
|
|
|
|
|
|
class Sibling
|
|
|
|
include MongoMapper::Document
|
|
|
|
key :title
|
|
|
|
end
|
|
|
|
|
|
|
|
class Parent
|
|
|
|
include MongoMapper::Document
|
|
|
|
key :name
|
|
|
|
|
|
|
|
one :child
|
|
|
|
one :sibling
|
|
|
|
end
|
|
|
|
end
|
2014-12-31 15:59:32 -05:00
|
|
|
end
|
2012-09-06 21:11:13 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'with show associations turned off (default)' do
|
|
|
|
it 'should render the class as normal' do
|
2014-12-31 15:59:32 -05:00
|
|
|
expect(@ap.send(:awesome, Parent)).to eq <<-EOS.strip
|
2012-09-06 21:11:13 -04:00
|
|
|
class Parent < Object {
|
|
|
|
"_id" => :object_id,
|
|
|
|
"name" => :undefined
|
|
|
|
}
|
2014-12-31 15:59:32 -05:00
|
|
|
EOS
|
|
|
|
end
|
2012-09-06 21:11:13 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'should render an instance as normal' do
|
2016-11-08 09:53:17 -05:00
|
|
|
parent = Parent.new(name: 'test')
|
2014-12-31 15:59:32 -05:00
|
|
|
out = @ap.send(:awesome, parent)
|
|
|
|
str = <<-EOS.strip
|
2016-05-09 10:04:42 -04:00
|
|
|
#<Parent:placeholder_id> {
|
2016-05-09 16:12:59 -04:00
|
|
|
"_id" => placeholder_bson_id,
|
2012-09-06 21:11:13 -04:00
|
|
|
"name" => "test"
|
|
|
|
}
|
2014-12-31 15:59:32 -05:00
|
|
|
EOS
|
2016-05-09 10:04:42 -04:00
|
|
|
expect(out).to be_similar_to(str)
|
2012-09-06 21:11:13 -04:00
|
|
|
end
|
2014-12-31 15:59:32 -05:00
|
|
|
end
|
2012-09-06 21:11:13 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'with show associations turned on and inline embedded turned off' do
|
2014-12-31 15:59:32 -05:00
|
|
|
before :each do
|
2016-11-08 09:53:17 -05:00
|
|
|
@ap = AwesomePrint::Inspector.new(plain: true, mongo_mapper: { show_associations: true })
|
2014-12-31 15:59:32 -05:00
|
|
|
end
|
2012-09-06 21:11:13 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'should render the class with associations shown' do
|
2014-12-31 15:59:32 -05:00
|
|
|
expect(@ap.send(:awesome, Parent)).to eq <<-EOS.strip
|
2012-09-06 21:11:13 -04:00
|
|
|
class Parent < Object {
|
|
|
|
"_id" => :object_id,
|
|
|
|
"name" => :undefined,
|
|
|
|
"child" => embeds one Child,
|
|
|
|
"sibling" => one Sibling
|
|
|
|
}
|
2014-12-31 15:59:32 -05:00
|
|
|
EOS
|
|
|
|
end
|
2012-09-06 21:11:13 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'should render an instance with associations shown' do
|
2016-11-08 09:53:17 -05:00
|
|
|
parent = Parent.new(name: 'test')
|
2014-12-31 15:59:32 -05:00
|
|
|
out = @ap.send(:awesome, parent)
|
|
|
|
str = <<-EOS.strip
|
2016-05-09 10:04:42 -04:00
|
|
|
#<Parent:placeholder_id> {
|
2016-05-09 16:12:59 -04:00
|
|
|
"_id" => placeholder_bson_id,
|
2012-09-06 21:11:13 -04:00
|
|
|
"name" => "test",
|
|
|
|
"child" => embeds one Child,
|
|
|
|
"sibling" => one Sibling
|
|
|
|
}
|
2014-12-31 15:59:32 -05:00
|
|
|
EOS
|
2016-05-09 10:04:42 -04:00
|
|
|
expect(out).to be_similar_to(str)
|
2012-09-06 21:11:13 -04:00
|
|
|
end
|
2014-12-31 15:59:32 -05:00
|
|
|
end
|
2012-09-06 21:11:13 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
describe 'with show associations turned on and inline embedded turned on' do
|
2014-12-31 15:59:32 -05:00
|
|
|
before :each do
|
2016-11-08 09:53:17 -05:00
|
|
|
@ap = AwesomePrint::Inspector.new(plain: true,
|
|
|
|
mongo_mapper: {
|
|
|
|
show_associations: true,
|
|
|
|
inline_embedded: true
|
2014-12-31 15:59:32 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
2012-09-06 21:11:13 -04:00
|
|
|
|
2016-11-08 00:07:03 -05:00
|
|
|
it 'should render an instance with associations shown and embeds there' do
|
2016-11-08 09:53:17 -05:00
|
|
|
parent = Parent.new(name: 'test', child: Child.new(data: 5))
|
2014-12-31 15:59:32 -05:00
|
|
|
out = @ap.send(:awesome, parent)
|
|
|
|
str = <<-EOS.strip
|
2016-05-09 10:04:42 -04:00
|
|
|
#<Parent:placeholder_id> {
|
2016-05-09 16:12:59 -04:00
|
|
|
"_id" => placeholder_bson_id,
|
2012-09-06 21:11:13 -04:00
|
|
|
"name" => "test",
|
2016-05-09 10:04:42 -04:00
|
|
|
"child" => embedded #<Child:placeholder_id> {
|
2016-05-09 16:12:59 -04:00
|
|
|
"_id" => placeholder_bson_id,
|
2012-09-06 21:11:13 -04:00
|
|
|
"data" => 5
|
|
|
|
},
|
|
|
|
"sibling" => one Sibling
|
|
|
|
}
|
2014-12-31 15:59:32 -05:00
|
|
|
EOS
|
2016-05-09 10:04:42 -04:00
|
|
|
expect(out).to be_similar_to(str)
|
2012-09-06 21:11:13 -04:00
|
|
|
end
|
2011-03-29 03:48:52 -04:00
|
|
|
end
|
|
|
|
end
|
2011-05-04 20:55:31 -04:00
|
|
|
end
|