1
0
Fork 0
mirror of https://github.com/awesome-print/awesome_print synced 2023-03-27 23:22:34 -04:00
awesome_print/spec/ext/mongo_mapper_spec.rb

190 lines
5.1 KiB
Ruby
Raw Normal View History

2011-05-13 19:37:24 -04:00
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2011-03-29 03:48:52 -04:00
begin
require "mongo_mapper"
2011-05-13 19:37:24 -04:00
require "awesome_print/ext/mongo_mapper"
2011-03-29 03:48:52 -04:00
2011-05-06 17:29:53 -04:00
describe "AwesomePrint/MongoMapper" do
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
after :all do
Object.instance_eval{ remove_const :MongoUser }
Object.instance_eval{ remove_const :Chamelion }
end
before do
stub_dotfile!
@ap = AwesomePrint::Inspector.new(:plain => true, :sort_keys => true)
2011-05-06 17:29:53 -04:00
end
describe "with the raw option set to true" do
# before { @ap.options[:raw] = true }
before { @ap = AwesomePrint::Inspector.new(:plain => true, :sort_keys => true, :raw => true) }
it "should print class instance" do
user = MongoUser.new(:first_name => "Al", :last_name => "Capone")
out = @ap.send(:awesome, user)
str = <<-EOS.strip
#<MongoUser:0x01234567
@_new = true,
attr_accessor :first_name = "Al",
attr_accessor :last_name = "Capone",
attr_reader :changed_attributes = {
"first_name" => nil,
"last_name" => nil
},
attr_reader :first_name_before_type_cast = "Al",
attr_reader :last_name_before_type_cast = "Capone"
>
2011-03-29 03:48:52 -04:00
EOS
out.gsub!(/0x([a-f\d]+)/, "0x01234567")
out.should == str
end
2011-03-29 03:48:52 -04:00
it "should print the class" do
@ap.send(:awesome, MongoUser).should == <<-EOS.strip
2011-05-06 17:29:53 -04:00
class MongoUser < Object {
2011-03-29 03:48:52 -04:00
"_id" => :object_id,
"first_name" => :string,
"last_name" => :string
2011-03-29 03:48:52 -04:00
}
EOS
2011-05-06 17:29:53 -04:00
end
it "should print the class when type is undefined" do
class Chamelion
include MongoMapper::Document
key :last_attribute
end
@ap.send(:awesome, Chamelion).should == <<-EOS.strip
class Chamelion < Object {
"_id" => :object_id,
"last_attribute" => :undefined
}
2011-03-29 03:48:52 -04:00
EOS
end
end
describe "with associations" do
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
describe "with show associations turned off (default)" do
it "should render the class as normal" do
@ap.send(:awesome, Parent).should == <<-EOS.strip
class Parent < Object {
"_id" => :object_id,
"name" => :undefined
}
EOS
end
it "should render an instance as normal" do
parent = Parent.new(:name => 'test')
out = @ap.send(:awesome, parent)
str = <<-EOS.strip
#<Parent:0x01234567> {
"_id" => BSON::ObjectId('4d9183739a546f6806000001'),
"name" => "test"
}
EOS
out.gsub!(/'([\w]+){23}'/, "'4d9183739a546f6806000001'")
out.gsub!(/0x([a-f\d]+)/, "0x01234567")
out.should == str
end
end
describe "with show associations turned on and inline embedded turned off" do
before :each do
@ap = AwesomePrint::Inspector.new(:plain => true, :mongo_mapper => { :show_associations => true })
end
it "should render the class with associations shown" do
@ap.send(:awesome, Parent).should == <<-EOS.strip
class Parent < Object {
"_id" => :object_id,
"name" => :undefined,
"child" => embeds one Child,
"sibling" => one Sibling
}
EOS
end
it "should render an instance with associations shown" do
parent = Parent.new(:name => 'test')
out = @ap.send(:awesome, parent)
str = <<-EOS.strip
#<Parent:0x01234567> {
"_id" => BSON::ObjectId('4d9183739a546f6806000001'),
"name" => "test",
"child" => embeds one Child,
"sibling" => one Sibling
}
EOS
out.gsub!(/'([\w]+){23}'/, "'4d9183739a546f6806000001'")
out.gsub!(/0x([a-f\d]+)/, "0x01234567")
out.should == str
end
end
describe "with show associations turned on and inline embedded turned on" do
before :each do
@ap = AwesomePrint::Inspector.new(:plain => true,
:mongo_mapper => {
:show_associations => true,
:inline_embedded => true
}
)
end
it "should render an instance with associations shown and embeds there" do
parent = Parent.new(:name => 'test', :child => Child.new(:data => 5))
out = @ap.send(:awesome, parent)
str = <<-EOS.strip
#<Parent:0x01234567> {
"_id" => BSON::ObjectId('4d9183739a546f6806000001'),
"name" => "test",
"child" => embedded #<Child:0x01234567> {
"_id" => BSON::ObjectId('4d9183739a546f6806000001'),
"data" => 5
},
"sibling" => one Sibling
}
EOS
out.gsub!(/'([\w]+){23}'/, "'4d9183739a546f6806000001'")
out.gsub!(/0x([a-f\d]+)/, "0x01234567")
out.should == str
end
end
2011-03-29 03:48:52 -04:00
end
end
rescue LoadError => error
puts "Skipping MongoMapper specs: #{error}"
end