From fbebdb0c091c37b0bc75ab774d187d8bc8795bd2 Mon Sep 17 00:00:00 2001 From: Frederick Cheung Date: Fri, 2 May 2008 00:00:42 +0100 Subject: Ensure correct record is returned when preloading has_one where more than one row exists Signed-off-by: Michael Koziarski [#73 state:closed] --- activerecord/test/cases/associations/eager_test.rb | 4 ++++ activerecord/test/models/post.rb | 2 ++ 2 files changed, 6 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 546ed80894..67b57ceb42 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -29,6 +29,10 @@ class EagerAssociationTest < ActiveRecord::TestCase post = Post.find(:first, :include => :comments, :conditions => "posts.title = 'Welcome to the weblog'") assert_equal 2, post.comments.size assert post.comments.include?(comments(:greetings)) + + posts = Post.find(:all, :include => :last_comment) + post = posts.find { |p| p.id == 1 } + assert_equal Post.find(1).last_comment, post.last_comment end def test_loading_conditions_with_or diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index 22c5a645b8..d9101706b5 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -9,6 +9,8 @@ class Post < ActiveRecord::Base belongs_to :author_with_posts, :class_name => "Author", :foreign_key => :author_id, :include => :posts + has_one :last_comment, :class_name => 'Comment', :order => 'id desc' + has_many :comments, :order => "body" do def find_most_recent find(:first, :order => "id DESC") -- cgit v1.2.3 From 8ded457b1b31b157d6fe89b553749579e5ac4a27 Mon Sep 17 00:00:00 2001 From: John Devine Date: Sat, 3 May 2008 22:49:18 -0500 Subject: Added logic to associations.rb to make sure select_for_limited_ids includes joins that are needed to reach tables listed in the :order or :conditions options if they are not joined directly to the main active_record table. Signed-off-by: Michael Koziarski [#109 state:resolved] --- activerecord/test/cases/finder_test.rb | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index b7f87fe6e8..2acfe9b387 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -8,6 +8,7 @@ require 'models/entrant' require 'models/developer' require 'models/post' require 'models/customer' +require 'models/job' class FinderTest < ActiveRecord::TestCase fixtures :companies, :topics, :entrants, :developers, :developers_projects, :posts, :comments, :accounts, :authors, :customers @@ -857,6 +858,14 @@ class FinderTest < ActiveRecord::TestCase Company.connection.select_rows("SELECT id, name FROM companies WHERE id IN (1,2,3) ORDER BY id").map! {|i| i.map! {|j| j.to_s unless j.nil?}} end + def test_find_with_order_on_included_associations_with_construct_finder_sql_for_association_limiting_and_is_distinct + assert_equal 2, Post.find(:all,:include=>{:authors=>:author_address},:order=>' author_addresses.id DESC ', :limit=>2).size + + assert_equal 3, Post.find(:all,:include=>{:author=>:author_address,:authors=>:author_address}, + :order=>' author_addresses_authors.id DESC ', :limit=>3).size + end + + protected def bind(statement, *vars) if vars.first.is_a?(Hash) -- cgit v1.2.3 From 8877ab5852d9a1133eb9a730ae47dde214bafe55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tarmo=20T=C3=A4nav?= Date: Wed, 7 May 2008 02:08:23 +0300 Subject: Added AbstractAdapter#table_exists? and made AbstractAdapter#table implementation non-optional Signed-off-by: Michael Koziarski --- activerecord/test/cases/adapter_test.rb | 19 +-- activerecord/test/cases/schema_dumper_test.rb | 211 +++++++++++++------------- 2 files changed, 114 insertions(+), 116 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapter_test.rb b/activerecord/test/cases/adapter_test.rb index 8a74b6d6f5..91504af901 100644 --- a/activerecord/test/cases/adapter_test.rb +++ b/activerecord/test/cases/adapter_test.rb @@ -6,15 +6,16 @@ class AdapterTest < ActiveRecord::TestCase end def test_tables - if @connection.respond_to?(:tables) - tables = @connection.tables - assert tables.include?("accounts") - assert tables.include?("authors") - assert tables.include?("tasks") - assert tables.include?("topics") - else - warn "#{@connection.class} does not respond to #tables" - end + tables = @connection.tables + assert tables.include?("accounts") + assert tables.include?("authors") + assert tables.include?("tasks") + assert tables.include?("topics") + end + + def test_table_exists? + assert @connection.table_exists?("accounts") + assert !@connection.table_exists?("nonexistingtable") end def test_indexes diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index ba8bff3b44..c42b0efba0 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -2,140 +2,137 @@ require "cases/helper" require 'active_record/schema_dumper' require 'stringio' -if ActiveRecord::Base.connection.respond_to?(:tables) - class SchemaDumperTest < ActiveRecord::TestCase - def standard_dump - stream = StringIO.new - ActiveRecord::SchemaDumper.ignore_tables = [] - ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) - stream.string - end +class SchemaDumperTest < ActiveRecord::TestCase + def standard_dump + stream = StringIO.new + ActiveRecord::SchemaDumper.ignore_tables = [] + ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) + stream.string + end - def test_schema_dump - output = standard_dump - assert_match %r{create_table "accounts"}, output - assert_match %r{create_table "authors"}, output - assert_no_match %r{create_table "schema_migrations"}, output - end + def test_schema_dump + output = standard_dump + assert_match %r{create_table "accounts"}, output + assert_match %r{create_table "authors"}, output + assert_no_match %r{create_table "schema_migrations"}, output + end - def test_schema_dump_excludes_sqlite_sequence - output = standard_dump - assert_no_match %r{create_table "sqlite_sequence"}, output - end + def test_schema_dump_excludes_sqlite_sequence + output = standard_dump + assert_no_match %r{create_table "sqlite_sequence"}, output + end - def assert_line_up(lines, pattern, required = false) - return assert(true) if lines.empty? - matches = lines.map { |line| line.match(pattern) } - assert matches.all? if required - matches.compact! - return assert(true) if matches.empty? - assert_equal 1, matches.map{ |match| match.offset(0).first }.uniq.length - end + def assert_line_up(lines, pattern, required = false) + return assert(true) if lines.empty? + matches = lines.map { |line| line.match(pattern) } + assert matches.all? if required + matches.compact! + return assert(true) if matches.empty? + assert_equal 1, matches.map{ |match| match.offset(0).first }.uniq.length + end - def column_definition_lines(output = standard_dump) - output.scan(/^( *)create_table.*?\n(.*?)^\1end/m).map{ |m| m.last.split(/\n/) } - end + def column_definition_lines(output = standard_dump) + output.scan(/^( *)create_table.*?\n(.*?)^\1end/m).map{ |m| m.last.split(/\n/) } + end - def test_types_line_up - column_definition_lines.each do |column_set| - next if column_set.empty? + def test_types_line_up + column_definition_lines.each do |column_set| + next if column_set.empty? - lengths = column_set.map do |column| - if match = column.match(/t\.(?:integer|decimal|float|datetime|timestamp|time|date|text|binary|string|boolean)\s+"/) - match[0].length - end + lengths = column_set.map do |column| + if match = column.match(/t\.(?:integer|decimal|float|datetime|timestamp|time|date|text|binary|string|boolean)\s+"/) + match[0].length end - - assert_equal 1, lengths.uniq.length end - end - def test_arguments_line_up - column_definition_lines.each do |column_set| - assert_line_up(column_set, /:default => /) - assert_line_up(column_set, /:limit => /) - assert_line_up(column_set, /:null => /) - end + assert_equal 1, lengths.uniq.length end + end - def test_no_dump_errors - output = standard_dump - assert_no_match %r{\# Could not dump table}, output + def test_arguments_line_up + column_definition_lines.each do |column_set| + assert_line_up(column_set, /:default => /) + assert_line_up(column_set, /:limit => /) + assert_line_up(column_set, /:null => /) end + end - def test_schema_dump_includes_not_null_columns - stream = StringIO.new + def test_no_dump_errors + output = standard_dump + assert_no_match %r{\# Could not dump table}, output + end - ActiveRecord::SchemaDumper.ignore_tables = [/^[^r]/] - ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) - output = stream.string - assert_match %r{:null => false}, output - end + def test_schema_dump_includes_not_null_columns + stream = StringIO.new - def test_schema_dump_with_string_ignored_table - stream = StringIO.new + ActiveRecord::SchemaDumper.ignore_tables = [/^[^r]/] + ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) + output = stream.string + assert_match %r{:null => false}, output + end - ActiveRecord::SchemaDumper.ignore_tables = ['accounts'] - ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) - output = stream.string - assert_no_match %r{create_table "accounts"}, output - assert_match %r{create_table "authors"}, output - assert_no_match %r{create_table "schema_migrations"}, output - end + def test_schema_dump_with_string_ignored_table + stream = StringIO.new + + ActiveRecord::SchemaDumper.ignore_tables = ['accounts'] + ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) + output = stream.string + assert_no_match %r{create_table "accounts"}, output + assert_match %r{create_table "authors"}, output + assert_no_match %r{create_table "schema_migrations"}, output + end + + def test_schema_dump_with_regexp_ignored_table + stream = StringIO.new - def test_schema_dump_with_regexp_ignored_table - stream = StringIO.new + ActiveRecord::SchemaDumper.ignore_tables = [/^account/] + ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) + output = stream.string + assert_no_match %r{create_table "accounts"}, output + assert_match %r{create_table "authors"}, output + assert_no_match %r{create_table "schema_migrations"}, output + end - ActiveRecord::SchemaDumper.ignore_tables = [/^account/] + def test_schema_dump_illegal_ignored_table_value + stream = StringIO.new + ActiveRecord::SchemaDumper.ignore_tables = [5] + assert_raise(StandardError) do ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) - output = stream.string - assert_no_match %r{create_table "accounts"}, output - assert_match %r{create_table "authors"}, output - assert_no_match %r{create_table "schema_migrations"}, output end + end - def test_schema_dump_illegal_ignored_table_value - stream = StringIO.new - ActiveRecord::SchemaDumper.ignore_tables = [5] - assert_raise(StandardError) do - ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) - end + if current_adapter?(:MysqlAdapter) + def test_schema_dump_should_not_add_default_value_for_mysql_text_field + output = standard_dump + assert_match %r{t.text\s+"body",\s+:null => false$}, output end - if current_adapter?(:MysqlAdapter) - def test_schema_dump_should_not_add_default_value_for_mysql_text_field - output = standard_dump - assert_match %r{t.text\s+"body",\s+:null => false$}, output - end - - def test_mysql_schema_dump_should_honor_nonstandard_primary_keys - output = standard_dump - match = output.match(%r{create_table "movies"(.*)do}) - assert_not_nil(match, "nonstandardpk table not found") - assert_match %r(:primary_key => "movieid"), match[1], "non-standard primary key not preserved" - end - - def test_schema_dump_includes_length_for_mysql_blob_and_text_fields - output = standard_dump - assert_match %r{t.binary\s+"tiny_blob",\s+:limit => 255$}, output - assert_match %r{t.binary\s+"normal_blob"$}, output - assert_match %r{t.binary\s+"medium_blob",\s+:limit => 16777215$}, output - assert_match %r{t.binary\s+"long_blob",\s+:limit => 2147483647$}, output - assert_match %r{t.text\s+"tiny_text",\s+:limit => 255$}, output - assert_match %r{t.text\s+"normal_text"$}, output - assert_match %r{t.text\s+"medium_text",\s+:limit => 16777215$}, output - assert_match %r{t.text\s+"long_text",\s+:limit => 2147483647$}, output - end + def test_mysql_schema_dump_should_honor_nonstandard_primary_keys + output = standard_dump + match = output.match(%r{create_table "movies"(.*)do}) + assert_not_nil(match, "nonstandardpk table not found") + assert_match %r(:primary_key => "movieid"), match[1], "non-standard primary key not preserved" end - def test_schema_dump_includes_decimal_options - stream = StringIO.new - ActiveRecord::SchemaDumper.ignore_tables = [/^[^n]/] - ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) - output = stream.string - assert_match %r{:precision => 3,[[:space:]]+:scale => 2,[[:space:]]+:default => 2.78}, output + def test_schema_dump_includes_length_for_mysql_blob_and_text_fields + output = standard_dump + assert_match %r{t.binary\s+"tiny_blob",\s+:limit => 255$}, output + assert_match %r{t.binary\s+"normal_blob"$}, output + assert_match %r{t.binary\s+"medium_blob",\s+:limit => 16777215$}, output + assert_match %r{t.binary\s+"long_blob",\s+:limit => 2147483647$}, output + assert_match %r{t.text\s+"tiny_text",\s+:limit => 255$}, output + assert_match %r{t.text\s+"normal_text"$}, output + assert_match %r{t.text\s+"medium_text",\s+:limit => 16777215$}, output + assert_match %r{t.text\s+"long_text",\s+:limit => 2147483647$}, output end end + def test_schema_dump_includes_decimal_options + stream = StringIO.new + ActiveRecord::SchemaDumper.ignore_tables = [/^[^n]/] + ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) + output = stream.string + assert_match %r{:precision => 3,[[:space:]]+:scale => 2,[[:space:]]+:default => 2.78}, output + end end -- cgit v1.2.3 From 0a21193dc660396fb993b06d1d3c168a9cd900a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tarmo=20T=C3=A4nav?= Date: Wed, 7 May 2008 02:08:57 +0300 Subject: create_table :force => true no longer tries to drop a non-existing table Signed-off-by: Michael Koziarski --- activerecord/test/cases/migration_test.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index d4e81827aa..6be31b5f86 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -209,6 +209,24 @@ if ActiveRecord::Base.connection.supports_migrations? ActiveRecord::Base.primary_key_prefix_type = nil end + uses_mocha('test_create_table_with_force_true_does_not_drop_nonexisting_table') do + def test_create_table_with_force_true_does_not_drop_nonexisting_table + if Person.connection.table_exists?(:testings2) + Person.connection.drop_table :testings2 + end + + # using a copy as we need the drop_table method to + # continue to work for the ensure block of the test + temp_conn = Person.connection.dup + temp_conn.expects(:drop_table).never + temp_conn.create_table :testings2, :force => true do |t| + t.column :foo, :string + end + ensure + Person.connection.drop_table :testings2 rescue nil + end + end + # SQL Server, Sybase, and SQLite3 will not allow you to add a NOT NULL # column to a table without a default value. -- cgit v1.2.3 From bcb090c56b842a76397e0ea32f54c942fd11910e Mon Sep 17 00:00:00 2001 From: Andreas Neuhaus Date: Thu, 8 May 2008 00:04:53 -0500 Subject: Calling ActiveRecord#inspect on an unloaded association won't wipe the collection [#9 state:resolved] Signed-off-by: Joshua Peek --- activerecord/test/cases/associations_test.rb | 6 ++++++ activerecord/test/models/developer.rb | 4 ++++ 2 files changed, 10 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb index ed2fab6d22..d8fe98bf57 100755 --- a/activerecord/test/cases/associations_test.rb +++ b/activerecord/test/cases/associations_test.rb @@ -149,6 +149,12 @@ class AssociationProxyTest < ActiveRecord::TestCase assert !david.projects.loaded? end + def test_inspect_does_not_reload_a_not_yet_loaded_target + andreas = Developer.new :name => 'Andreas', :log => 'new developer added' + assert !andreas.audit_logs.loaded? + assert_match(/message: "new developer added"/, andreas.audit_logs.inspect) + end + def test_save_on_parent_saves_children developer = Developer.create :name => "Bryan", :salary => 50_000 assert_equal 1, developer.reload.audit_logs.size diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index 192c2cb5ab..f77fd0e96d 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -49,6 +49,10 @@ class Developer < ActiveRecord::Base before_create do |developer| developer.audit_logs.build :message => "Computer created" end + + def log=(message) + audit_logs.build :message => message + end end class AuditLog < ActiveRecord::Base -- cgit v1.2.3 From eb5b93be74ed3eca925c1ab9bd4739919ae39a41 Mon Sep 17 00:00:00 2001 From: Scott Fleckenstein Date: Wed, 7 May 2008 06:54:07 -0700 Subject: Fix Time.zone.parse from stripping time zone information and make Time aware attribute methods use Time.zone.parse instead of to_time --- activerecord/test/cases/attribute_methods_test.rb | 27 +++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index 61a049ab36..ab9abf27f9 100755 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -173,6 +173,33 @@ class AttributeMethodsTest < ActiveRecord::TestCase end end + def test_setting_time_zone_aware_attribute_with_string + utc_time = Time.utc(2008, 1, 1) + (-11..13).each do |timezone_offset| + time_string = utc_time.in_time_zone(timezone_offset).to_s + in_time_zone "Pacific Time (US & Canada)" do + record = @target.new + record.written_on = time_string + assert_equal Time.zone.parse(time_string), record.written_on + assert_equal TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone + assert_equal Time.utc(2007, 12, 31, 16), record.written_on.time + end + end + end + + def test_setting_time_zone_aware_attribute_interprets_time_zone_unaware_string_in_time_zone + time_string = 'Tue Jan 01 00:00:00 2008' + (-11..13).each do |timezone_offset| + in_time_zone timezone_offset do + record = @target.new + record.written_on = time_string + assert_equal Time.zone.parse(time_string), record.written_on + assert_equal TimeZone[timezone_offset], record.written_on.time_zone + assert_equal Time.utc(2008, 1, 1), record.written_on.time + end + end + end + def test_setting_time_zone_aware_attribute_in_current_time_zone utc_time = Time.utc(2008, 1, 1) in_time_zone "Pacific Time (US & Canada)" do -- cgit v1.2.3 From 328fada610aa9128386bc4b372d3e0b68aede945 Mon Sep 17 00:00:00 2001 From: gbuesing Date: Thu, 8 May 2008 20:31:54 -0500 Subject: ActiveRecord time zone aware attributes: blank string is treated as nil when assigned to writer --- activerecord/test/cases/attribute_methods_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index ab9abf27f9..c336fd9afb 100755 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -186,6 +186,14 @@ class AttributeMethodsTest < ActiveRecord::TestCase end end end + + def test_setting_time_zone_aware_attribute_to_blank_string_returns_nil + in_time_zone "Pacific Time (US & Canada)" do + record = @target.new + record.written_on = ' ' + assert_nil record.written_on + end + end def test_setting_time_zone_aware_attribute_interprets_time_zone_unaware_string_in_time_zone time_string = 'Tue Jan 01 00:00:00 2008' -- cgit v1.2.3