forked from datamapper/dm-core
/
object_spec.rb
107 lines (77 loc) · 2.13 KB
/
object_spec.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
require 'spec_helper'
describe DataMapper::Property, 'Object type' do
before :all do
module ::Blog
class Article
include DataMapper::Resource
property :id, Serial
property :title, String
property :meta, Object, :required => true
end
end
DataMapper.finalize
@model = Blog::Article
@property = @model.properties[:meta]
end
subject { @property }
describe '.options' do
subject { described_class.options }
it { should be_kind_of(Hash) }
it { should be_empty }
end
it { should respond_to(:typecast) }
describe '#typecast' do
before do
@value = { 'lang' => 'en_CA' }
end
subject { @property.typecast(@value) }
it { should equal(@value) }
end
it { should respond_to(:dump) }
describe '#dump' do
describe 'with a value' do
before do
@value = { 'lang' => 'en_CA' }
end
subject { @property.dump(@value) }
it { @property.load(subject).should == @value }
end
describe 'with nil' do
subject { @property.dump(nil) }
it { should be_nil }
end
end
it { should respond_to(:valid?) }
describe '#valid?' do
describe 'with a valid load_as' do
subject { @property.valid?('lang' => 'en_CA') }
it { should be(true) }
end
describe 'with nil and property is not required' do
before do
@property = @model.property(:meta, Object, :required => false)
end
subject { @property.valid?(nil) }
it { should be(true) }
end
describe 'with nil and property is required' do
subject { @property.valid?(nil) }
it { should be(false) }
end
describe 'with nil and property is required, but validity is negated' do
subject { @property.valid?(nil, true) }
it { should be(true) }
end
end
describe 'persistable' do
supported_by :all do
before :all do
@resource = @model.create(:title => 'Test', :meta => { 'lang' => 'en_CA' })
end
subject { @resource.reload.meta }
it 'should load the correct value' do
should == { 'lang' => 'en_CA' }
end
end
end
end