From 38da0ace772e6f9f5e2fff74db76237ab31790fa Mon Sep 17 00:00:00 2001 From: Mathieu Arnold Date: Thu, 15 Apr 2010 12:02:26 +0200 Subject: Use primary key in conditions, not 'id' [#4395 state:resolved] Signed-off-by: Pratik Naik --- activerecord/test/cases/nested_attributes_test.rb | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/nested_attributes_test.rb b/activerecord/test/cases/nested_attributes_test.rb index eae8ae7e39..fadd62b5a1 100644 --- a/activerecord/test/cases/nested_attributes_test.rb +++ b/activerecord/test/cases/nested_attributes_test.rb @@ -6,6 +6,8 @@ require "models/parrot" require "models/treasure" require "models/man" require "models/interest" +require "models/owner" +require "models/pet" require 'active_support/hash_with_indifferent_access' module AssertRaiseWithMessage @@ -707,3 +709,26 @@ class TestNestedAttributesLimit < ActiveRecord::TestCase end end end + +class TestNestedAttributesWithNonStandardPrimaryKeys < ActiveRecord::TestCase + fixtures :owners, :pets + + def setup + Owner.accepts_nested_attributes_for :pets + + @owner = owners(:ashley) + @pet1, @pet2 = pets(:chew), pets(:mochi) + + @params = { + :pets_attributes => { + '0' => { :id => @pet1.id, :name => 'Foo' }, + '1' => { :id => @pet2.id, :name => 'Bar' } + } + } + end + + def test_should_update_existing_records_with_non_standard_primary_key + @owner.update_attributes(@params) + assert_equal ['Foo', 'Bar'], @owner.pets.map(&:name) + end +end -- cgit v1.2.3 From 902861a43ae90032063f4a14a3e8b4b9b9c3ca2f Mon Sep 17 00:00:00 2001 From: Ernie Miller Date: Thu, 6 May 2010 16:14:09 -0400 Subject: Fix unintuitive behavior with multiple order and group clauses [#4545 state:committed] Signed-off-by: Jeremy Kemper --- activerecord/test/cases/base_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 3623680de9..bbc4e543d5 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1994,6 +1994,16 @@ class BasicsTest < ActiveRecord::TestCase assert_equal last, Developer.find(:all, :order => 'developers.name, developers.salary DESC').last end + def test_find_keeps_multiple_order_values + combined = Developer.find(:all, :order => 'developers.name, developers.salary') + assert_equal combined, Developer.find(:all, :order => ['developers.name', 'developers.salary']) + end + + def test_find_keeps_multiple_group_values + combined = Developer.find(:all, :group => 'developers.name, developers.salary, developers.id, developers.created_at, developers.updated_at') + assert_equal combined, Developer.find(:all, :group => ['developers.name', 'developers.salary', 'developers.id', 'developers.created_at', 'developers.updated_at']) + end + def test_find_symbol_ordered_last last = Developer.find :last, :order => :salary assert_equal last, Developer.find(:all, :order => :salary).last -- cgit v1.2.3 From 9aaef5935660ba13531512fb7def4b8bdf14511d Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 6 May 2010 23:47:23 -0300 Subject: Make find_or_create and find_or_initialize work mixing explicit parameters and a hash [#4457 state:committed] Signed-off-by: Jeremy Kemper --- activerecord/test/cases/finder_test.rb | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 77b2b748b1..e78db8969d 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -840,7 +840,7 @@ class FinderTest < ActiveRecord::TestCase assert c.new_record? end - def test_find_or_create_from_one_attribute_should_set_not_attribute_even_when_protected + def test_find_or_create_from_one_attribute_should_not_set_attribute_even_when_protected c = Company.find_or_create_by_name({:name => "Fortune 1000", :rating => 1000}) assert_equal "Fortune 1000", c.name assert_not_equal 1000, c.rating @@ -864,6 +864,22 @@ class FinderTest < ActiveRecord::TestCase assert !c.new_record? end + def test_find_or_initialize_from_one_attribute_should_set_attribute_even_when_protected_and_also_set_the_hash + c = Company.find_or_initialize_by_rating(1000, {:name => "Fortune 1000"}) + assert_equal "Fortune 1000", c.name + assert_equal 1000, c.rating + assert c.valid? + assert c.new_record? + end + + def test_find_or_create_from_one_attribute_should_set_attribute_even_when_protected_and_also_set_the_hash + c = Company.find_or_create_by_rating(1000, {:name => "Fortune 1000"}) + assert_equal "Fortune 1000", c.name + assert_equal 1000, c.rating + assert c.valid? + assert !c.new_record? + end + def test_find_or_initialize_should_set_protected_attributes_if_given_as_block c = Company.find_or_initialize_by_name(:name => "Fortune 1000") { |f| f.rating = 1000 } assert_equal "Fortune 1000", c.name -- cgit v1.2.3 From 5b95730edc33ee97f53da26a3868eb983305a771 Mon Sep 17 00:00:00 2001 From: Emili Parreno Date: Sat, 8 May 2010 12:35:39 +0100 Subject: Add index length support for MySQL [#1852 state:resolved] Example: add_index(:accounts, :name, :name => 'by_name', :length => 10) => CREATE INDEX by_name ON accounts(name(10)) add_index(:accounts, [:name, :surname], :name => 'by_name_surname', :length => {:name => 10, :surname => 15}) => CREATE INDEX by_name_surname ON accounts(name(10), surname(15)) Signed-off-by: Pratik Naik --- activerecord/test/cases/active_schema_test_mysql.rb | 17 +++++++++++++++++ activerecord/test/cases/migration_test.rb | 8 ++++++++ 2 files changed, 25 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/active_schema_test_mysql.rb b/activerecord/test/cases/active_schema_test_mysql.rb index 9aff538ce9..f4d123be15 100644 --- a/activerecord/test/cases/active_schema_test_mysql.rb +++ b/activerecord/test/cases/active_schema_test_mysql.rb @@ -15,6 +15,23 @@ class ActiveSchemaTest < ActiveRecord::TestCase end end + def test_add_index + expected = "CREATE INDEX `index_people_on_last_name` ON `people` (`last_name`)" + assert_equal expected, add_index(:people, :last_name, :length => nil) + + expected = "CREATE INDEX `index_people_on_last_name` ON `people` (`last_name`(10))" + assert_equal expected, add_index(:people, :last_name, :length => 10) + + expected = "CREATE INDEX `index_people_on_last_name_and_first_name` ON `people` (`last_name`(15), `first_name`(15))" + assert_equal expected, add_index(:people, [:last_name, :first_name], :length => 15) + + expected = "CREATE INDEX `index_people_on_last_name_and_first_name` ON `people` (`last_name`(15), `first_name`)" + assert_equal expected, add_index(:people, [:last_name, :first_name], :length => {:last_name => 15}) + + expected = "CREATE INDEX `index_people_on_last_name_and_first_name` ON `people` (`last_name`(15), `first_name`(10))" + assert_equal expected, add_index(:people, [:last_name, :first_name], :length => {:last_name => 15, :first_name => 10}) + end + def test_drop_table assert_equal "DROP TABLE `people`", drop_table(:people) end diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index a3d1ceaa1f..f67344445a 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -92,6 +92,14 @@ if ActiveRecord::Base.connection.supports_migrations? assert_nothing_raised { Person.connection.remove_index("people", "last_name_and_first_name") } assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) } assert_nothing_raised { Person.connection.remove_index("people", ["last_name", "first_name"]) } + assert_nothing_raised { Person.connection.add_index("people", ["last_name"], :length => 10) } + assert_nothing_raised { Person.connection.remove_index("people", "last_name") } + assert_nothing_raised { Person.connection.add_index("people", ["last_name"], :length => {:last_name => 10}) } + assert_nothing_raised { Person.connection.remove_index("people", ["last_name"]) } + assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"], :length => 10) } + assert_nothing_raised { Person.connection.remove_index("people", ["last_name", "first_name"]) } + assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"], :length => {:last_name => 10, :first_name => 20}) } + assert_nothing_raised { Person.connection.remove_index("people", ["last_name", "first_name"]) } end # quoting -- cgit v1.2.3 From 6626833db13a69786f9f6cd56b9f53c4017c3e39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 8 May 2010 17:44:40 +0300 Subject: Revert "Add index length support for MySQL [#1852 state:open]" This commit breaks dumping a few tables, as the sessions table. To reproduce, just create a new application and: rake db:sessions:create rake db:migrate rake db:test:prepare And then look at the db/schema.rb file (ht: Sam Ruby). This reverts commit 5b95730edc33ee97f53da26a3868eb983305a771. --- activerecord/test/cases/active_schema_test_mysql.rb | 17 ----------------- activerecord/test/cases/migration_test.rb | 8 -------- 2 files changed, 25 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/active_schema_test_mysql.rb b/activerecord/test/cases/active_schema_test_mysql.rb index f4d123be15..9aff538ce9 100644 --- a/activerecord/test/cases/active_schema_test_mysql.rb +++ b/activerecord/test/cases/active_schema_test_mysql.rb @@ -15,23 +15,6 @@ class ActiveSchemaTest < ActiveRecord::TestCase end end - def test_add_index - expected = "CREATE INDEX `index_people_on_last_name` ON `people` (`last_name`)" - assert_equal expected, add_index(:people, :last_name, :length => nil) - - expected = "CREATE INDEX `index_people_on_last_name` ON `people` (`last_name`(10))" - assert_equal expected, add_index(:people, :last_name, :length => 10) - - expected = "CREATE INDEX `index_people_on_last_name_and_first_name` ON `people` (`last_name`(15), `first_name`(15))" - assert_equal expected, add_index(:people, [:last_name, :first_name], :length => 15) - - expected = "CREATE INDEX `index_people_on_last_name_and_first_name` ON `people` (`last_name`(15), `first_name`)" - assert_equal expected, add_index(:people, [:last_name, :first_name], :length => {:last_name => 15}) - - expected = "CREATE INDEX `index_people_on_last_name_and_first_name` ON `people` (`last_name`(15), `first_name`(10))" - assert_equal expected, add_index(:people, [:last_name, :first_name], :length => {:last_name => 15, :first_name => 10}) - end - def test_drop_table assert_equal "DROP TABLE `people`", drop_table(:people) end diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index f67344445a..a3d1ceaa1f 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -92,14 +92,6 @@ if ActiveRecord::Base.connection.supports_migrations? assert_nothing_raised { Person.connection.remove_index("people", "last_name_and_first_name") } assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) } assert_nothing_raised { Person.connection.remove_index("people", ["last_name", "first_name"]) } - assert_nothing_raised { Person.connection.add_index("people", ["last_name"], :length => 10) } - assert_nothing_raised { Person.connection.remove_index("people", "last_name") } - assert_nothing_raised { Person.connection.add_index("people", ["last_name"], :length => {:last_name => 10}) } - assert_nothing_raised { Person.connection.remove_index("people", ["last_name"]) } - assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"], :length => 10) } - assert_nothing_raised { Person.connection.remove_index("people", ["last_name", "first_name"]) } - assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"], :length => {:last_name => 10, :first_name => 20}) } - assert_nothing_raised { Person.connection.remove_index("people", ["last_name", "first_name"]) } end # quoting -- cgit v1.2.3 From e17ff6d617abe3fa36c053eaed52e134074035a1 Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Sat, 8 May 2010 23:29:20 +0300 Subject: updated AR to work with the AMo model validation changes --- activerecord/test/cases/validations_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb index 937e08ac68..e1fb911cc9 100644 --- a/activerecord/test/cases/validations_test.rb +++ b/activerecord/test/cases/validations_test.rb @@ -44,7 +44,7 @@ class ValidationsTest < ActiveRecord::TestCase def test_error_on_create r = WrongReply.new r.title = "Wrong Create" - assert !r.valid? + assert !r.save assert r.errors[:title].any?, "A reply with a bad title should mark that attribute as invalid" assert_equal ["is Wrong Create"], r.errors[:title], "A reply with a bad content should contain an error" end -- cgit v1.2.3 From d916c62cfc7c59ab6411407a05b946d3dd7535e9 Mon Sep 17 00:00:00 2001 From: wycats Date: Sun, 9 May 2010 02:06:05 +0300 Subject: eliminate alias_method_chain from ActiveRecord --- activerecord/test/cases/locking_test.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/locking_test.rb b/activerecord/test/cases/locking_test.rb index aa2d9527f9..66874cdad1 100644 --- a/activerecord/test/cases/locking_test.rb +++ b/activerecord/test/cases/locking_test.rb @@ -195,7 +195,7 @@ class OptimisticLockingTest < ActiveRecord::TestCase assert_raises(ActiveRecord::RecordNotFound) { Person.find(p1.id) } assert_raises(ActiveRecord::RecordNotFound) { LegacyThing.find(t.id) } end - + def test_quote_table_name ref = references(:michael_magician) ref.favourite = !ref.favourite @@ -206,8 +206,11 @@ class OptimisticLockingTest < ActiveRecord::TestCase # is nothing else being updated. def test_update_without_attributes_does_not_only_update_lock_version assert_nothing_raised do - p1 = Person.new(:first_name => 'anika') - p1.send(:update_with_lock, []) + p1 = Person.create!(:first_name => 'anika') + lock_version = p1.lock_version + p1.save + p1.reload + assert_equal lock_version, p1.lock_version end end -- cgit v1.2.3