Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Identity Map + Fields + Test Environment #432

Closed
bkimble opened this Issue · 3 comments

2 participants

@bkimble

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

@bkimble

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.

@bkimble

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.

@cheald
Owner

Closing as this appears to no longer be an issue.

@cheald cheald closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.