I tried to save and load a model and got the wrong value for a
property if the property was set from a method
The following test shows, what works and what doesn't

I want to know, if I do something wrong and
if there is a way to avoid the failure e.g. by setting a condition

==
# require File.dirname(__FILE__) + "/test_helper"
require 'test/unit'
require 'rubygems'

require 'dm-core'
require 'dm-migrations'

# An in-memory Sqlite3 connection:
DataMapper.setup(:default, 'sqlite::memory:')
class SimpleChunk
  include DataMapper::Resource

  property :id,           Serial      # auto-increment integer key
  property :content,      String
  property :occured,      Integer,    :default => 0       # number of
occurences

  def occur(ntimes = 1)
    @occured += ntimes
  end
end

# I tried to save and load a model and got the wrong value for a
property if the property was set from a method
# The following test shows, what works and what doesn't
#
# I want to know, if I do something wrong and
# if there is a way to avoid the failure e.g. by setting a condition

class SimpleChunkWithoutDefault
  include DataMapper::Resource

  property :id,           Serial      # auto-increment integer key
  property :content,      String
  property :occured,      Integer     # number of occurences - NO
Default!

  def occur(ntimes = 1)
    @occured += ntimes
  end
end
DataMapper.finalize
DataMapper.auto_migrate!


class TestFailure < Test::Unit::TestCase

  def setup
    DataMapper.finalize.auto_migrate! # make sure db is empty
  end

  # this is what I wanted to do
  # and it fails
  def test_save_chunk_fails_when_property_is_set_from_method
      chunk1 = SimpleChunk.first_or_create(:content => 'xyz')
      chunk1.occur(2)

      # everything seems to work, state is ok
      assert_equal 2, chunk1.occured

      chunk1.raise_on_save_failure = true
      chunk1.save

      # state is still ok
      assert_equal 2, chunk1.occured

      # now the error comes ..
      # first_or_create loads the corret object, but
      # the property 'occured' is <0> (default value) instead of <2>
(as it was saved)
      chunk2 = SimpleChunk.first_or_create(:content => 'xyz')
      # assert_equal 2, chunk2.occured # fails: <2> expected but was
<0>.
      chunk2.occur
      assert_equal 3, chunk2.occured # fails: <3> expected but was
<1>.
  end

  def test_but_save_chunk_without_default_works
      chunk1 = SimpleChunkWithoutDefault.first_or_create(:content =>
'xyz')
      chunk1.occured = 0 # there is no default, so we have to init the
property before we can count occurences
      chunk1.occur(2)

      chunk1.raise_on_save_failure = true
      chunk1.save
      assert_equal 2, chunk1.occured

      # now everything works
      chunk2 = SimpleChunkWithoutDefault.first_or_create(:content =>
'xyz')
      chunk2.occur
      assert_equal 3, chunk2.occured
  end

  def test_and_save_chunk_with_property_set_from_outside_works_too
      chunk1 = SimpleChunk.first_or_create(:content => 'xyz')
      chunk1.occured += 2

      chunk1.raise_on_save_failure = true
      chunk1.save
      assert_equal 2, chunk1.occured

      # now everything works
      chunk2 = SimpleChunk.first_or_create(:content => 'xyz')
      chunk2.occur
      assert_equal 3, chunk2.occured
  end
end

-- 
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en.

Reply via email to