ltree_test.rb 1.2 KB
Newer Older
1
require "cases/helper"
2
require 'support/schema_dumping_helper'
3 4

class PostgresqlLtreeTest < ActiveRecord::TestCase
5
  include SchemaDumpingHelper
6 7 8 9 10 11
  class Ltree < ActiveRecord::Base
    self.table_name = 'ltrees'
  end

  def setup
    @connection = ActiveRecord::Base.connection
12

13
    enable_extension!('ltree', @connection)
14

15 16 17
    @connection.transaction do
      @connection.create_table('ltrees') do |t|
        t.ltree 'path'
18 19
      end
    end
20 21
  rescue ActiveRecord::StatementInvalid
    skip "do not test on PG without ltree"
22 23
  end

G
Guo Xiang Tan 已提交
24
  teardown do
25 26 27 28
    @connection.execute 'drop table if exists ltrees'
  end

  def test_column
29 30
    column = Ltree.columns_hash['path']
    assert_equal :ltree, column.type
31
    assert_equal "ltree", column.sql_type
32
    assert_not column.array?
33 34 35

    type = Ltree.type_for_attribute('path')
    assert_not type.binary?
36 37 38
  end

  def test_write
39 40
    ltree = Ltree.new(path: '1.2.3.4')
    assert ltree.save!
41 42 43 44
  end

  def test_select
    @connection.execute "insert into ltrees (path) VALUES ('1.2.3')"
45 46
    ltree = Ltree.first
    assert_equal '1.2.3', ltree.path
47
  end
48 49 50

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