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

Code before:

      test "idempotent SELECT queries allow retries" do
        notifications = capture_notifications("sql.active_record") do
          assert (a = Author.first)
          assert Post.where(id: [1, 2]).first
          assert Post.find(1)
          assert Post.find_by(title: "Welcome to the weblog")
          assert_predicate Post, :exists?
          a.books.to_a
        end.select { |n| n.payload[:name] != "SCHEMA" }

        assert_equal 6, notifications.length

        notifications.each do |n|
          assert n.payload[:allow_retry]
        end
      end

      test "query cacheable idempotent SELECT queries allow retries" do
        @connection.enable_query_cache!

        notifications = capture_notifications("sql.active_record") do
          assert (a = Author.first)
          assert Post.where(id: [1, 2]).first
          assert Post.find(1)
          assert Post.find_by(title: "Welcome to the weblog")
          assert_predicate Post, :exists?
          a.books.to_a
        end.select { |n| n.payload[:name] != "SCHEMA" }

        assert_equal 6, notifications.length

        notifications.each do |n|
          assert n.payload[:allow_retry], "#{n.payload[:sql]} was not retryable"
        end
      ensure
        @connection.disable_query_cache!
      end

      test "queries containing SQL fragments do not allow retries" do
        notifications = capture_notifications("sql.active_record") do
          Post.where("1 = 1").to_a
          Post.select("title AS custom_title").first
          Book.find_by("updated_at < ?", 2.weeks.ago)
        end.select { |n| n.payload[:name] != "SCHEMA" }

        assert_equal 3, notifications.length

        notifications.each do |n|
          assert_not n.payload[:allow_retry]
        end

Review comment (bug):
```suggestion
          assert_not_nil (a = Author.first)
          assert_not_nil Post.where(id: [1, 2]).first
          assert_not_nil Post.find(1)
          assert_not_nil Post.find_by(title: "Welcome to the weblog")
```

Would produce a much better error message in case this ever fails.

Code after:

      test "idempotent SELECT queries allow retries" do
        notifications = capture_notifications("sql.active_record") do
          assert (a = Author.first)
          assert Post.where(id: [1, 2]).first
          assert Post.find(1)
          assert Post.find_by(title: "Welcome to the weblog")
          assert_predicate Post, :exists?
          a.books.to_a
        end.select { |n| n.payload[:name] != "SCHEMA" }

        assert_equal 6, notifications.length

        notifications.each do |n|
          assert n.payload[:allow_retry]
        end
      end

      test "query cacheable idempotent SELECT queries allow retries" do
        @connection.enable_query_cache!

        notifications = capture_notifications("sql.active_record") do
          assert_not_nil (a = Author.first)
          assert_not_nil Post.where(id: [1, 2]).first
          assert_not_nil Post.find(1)
          assert_not_nil Post.find_by(title: "Welcome to the weblog")
          assert_predicate Post, :exists?
          a.books.to_a
        end.select { |n| n.payload[:name] != "SCHEMA" }

        assert_equal 6, notifications.length

        notifications.each do |n|
          assert n.payload[:allow_retry], "#{n.payload[:sql]} was not retryable"
        end
      ensure
        @connection.disable_query_cache!
      end

      test "queries containing SQL fragments do not allow retries" do
        notifications = capture_notifications("sql.active_record") do
          Post.where("1 = 1").to_a
          Post.select("title AS custom_title").first
          Book.find_by("updated_at < ?", 2.weeks.ago)
        end.select { |n| n.payload[:name] != "SCHEMA" }

        assert_equal 3, notifications.length

        notifications.each do |n|
          assert_not n.payload[:allow_retry]
        end