aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/adapters/postgresql/citext_test.rb
blob: 9eb0b7d99c2bdddb4f0bff99706c2a39fd4e57b4 (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
# frozen_string_literal: true

require "cases/helper"
require "support/schema_dumping_helper"

class PostgresqlCitextTest < ActiveRecord::PostgreSQLTestCase
  include SchemaDumpingHelper
  class Citext < ActiveRecord::Base
    self.table_name = "citexts"
  end

  def setup
    @connection = ActiveRecord::Base.connection

    enable_extension!("citext", @connection)

    @connection.create_table("citexts") do |t|
      t.citext "cival"
    end
  end

  teardown do
    @connection.drop_table "citexts", if_exists: true
    disable_extension!("citext", @connection)
  end

  def test_citext_enabled
    assert @connection.extension_enabled?("citext")
  end

  def test_column
    column = Citext.columns_hash["cival"]
    assert_equal :citext, column.type
    assert_equal "citext", column.sql_type
    assert_not_predicate column, :array?

    type = Citext.type_for_attribute("cival")
    assert_not_predicate type, :binary?
  end

  def test_change_table_supports_json
    @connection.transaction do
      @connection.change_table("citexts") do |t|
        t.citext "username"
      end
      Citext.reset_column_information
      column = Citext.columns_hash["username"]
      assert_equal :citext, column.type

      raise ActiveRecord::Rollback # reset the schema change
    end
  ensure
    Citext.reset_column_information
  end

  def test_write
    x = Citext.new(cival: "Some CI Text")
    x.save!
    citext = Citext.first
    assert_equal "Some CI Text", citext.cival

    citext.cival = "Some NEW CI Text"
    citext.save!

    assert_equal "Some NEW CI Text", citext.reload.cival
  end

  def test_select_case_insensitive
    @connection.execute "insert into citexts (cival) values('Cased Text')"
    x = Citext.where(cival: "cased text").first
    assert_equal "Cased Text", x.cival
  end

  def test_schema_dump_with_shorthand
    output = dump_table_schema("citexts")
    assert_match %r[t\.citext "cival"], output
  end
end