Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Tests for HABTM #31

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions test/active_record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,24 @@
end

create_table "namespaced", :force => true

create_table "clubs", :force => true do |t|
t.integer "id"
t.string "name"
end

create_table "clubs_users", :id => false, :force => true do |t|
t.integer "club_id"
t.integer "user_id"
end
end

# models
class User < ActiveRecord::Base
has_one :profile, :dependent => :destroy
has_many :emails, :dependent => :destroy, :order => 'id'
has_many :notes, :as => :notable
has_and_belongs_to_many :clubs
replicate_natural_key :login
end

Expand Down Expand Up @@ -93,6 +104,10 @@ class User::Namespaced < ActiveRecord::Base
self.table_name = "namespaced"
end

class Club < ActiveRecord::Base
has_and_belongs_to_many :users
end

# The test case loads some fixture data once and uses transaction rollback to
# reset fixture state for each test's setup.
class ActiveRecordTest < Test::Unit::TestCase
Expand Down Expand Up @@ -123,10 +138,13 @@ def self.fixtures
user.create_profile :name => 'Ryan Tomayko', :homepage => 'http://tomayko.com'
user.emails.create! :email => '[email protected]'
user.emails.create! :email => '[email protected]'
user.clubs.create! :name => 'chess'
battlebots = user.clubs.create! :name => 'battlebots'

user = User.create! :login => 'kneath'
user.create_profile :name => 'Kyle Neath', :homepage => 'http://warpspire.com'
user.emails.create! :email => '[email protected]'
user.clubs << battlebots

user = User.create! :login => 'tmm1'
user.create_profile :name => 'tmm1', :homepage => 'https://github.com/tmm1'
Expand Down Expand Up @@ -352,6 +370,51 @@ def test_omit_attributes_at_dump_time
assert_nil attrs['created_at']
end

def test_dumping_and_loading_has_and_belongs_to_many_associations
objects = []
@dumper.listen { |type, id, attrs, obj| objects << [type, id, attrs, obj] }

User.replicate_associations :clubs
rtomayko = User.find_by_login('rtomayko')
kneath = User.find_by_login('kneath')
@dumper.dump rtomayko
@dumper.dump kneath

User.destroy_all
Club.destroy_all
objects.each { |type, id, attrs, obj| @loader.feed type, id, attrs }

rtomayko = User.find_by_login('rtomayko')
kneath = User.find_by_login('kneath')
battlebots = Club.find_by_name('battlebots')
chess = Club.find_by_name('chess')
assert_equal [chess, battlebots], rtomayko.clubs.to_a
assert_equal [battlebots], kneath.clubs.to_a
assert_equal [rtomayko, kneath], battlebots.users.to_a
end

def test_has_and_belongs_to_many_associations_at_dump_time
objects = []
@dumper.listen { |type, id, attrs, obj| objects << [type, id, attrs, obj] }

rtomayko = User.find_by_login('rtomayko')
kneath = User.find_by_login('kneath')
@dumper.dump rtomayko, :associations => [:clubs]
@dumper.dump kneath, :associations => [:clubs]

User.destroy_all
Club.destroy_all
objects.each { |type, id, attrs, obj| @loader.feed type, id, attrs }

rtomayko = User.find_by_login('rtomayko')
kneath = User.find_by_login('kneath')
battlebots = Club.find_by_name('battlebots')
chess = Club.find_by_name('chess')
assert_equal [chess, battlebots], rtomayko.clubs.to_a
assert_equal [battlebots], kneath.clubs.to_a
assert_equal [rtomayko, kneath], battlebots.users.to_a
end

def test_dumping_polymorphic_associations
objects = []
@dumper.listen { |type, id, attrs, obj| objects << [type, id, attrs, obj] }
Expand Down