# HG changeset patch # User tyler.rick # Date 1192429107 0 # Node ID 7abf2daec7572e3a46030884a1b5d8d80b8152f3 # Parent ac95efc9417bae4ae152c42fc94160e8976a7c08 Imported tests I'd originally written... Made sure test still passes. git-svn-id: https://habtm-with-deferred-save.googlecode.com/svn/trunk@3 e8f5f159-813c-0410-8c33-cd1931e36101 diff -r ac95efc9417bae4ae152c42fc94160e8976a7c08 -r 7abf2daec7572e3a46030884a1b5d8d80b8152f3 has_and_belong_to_many_with_deferred_save/init.rb --- a/has_and_belong_to_many_with_deferred_save/init.rb Mon Oct 15 05:48:30 2007 +0000 +++ b/has_and_belong_to_many_with_deferred_save/init.rb Mon Oct 15 06:18:27 2007 +0000 @@ -1,1 +1,1 @@ -# Include hook code here \ No newline at end of file +require File.join(File.dirname(__FILE__), 'lib/has_and_belongs_to_many_with_deferred_save') diff -r ac95efc9417bae4ae152c42fc94160e8976a7c08 -r 7abf2daec7572e3a46030884a1b5d8d80b8152f3 has_and_belong_to_many_with_deferred_save/test/has_and_belongs_to_many_with_deferred_save_test.rb --- a/has_and_belong_to_many_with_deferred_save/test/has_and_belongs_to_many_with_deferred_save_test.rb Mon Oct 15 05:48:30 2007 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,8 +0,0 @@ -require 'test/unit' - -class HasAndBelongsToManyWithDeferredSaveTest < Test::Unit::TestCase - # Replace this with your real tests. - def test_this_plugin - flunk - end -end diff -r ac95efc9417bae4ae152c42fc94160e8976a7c08 -r 7abf2daec7572e3a46030884a1b5d8d80b8152f3 has_and_belong_to_many_with_deferred_save/test/rails_root/app/models/person.rb --- a/has_and_belong_to_many_with_deferred_save/test/rails_root/app/models/person.rb Mon Oct 15 05:48:30 2007 +0000 +++ b/has_and_belong_to_many_with_deferred_save/test/rails_root/app/models/person.rb Mon Oct 15 06:18:27 2007 +0000 @@ -1,2 +1,3 @@ class Person < ActiveRecord::Base + has_and_belongs_to_many :room end diff -r ac95efc9417bae4ae152c42fc94160e8976a7c08 -r 7abf2daec7572e3a46030884a1b5d8d80b8152f3 has_and_belong_to_many_with_deferred_save/test/rails_root/app/models/room.rb --- a/has_and_belong_to_many_with_deferred_save/test/rails_root/app/models/room.rb Mon Oct 15 05:48:30 2007 +0000 +++ b/has_and_belong_to_many_with_deferred_save/test/rails_root/app/models/room.rb Mon Oct 15 06:18:27 2007 +0000 @@ -1,2 +1,17 @@ class Room < ActiveRecord::Base + has_and_belongs_to_many_with_deferred_save :people, :before_add => :before_adding_person + + def validate + if people.size > maximum_occupancy + errors.add :people, "There are too many people in this room" + end + end + + # Just in case they try to bypass our new accessor and call people_without_deferred_save directly... + # (This should never be necessary; it is for demonstration purposes only...) + def before_adding_person(person) + if self.people_without_deferred_save.size + [person].size > maximum_occupancy + raise "There are too many people in this room" + end + end end diff -r ac95efc9417bae4ae152c42fc94160e8976a7c08 -r 7abf2daec7572e3a46030884a1b5d8d80b8152f3 has_and_belong_to_many_with_deferred_save/test/rails_root/db/migrate/001_create_rooms.rb --- a/has_and_belong_to_many_with_deferred_save/test/rails_root/db/migrate/001_create_rooms.rb Mon Oct 15 05:48:30 2007 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,10 +0,0 @@ -class CreateRooms < ActiveRecord::Migration - def self.up - create_table :rooms do |t| - end - end - - def self.down - drop_table :rooms - end -end diff -r ac95efc9417bae4ae152c42fc94160e8976a7c08 -r 7abf2daec7572e3a46030884a1b5d8d80b8152f3 has_and_belong_to_many_with_deferred_save/test/rails_root/db/migrate/001_create_rooms_and_people.rb --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/has_and_belong_to_many_with_deferred_save/test/rails_root/db/migrate/001_create_rooms_and_people.rb Mon Oct 15 06:18:27 2007 +0000 @@ -0,0 +1,21 @@ +class CreateRoomsAndPeople < ActiveRecord::Migration + def self.up + create_table :people do |t| + t.column :name, :string + end + create_table :rooms do |t| + t.column :name, :string + t.column :maximum_occupancy, :integer + end + create_table :people_rooms do |t| + t.column :person_id, :integer + t.column :room_id, :integer + end + end + + def self.down + drop_table :people + drop_table :rooms + drop_table :people_rooms + end +end diff -r ac95efc9417bae4ae152c42fc94160e8976a7c08 -r 7abf2daec7572e3a46030884a1b5d8d80b8152f3 has_and_belong_to_many_with_deferred_save/test/rails_root/db/migrate/002_create_people.rb --- a/has_and_belong_to_many_with_deferred_save/test/rails_root/db/migrate/002_create_people.rb Mon Oct 15 05:48:30 2007 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,10 +0,0 @@ -class CreatePeople < ActiveRecord::Migration - def self.up - create_table :people do |t| - end - end - - def self.down - drop_table :people - end -end diff -r ac95efc9417bae4ae152c42fc94160e8976a7c08 -r 7abf2daec7572e3a46030884a1b5d8d80b8152f3 has_and_belong_to_many_with_deferred_save/test/rails_root/db/schema.rb --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/has_and_belong_to_many_with_deferred_save/test/rails_root/db/schema.rb Mon Oct 15 06:18:27 2007 +0000 @@ -0,0 +1,21 @@ +# This file is autogenerated. Instead of editing this file, please use the +# migrations feature of ActiveRecord to incrementally modify your database, and +# then regenerate this schema definition. + +ActiveRecord::Schema.define(:version => 1) do + + create_table "people", :force => true do |t| + t.column "name", :string + end + + create_table "people_rooms", :force => true do |t| + t.column "person_id", :integer + t.column "room_id", :integer + end + + create_table "rooms", :force => true do |t| + t.column "name", :string + t.column "maximum_occupancy", :integer + end + +end diff -r ac95efc9417bae4ae152c42fc94160e8976a7c08 -r 7abf2daec7572e3a46030884a1b5d8d80b8152f3 has_and_belong_to_many_with_deferred_save/test/rails_root/log/development.log --- a/has_and_belong_to_many_with_deferred_save/test/rails_root/log/development.log Mon Oct 15 05:48:30 2007 +0000 +++ b/has_and_belong_to_many_with_deferred_save/test/rails_root/log/development.log Mon Oct 15 06:18:27 2007 +0000 @@ -0,0 +1,100 @@ + SQL (0.000000) SQLite3::SQLException: no such table: schema_info: SELECT * FROM schema_info + SQL (0.000230) SELECT name FROM sqlite_master WHERE type = 'table' + SQL (0.024843) CREATE TABLE schema_info (version integer) + SQL (0.001990) INSERT INTO schema_info (version) VALUES(0) + SQL (0.000000) SQLite3::SQLException: table schema_info already exists: CREATE TABLE schema_info (version integer) + SQL (0.000331) SELECT version FROM schema_info +Migrating to CreateRooms (1) + SQL (0.002799) CREATE TABLE rooms ("id" INTEGER PRIMARY KEY NOT NULL)  + SQL (0.001995) UPDATE schema_info SET version = 1 + SQL (0.000359) SELECT version FROM schema_info +Migrating to CreatePeople (2) + SQL (0.009934) CREATE TABLE people ("id" INTEGER PRIMARY KEY NOT NULL)  + SQL (0.002042) UPDATE schema_info SET version = 2 + SQL (0.000309) SELECT * FROM schema_info + SQL (0.000367) SELECT name FROM sqlite_master WHERE type = 'table' + SQL (0.000088) PRAGMA index_list(people) + SQL (0.000082) PRAGMA index_list(rooms) + SQL (0.000000) SQLite3::SQLException: table schema_info already exists: CREATE TABLE schema_info (version integer) + SQL (0.000302) SELECT version FROM schema_info + SQL (0.000225) SELECT version FROM schema_info + SQL (0.000000) SQLite3::SQLException: table schema_info already exists: CREATE TABLE schema_info (version integer) + SQL (0.000284) SELECT version FROM schema_info +Migrating to CreatePeople (2) + SQL (0.009962) DROP TABLE people + SQL (0.002864) UPDATE schema_info SET version = 1 + SQL (0.000313) SELECT version FROM schema_info +Migrating to CreateRooms (1) + SQL (0.002600) DROP TABLE rooms + SQL (0.002322) UPDATE schema_info SET version = 0 + SQL (0.000313) SELECT * FROM schema_info + SQL (0.000251) SELECT name FROM sqlite_master WHERE type = 'table' + SQL (0.000000) SQLite3::SQLException: table schema_info already exists: CREATE TABLE schema_info (version integer) + SQL (0.000000) SQLite3::SQLException: table schema_info already exists: CREATE TABLE schema_info (version integer) + SQL (0.000339) SELECT version FROM schema_info +Migrating to CreateRoomsAndPeople (1) + SQL (0.024409) CREATE TABLE people ("id" INTEGER PRIMARY KEY NOT NULL, "name" varchar(255) DEFAULT NULL)  + SQL (0.007128) CREATE TABLE rooms ("id" INTEGER PRIMARY KEY NOT NULL, "name" varchar(255) DEFAULT NULL, "maximum_occupancy" integer DEFAULT NULL)  + SQL (0.003440) CREATE TABLE people_rooms ("id" INTEGER PRIMARY KEY NOT NULL, "person_id" integer DEFAULT NULL, "room_id" integer DEFAULT NULL)  + SQL (0.002608) UPDATE schema_info SET version = 1 + SQL (0.000318) SELECT * FROM schema_info + SQL (0.000429) SELECT name FROM sqlite_master WHERE type = 'table' + SQL (0.000087) PRAGMA index_list(people) + SQL (0.000085) PRAGMA index_list(people_rooms) + SQL (0.000084) PRAGMA index_list(rooms) + SQL (0.000861) SELECT * FROM schema_info + SQL (0.000564) SELECT name FROM sqlite_master WHERE type = 'table' + SQL (0.000085) PRAGMA index_list(people) + SQL (0.000082) PRAGMA index_list(people_rooms) + SQL (0.000082) PRAGMA index_list(rooms) + SQL (0.000000) SQLite3::SQLException: no such table: people: DROP TABLE people + SQL (0.009971) CREATE TABLE people ("id" INTEGER PRIMARY KEY NOT NULL, "name" varchar(255) DEFAULT NULL)  + SQL (0.000000) SQLite3::SQLException: no such table: people_rooms: DROP TABLE people_rooms + SQL (0.002587) CREATE TABLE people_rooms ("id" INTEGER PRIMARY KEY NOT NULL, "person_id" integer DEFAULT NULL, "room_id" integer DEFAULT NULL)  + SQL (0.000000) SQLite3::SQLException: no such table: rooms: DROP TABLE rooms + SQL (0.002565) CREATE TABLE rooms ("id" INTEGER PRIMARY KEY NOT NULL, "name" varchar(255) DEFAULT NULL, "maximum_occupancy" integer DEFAULT NULL)  + SQL (0.002742) CREATE TABLE schema_info (version integer) + SQL (0.009701) INSERT INTO schema_info (version) VALUES(0) + SQL (0.002448) UPDATE schema_info SET version = 1 + SQL (0.000780) SELECT * FROM schema_info + SQL (0.000436) SELECT name FROM sqlite_master WHERE type = 'table' + SQL (0.000084) PRAGMA index_list(people) + SQL (0.000082) PRAGMA index_list(people_rooms) + SQL (0.000081) PRAGMA index_list(rooms) + SQL (0.000000) SQLite3::SQLException: no such table: people: DROP TABLE people + SQL (0.022222) CREATE TABLE people ("id" INTEGER PRIMARY KEY NOT NULL, "name" varchar(255) DEFAULT NULL)  + SQL (0.000000) SQLite3::SQLException: no such table: people_rooms: DROP TABLE people_rooms + SQL (0.004313) CREATE TABLE people_rooms ("id" INTEGER PRIMARY KEY NOT NULL, "person_id" integer DEFAULT NULL, "room_id" integer DEFAULT NULL)  + SQL (0.000000) SQLite3::SQLException: no such table: rooms: DROP TABLE rooms + SQL (0.002948) CREATE TABLE rooms ("id" INTEGER PRIMARY KEY NOT NULL, "name" varchar(255) DEFAULT NULL, "maximum_occupancy" integer DEFAULT NULL)  + SQL (0.004096) CREATE TABLE schema_info (version integer) + SQL (0.002451) INSERT INTO schema_info (version) VALUES(0) + SQL (0.004048) UPDATE schema_info SET version = 1 + SQL (0.000771) SELECT * FROM schema_info + SQL (0.000434) SELECT name FROM sqlite_master WHERE type = 'table' + SQL (0.000110) PRAGMA index_list(people) + SQL (0.000082) PRAGMA index_list(people_rooms) + SQL (0.000083) PRAGMA index_list(rooms) + SQL (0.000000) SQLite3::SQLException: no such table: people: DROP TABLE people + SQL (0.105043) CREATE TABLE people ("id" INTEGER PRIMARY KEY NOT NULL, "name" varchar(255) DEFAULT NULL)  + SQL (0.000000) SQLite3::SQLException: no such table: people_rooms: DROP TABLE people_rooms + SQL (0.003520) CREATE TABLE people_rooms ("id" INTEGER PRIMARY KEY NOT NULL, "person_id" integer DEFAULT NULL, "room_id" integer DEFAULT NULL)  + SQL (0.000000) SQLite3::SQLException: no such table: rooms: DROP TABLE rooms + SQL (0.002869) CREATE TABLE rooms ("id" INTEGER PRIMARY KEY NOT NULL, "name" varchar(255) DEFAULT NULL, "maximum_occupancy" integer DEFAULT NULL)  + SQL (0.003102) CREATE TABLE schema_info (version integer) + SQL (0.002188) INSERT INTO schema_info (version) VALUES(0) + SQL (0.002526) UPDATE schema_info SET version = 1 + SQL (0.000807) SELECT * FROM schema_info + SQL (0.000434) SELECT name FROM sqlite_master WHERE type = 'table' + SQL (0.000085) PRAGMA index_list(people) + SQL (0.000084) PRAGMA index_list(people_rooms) + SQL (0.000082) PRAGMA index_list(rooms) + SQL (0.000000) SQLite3::SQLException: no such table: people: DROP TABLE people + SQL (0.017608) CREATE TABLE people ("id" INTEGER PRIMARY KEY NOT NULL, "name" varchar(255) DEFAULT NULL)  + SQL (0.000000) SQLite3::SQLException: no such table: people_rooms: DROP TABLE people_rooms + SQL (0.002525) CREATE TABLE people_rooms ("id" INTEGER PRIMARY KEY NOT NULL, "person_id" integer DEFAULT NULL, "room_id" integer DEFAULT NULL)  + SQL (0.000000) SQLite3::SQLException: no such table: rooms: DROP TABLE rooms + SQL (0.002427) CREATE TABLE rooms ("id" INTEGER PRIMARY KEY NOT NULL, "name" varchar(255) DEFAULT NULL, "maximum_occupancy" integer DEFAULT NULL)  + SQL (0.002475) CREATE TABLE schema_info (version integer) + SQL (0.009441) INSERT INTO schema_info (version) VALUES(0) + SQL (0.002270) UPDATE schema_info SET version = 1 diff -r ac95efc9417bae4ae152c42fc94160e8976a7c08 -r 7abf2daec7572e3a46030884a1b5d8d80b8152f3 has_and_belong_to_many_with_deferred_save/test/rails_root/log/test.log --- a/has_and_belong_to_many_with_deferred_save/test/rails_root/log/test.log Mon Oct 15 05:48:30 2007 +0000 +++ b/has_and_belong_to_many_with_deferred_save/test/rails_root/log/test.log Mon Oct 15 06:18:27 2007 +0000 @@ -0,0 +1,41 @@ + Person Load (0.000276) SELECT * FROM people WHERE (people."id" = 1)  + Person Load (0.000221) SELECT * FROM people WHERE (people."id" = 2)  + Room Count (0.000244) select count(*) from people_rooms + SQL (0.000217) INSERT INTO rooms ("name", "maximum_occupancy") VALUES(NULL, 2) + Room Count (0.000198) select count(*) from people_rooms + Person Load (0.000281) SELECT * FROM people WHERE (people."id" = 1)  + Person Load (0.000221) SELECT * FROM people WHERE (people."id" = 2)  + Room Count (0.000225) select count(*) from people_rooms + SQL (0.000227) INSERT INTO rooms ("name", "maximum_occupancy") VALUES(NULL, 2) + SQL (0.000125) INSERT INTO people_rooms ("id", "room_id", "person_id") VALUES (1, 1, 1) + SQL (0.000108) INSERT INTO people_rooms ("id", "room_id", "person_id") VALUES (2, 1, 2) + Room Count (0.000193) select count(*) from people_rooms + Person Load (0.000220) SELECT * FROM people WHERE (people."id" = 3)  + Room Count (0.000184) select count(*) from people_rooms + Person Load (0.000278) SELECT * FROM people WHERE (people."id" = 1)  + Person Load (0.000227) SELECT * FROM people WHERE (people."id" = 2)  + Room Count (0.000227) select count(*) from people_rooms + SQL (0.000224) INSERT INTO rooms ("name", "maximum_occupancy") VALUES(NULL, 2) + SQL (0.000122) INSERT INTO people_rooms ("id", "room_id", "person_id") VALUES (1, 1, 1) + SQL (0.000102) INSERT INTO people_rooms ("id", "room_id", "person_id") VALUES (2, 1, 2) + Room Count (0.000189) select count(*) from people_rooms + Person Load (0.000220) SELECT * FROM people WHERE (people."id" = 3)  + Room Count (0.000183) select count(*) from people_rooms + Room Count (0.000186) select count(*) from people_rooms + Room Load (0.000239) SELECT * FROM rooms WHERE (rooms."id" = 1)  + Person Load (0.000444) SELECT * FROM people INNER JOIN people_rooms ON people.id = people_rooms.person_id WHERE (people_rooms.room_id = 1 )  + Room Count (0.000199) select count(*) from people_rooms + SQL (0.000110) INSERT INTO people_rooms ("id", "room_id", "person_id") VALUES (3, 1, 3) + Person Load (0.000276) SELECT * FROM people WHERE (people."id" = 1)  + Person Load (0.000219) SELECT * FROM people WHERE (people."id" = 2)  + Room Count (0.000224) select count(*) from people_rooms + SQL (0.000245) INSERT INTO rooms ("name", "maximum_occupancy") VALUES(NULL, 2) + SQL (0.000121) INSERT INTO people_rooms ("id", "room_id", "person_id") VALUES (1, 1, 1) + SQL (0.000103) INSERT INTO people_rooms ("id", "room_id", "person_id") VALUES (2, 1, 2) + Room Count (0.000190) select count(*) from people_rooms + Person Load (0.000234) SELECT * FROM people WHERE (people."id" = 3)  + Room Count (0.000183) select count(*) from people_rooms + Room Count (0.000188) select count(*) from people_rooms + Room Load (0.000237) SELECT * FROM rooms WHERE (rooms."id" = 1)  + Person Load (0.000442) SELECT * FROM people INNER JOIN people_rooms ON people.id = people_rooms.person_id WHERE (people_rooms.room_id = 1 )  + Room Count (0.000206) select count(*) from people_rooms diff -r ac95efc9417bae4ae152c42fc94160e8976a7c08 -r 7abf2daec7572e3a46030884a1b5d8d80b8152f3 has_and_belong_to_many_with_deferred_save/test/rails_root/test/fixtures/people.yml --- a/has_and_belong_to_many_with_deferred_save/test/rails_root/test/fixtures/people.yml Mon Oct 15 05:48:30 2007 +0000 +++ b/has_and_belong_to_many_with_deferred_save/test/rails_root/test/fixtures/people.yml Mon Oct 15 06:18:27 2007 +0000 @@ -1,5 +1,7 @@ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html -one: +person1: id: 1 -two: +person2: id: 2 +person3: + id: 3 diff -r ac95efc9417bae4ae152c42fc94160e8976a7c08 -r 7abf2daec7572e3a46030884a1b5d8d80b8152f3 has_and_belong_to_many_with_deferred_save/test/rails_root/test/fixtures/rooms.yml --- a/has_and_belong_to_many_with_deferred_save/test/rails_root/test/fixtures/rooms.yml Mon Oct 15 05:48:30 2007 +0000 +++ b/has_and_belong_to_many_with_deferred_save/test/rails_root/test/fixtures/rooms.yml Mon Oct 15 06:18:27 2007 +0000 @@ -1,5 +1,5 @@ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html -one: +first: id: 1 -two: +another: id: 2 diff -r ac95efc9417bae4ae152c42fc94160e8976a7c08 -r 7abf2daec7572e3a46030884a1b5d8d80b8152f3 has_and_belong_to_many_with_deferred_save/test/rails_root/test/unit/person_test.rb --- a/has_and_belong_to_many_with_deferred_save/test/rails_root/test/unit/person_test.rb Mon Oct 15 05:48:30 2007 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,10 +0,0 @@ -require File.dirname(__FILE__) + '/../test_helper' - -class PersonTest < Test::Unit::TestCase - fixtures :people - - # Replace this with your real tests. - def test_truth - assert true - end -end diff -r ac95efc9417bae4ae152c42fc94160e8976a7c08 -r 7abf2daec7572e3a46030884a1b5d8d80b8152f3 has_and_belong_to_many_with_deferred_save/test/rails_root/test/unit/room_maximum_occupancy_test.rb --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/has_and_belong_to_many_with_deferred_save/test/rails_root/test/unit/room_maximum_occupancy_test.rb Mon Oct 15 06:18:27 2007 +0000 @@ -0,0 +1,56 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class RoomMaximumOccupancyTest < Test::Unit::TestCase + fixtures :people + +# def test_1 +# room = Room.new(:maximum_occupancy => 4) +# room.maximum_occupancy = 10 +# +# assert_equal 10, room.maximum_occupancy # Still invalid +# assert_equal false, room.save +# assert_equal "You can't have the maximum set so high", room.errors.on(:maximum_occupancy) +# assert_equal 10, room.maximum_occupancy # Still invalid +# end + + def test_maximum_occupancy + room = Room.new(:maximum_occupancy => 2) + assert_equal [], room.people + assert_equal [], room.people_without_deferred_save + assert_not_equal room.unsaved_people.object_id, + room.people_without_deferred_save.object_id + + assert_nothing_raised { room.people << people(:person1) } + assert_nothing_raised { room.people << people(:person2) } + assert_equal 0, Room.count_by_sql("select count(*) from people_rooms") # Still not saved to the association table! + assert_equal 0, room.people_without_deferred_save.size + assert_equal 2, room.people.size # 2 because this looks at unsaved_people + + assert room.save # Only here is it actually saved to the association table! + assert_equal 2, Room.count_by_sql("select count(*) from people_rooms") + assert_equal 2, room.people.size + assert_equal 2, room.people_without_deferred_save.size + + assert_nothing_raised { room.people << people(:person3) } + assert_equal 2, Room.count_by_sql("select count(*) from people_rooms") # person3 is not yet saved to the association table + assert_equal false, room.valid? + assert_equal "There are too many people in this room", room.errors.on(:people) + + assert_equal false, room.save + assert_equal 2, Room.count_by_sql("select count(*) from people_rooms") # It's still not there, because it didn't pass the validation. + assert_equal "There are too many people in this room", room.errors.on(:people) + assert_equal 3, room.people.size # Just like with normal attributes that fail validation... the attribute still contains the invalid data but we refuse to save until it is changed to something that is *valid*. + + room.reload + assert_equal 2, room.people.size + assert_equal 2, room.people_without_deferred_save.size + + assert_nothing_raised { room.people << people(:person3) } + assert_equal 2, Room.count_by_sql("select count(*) from people_rooms") # person3 is not yet saved to the association table + + # If they try to go around our accessors and use the original accessors, then (and only then) will the exception be raised in before_adding_person... + assert_raise RuntimeError do + room.people_without_deferred_save << people(:person3) + end + end +end diff -r ac95efc9417bae4ae152c42fc94160e8976a7c08 -r 7abf2daec7572e3a46030884a1b5d8d80b8152f3 has_and_belong_to_many_with_deferred_save/test/rails_root/test/unit/room_maximum_occupancy_test_1.rb --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/has_and_belong_to_many_with_deferred_save/test/rails_root/test/unit/room_maximum_occupancy_test_1.rb Mon Oct 15 06:18:27 2007 +0000 @@ -0,0 +1,26 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class RoomMaximumOccupancyTest < Test::Unit::TestCase + fixtures :people + + def test_maximum_occupancy + room = Room.new(:maximum_occupancy => 2) + assert_equal 0, Room.count_by_sql("select count(*) from people_rooms") + assert_equal 0, room.people.size + + room.people << people(:person1) + room.people << people(:person2) + assert room.save + assert_equal 2, Room.count_by_sql("select count(*) from people_rooms") + assert_equal 2, room.people.size + + room.people << people(:person3) + #assert_equal 2, Room.count_by_sql("select count(*) from people_rooms") # FAILS because it saves it in people_rooms before we even call room.save ! + + assert_equal false, room.save + # Good, it has the error ... + assert_equal "There are too many people in this room", room.errors.on(:people) + # ... but it's too late. It didn't prevent the invalid data from getting in there! + #assert_equal 2, room.people.size # FAILS + end +end diff -r ac95efc9417bae4ae152c42fc94160e8976a7c08 -r 7abf2daec7572e3a46030884a1b5d8d80b8152f3 has_and_belong_to_many_with_deferred_save/test/rails_root/test/unit/room_maximum_occupancy_test_2.rb --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/has_and_belong_to_many_with_deferred_save/test/rails_root/test/unit/room_maximum_occupancy_test_2.rb Mon Oct 15 06:18:27 2007 +0000 @@ -0,0 +1,31 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class RoomMaximumOccupancyTest < Test::Unit::TestCase + fixtures :people + + def test_maximum_occupancy_using_build + room = Room.new(:maximum_occupancy => 2) + assert_equal 0, Room.count_by_sql("select count(*) from people_rooms") + assert_equal 0, room.people.size + + room.people.build(:name => 'person1') + room.people.build(:name => 'person2') + assert room.save + assert_equal 2, Room.count_by_sql("select count(*) from people_rooms") + assert_equal 2, room.people.size + + room.people.build(:name => 'person3') + # Good, it prevented it from being saved to the database ... + assert_equal 2, Room.count_by_sql("select count(*) from people_rooms") + # ... but it still added it to the collection stored in memory! + #assert_equal 2, room.people.size # Still FAILs. It thinks it has 3, even though the 3rd one is invalid. + + assert_equal false, room.save + assert_equal "There are too many people in this room", room.errors.on(:people) + + # If we reload from what is stored in memory, it will still just have the 2 valid people... + room.reload + assert_equal 2, room.people.size + end + +end diff -r ac95efc9417bae4ae152c42fc94160e8976a7c08 -r 7abf2daec7572e3a46030884a1b5d8d80b8152f3 has_and_belong_to_many_with_deferred_save/test/rails_root/test/unit/room_maximum_occupancy_test_3.rb --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/has_and_belong_to_many_with_deferred_save/test/rails_root/test/unit/room_maximum_occupancy_test_3.rb Mon Oct 15 06:18:27 2007 +0000 @@ -0,0 +1,33 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class RoomMaximumOccupancyTest < Test::Unit::TestCase + fixtures :people + + def test_maximum_occupancy + room = Room.new(:maximum_occupancy => 2) + assert_equal 0, Room.count_by_sql("select count(*) from people_rooms") + assert_equal 0, room.people.size + + assert_nothing_raised { room.people << people(:person1) } + assert_nothing_raised { room.people << people(:person2) } + assert room.save + assert_equal 2, Room.count_by_sql("select count(*) from people_rooms") + assert_equal 2, room.people.size + + assert_raise RuntimeError do + room.people << people(:person3) + end + assert_equal 2, Room.count_by_sql("select count(*) from people_rooms") + + assert_equal "There are too many people in this room", room.errors.on(:people) # Passes (for now!) + + # But as soon as I go to save it, it clears out the errors array!! Arg! + room.save + #assert_equal "There are too many people in this room", room.errors.on(:people) # FAILS + + #assert_equal false, room.valid? # FAILS + #assert_equal false, room.save # FAILS + assert_equal 2, room.people.size + end + +end diff -r ac95efc9417bae4ae152c42fc94160e8976a7c08 -r 7abf2daec7572e3a46030884a1b5d8d80b8152f3 has_and_belong_to_many_with_deferred_save/test/rails_root/test/unit/room_test.rb --- a/has_and_belong_to_many_with_deferred_save/test/rails_root/test/unit/room_test.rb Mon Oct 15 05:48:30 2007 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,10 +0,0 @@ -require File.dirname(__FILE__) + '/../test_helper' - -class RoomTest < Test::Unit::TestCase - fixtures :rooms - - # Replace this with your real tests. - def test_truth - assert true - end -end