2 changed files with 143 additions and 2 deletions
@ -0,0 +1,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 |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue