semanticart (owner)

Forks

Revisions

gist: 212736 Download_button fork
public
Public Clone URL: git://gist.github.com/212736.git
Embed All Files: show embed
result.txt #
1
2
3
4
5
6
7
8
9
10
11
12
$ 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
test_custom_data.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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