test_model.rb
3.14 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
$:.unshift File.join(File.dirname(__FILE__), "..", "app")
require 'test/unit'
require 'mocha'
require 'ds_admin'
class ModelStub
include DsAdmin::Model
attr_accessor :data
def initialize(args = {})
return if args.empty?
super(args)
@data = args[:data]
end
end
class ModelTest < Test::Unit::TestCase
# def setup
# end
# def teardown
# end
def test_initialize
model = ModelStub.new
assert_instance_of(ModelStub, model)
assert_kind_of(DsAdmin::Model, model)
model = ModelStub.new({:data => 'data'})
assert_instance_of(ModelStub, model)
assert_kind_of(DsAdmin::Model, model)
assert_equal('data', model.data)
assert_nil(model.id)
end
def test_all_and_each
model = ModelStub.new
storage_config_mock = mock(:model= => model)
storage_mock = mock(:config => storage_config_mock)
storage_mock.extend(Enumerable).
expects(:each).multiple_yields([{:data => 'data1'}], [{:data => 'data2'}])
DsAdmin::Model.storage = storage_mock
all = model.all
assert_instance_of(Array, all)
assert_instance_of(ModelStub, all[0])
assert_instance_of(ModelStub, all[1])
assert_equal('data1', all[0].data)
assert_equal('data2', all[1].data)
found = Array.new
all.each do |a|
assert_instance_of(ModelStub, a)
found << a.data
end
assert_equal(['data1', 'data2'], found)
end
def test_config_key
assert_equal(:ModelStub, ModelStub.new.config_key)
end
def test_to_h
model_hash = ModelStub.new({:data => 'data'}).to_h
assert_instance_of(Hash, model_hash)
assert_equal('data', model_hash[:data])
end
def test_load
model = ModelStub.new
storage_config_mock = mock
storage_config_mock.expects(:model=).twice.returns(model)
storage_mock = mock().extend(Enumerable)
storage_mock.expects(:config).twice.returns(storage_config_mock)
storage_mock.expects(:each).twice.multiple_yields(
[{:id => 1, :data => 'data1'}],
[{:id => 2, :data => 'data2'}])
DsAdmin::Model.storage = storage_mock
loaded = model.load(2)
assert_instance_of(ModelStub, model)
assert_instance_of(ModelStub, loaded)
assert_not_equal(model, loaded)
assert_nil(model.data)
assert_equal('data2', loaded.data)
assert_raise RuntimeError do
loaded = model.load(3)
end
end
def test_load!
model = ModelStub.new
storage_config_mock = mock
storage_config_mock.expects(:model=).twice.returns(model)
storage_mock = mock().extend(Enumerable)
storage_mock.expects(:config).twice.returns(storage_config_mock)
storage_mock.expects(:each).twice.multiple_yields(
[{:id => 1, :data => 'data1'}],
[{:id => 2, :data => 'data2'}])
DsAdmin::Model.storage = storage_mock
model.load!(1)
assert_instance_of(ModelStub, model)
assert_equal('data1', model.data)
assert_raise RuntimeError do
model.load!(3)
end
end
def test_save_stores_new_id
model = ModelStub.new({:data => 'data'})
storage_mock = mock()
storage_mock.expects(:write).returns(3)
DsAdmin::Model.storage = storage_mock
model.save
assert_equal(3, model.id)
end
end