From f74a5616e89f03e13a025401475ac1101bb159ae Mon Sep 17 00:00:00 2001 From: Alan Kennedy Date: Sun, 25 Nov 2012 21:01:27 +0000 Subject: Save has_one associations only if record has changes Prevents save related callbacks such as `after_commit` being triggered when `has_one` objects are already persisted and have no changes. --- activerecord/test/cases/autosave_association_test.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index 517d2674a7..acb19032ea 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -626,10 +626,25 @@ class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase end end + @ship.pirate.catchphrase = "Changed Catchphrase" + assert_raise(RuntimeError) { assert !@pirate.save } assert_not_nil @pirate.reload.ship end + def test_should_save_changed_has_one_changed_object_if_child_is_saved + @pirate.ship.name = "NewName" + @pirate.ship.expects(:save).once.returns(true) + + assert @pirate.save + end + + def test_should_not_save_changed_has_one_unchanged_object_if_child_is_saved + @pirate.ship.expects(:save).never + + assert @pirate.save + end + # belongs_to def test_should_destroy_a_parent_association_as_part_of_the_save_transaction_if_it_was_marked_for_destroyal assert !@ship.pirate.marked_for_destruction? -- cgit v1.2.3 From 501ae92a32285528c8bbdcf2af422eadb6a63c0e Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Tue, 26 Nov 2013 04:59:18 +0900 Subject: Only use BINARY for mysql case sensitive uniqueness check when column has a case insensitive collation. --- .../cases/adapters/mysql/case_sensitivity_test.rb | 24 ++++++++++++++++++++-- .../cases/adapters/mysql2/case_sensitivity_test.rb | 24 ++++++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapters/mysql/case_sensitivity_test.rb b/activerecord/test/cases/adapters/mysql/case_sensitivity_test.rb index 97adb6b297..340fc95503 100644 --- a/activerecord/test/cases/adapters/mysql/case_sensitivity_test.rb +++ b/activerecord/test/cases/adapters/mysql/case_sensitivity_test.rb @@ -3,10 +3,10 @@ require 'models/person' class MysqlCaseSensitivityTest < ActiveRecord::TestCase class CollationTest < ActiveRecord::Base - validates_uniqueness_of :string_cs_column, :case_sensitive => false - validates_uniqueness_of :string_ci_column, :case_sensitive => false end + repair_validations(CollationTest) + def test_columns_include_collation_different_from_table assert_equal 'utf8_bin', CollationTest.columns_hash['string_cs_column'].collation assert_equal 'utf8_general_ci', CollationTest.columns_hash['string_ci_column'].collation @@ -18,6 +18,7 @@ class MysqlCaseSensitivityTest < ActiveRecord::TestCase end def test_case_insensitive_comparison_for_ci_column + CollationTest.validates_uniqueness_of(:string_ci_column, :case_sensitive => false) CollationTest.create!(:string_ci_column => 'A') invalid = CollationTest.new(:string_ci_column => 'a') queries = assert_sql { invalid.save } @@ -26,10 +27,29 @@ class MysqlCaseSensitivityTest < ActiveRecord::TestCase end def test_case_insensitive_comparison_for_cs_column + CollationTest.validates_uniqueness_of(:string_cs_column, :case_sensitive => false) CollationTest.create!(:string_cs_column => 'A') invalid = CollationTest.new(:string_cs_column => 'a') queries = assert_sql { invalid.save } cs_uniqueness_query = queries.detect { |q| q.match(/string_cs_column/) } assert_match(/lower/i, cs_uniqueness_query) end + + def test_case_sensitive_comparison_for_ci_column + CollationTest.validates_uniqueness_of(:string_ci_column, :case_sensitive => true) + CollationTest.create!(:string_ci_column => 'A') + invalid = CollationTest.new(:string_ci_column => 'A') + queries = assert_sql { invalid.save } + ci_uniqueness_query = queries.detect { |q| q.match(/string_ci_column/) } + assert_match(/binary/i, ci_uniqueness_query) + end + + def test_case_sensitive_comparison_for_cs_column + CollationTest.validates_uniqueness_of(:string_cs_column, :case_sensitive => true) + CollationTest.create!(:string_cs_column => 'A') + invalid = CollationTest.new(:string_cs_column => 'A') + queries = assert_sql { invalid.save } + cs_uniqueness_query = queries.detect { |q| q.match(/string_cs_column/) } + assert_no_match(/binary/i, cs_uniqueness_query) + end end diff --git a/activerecord/test/cases/adapters/mysql2/case_sensitivity_test.rb b/activerecord/test/cases/adapters/mysql2/case_sensitivity_test.rb index 6bcc113482..09bebf3071 100644 --- a/activerecord/test/cases/adapters/mysql2/case_sensitivity_test.rb +++ b/activerecord/test/cases/adapters/mysql2/case_sensitivity_test.rb @@ -3,10 +3,10 @@ require 'models/person' class Mysql2CaseSensitivityTest < ActiveRecord::TestCase class CollationTest < ActiveRecord::Base - validates_uniqueness_of :string_cs_column, :case_sensitive => false - validates_uniqueness_of :string_ci_column, :case_sensitive => false end + repair_validations(CollationTest) + def test_columns_include_collation_different_from_table assert_equal 'utf8_bin', CollationTest.columns_hash['string_cs_column'].collation assert_equal 'utf8_general_ci', CollationTest.columns_hash['string_ci_column'].collation @@ -18,6 +18,7 @@ class Mysql2CaseSensitivityTest < ActiveRecord::TestCase end def test_case_insensitive_comparison_for_ci_column + CollationTest.validates_uniqueness_of(:string_ci_column, :case_sensitive => false) CollationTest.create!(:string_ci_column => 'A') invalid = CollationTest.new(:string_ci_column => 'a') queries = assert_sql { invalid.save } @@ -26,10 +27,29 @@ class Mysql2CaseSensitivityTest < ActiveRecord::TestCase end def test_case_insensitive_comparison_for_cs_column + CollationTest.validates_uniqueness_of(:string_cs_column, :case_sensitive => false) CollationTest.create!(:string_cs_column => 'A') invalid = CollationTest.new(:string_cs_column => 'a') queries = assert_sql { invalid.save } cs_uniqueness_query = queries.detect { |q| q.match(/string_cs_column/)} assert_match(/lower/i, cs_uniqueness_query) end + + def test_case_sensitive_comparison_for_ci_column + CollationTest.validates_uniqueness_of(:string_ci_column, :case_sensitive => true) + CollationTest.create!(:string_ci_column => 'A') + invalid = CollationTest.new(:string_ci_column => 'A') + queries = assert_sql { invalid.save } + ci_uniqueness_query = queries.detect { |q| q.match(/string_ci_column/) } + assert_match(/binary/i, ci_uniqueness_query) + end + + def test_case_sensitive_comparison_for_cs_column + CollationTest.validates_uniqueness_of(:string_cs_column, :case_sensitive => true) + CollationTest.create!(:string_cs_column => 'A') + invalid = CollationTest.new(:string_cs_column => 'A') + queries = assert_sql { invalid.save } + cs_uniqueness_query = queries.detect { |q| q.match(/string_cs_column/) } + assert_no_match(/binary/i, cs_uniqueness_query) + end end -- cgit v1.2.3 From 6e53d92498ada27cea6c7bae85708fcf5579223c Mon Sep 17 00:00:00 2001 From: TheMonster Date: Wed, 26 Feb 2014 03:24:44 +0200 Subject: Fix a bug affecting validations of enum attributes This fixes a bug where any enum attribute of a model would be evaluated always as 0 when calling the database on validations. This fix converts the value of the enum attribute to its integer value rather than the string before building the relation as the bug occured when the string finally gets converted to integer using string.to_i which converts it to 0. [Vilius Luneckas, Ahmed AbouElhamayed] --- activerecord/test/cases/enum_test.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/enum_test.rb b/activerecord/test/cases/enum_test.rb index 1b95708cb3..f8ebd7caee 100644 --- a/activerecord/test/cases/enum_test.rb +++ b/activerecord/test/cases/enum_test.rb @@ -222,4 +222,31 @@ class EnumTest < ActiveRecord::TestCase end end end + + test "validate uniqueness" do + klass = Class.new(ActiveRecord::Base) do + def self.name; 'Book'; end + enum status: [:proposed, :written] + validates_uniqueness_of :status + end + klass.delete_all + klass.create!(status: "proposed") + book = klass.new(status: "written") + assert book.valid? + book.status = "proposed" + assert_not book.valid? + end + + test "validate inclusion of value in array" do + klass = Class.new(ActiveRecord::Base) do + def self.name; 'Book'; end + enum status: [:proposed, :written] + validates_inclusion_of :status, in: ["written"] + end + klass.delete_all + invalid_book = klass.new(status: "proposed") + assert_not invalid_book.valid? + valid_book = klass.new(status: "written") + assert valid_book.valid? + end end -- cgit v1.2.3 From d1e7cd14c29f1ef45f2f36e79cd99eb580036991 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Fri, 28 Feb 2014 09:49:52 +0100 Subject: `includes` uses SQL parsing when String joins are involved. This is a partial revert of 22b3481ba2aa55fad1f9a5db94072312b345fb55. The current implementation of `references_eager_loaded_tables?` needs to know every table involved in the query. With the current API this is not possible without SQL parsing. While a2dab46cae35a06fd5c5500037177492a047c252 deprecated SQL parsing for `includes`. It did not issue deprecation warnings when String joins are involved. This resulted in a breaking change after the deprecated behavior was removed (22b3481ba2aa55fad1f9a5db94072312b345fb55). We will need to rethink the usage of `includes`, `preload` and `eager_load` but for now, this brings back the old *working* behavior. --- activerecord/test/cases/associations/eager_test.rb | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 498a4e8144..5522a33b79 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -1194,4 +1194,13 @@ class EagerAssociationTest < ActiveRecord::TestCase authors(:david).essays.includes(:writer).any? end end + + test "preloading associations with string joins and order references" do + author = assert_queries(2) { + Author.includes(:posts).joins("LEFT JOIN posts ON posts.author_id = authors.id").order("posts.title DESC").first + } + assert_no_queries { + assert_equal 5, author.posts.size + } + end end -- cgit v1.2.3 From 774160b9ad6908435bf3485e7ac98633deff76c6 Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Fri, 28 Feb 2014 17:00:38 -0500 Subject: Remove unnecessary db call when replacing. When replacing a has_many association with the same one, there is no need to do a round-trip to the db to create/and drop a new transaction. [fixes #14220] --- .../test/cases/associations/has_many_associations_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index a86fb15719..49d3301044 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1242,6 +1242,16 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal orig_accounts, firm.accounts end + def test_replace_with_same_content + firm = Firm.first + firm.clients = [] + firm.save + + assert_queries(0, ignore_none: true) do + firm.clients = [] + end + end + def test_transactions_when_replacing_on_persisted good = Client.new(:name => "Good") bad = Client.new(:name => "Bad", :raise_on_save => true) -- cgit v1.2.3 From 398b4de011e5bbfd10408ed591e92f192cb37327 Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Mon, 3 Mar 2014 02:35:42 +0530 Subject: Fix warnings due to: - unused variable in PG Adapter. - Ambiguous argument warning from range_test for use - to + Infinity range without brackets. --- activerecord/test/cases/adapters/postgresql/range_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapters/postgresql/range_test.rb b/activerecord/test/cases/adapters/postgresql/range_test.rb index 306bb09a0d..4e84335e9b 100644 --- a/activerecord/test/cases/adapters/postgresql/range_test.rb +++ b/activerecord/test/cases/adapters/postgresql/range_test.rb @@ -156,7 +156,7 @@ _SQL assert_equal 0.5..0.7, @first_range.float_range assert_equal 0.5...0.7, @second_range.float_range assert_equal 0.5...Float::INFINITY, @third_range.float_range - assert_equal -Float::INFINITY...Float::INFINITY, @fourth_range.float_range + assert_equal (-Float::INFINITY...Float::INFINITY), @fourth_range.float_range assert_nil @empty_range.float_range end -- cgit v1.2.3 From 3413b88a3d6b2b022dfb57e42565446b1e024314 Mon Sep 17 00:00:00 2001 From: Erik Michaels-Ober Date: Mon, 3 Mar 2014 19:23:22 -0800 Subject: Replace map.flatten with flat_map in activerecord --- activerecord/test/cases/associations/callbacks_test.rb | 2 +- activerecord/test/cases/autosave_association_test.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/callbacks_test.rb b/activerecord/test/cases/associations/callbacks_test.rb index e555c52281..cf71bc1597 100644 --- a/activerecord/test/cases/associations/callbacks_test.rb +++ b/activerecord/test/cases/associations/callbacks_test.rb @@ -159,7 +159,7 @@ class AssociationCallbacksTest < ActiveRecord::TestCase activerecord.reload assert activerecord.developers_with_callbacks.size == 2 end - log_array = activerecord.developers_with_callbacks.collect {|d| ["before_removing#{d.id}","after_removing#{d.id}"]}.flatten.sort + log_array = activerecord.developers_with_callbacks.flat_map {|d| ["before_removing#{d.id}","after_removing#{d.id}"]}.sort assert activerecord.developers_with_callbacks.clear assert_equal log_array, activerecord.developers_log.sort end diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index d2f97df0fc..9651244372 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -76,9 +76,9 @@ class TestAutosaveAssociationsInGeneral < ActiveRecord::TestCase end def callbacks_for_model(model) - model.instance_variables.grep(/_callbacks$/).map do |ivar| + model.instance_variables.grep(/_callbacks$/).flat_map do |ivar| model.instance_variable_get(ivar) - end.flatten + end end end -- cgit v1.2.3 From f317cc8bc007978d7b135ddd1acdd7e3d1e582a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Schu=CC=88rrer?= Date: Mon, 3 Mar 2014 17:44:55 +0100 Subject: Make exists? use bound values. When we build a query with an inline value that is a numeric (e.g. because it's out of range for an int4) PostgreSQL doesn't use an index on the column, since it's now comparing numerics and not int4s. This leads to a _very_ slow query. When we use bound parameters instead of inline values PostgreSQL raises numeric_value_out_of_range since no automatic coercion happens. --- activerecord/test/cases/finder_test.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index b1eded6494..78c4e02434 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -58,15 +58,27 @@ class FinderTest < ActiveRecord::TestCase assert_equal false, Topic.exists?(45) assert_equal false, Topic.exists?(Topic.new) + assert_raise(NoMethodError) { Topic.exists?([1,2]) } + end + + def test_exists_fails_when_parameter_has_invalid_type + begin + assert_equal false, Topic.exists?(("9"*53).to_i) # number that's bigger than int + flunk if defined? ActiveRecord::ConnectionAdapters::PostgreSQLAdapter and Topic.connection.is_a? ActiveRecord::ConnectionAdapters::PostgreSQLAdapter # PostgreSQL does raise here + rescue ActiveRecord::StatementInvalid + # PostgreSQL complains that it can't coerce a numeric that's bigger than int into int + rescue Exception + flunk + end + begin assert_equal false, Topic.exists?("foo") + flunk if defined? ActiveRecord::ConnectionAdapters::PostgreSQLAdapter and Topic.connection.is_a? ActiveRecord::ConnectionAdapters::PostgreSQLAdapter # PostgreSQL does raise here rescue ActiveRecord::StatementInvalid # PostgreSQL complains about string comparison with integer field rescue Exception flunk end - - assert_raise(NoMethodError) { Topic.exists?([1,2]) } end def test_exists_does_not_select_columns_without_alias -- cgit v1.2.3 From 5c55aafd38f45ac019573f98438ffdbdc8c580f9 Mon Sep 17 00:00:00 2001 From: Dieter Komendera Date: Mon, 9 Dec 2013 18:52:36 +0100 Subject: Add Enum type to postgresql adapter's oids to prevent unknown OID warnings. --- .../test/cases/adapters/postgresql/enum_test.rb | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapters/postgresql/enum_test.rb b/activerecord/test/cases/adapters/postgresql/enum_test.rb index bf8dbd6c3f..a9a2901436 100644 --- a/activerecord/test/cases/adapters/postgresql/enum_test.rb +++ b/activerecord/test/cases/adapters/postgresql/enum_test.rb @@ -23,6 +23,8 @@ class PostgresqlEnumTest < ActiveRecord::TestCase t.column :current_mood, :mood end end + # reload type map after creating the enum type + @connection.send(:reload_type_map) end def test_enum_mapping @@ -35,4 +37,30 @@ class PostgresqlEnumTest < ActiveRecord::TestCase assert_equal "happy", enum.reload.current_mood end + + def test_invalid_enum_update + @connection.execute "INSERT INTO postgresql_enums VALUES (1, 'sad');" + enum = PostgresqlEnum.first + enum.current_mood = "angry" + + assert_raise ActiveRecord::StatementInvalid do + enum.save + end + end + + def test_no_oid_warning + @connection.execute "INSERT INTO postgresql_enums VALUES (1, 'sad');" + stderr_output = capture(:stderr) { + enum = PostgresqlEnum.first + } + + assert stderr_output.blank? + end + + def test_enum_type_cast + enum = PostgresqlEnum.new + enum.current_mood = :happy + + assert_equal "happy", enum.current_mood + end end -- cgit v1.2.3 From 6e66de6091f15bcd0c3cbec0d6a6b8ba66d227d3 Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Tue, 4 Mar 2014 18:06:19 +0530 Subject: Fix enum test unused variable warning. Related - https://github.com/rails/rails/commit/5c55aafd38f45ac019573f98438ffdbdc8c580f9 --- activerecord/test/cases/adapters/postgresql/enum_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapters/postgresql/enum_test.rb b/activerecord/test/cases/adapters/postgresql/enum_test.rb index a9a2901436..79cb47a02e 100644 --- a/activerecord/test/cases/adapters/postgresql/enum_test.rb +++ b/activerecord/test/cases/adapters/postgresql/enum_test.rb @@ -51,7 +51,7 @@ class PostgresqlEnumTest < ActiveRecord::TestCase def test_no_oid_warning @connection.execute "INSERT INTO postgresql_enums VALUES (1, 'sad');" stderr_output = capture(:stderr) { - enum = PostgresqlEnum.first + PostgresqlEnum.first } assert stderr_output.blank? -- cgit v1.2.3 From 8ed0f542ddd1f38b0b42f595d828d2e4db9b6682 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Tue, 4 Mar 2014 09:50:39 -0300 Subject: Inline block to fix indent [ci skip] --- activerecord/test/cases/adapters/postgresql/enum_test.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapters/postgresql/enum_test.rb b/activerecord/test/cases/adapters/postgresql/enum_test.rb index 79cb47a02e..62f84caf91 100644 --- a/activerecord/test/cases/adapters/postgresql/enum_test.rb +++ b/activerecord/test/cases/adapters/postgresql/enum_test.rb @@ -50,9 +50,7 @@ class PostgresqlEnumTest < ActiveRecord::TestCase def test_no_oid_warning @connection.execute "INSERT INTO postgresql_enums VALUES (1, 'sad');" - stderr_output = capture(:stderr) { - PostgresqlEnum.first - } + stderr_output = capture(:stderr) { PostgresqlEnum.first } assert stderr_output.blank? end -- cgit v1.2.3 From acbd7ab22e5d9179487bd98110234c54535036c4 Mon Sep 17 00:00:00 2001 From: Marcelo Casiraghi Date: Thu, 23 May 2013 00:17:15 -0300 Subject: Allow string hash values on AR order method This behavior has almost no performance impact: String not allowed 66.910000 0.030000 66.940000 ( 67.024976) String allowed 69.360000 0.030000 69.390000 ( 69.503096) Benchmarked with http://git.io/Y0YuRw. --- activerecord/test/cases/relations_test.rb | 38 +++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 8718110c36..a08171cfcc 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -171,7 +171,6 @@ class RelationTest < ActiveRecord::TestCase assert_equal topics(:first).title, topics.first.title end - def test_finding_with_arel_order topics = Topic.order(Topic.arel_table[:id].asc) assert_equal 5, topics.to_a.size @@ -194,8 +193,43 @@ class RelationTest < ActiveRecord::TestCase assert_equal Topic.order(:id).to_sql, Topic.order(:id => :asc).to_sql end + def test_finding_with_desc_order_with_string + topics = Topic.order(id: "desc") + assert_equal 5, topics.to_a.size + assert_equal [topics(:fifth), topics(:fourth), topics(:third), topics(:second), topics(:first)], topics.to_a + end + + def test_finding_with_asc_order_with_string + topics = Topic.order(id: 'asc') + assert_equal 5, topics.to_a.size + assert_equal [topics(:first), topics(:second), topics(:third), topics(:fourth), topics(:fifth)], topics.to_a + end + + def test_nothing_raises_on_upcase_desc_arg + Topic.order(id: "DESC") + end + + def test_nothing_raises_on_downcase_desc_arg + Topic.order(id: "desc") + end + + def test_nothing_raises_on_upcase_asc_arg + Topic.order(id: "ASC") + end + + def test_nothing_raises_on_downcase_asc_arg + Topic.order(id: "asc") + end + + def test_nothing_raises_on_case_insensitive_args + Topic.order(id: "DeSc") + Topic.order(id: :DeSc) + Topic.order(id: "aSc") + Topic.order(id: :aSc) + end + def test_raising_exception_on_invalid_hash_params - assert_raise(ArgumentError) { Topic.order(:name, "id DESC", :id => :DeSc) } + assert_raise(ArgumentError) { Topic.order(:name, "id DESC", id: :asfsdf) } end def test_finding_last_with_arel_order -- cgit v1.2.3 From f6aeb8b1a3687c8523e4a56309fe3736011b2935 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Wed, 5 Mar 2014 10:24:13 +0100 Subject: we only need to support `asc` and `ASC`. No need for mixed cases. #14263 This is a result of the discussion at https://github.com/rails/rails/pull/14263/files#r10291489 --- activerecord/test/cases/relations_test.rb | 32 +++++++++++-------------------- 1 file changed, 11 insertions(+), 21 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index a08171cfcc..72b96dd3e9 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -205,31 +205,21 @@ class RelationTest < ActiveRecord::TestCase assert_equal [topics(:first), topics(:second), topics(:third), topics(:fourth), topics(:fifth)], topics.to_a end - def test_nothing_raises_on_upcase_desc_arg - Topic.order(id: "DESC") - end - - def test_nothing_raises_on_downcase_desc_arg - Topic.order(id: "desc") - end - - def test_nothing_raises_on_upcase_asc_arg - Topic.order(id: "ASC") - end - - def test_nothing_raises_on_downcase_asc_arg - Topic.order(id: "asc") - end + def test_support_upper_and_lower_case_directions + assert_includes Topic.order(id: "ASC").to_sql, "ASC" + assert_includes Topic.order(id: "asc").to_sql, "ASC" + assert_includes Topic.order(id: :ASC).to_sql, "ASC" + assert_includes Topic.order(id: :asc).to_sql, "ASC" - def test_nothing_raises_on_case_insensitive_args - Topic.order(id: "DeSc") - Topic.order(id: :DeSc) - Topic.order(id: "aSc") - Topic.order(id: :aSc) + assert_includes Topic.order(id: "DESC").to_sql, "DESC" + assert_includes Topic.order(id: "desc").to_sql, "DESC" + assert_includes Topic.order(id: :DESC).to_sql, "DESC" + assert_includes Topic.order(id: :desc).to_sql,"DESC" end def test_raising_exception_on_invalid_hash_params - assert_raise(ArgumentError) { Topic.order(:name, "id DESC", id: :asfsdf) } + e = assert_raise(ArgumentError) { Topic.order(:name, "id DESC", id: :asfsdf) } + assert_equal 'Direction "asfsdf" is invalid. Valid directions are: [:asc, :desc, :ASC, :DESC, "asc", "desc", "ASC", "DESC"]', e.message end def test_finding_last_with_arel_order -- cgit v1.2.3 From 5ade4b05933aa986ff130d0f1f881a6d060db6be Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Sun, 9 Mar 2014 13:16:01 -0700 Subject: Cleanup Parrot after each test. --- activerecord/test/cases/autosave_association_test.rb | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index 9651244372..f7584c3a51 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -64,10 +64,6 @@ class TestAutosaveAssociationsInGeneral < ActiveRecord::TestCase private - def base - ActiveRecord::Base - end - def assert_no_difference_when_adding_callbacks_twice_for(model, association_name) reflection = model.reflect_on_association(association_name) assert_no_difference "callbacks_for_model(#{model.name}).length" do @@ -622,15 +618,15 @@ end class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase self.use_transactional_fixtures = false - def setup - super + setup do @pirate = Pirate.create(:catchphrase => "Don' botharrr talkin' like one, savvy?") @ship = @pirate.create_ship(:name => 'Nights Dirty Lightning') end - def teardown + teardown do # We are running without transactional fixtures and need to cleanup. Bird.delete_all + Parrot.delete_all @ship.delete @pirate.delete end -- cgit v1.2.3 From 18b9595814057095084f508b6837ad3c7331079f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 10 Mar 2014 10:20:27 -0300 Subject: Do proper adapter check --- activerecord/test/cases/finder_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 78c4e02434..0ab3830323 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -64,7 +64,7 @@ class FinderTest < ActiveRecord::TestCase def test_exists_fails_when_parameter_has_invalid_type begin assert_equal false, Topic.exists?(("9"*53).to_i) # number that's bigger than int - flunk if defined? ActiveRecord::ConnectionAdapters::PostgreSQLAdapter and Topic.connection.is_a? ActiveRecord::ConnectionAdapters::PostgreSQLAdapter # PostgreSQL does raise here + flunk if current_adapter?(:PostgreSQLAdapter) # PostgreSQL does raise here rescue ActiveRecord::StatementInvalid # PostgreSQL complains that it can't coerce a numeric that's bigger than int into int rescue Exception @@ -73,7 +73,7 @@ class FinderTest < ActiveRecord::TestCase begin assert_equal false, Topic.exists?("foo") - flunk if defined? ActiveRecord::ConnectionAdapters::PostgreSQLAdapter and Topic.connection.is_a? ActiveRecord::ConnectionAdapters::PostgreSQLAdapter # PostgreSQL does raise here + flunk if current_adapter?(:PostgreSQLAdapter) # PostgreSQL does raise here rescue ActiveRecord::StatementInvalid # PostgreSQL complains about string comparison with integer field rescue Exception -- cgit v1.2.3 From d547b2375465ec1a669f01386e988d32845eaa4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 10 Mar 2014 10:23:17 -0300 Subject: Change the assertions depending in the database adapter This will avoid the confusing flunk logic --- activerecord/test/cases/finder_test.rb | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 0ab3830323..0da2fe41c5 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -62,22 +62,17 @@ class FinderTest < ActiveRecord::TestCase end def test_exists_fails_when_parameter_has_invalid_type - begin - assert_equal false, Topic.exists?(("9"*53).to_i) # number that's bigger than int - flunk if current_adapter?(:PostgreSQLAdapter) # PostgreSQL does raise here - rescue ActiveRecord::StatementInvalid - # PostgreSQL complains that it can't coerce a numeric that's bigger than int into int - rescue Exception - flunk - end + if current_adapter?(:PostgreSQLAdapter) + assert_raises ActiveRecord::StatementInvalid do + Topic.exists?(("9"*53).to_i) # number that's bigger than int + end - begin + assert_raises ActiveRecord::StatementInvalid do + Topic.exists?("foo") + end + else + assert_equal false, Topic.exists?(("9"*53).to_i) # number that's bigger than int assert_equal false, Topic.exists?("foo") - flunk if current_adapter?(:PostgreSQLAdapter) # PostgreSQL does raise here - rescue ActiveRecord::StatementInvalid - # PostgreSQL complains about string comparison with integer field - rescue Exception - flunk end end -- cgit v1.2.3 From d50f75367bbd1baf52a8c9c246e7b27d6d9addff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 10 Mar 2014 11:07:10 -0300 Subject: mysql adapter also fails with a number bigger than int --- activerecord/test/cases/finder_test.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 0da2fe41c5..98b1dafdf0 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -62,16 +62,19 @@ class FinderTest < ActiveRecord::TestCase end def test_exists_fails_when_parameter_has_invalid_type - if current_adapter?(:PostgreSQLAdapter) + if current_adapter?(:PostgreSQLAdapter, :MysqlAdapter) assert_raises ActiveRecord::StatementInvalid do Topic.exists?(("9"*53).to_i) # number that's bigger than int end + else + assert_equal false, Topic.exists?(("9"*53).to_i) # number that's bigger than int + end + if current_adapter?(:PostgreSQLAdapter) assert_raises ActiveRecord::StatementInvalid do Topic.exists?("foo") end else - assert_equal false, Topic.exists?(("9"*53).to_i) # number that's bigger than int assert_equal false, Topic.exists?("foo") end end -- cgit v1.2.3 From 39e07b64ce3f4bb55e60ba0266e677f8e4f4893a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 10 Mar 2014 11:09:39 -0300 Subject: current_adapter? accepts multiple arguments --- activerecord/test/cases/schema_dumper_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index c085663efb..2748cbdbf4 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -177,7 +177,7 @@ class SchemaDumperTest < ActiveRecord::TestCase def test_schema_dumps_index_columns_in_right_order index_definition = standard_dump.split(/\n/).grep(/add_index.*companies/).first.strip - if current_adapter?(:MysqlAdapter) || current_adapter?(:Mysql2Adapter) || current_adapter?(:PostgreSQLAdapter) + if current_adapter?(:MysqlAdapter, :Mysql2Adapter, :PostgreSQLAdapter) assert_equal 'add_index "companies", ["firm_id", "type", "rating"], name: "company_index", using: :btree', index_definition else assert_equal 'add_index "companies", ["firm_id", "type", "rating"], name: "company_index"', index_definition @@ -188,7 +188,7 @@ class SchemaDumperTest < ActiveRecord::TestCase index_definition = standard_dump.split(/\n/).grep(/add_index.*company_partial_index/).first.strip if current_adapter?(:PostgreSQLAdapter) assert_equal 'add_index "companies", ["firm_id", "type"], name: "company_partial_index", where: "(rating > 10)", using: :btree', index_definition - elsif current_adapter?(:MysqlAdapter) || current_adapter?(:Mysql2Adapter) + elsif current_adapter?(:MysqlAdapter, :Mysql2Adapter) assert_equal 'add_index "companies", ["firm_id", "type"], name: "company_partial_index", using: :btree', index_definition elsif current_adapter?(:SQLite3Adapter) && ActiveRecord::Base.connection.supports_partial_index? assert_equal 'add_index "companies", ["firm_id", "type"], name: "company_partial_index", where: "rating > 10"', index_definition -- cgit v1.2.3 From e5f15a83656f6d005dab40a30fb9f7fa2cf65d77 Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Mon, 10 Mar 2014 11:37:33 -0400 Subject: Fixes STI when 2+ levels deep. PR #14052 Added a regression where it was only looking for methods in one level up, So when the method was defined in a 2+ levels up the inheritance chain, the method was not found as defined. --- activerecord/test/cases/attribute_methods_test.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index 1a1e442df0..173081cb28 100644 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -746,6 +746,19 @@ class AttributeMethodsTest < ActiveRecord::TestCase assert "unknown attribute: hello", error.message end + def test_methods_override_in_multi_level_subclass + klass = Class.new(Developer) do + def name + "dev:#{read_attribute(:name)}" + end + end + + 2.times { klass = Class.new klass } + dev = klass.new(name: 'arthurnn') + dev.save! + assert_equal 'dev:arthurnn', dev.reload.name + end + def test_global_methods_are_overwritten klass = Class.new(ActiveRecord::Base) do self.table_name = 'computers' -- cgit v1.2.3 From b3e0da3062f809599fb4da9f9a29ed45aebd1ff6 Mon Sep 17 00:00:00 2001 From: lsylvester Date: Tue, 11 Mar 2014 08:10:25 +1100 Subject: register OID for PostgreSQL citex datatype [Troy Kruthoff & Lachlan Sylvester] citext makes it possible to use AR Hash finders for case-insensitive matching as sql UPPER/LOWER functions are not needed. --- .../test/cases/adapters/postgresql/citext_test.rb | 58 ++++++++++++++++++++++ activerecord/test/cases/schema_dumper_test.rb | 7 +++ .../test/schema/postgresql_specific_schema.rb | 11 +++- 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 activerecord/test/cases/adapters/postgresql/citext_test.rb (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapters/postgresql/citext_test.rb b/activerecord/test/cases/adapters/postgresql/citext_test.rb new file mode 100644 index 0000000000..89ef46b539 --- /dev/null +++ b/activerecord/test/cases/adapters/postgresql/citext_test.rb @@ -0,0 +1,58 @@ +# encoding: utf-8 + +require 'cases/helper' +require 'active_record/base' +require 'active_record/connection_adapters/postgresql_adapter' + +class PostgresqlCitextTest < ActiveRecord::TestCase + class Citext < ActiveRecord::Base + self.table_name = 'citexts' + end + + def setup + @connection = ActiveRecord::Base.connection + + unless @connection.extension_enabled?('citext') + @connection.enable_extension 'citext' + @connection.commit_db_transaction + end + + @connection.reconnect! + + @connection.transaction do + @connection.create_table('citexts') do |t| + t.citext 'cival' + end + end + @column = Citext.columns_hash['cival'] + end + + def teardown + @connection.execute 'DROP TABLE IF EXISTS citexts;' + @connection.execute 'DROP EXTENSION IF EXISTS citext CASCADE;' + end + + def test_citext_enabled + assert @connection.extension_enabled?('citext') + end + + def test_column_type + assert_equal :citext, @column.type + end + + def test_column_sql_type + assert_equal 'citext', @column.sql_type + end + + def test_write + x = Citext.new(cival: 'Some CI Text') + assert x.save! + end + + def test_select_case_insensitive + @connection.execute "insert into citexts (cival) values('Cased Text')" + x = Citext.where(cival: 'cased text').first + assert_equal('Cased Text', x.cival) + end + +end \ No newline at end of file diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index 2748cbdbf4..575eb34a9c 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -319,6 +319,13 @@ class SchemaDumperTest < ActiveRecord::TestCase end end + def test_schema_dump_includes_citext_shorthand_definition + output = standard_dump + if %r{create_table "postgresql_citext"} =~ output + assert_match %r[t.citext "text_citext"], output + end + end + def test_schema_dump_includes_ltrees_shorthand_definition output = standard_dump if %r{create_table "postgresql_ltrees"} =~ output diff --git a/activerecord/test/schema/postgresql_specific_schema.rb b/activerecord/test/schema/postgresql_specific_schema.rb index a86a188bcf..4fcbf4dbd2 100644 --- a/activerecord/test/schema/postgresql_specific_schema.rb +++ b/activerecord/test/schema/postgresql_specific_schema.rb @@ -1,7 +1,7 @@ ActiveRecord::Schema.define do %w(postgresql_tsvectors postgresql_hstores postgresql_arrays postgresql_moneys postgresql_numbers postgresql_times postgresql_network_addresses postgresql_bit_strings postgresql_uuids postgresql_ltrees - postgresql_oids postgresql_xml_data_type defaults geometrics postgresql_timestamp_with_zones postgresql_partitioned_table postgresql_partitioned_table_parent postgresql_json_data_type).each do |table_name| + postgresql_oids postgresql_xml_data_type defaults geometrics postgresql_timestamp_with_zones postgresql_partitioned_table postgresql_partitioned_table_parent postgresql_json_data_type postgresql_citext).each do |table_name| execute "DROP TABLE IF EXISTS #{quote_table_name table_name}" end @@ -99,6 +99,15 @@ _SQL _SQL end + if 't' == select_value("select 'citext'=ANY(select typname from pg_type)") + execute <<_SQL + CREATE TABLE postgresql_citext ( + id SERIAL PRIMARY KEY, + text_citext citext default ''::citext + ); +_SQL + end + if 't' == select_value("select 'json'=ANY(select typname from pg_type)") execute <<_SQL CREATE TABLE postgresql_json_data_type ( -- cgit v1.2.3 From 3f5339f48e3ce3eb40eb51fb1b686914a719a26a Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Tue, 11 Mar 2014 08:27:59 +0100 Subject: `change_table` supports `citext`. Follow up to #12523. --- .../test/cases/adapters/postgresql/citext_test.rb | 35 +++++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapters/postgresql/citext_test.rb b/activerecord/test/cases/adapters/postgresql/citext_test.rb index 89ef46b539..ebb9ff98b1 100644 --- a/activerecord/test/cases/adapters/postgresql/citext_test.rb +++ b/activerecord/test/cases/adapters/postgresql/citext_test.rb @@ -19,10 +19,8 @@ class PostgresqlCitextTest < ActiveRecord::TestCase @connection.reconnect! - @connection.transaction do - @connection.create_table('citexts') do |t| - t.citext 'cival' - end + @connection.create_table('citexts') do |t| + t.citext 'cival' end @column = Citext.columns_hash['cival'] end @@ -44,15 +42,36 @@ class PostgresqlCitextTest < ActiveRecord::TestCase assert_equal 'citext', @column.sql_type end + def test_change_table_supports_json + @connection.transaction do + @connection.change_table('citexts') do |t| + t.citext 'username' + end + Citext.reset_column_information + column = Citext.columns.find { |c| c.name == 'username' } + assert_equal :citext, column.type + + raise ActiveRecord::Rollback # reset the schema change + end + ensure + Citext.reset_column_information + end + def test_write x = Citext.new(cival: 'Some CI Text') - assert x.save! + x.save! + citext = Citext.first + assert_equal "Some CI Text", citext.cival + + citext.cival = "Some NEW CI Text" + citext.save! + + assert_equal "Some NEW CI Text", citext.reload.cival end def test_select_case_insensitive @connection.execute "insert into citexts (cival) values('Cased Text')" x = Citext.where(cival: 'cased text').first - assert_equal('Cased Text', x.cival) + assert_equal 'Cased Text', x.cival end - -end \ No newline at end of file +end -- cgit v1.2.3 From 3c23f768440659c7c4eb20cea82032f71ef5556d Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Tue, 11 Mar 2014 14:07:29 -0400 Subject: Remove mocking on save, when not necessary --- activerecord/test/cases/autosave_association_test.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index 8e3825999d..09892d50ba 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -691,14 +691,12 @@ class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase def test_should_save_changed_has_one_changed_object_if_child_is_saved @pirate.ship.name = "NewName" - @pirate.ship.expects(:save).once.returns(true) - assert @pirate.save + assert_equal "NewName", @pirate.ship.reload.name end def test_should_not_save_changed_has_one_unchanged_object_if_child_is_saved @pirate.ship.expects(:save).never - assert @pirate.save end -- cgit v1.2.3 From 5a934e6787337e51d8174669ea0ca6e0c9ea8081 Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Wed, 12 Mar 2014 09:06:12 -0400 Subject: Test microsecond on mysql 5.6 --- activerecord/test/cases/base_test.rb | 2 +- activerecord/test/cases/helper.rb | 5 +++++ activerecord/test/schema/schema.rb | 6 +++++- 3 files changed, 11 insertions(+), 2 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 8a0b0b9589..6acb342d0b 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -212,7 +212,7 @@ class BasicsTest < ActiveRecord::TestCase ) # For adapters which support microsecond resolution. - if current_adapter?(:PostgreSQLAdapter, :SQLite3Adapter) + if current_adapter?(:PostgreSQLAdapter, :SQLite3Adapter) || mysql_56? assert_equal 11, Topic.find(1).written_on.sec assert_equal 223300, Topic.find(1).written_on.usec assert_equal 9900, Topic.find(2).written_on.usec diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index 3758224b0c..8a49dfbb44 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -41,6 +41,11 @@ def in_memory_db? ActiveRecord::Base.connection_pool.spec.config[:database] == ":memory:" end +def mysql_56? + current_adapter?(:Mysql2Adapter) && + ActiveRecord::Base.connection.send(:version).join(".") >= "5.6.0" +end + def supports_savepoints? ActiveRecord::Base.connection.supports_savepoints? end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 5f2d6acbcb..b44e72a67c 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -674,7 +674,11 @@ ActiveRecord::Schema.define do t.string :title t.string :author_name t.string :author_email_address - t.datetime :written_on + if mysql_56? + t.datetime :written_on, limit: 6 + else + t.datetime :written_on + end t.time :bonus_time t.date :last_read # use VARCHAR2(4000) instead of CLOB datatype as CLOB data type has many limitations in -- cgit v1.2.3 From 66d61ab63b8f0d6acb42a3343507b44b4591caae Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Wed, 12 Mar 2014 11:50:50 -0400 Subject: Unit test for mysql quote time usec --- activerecord/test/cases/adapters/mysql2/connection_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapters/mysql2/connection_test.rb b/activerecord/test/cases/adapters/mysql2/connection_test.rb index 9b7202c915..98febd2d75 100644 --- a/activerecord/test/cases/adapters/mysql2/connection_test.rb +++ b/activerecord/test/cases/adapters/mysql2/connection_test.rb @@ -97,6 +97,13 @@ class MysqlConnectionTest < ActiveRecord::TestCase @connection.execute "DROP TABLE `bar_baz`" end + if mysql_56? + def test_quote_time_usec + assert_equal "'1970-01-01 00:00:00.000000'", @connection.quote(Time.at(0)) + assert_equal "'1970-01-01 00:00:00.000000'", @connection.quote(Time.at(0).to_datetime) + end + end + private def run_without_connection -- cgit v1.2.3 From 7058321725ba0054390a65d8f6343613df31eea9 Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Wed, 12 Mar 2014 11:52:39 -0400 Subject: Change usec to 0 on tests that compare seconds Avoid rounding problems with `.usec` method rounding the seconds when the field doesn't persist the `.usec` piece. --- activerecord/test/cases/persistence_test.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb index b9f0624f76..046fe83e54 100644 --- a/activerecord/test/cases/persistence_test.rb +++ b/activerecord/test/cases/persistence_test.rb @@ -452,7 +452,7 @@ class PersistenceTest < ActiveRecord::TestCase def test_update_attribute_for_updated_at_on developer = Developer.find(1) - prev_month = Time.now.prev_month + prev_month = Time.now.prev_month.change(usec: 0) developer.update_attribute(:updated_at, prev_month) assert_equal prev_month, developer.updated_at @@ -523,7 +523,7 @@ class PersistenceTest < ActiveRecord::TestCase def test_update_column_should_not_modify_updated_at developer = Developer.find(1) - prev_month = Time.now.prev_month + prev_month = Time.now.prev_month.change(usec: 0) developer.update_column(:updated_at, prev_month) assert_equal prev_month, developer.updated_at @@ -620,7 +620,7 @@ class PersistenceTest < ActiveRecord::TestCase def test_update_columns_should_not_modify_updated_at developer = Developer.find(1) - prev_month = Time.now.prev_month + prev_month = Time.now.prev_month.change(usec: 0) developer.update_columns(updated_at: prev_month) assert_equal prev_month, developer.updated_at -- cgit v1.2.3