aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/adapters/postgresql/ltree_test.rb
blob: 2b5ac1cac664c4b6d3bd01b62c555701a9241a43 (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
require "cases/helper"
require "support/schema_dumping_helper"

class PostgresqlLtreeTest < ActiveRecord::PostgreSQLTestCase
  include SchemaDumpingHelper
  class Ltree < ActiveRecord::Base
    self.table_name = "ltrees"
  end

  def setup
    @connection = ActiveRecord::Base.connection

    enable_extension!("ltree", @connection)

    @connection.transaction do
      @connection.create_table("ltrees") do |t|
        t.ltree "path"
      end
    end
  rescue ActiveRecord::StatementInvalid
    skip "do not test on PG without ltree"
  end

  teardown do
    @connection.drop_table "ltrees", if_exists: true
  end

  def test_column
    column = Ltree.columns_hash["path"]
    assert_equal :ltree, column.type
    assert_equal "ltree", column.sql_type
    assert_not column.array?

    type = Ltree.type_for_attribute("path")
    assert_not type.binary?
  end

  def test_write
    ltree = Ltree.new(path: "1.2.3.4")
    assert ltree.save!
  end

  def test_select
    @connection.execute "insert into ltrees (path) VALUES ('1.2.3')"
    ltree = Ltree.first
    assert_equal "1.2.3", ltree.path
  end

  def test_schema_dump_with_shorthand
    output = dump_table_schema("ltrees")
    assert_match %r[t\.ltree "path"], output
  end
end