Skip to content

Instantly share code, notes, and snippets.

@coderifous
Created October 21, 2011 14:53
Show Gist options
  • Save coderifous/1304035 to your computer and use it in GitHub Desktop.
Save coderifous/1304035 to your computer and use it in GitHub Desktop.
Test for broken deletes on habtm relations in Mongoid.
source "http://rubygems.org"
# Test passes with version 2.2.3
# gem "mongoid", "2.2.3"
# Test fails with version 2.3.2
gem "mongoid", "2.3.2"
require "rubygems"
require "bundler/setup"
require "mongoid"
require "minitest/spec"
require 'minitest/autorun'
Mongoid.configure do |config|
config.skip_version_check = true
config.database = Mongo::Connection.new.db("mongoid_sandbox")
end
class Person
include Mongoid::Document
has_and_belongs_to_many :companies
end
class Company
include Mongoid::Document
has_and_belongs_to_many :people
end
describe "mongoid habtm" do
before do
@jim = Person.create
@busyconf = Company.create
@busyconf.people << @jim
@busyconf.reload
assert_equal 1, @busyconf.people.count
end
# This test fails in Mongoid 2.3
it "should handle appends/deletes properly" do
@busyconf.people.delete(@jim)
@busyconf.reload
assert_equal 0, @busyconf.people.count
end
# This test passes in Mongoid 2.3
it "should not be affected by this" do
@busyconf.people.load_all! # <== this is the only difference from the previous test.
@busyconf.people.delete(@jim)
@busyconf.reload
assert_equal 0, @busyconf.people.count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment