Browse Source

added test for module Model

master
Georg Hopp 15 years ago
committed by Georg Hopp
parent
commit
193249082f
  1. 8
      app/model.rb
  2. 137
      tests/test_model.rb

8
app/model.rb

@ -16,7 +16,7 @@ module DsAdmin::Model
@@storage
end
attr_accessor :id
attr_reader :id
def initialize(args = {})
@id = args[:id] if args[:id]
@ -49,6 +49,10 @@ module DsAdmin::Model
# for organizational, indexing or other reasons thus it
# always has to be stored back into @id
#
# Note: We do not need to set the actual model here, as
# write gets a reference of self and thus can extract the used
# model with self.class
#
def save
@id = @@storage.write(self)
end
@ -68,7 +72,7 @@ module DsAdmin::Model
@@storage.config.model = self
data = @@storage.find {|data| data[:id] == id}
throw "unknown id (#{id})" unless data
raise "unknown id (#{id})" unless data
data
end
end

137
tests/test_model.rb

@ -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
Loading…
Cancel
Save