Skip to content

Instantly share code, notes, and snippets.

@semanticart
Created October 18, 2009 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save semanticart/212736 to your computer and use it in GitHub Desktop.
Save semanticart/212736 to your computer and use it in GitHub Desktop.
$ ruby test_custom_data.rb
Loaded suite test_custom_data
Started
.F..
Finished in 0.018501 seconds.
1) Failure:
test_creating_a_thing_with_a_foo(MyTest) [test_custom_data.rb:58]:
<"--- !ruby/object:Foo \nfirst: 1st\nsecond: 2nd\n"> expected but was
<#<Foo:0x1023a5ce8 @first="1st", @second="2nd">>.
4 tests, 5 assertions, 1 failures, 0 errors
require 'rubygems'
require 'test/unit'
require 'mongomapper'
require 'yaml'
MongoMapper.database = 'test_custom_data'
class Foo
def initialize first, second
@first, @second = first, second
end
def self.to_mongo(value)
value ? YAML.dump(value) : nil
end
def self.from_mongo(value)
value ? YAML.load(value) : nil
end
def to_s
[@first, @second].join('-')
end
end
class Thing
include MongoMapper::Document
key :title, String
key :name, Foo
end
class MyTest < Test::Unit::TestCase
def test_foo_is_serializable
foo = Foo.new("1st", "2nd")
assert_equal Foo.to_mongo(foo), YAML.dump(foo)
end
def test_foo_is_deserializable
foo = Foo.new("1st", "2nd")
as_yaml = YAML.dump(foo)
assert_equal Foo.from_mongo(as_yaml).to_s, YAML.load(as_yaml).to_s
end
def test_creating_a_thing
t = Time.now.to_s
Thing.create(:title => t)
assert_equal Thing.last.title, t
end
def test_creating_a_thing_with_a_foo
foo = Foo.new("1st", "2nd")
t = Time.now.to_s
Thing.create(:title => t, :name => foo)
assert_equal Thing.last.title, t
assert_equal Thing.last.name, foo
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment