|
4 | 4 | require 'active_record/base'
|
5 | 5 | require 'active_record/connection_adapters/postgresql_adapter'
|
6 | 6 |
|
7 |
| -class PostgresqlCitextTest < ActiveRecord::TestCase |
8 |
| - class Citext < ActiveRecord::Base |
9 |
| - self.table_name = 'citexts' |
10 |
| - end |
| 7 | +if ActiveRecord::Base.connection.supports_extensions? |
| 8 | + class PostgresqlCitextTest < ActiveRecord::TestCase |
| 9 | + class Citext < ActiveRecord::Base |
| 10 | + self.table_name = 'citexts' |
| 11 | + end |
11 | 12 |
|
12 |
| - def setup |
13 |
| - @connection = ActiveRecord::Base.connection |
| 13 | + def setup |
| 14 | + @connection = ActiveRecord::Base.connection |
14 | 15 |
|
15 |
| - unless @connection.extension_enabled?('citext') |
16 |
| - @connection.enable_extension 'citext' |
17 |
| - @connection.commit_db_transaction |
18 |
| - end |
| 16 | + unless @connection.extension_enabled?('citext') |
| 17 | + @connection.enable_extension 'citext' |
| 18 | + @connection.commit_db_transaction |
| 19 | + end |
19 | 20 |
|
20 |
| - @connection.reconnect! |
| 21 | + @connection.reconnect! |
21 | 22 |
|
22 |
| - @connection.create_table('citexts') do |t| |
23 |
| - t.citext 'cival' |
| 23 | + @connection.create_table('citexts') do |t| |
| 24 | + t.citext 'cival' |
| 25 | + end |
| 26 | + @column = Citext.columns_hash['cival'] |
24 | 27 | end
|
25 |
| - @column = Citext.columns_hash['cival'] |
26 |
| - end |
27 | 28 |
|
28 |
| - teardown do |
29 |
| - @connection.execute 'DROP TABLE IF EXISTS citexts;' |
30 |
| - @connection.execute 'DROP EXTENSION IF EXISTS citext CASCADE;' |
31 |
| - end |
| 29 | + teardown do |
| 30 | + @connection.execute 'DROP TABLE IF EXISTS citexts;' |
| 31 | + @connection.execute 'DROP EXTENSION IF EXISTS citext CASCADE;' |
| 32 | + end |
32 | 33 |
|
33 |
| - def test_citext_enabled |
34 |
| - assert @connection.extension_enabled?('citext') |
35 |
| - end |
| 34 | + def test_citext_enabled |
| 35 | + assert @connection.extension_enabled?('citext') |
| 36 | + end |
36 | 37 |
|
37 |
| - def test_column_type |
38 |
| - assert_equal :citext, @column.type |
39 |
| - end |
| 38 | + def test_column_type |
| 39 | + assert_equal :citext, @column.type |
| 40 | + end |
40 | 41 |
|
41 |
| - def test_column_sql_type |
42 |
| - assert_equal 'citext', @column.sql_type |
43 |
| - end |
| 42 | + def test_column_sql_type |
| 43 | + assert_equal 'citext', @column.sql_type |
| 44 | + end |
| 45 | + |
| 46 | + def test_change_table_supports_json |
| 47 | + @connection.transaction do |
| 48 | + @connection.change_table('citexts') do |t| |
| 49 | + t.citext 'username' |
| 50 | + end |
| 51 | + Citext.reset_column_information |
| 52 | + column = Citext.columns.find { |c| c.name == 'username' } |
| 53 | + assert_equal :citext, column.type |
44 | 54 |
|
45 |
| - def test_change_table_supports_json |
46 |
| - @connection.transaction do |
47 |
| - @connection.change_table('citexts') do |t| |
48 |
| - t.citext 'username' |
| 55 | + raise ActiveRecord::Rollback # reset the schema change |
49 | 56 | end
|
| 57 | + ensure |
50 | 58 | Citext.reset_column_information
|
51 |
| - column = Citext.columns.find { |c| c.name == 'username' } |
52 |
| - assert_equal :citext, column.type |
53 |
| - |
54 |
| - raise ActiveRecord::Rollback # reset the schema change |
55 | 59 | end
|
56 |
| - ensure |
57 |
| - Citext.reset_column_information |
58 |
| - end |
59 | 60 |
|
60 |
| - def test_write |
61 |
| - x = Citext.new(cival: 'Some CI Text') |
62 |
| - x.save! |
63 |
| - citext = Citext.first |
64 |
| - assert_equal "Some CI Text", citext.cival |
| 61 | + def test_write |
| 62 | + x = Citext.new(cival: 'Some CI Text') |
| 63 | + x.save! |
| 64 | + citext = Citext.first |
| 65 | + assert_equal "Some CI Text", citext.cival |
65 | 66 |
|
66 |
| - citext.cival = "Some NEW CI Text" |
67 |
| - citext.save! |
| 67 | + citext.cival = "Some NEW CI Text" |
| 68 | + citext.save! |
68 | 69 |
|
69 |
| - assert_equal "Some NEW CI Text", citext.reload.cival |
70 |
| - end |
| 70 | + assert_equal "Some NEW CI Text", citext.reload.cival |
| 71 | + end |
71 | 72 |
|
72 |
| - def test_select_case_insensitive |
73 |
| - @connection.execute "insert into citexts (cival) values('Cased Text')" |
74 |
| - x = Citext.where(cival: 'cased text').first |
75 |
| - assert_equal 'Cased Text', x.cival |
| 73 | + def test_select_case_insensitive |
| 74 | + @connection.execute "insert into citexts (cival) values('Cased Text')" |
| 75 | + x = Citext.where(cival: 'cased text').first |
| 76 | + assert_equal 'Cased Text', x.cival |
| 77 | + end |
76 | 78 | end
|
77 | 79 | end
|
0 commit comments