aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/connection_specification/resolver_test.rb
blob: 0a04117795c883347d45de3f1d6628cb020fff35 (plain) (blame)
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
require "cases/helper"

module ActiveRecord
  module ConnectionAdapters
    class ConnectionSpecification
      class ResolverTest < ActiveRecord::TestCase
        def resolve(spec)
          Resolver.new(spec, {}).spec.config
        end

        def test_url_invalid_adapter
          assert_raises(LoadError) do
            resolve 'ridiculous://foo?encoding=utf8'
          end
        end

        # The abstract adapter is used simply to bypass the bit of code that
        # checks that the adapter file can be required in.

        def test_url_host_no_db
          spec = resolve 'abstract://foo?encoding=utf8'
          assert_equal({
            adapter:  "abstract",
            host:     "foo",
            encoding: "utf8" }, spec)
        end

        def test_url_host_db
          spec = resolve 'abstract://foo/bar?encoding=utf8'
          assert_equal({
            adapter:  "abstract",
            database: "bar",
            host:     "foo",
            encoding: "utf8" }, spec)
        end

        def test_url_port
          spec = resolve 'abstract://foo:123?encoding=utf8'
          assert_equal({
            adapter:  "abstract",
            port:     123,
            host:     "foo",
            encoding: "utf8" }, spec)
        end

        def test_url_query_numeric
          spec = resolve 'abstract://foo:123?encoding=utf8&int=500&float=10.9'
          assert_equal({
            adapter:  "abstract",
            port:     123,
            int:      500,
            float:    10.9,
            host:     "foo",
            encoding: "utf8" }, spec)
        end

        def test_url_query_boolean
          spec = resolve 'abstract://foo:123?true=true&false=false'
          assert_equal({
            adapter: "abstract",
            port:    123,
            true:    true,
            false:   false,
            host:    "foo" }, spec)
        end

        def test_encoded_password
          password = 'am@z1ng_p@ssw0rd#!'
          encoded_password = URI.encode_www_form_component(password)
          spec = resolve "abstract://foo:#{encoded_password}@localhost/bar"
          assert_equal password, spec[:password]
        end

        def test_descriptive_error_message_when_adapter_is_missing
          error = assert_raise(LoadError) do
            resolve(adapter: 'non-existing')
          end

          assert_match "Could not load 'active_record/connection_adapters/non-existing_adapter'", error.message
        end
      end
    end
  end
end