Repository: rails/rails
File: activerecord/test/cases/adapter_test.rb (Ruby)

Code before:
# frozen_string_literal: true

require "cases/helper"
require "support/connection_helper"
require "models/book"
require "models/post"
require "models/author"
require "models/event"

module ActiveRecord
  class AdapterTest < ActiveRecord::TestCase
    def setup
      @connection = ActiveRecord::Base.lease_connection
      @connection.materialize_transactions
    end

    def test_type_map_is_ractor_shareable
      # This is testing internals. Please feel free to remove this test
      # or change it when internals change. The point is to make sure
      # the type map is Ractor shareable.
      skip unless defined?(Ractor)
      @connection.tables.each do |table|
        @connection.columns(table).each do |column|
          type = @connection.lookup_cast_type_from_column(column)
          assert Ractor.shareable?(type)
        end
      end
    end

    ##
    # PostgreSQL does not support null bytes in strings
    unless current_adapter?(:PostgreSQLAdapter) ||
        (current_adapter?(:SQLite3Adapter) && !ActiveRecord::Base.lease_connection.prepared_statements)
      def test_update_prepared_statement
        b = Book.create(name: "my \x00 book")
        b.reload
        assert_equal "my \x00 book", b.name
        b.update(name: "my other \x00 book")
        b.reload
        assert_equal "my other \x00 book", b.name
      end
    end

    def test_create_record_with_pk_as_zero
      Book.create(id: 0)
      assert_equal 0, Book.find(0).id
      assert_nothing_raised { Book.destroy(0) }
    end


Review comment (bug):
f this sort of test will become common, maybe we ought to have some `assert_ractor_sharable` method? So that we'd at least get the object `inspect` or something on failure?

Code after:
# frozen_string_literal: true

require "cases/helper"
require "support/connection_helper"
require "models/book"
require "models/post"
require "models/author"
require "models/event"

module ActiveRecord
  class AdapterTest < ActiveRecord::TestCase
    def setup
      @connection = ActiveRecord::Base.lease_connection
      @connection.materialize_transactions
    end

    def test_type_map_is_ractor_shareable
      # This is testing internals. Please feel free to remove this test
      # or change it when internals change. The point is to make sure
      # the type map is Ractor shareable.
      @connection.tables.each do |table|
        @connection.columns(table).each do |column|
          assert_ractor_shareable @connection.lookup_cast_type_from_column(column)
        end
      end
    end

    ##
    # PostgreSQL does not support null bytes in strings
    unless current_adapter?(:PostgreSQLAdapter) ||
        (current_adapter?(:SQLite3Adapter) && !ActiveRecord::Base.lease_connection.prepared_statements)
      def test_update_prepared_statement
        b = Book.create(name: "my \x00 book")
        b.reload
        assert_equal "my \x00 book", b.name
        b.update(name: "my other \x00 book")
        b.reload
        assert_equal "my other \x00 book", b.name
      end
    end

    def test_create_record_with_pk_as_zero
      Book.create(id: 0)
      assert_equal 0, Book.find(0).id
      assert_nothing_raised { Book.destroy(0) }
    end

    def test_valid_column
      @connection.native_database_types.each_key do |type|