Commit 193249082fb315bf3a928ceaea2ce7904ef49ecf

Authored by Georg GH. Hopp
Committed by Georg Hopp
1 parent 90589209

added test for module Model

@@ -16,7 +16,7 @@ module DsAdmin::Model @@ -16,7 +16,7 @@ module DsAdmin::Model
16 @@storage 16 @@storage
17 end 17 end
18 18
19 - attr_accessor :id 19 + attr_reader :id
20 20
21 def initialize(args = {}) 21 def initialize(args = {})
22 @id = args[:id] if args[:id] 22 @id = args[:id] if args[:id]
@@ -49,6 +49,10 @@ module DsAdmin::Model @@ -49,6 +49,10 @@ module DsAdmin::Model
49 # for organizational, indexing or other reasons thus it 49 # for organizational, indexing or other reasons thus it
50 # always has to be stored back into @id 50 # always has to be stored back into @id
51 # 51 #
  52 + # Note: We do not need to set the actual model here, as
  53 + # write gets a reference of self and thus can extract the used
  54 + # model with self.class
  55 + #
52 def save 56 def save
53 @id = @@storage.write(self) 57 @id = @@storage.write(self)
54 end 58 end
@@ -68,7 +72,7 @@ module DsAdmin::Model @@ -68,7 +72,7 @@ module DsAdmin::Model
68 @@storage.config.model = self 72 @@storage.config.model = self
69 73
70 data = @@storage.find {|data| data[:id] == id} 74 data = @@storage.find {|data| data[:id] == id}
71 - throw "unknown id (#{id})" unless data 75 + raise "unknown id (#{id})" unless data
72 data 76 data
73 end 77 end
74 end 78 end
  1 +$:.unshift File.join(File.dirname(__FILE__), "..", "app")
  2 +
  3 +require 'test/unit'
  4 +require 'mocha'
  5 +
  6 +require 'ds_admin'
  7 +
  8 +class ModelStub
  9 + include DsAdmin::Model
  10 +
  11 + attr_accessor :data
  12 +
  13 + def initialize(args = {})
  14 + return if args.empty?
  15 + super(args)
  16 +
  17 + @data = args[:data]
  18 + end
  19 +end
  20 +
  21 +class ModelTest < Test::Unit::TestCase
  22 + # def setup
  23 + # end
  24 +
  25 + # def teardown
  26 + # end
  27 +
  28 + def test_initialize
  29 + model = ModelStub.new
  30 + assert_instance_of(ModelStub, model)
  31 + assert_kind_of(DsAdmin::Model, model)
  32 +
  33 + model = ModelStub.new({:data => 'data'})
  34 + assert_instance_of(ModelStub, model)
  35 + assert_kind_of(DsAdmin::Model, model)
  36 + assert_equal('data', model.data)
  37 + assert_nil(model.id)
  38 + end
  39 +
  40 + def test_all_and_each
  41 + model = ModelStub.new
  42 +
  43 + storage_config_mock = mock(:model= => model)
  44 +
  45 + storage_mock = mock(:config => storage_config_mock)
  46 + storage_mock.extend(Enumerable).
  47 + expects(:each).multiple_yields([{:data => 'data1'}], [{:data => 'data2'}])
  48 +
  49 + DsAdmin::Model.storage = storage_mock
  50 + all = model.all
  51 +
  52 + assert_instance_of(Array, all)
  53 + assert_instance_of(ModelStub, all[0])
  54 + assert_instance_of(ModelStub, all[1])
  55 + assert_equal('data1', all[0].data)
  56 + assert_equal('data2', all[1].data)
  57 +
  58 + found = Array.new
  59 + all.each do |a|
  60 + assert_instance_of(ModelStub, a)
  61 + found << a.data
  62 + end
  63 +
  64 + assert_equal(['data1', 'data2'], found)
  65 + end
  66 +
  67 + def test_config_key
  68 + assert_equal(:ModelStub, ModelStub.new.config_key)
  69 + end
  70 +
  71 + def test_to_h
  72 + model_hash = ModelStub.new({:data => 'data'}).to_h
  73 + assert_instance_of(Hash, model_hash)
  74 + assert_equal('data', model_hash[:data])
  75 + end
  76 +
  77 + def test_load
  78 + model = ModelStub.new
  79 +
  80 + storage_config_mock = mock
  81 + storage_config_mock.expects(:model=).twice.returns(model)
  82 +
  83 + storage_mock = mock().extend(Enumerable)
  84 + storage_mock.expects(:config).twice.returns(storage_config_mock)
  85 + storage_mock.expects(:each).twice.multiple_yields(
  86 + [{:id => 1, :data => 'data1'}],
  87 + [{:id => 2, :data => 'data2'}])
  88 +
  89 + DsAdmin::Model.storage = storage_mock
  90 + loaded = model.load(2)
  91 +
  92 + assert_instance_of(ModelStub, model)
  93 + assert_instance_of(ModelStub, loaded)
  94 + assert_not_equal(model, loaded)
  95 + assert_nil(model.data)
  96 + assert_equal('data2', loaded.data)
  97 +
  98 + assert_raise RuntimeError do
  99 + loaded = model.load(3)
  100 + end
  101 + end
  102 +
  103 + def test_load!
  104 + model = ModelStub.new
  105 +
  106 + storage_config_mock = mock
  107 + storage_config_mock.expects(:model=).twice.returns(model)
  108 +
  109 + storage_mock = mock().extend(Enumerable)
  110 + storage_mock.expects(:config).twice.returns(storage_config_mock)
  111 + storage_mock.expects(:each).twice.multiple_yields(
  112 + [{:id => 1, :data => 'data1'}],
  113 + [{:id => 2, :data => 'data2'}])
  114 +
  115 + DsAdmin::Model.storage = storage_mock
  116 + model.load!(1)
  117 +
  118 + assert_instance_of(ModelStub, model)
  119 + assert_equal('data1', model.data)
  120 +
  121 + assert_raise RuntimeError do
  122 + model.load!(3)
  123 + end
  124 + end
  125 +
  126 + def test_save_stores_new_id
  127 + model = ModelStub.new({:data => 'data'})
  128 +
  129 + storage_mock = mock()
  130 + storage_mock.expects(:write).returns(3)
  131 +
  132 + DsAdmin::Model.storage = storage_mock
  133 + model.save
  134 +
  135 + assert_equal(3, model.id)
  136 + end
  137 +end
Please register or login to post a comment