File: registration_test.rb

package info (click to toggle)
rails 2%3A7.2.2.1%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 43,352 kB
  • sloc: ruby: 349,799; javascript: 30,703; yacc: 46; sql: 43; sh: 29; makefile: 27
file content (107 lines) | stat: -rw-r--r-- 4,281 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# frozen_string_literal: true

require "cases/helper"

module ActiveRecord
  module ConnectionAdapters
    class RegistrationTest < ActiveRecord::TestCase
      def setup
        @original_adapters = ActiveRecord::ConnectionAdapters.instance_variable_get(:@adapters).dup
        ActiveRecord::ConnectionAdapters.instance_variable_get(:@adapters).delete("fake")
        @fake_adapter_path = File.expand_path("../../support/fake_adapter.rb", __dir__)
      end

      def teardown
        ActiveRecord::ConnectionAdapters.instance_variable_set(:@adapters, @original_adapters)
      end

      test "#register registers a new database adapter and #resolve can find it and raises if it cannot" do
        exception = assert_raises(ActiveRecord::AdapterNotFound) do
          ActiveRecord::ConnectionAdapters.resolve("fake")
        end

        assert_match(
          /Database configuration specifies nonexistent 'fake' adapter. Available adapters are:/,
          exception.message
        )

        ActiveRecord::ConnectionAdapters.register("fake", "FakeActiveRecordAdapter", @fake_adapter_path)

        assert_equal "FakeActiveRecordAdapter", ActiveRecord::ConnectionAdapters.resolve("fake").name
      end

      test "#register allows for symbol key" do
        exception = assert_raises(ActiveRecord::AdapterNotFound) do
          ActiveRecord::ConnectionAdapters.resolve("fake")
        end

        assert_match(
          /Database configuration specifies nonexistent 'fake' adapter. Available adapters are:/,
          exception.message
        )

        ActiveRecord::ConnectionAdapters.register(:fake, "FakeActiveRecordAdapter", @fake_adapter_path)

        assert_equal "FakeActiveRecordAdapter", ActiveRecord::ConnectionAdapters.resolve("fake").name
      end

      test "#resolve allows for symbol key" do
        exception = assert_raises(ActiveRecord::AdapterNotFound) do
          ActiveRecord::ConnectionAdapters.resolve("fake")
        end

        assert_match(
          /Database configuration specifies nonexistent 'fake' adapter. Available adapters are:/,
          exception.message
        )

        ActiveRecord::ConnectionAdapters.register("fake", "FakeActiveRecordAdapter", @fake_adapter_path)

        assert_equal "FakeActiveRecordAdapter", ActiveRecord::ConnectionAdapters.resolve(:fake).name
      end
    end

    class RegistrationIsolatedTest < ActiveRecord::TestCase
      include ActiveSupport::Testing::Isolation

      def setup
        @original_adapters = ActiveRecord::ConnectionAdapters.instance_variable_get(:@adapters).dup
      end

      test "#resolve raises if the adapter is using the pre 7.2 adapter registration API" do
        exception = assert_raises(ActiveRecord::AdapterNotFound) do
          assert_deprecated(ActiveRecord.deprecator) do
            ActiveRecord::ConnectionAdapters.resolve("fake_legacy")
          end
        end

        assert_match(
          /Database configuration specifies 'fake_legacy' adapter but that adapter has not been registered. Ensure that the adapter in the Gemfile is at the latest version. If it is, then the adapter may need to be modified./,
          exception.message
        )
      ensure
        ActiveRecord::ConnectionAdapters.instance_variable_get(:@adapters).delete("fake_legacy")
      end

      test "#resolve raises if the adapter maybe is using the pre 7.2 adapter registration API but we are not sure" do
        exception = assert_raises(ActiveRecord::AdapterNotFound) do
          assert_deprecated(ActiveRecord.deprecator) do
            ActiveRecord::ConnectionAdapters.resolve("fake_misleading_legacy")
          end
        end

        assert_match(
          /Database configuration specifies nonexistent 'fake_misleading_legacy' adapter. Available adapters are:/,
          exception.message
        )

        assert_match(
          /Ensure that the adapter is spelled correctly in config\/database.yml and that you've added the necessary adapter gem to your Gemfile and that it is at its latest version. If it is up to date, the adapter may need to be modified./,
          exception.message
        )
      ensure
        ActiveRecord::ConnectionAdapters.instance_variable_get(:@adapters).delete("fake_misleading_legacy")
      end
    end
  end
end