forked from cheald/mongomapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_keys.rb
More file actions
89 lines (76 loc) · 2.04 KB
/
Copy pathtest_keys.rb
File metadata and controls
89 lines (76 loc) · 2.04 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require 'test_helper'
require 'models'
class KeyTest < Test::Unit::TestCase
include MongoMapper::Plugins::Keys
context ".new with no id and _id of type integer" do
should "not error" do
lambda {
klass = Doc() do
key :_id, Integer
end
# No sensible default id for integer, people better pass them in if they user this
klass.new.id.should be_nil
}.should_not raise_error
end
end
context ".new with no id and _id of type string" do
should "not error" do
lambda {
klass = Doc() do
key :_id, String
end
klass.new.id.should_not be_nil
}.should_not raise_error
end
end
context ".new with no id and _id of type object id" do
should "not error" do
lambda {
klass = Doc() do
key :_id, ObjectId
end
klass.new.should_not be_nil
}.should_not raise_error
end
end
context ".key?(:symbol)" do
should "be true if document has key" do
Address.key?(:city).should be_true
end
should "be false if document does not have key" do
Address.key?(:foo).should be_false
end
end
context ".key?('string')" do
should "be true if document has key" do
Address.key?('city').should be_true
end
should "be false if document does not have key" do
Address.key?('foo').should be_false
end
end
context ".new (from database)" do
setup do
@klass = Doc do
key :user, Hash
def user=(user)
super(:id => user.id, :name => user.name)
end
end
user_class = Struct.new(:id, :name)
@klass.create(:user => user_class.new(1, 'John Nunemaker'))
end
should "use []= for keys instead of public writer" do
assert_nothing_raised do
doc = @klass.first
doc.user['id'].should == 1
doc.user['name'].should == 'John Nunemaker'
end
end
end
context ".load" do
should "return nil if argument is nil" do
Doc().load(nil).should be_nil
end
end
end # KeyTest