Identity Map + Fields + Test Environment #432

Closed
bkimble opened this Issue Jul 12, 2012 · 3 comments

Comments

Projects
None yet
2 participants
Contributor

bkimble commented Jul 12, 2012

Are there any known issues around using .fields() with identity map? We have an issue that is only present when IM is turned on in a model, and it causes many tests to fail because most of the attributes are being set to nil after. We do a query that only finds the id field, then later on do another query for all of the data, makes 1 small change to a timestamp, then saves. When the query for all of the data comes back, it only contains the id field -- everything else is nil.

I was able to write a test to expose this in MM 0.11.1 and Rails 3.1.3. This shows the opposite behavior but I think it may be the same issue -- it's just not letting go of the first document stored in the IdentityMap. I haven't noticed issues in production with this -- only the test environment .

require 'test_helper'

class User
  include MongoMapper::Document
  plugin MongoMapper::Plugins::IdentityMap
  key :name, String
  key :number, Integer
end

class IdentityMapTest < ActiveSupport::TestCase
  setup do
    User.create(:name => "Bob", :number => 1)
    User.create(:name => "John", :number => 2)
    User.create(:name => "Roger", :number => 3)
  end

  test "identity map with id" do
    User.fields(:id).all.each do |u|
      assert_equal nil, u.name
      assert_equal nil, u.number
    end
  end

  test "identity map without fields" do
    User.all.each do |u|
      assert u.name.present?
      assert u.number.present?
    end
  end

end
  1. Failure:
    test_identity_map_with_id(IdentityMapTest)
    method test_identity_map_with_id in identity_map_test.rb at line 19
    method each in identity_map_test.rb at line 18
    method test_identity_map_with_id in identity_map_test.rb at line 18
    expected but was
    <"Bob">.

Without IM, this passes:

require 'test_helper'

class User
  include MongoMapper::Document
  key :name, String
  key :number, Integer
end

class IdentityMapTest < ActiveSupport::TestCase
  setup do
    User.create(:name => "Bob", :number => 1)
    User.create(:name => "John", :number => 2)
    User.create(:name => "Roger", :number => 3)
  end

  test "identity map with id" do
    User.fields(:id).all.each do |u|
      assert_equal nil, u.name
      assert_equal nil, u.number
    end
  end

  test "identity map without fields" do
    User.all.each do |u|
      assert u.name.present?
      assert u.number.present?
    end
  end

end

Started
..
Finished in 0.037808 seconds.
2 tests, 12 assertions, 0 failures, 0 errors

Contributor

bkimble commented Jul 12, 2012

I wanted to add that I did try to get around this by doing:

MongoMapper::Plugins::IdentityMap.clear    

In the global test:unit teardown method, but I still had the issue.

Contributor

bkimble commented Jul 13, 2012

Looking in to it further, it appears this also affects non test environments. I also tested this in clean Rails 3.1.3 and 3.2.6 environments and it still fails.

Owner

cheald commented Jul 8, 2013

Closing as this appears to no longer be an issue.

cheald closed this Jul 8, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment