diff options
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/adapters/mysql2/connection_test.rb | 27 | ||||
-rw-r--r-- | activerecord/test/cases/adapters/postgresql/connection_test.rb | 44 | ||||
-rw-r--r-- | activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb | 36 | ||||
-rw-r--r-- | activerecord/test/cases/base_test.rb | 9 | ||||
-rw-r--r-- | activerecord/test/cases/helper.rb | 16 | ||||
-rw-r--r-- | activerecord/test/cases/relation/where_test.rb | 19 | ||||
-rw-r--r-- | activerecord/test/cases/relations_test.rb | 7 | ||||
-rw-r--r-- | activerecord/test/cases/store_test.rb | 29 | ||||
-rw-r--r-- | activerecord/test/fixtures/admin/users.yml | 3 |
9 files changed, 161 insertions, 29 deletions
diff --git a/activerecord/test/cases/adapters/mysql2/connection_test.rb b/activerecord/test/cases/adapters/mysql2/connection_test.rb index 684c7f5929..276c499276 100644 --- a/activerecord/test/cases/adapters/mysql2/connection_test.rb +++ b/activerecord/test/cases/adapters/mysql2/connection_test.rb @@ -4,6 +4,13 @@ class MysqlConnectionTest < ActiveRecord::TestCase def setup super @connection = ActiveRecord::Model.connection + @connection.extend(LogIntercepter) + @connection.intercepted = true + end + + def teardown + @connection.intercepted = false + @connection.logged = [] end def test_no_automatic_reconnection_after_timeout @@ -45,6 +52,26 @@ class MysqlConnectionTest < ActiveRecord::TestCase end end + def test_logs_name_structure_dump + @connection.structure_dump + assert_equal "SCHEMA", @connection.logged[0][1] + assert_equal "SCHEMA", @connection.logged[2][1] + end + + def test_logs_name_show_variable + @connection.show_variable 'foo' + assert_equal "SCHEMA", @connection.logged[0][1] + end + + def test_logs_name_rename_column_sql + @connection.execute "CREATE TABLE `bar_baz` (`foo` varchar(255))" + @connection.logged = [] + @connection.send(:rename_column_sql, 'bar_baz', 'foo', 'foo2') + assert_equal "SCHEMA", @connection.logged[0][1] + ensure + @connection.execute "DROP TABLE `bar_baz`" + end + private def run_without_connection diff --git a/activerecord/test/cases/adapters/postgresql/connection_test.rb b/activerecord/test/cases/adapters/postgresql/connection_test.rb index 4baec749ff..adb2cef010 100644 --- a/activerecord/test/cases/adapters/postgresql/connection_test.rb +++ b/activerecord/test/cases/adapters/postgresql/connection_test.rb @@ -8,6 +8,13 @@ module ActiveRecord def setup super @connection = ActiveRecord::Base.connection + @connection.extend(LogIntercepter) + @connection.intercepted = true + end + + def teardown + @connection.intercepted = false + @connection.logged = [] end def test_encoding @@ -25,5 +32,42 @@ module ActiveRecord expect = NonExistentTable.connection.query('show geqo').first.first assert_equal 'off', expect end + + def test_tables_logs_name + @connection.tables('hello') + assert_equal 'SCHEMA', @connection.logged[0][1] + end + + def test_indexes_logs_name + @connection.indexes('items', 'hello') + assert_equal 'SCHEMA', @connection.logged[0][1] + end + + def test_table_exists_logs_name + @connection.table_exists?('items') + assert_equal 'SCHEMA', @connection.logged[0][1] + end + + def test_table_alias_length_logs_name + @connection.instance_variable_set("@table_alias_length", nil) + @connection.table_alias_length + assert_equal 'SCHEMA', @connection.logged[0][1] + end + + def test_current_database_logs_name + @connection.current_database + assert_equal 'SCHEMA', @connection.logged[0][1] + end + + def test_encoding_logs_name + @connection.encoding + assert_equal 'SCHEMA', @connection.logged[0][1] + end + + def test_schema_names_logs_name + @connection.schema_names + assert_equal 'SCHEMA', @connection.logged[0][1] + end + end end diff --git a/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb b/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb index 8a7f44d0a3..5e947799cc 100644 --- a/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb +++ b/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb @@ -20,6 +20,14 @@ module ActiveRecord number integer ) eosql + + @conn.extend(LogIntercepter) + @conn.intercepted = true + end + + def teardown + @conn.intercepted = false + @conn.logged = [] end def test_column_types @@ -232,13 +240,23 @@ module ActiveRecord end def test_tables_logs_name - name = "hello" - assert_logged [[name, []]] do - @conn.tables(name) + assert_logged [['SCHEMA', []]] do + @conn.tables('hello') assert_not_nil @conn.logged.first.shift end end + def test_indexes_logs_name + assert_logged [["PRAGMA index_list(\"items\")", 'SCHEMA', []]] do + @conn.indexes('items', 'hello') + end + end + + def test_table_exists_logs_name + assert @conn.table_exists?('items') + assert_equal 'SCHEMA', @conn.logged[0][1] + end + def test_columns columns = @conn.columns('items').sort_by { |x| x.name } assert_equal 2, columns.length @@ -274,7 +292,6 @@ module ActiveRecord end def test_indexes_logs - intercept_logs_on @conn assert_difference('@conn.logged.length') do @conn.indexes('items') end @@ -326,21 +343,10 @@ module ActiveRecord private def assert_logged logs - intercept_logs_on @conn yield assert_equal logs, @conn.logged end - def intercept_logs_on ctx - @conn.extend(Module.new { - attr_accessor :logged - def log sql, name, binds = [] - @logged << [sql, name, binds] - yield - end - }) - @conn.logged = [] - end end end end diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 619fb881fa..f95230ff50 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1309,6 +1309,15 @@ class BasicsTest < ActiveRecord::TestCase assert_equal({ :foo => :bar }, t.content_before_type_cast) end + def test_serialized_attribute_calling_dup_method + klass = Class.new(ActiveRecord::Base) + klass.table_name = "topics" + klass.serialize :content, JSON + + t = klass.new(:content => { :foo => :bar }).dup + assert_equal({ :foo => :bar }, t.content_before_type_cast) + end + def test_serialized_attribute_declared_in_subclass hash = { 'important1' => 'value1', 'important2' => 'value2' } important_topic = ImportantTopic.create("important" => hash) diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index 37fa13f771..afff020561 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -121,3 +121,19 @@ class << Time @now = nil end end + +module LogIntercepter + attr_accessor :logged, :intercepted + def self.extended(base) + base.logged = [] + end + def log(sql, name, binds = [], &block) + if @intercepted + @logged << [sql, name, binds] + yield + else + super(sql, name,binds, &block) + end + end +end + diff --git a/activerecord/test/cases/relation/where_test.rb b/activerecord/test/cases/relation/where_test.rb new file mode 100644 index 0000000000..90c690e266 --- /dev/null +++ b/activerecord/test/cases/relation/where_test.rb @@ -0,0 +1,19 @@ +require "cases/helper" +require 'models/post' + +module ActiveRecord + class WhereTest < ActiveRecord::TestCase + fixtures :posts + + def test_where_error + assert_raises(ActiveRecord::StatementInvalid) do + Post.where(:id => { 'posts.author_id' => 10 }).first + end + end + + def test_where_with_table_name + post = Post.first + assert_equal post, Post.where(:posts => { 'id' => post.id }).first + end + end +end diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 2dc8f0053b..90367df5ee 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -226,7 +226,6 @@ class RelationTest < ActiveRecord::TestCase assert_no_queries do assert_equal [], Developer.none assert_equal [], Developer.scoped.none - assert Developer.none.is_a?(ActiveRecord::NullRelation) end end @@ -236,6 +235,12 @@ class RelationTest < ActiveRecord::TestCase end end + def test_none_chainable_to_existing_scope_extension_method + assert_no_queries do + assert_equal 1, Topic.anonymous_extension.none.one + end + end + def test_none_chained_to_methods_firing_queries_straight_to_db assert_no_queries do assert_equal [], Developer.none.pluck(:id) # => uses select_all diff --git a/activerecord/test/cases/store_test.rb b/activerecord/test/cases/store_test.rb index 3a5d84df9f..79476ed2a4 100644 --- a/activerecord/test/cases/store_test.rb +++ b/activerecord/test/cases/store_test.rb @@ -3,8 +3,10 @@ require 'models/admin' require 'models/admin/user' class StoreTest < ActiveRecord::TestCase + fixtures :'admin/users' + setup do - @john = Admin::User.create(:name => 'John Doe', :color => 'black', :remember_login => true, :height => 'tall', :is_a_good_guy => true) + @john = Admin::User.create!(:name => 'John Doe', :color => 'black', :remember_login => true, :height => 'tall', :is_a_good_guy => true) end test "reading store attributes through accessors" do @@ -52,18 +54,19 @@ class StoreTest < ActiveRecord::TestCase end test "convert store attributes from Hash to HashWithIndifferentAccess saving the data and access attributes indifferently" do - @john.json_data = { :height => 'tall', 'weight' => 'heavy' } - assert_equal true, @john.json_data.instance_of?(Hash) - assert_equal 'tall', @john.json_data[:height] - assert_equal nil, @john.json_data['height'] - assert_equal nil, @john.json_data[:weight] - assert_equal 'heavy', @john.json_data['weight'] - @john.height = 'low' - assert_equal true, @john.json_data.instance_of?(HashWithIndifferentAccess) - assert_equal 'low', @john.json_data[:height] - assert_equal 'low', @john.json_data['height'] - assert_equal 'heavy', @john.json_data[:weight] - assert_equal 'heavy', @john.json_data['weight'] + user = Admin::User.find_by_name('Jamis') + assert_equal 'symbol', user.settings[:symbol] + assert_equal 'symbol', user.settings['symbol'] + assert_equal 'string', user.settings[:string] + assert_equal 'string', user.settings['string'] + assert_equal true, user.settings.instance_of?(ActiveSupport::HashWithIndifferentAccess) + + user.height = 'low' + assert_equal 'symbol', user.settings[:symbol] + assert_equal 'symbol', user.settings['symbol'] + assert_equal 'string', user.settings[:string] + assert_equal 'string', user.settings['string'] + assert_equal true, user.settings.instance_of?(ActiveSupport::HashWithIndifferentAccess) end test "convert store attributes from any format other than Hash or HashWithIndifferent access losing the data" do diff --git a/activerecord/test/fixtures/admin/users.yml b/activerecord/test/fixtures/admin/users.yml index 6f11f2509e..e2884beda5 100644 --- a/activerecord/test/fixtures/admin/users.yml +++ b/activerecord/test/fixtures/admin/users.yml @@ -5,3 +5,6 @@ david: jamis: name: Jamis account: signals37 + settings: + :symbol: symbol + string: string |