aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/adapters/postgresql/geometric_test.rb
blob: 89a72f08b4fed2d6d877460ee6852e508ed54738 (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
# -*- coding: utf-8 -*-
require "cases/helper"
require 'support/connection_helper'
require 'support/schema_dumping_helper'
require 'active_record/base'
require 'active_record/connection_adapters/postgresql_adapter'

class PostgresqlPointTest < ActiveRecord::TestCase
  include ConnectionHelper
  include SchemaDumpingHelper

  class PostgresqlPoint < ActiveRecord::Base; end

  def setup
    @connection = ActiveRecord::Base.connection
    @connection.transaction do
      @connection.create_table('postgresql_points') do |t|
        t.column :x, :point
        t.column :y, :point, default: [12.2, 13.3]
        t.column :z, :point, default: "(14.4,15.5)"
      end
    end
  end

  teardown do
    @connection.execute 'DROP TABLE IF EXISTS postgresql_points'
  end

  def test_column
    column = PostgresqlPoint.columns_hash["x"]
    assert_equal :string, column.type
    assert_equal "point", column.sql_type
    assert column.text?
    assert_not column.number?
    assert_not column.binary?
    assert_not column.array
  end

  def test_default
    column = PostgresqlPoint.columns_hash["y"]
    assert_equal [12.2, 13.3], column.default
    assert_equal [12.2, 13.3], PostgresqlPoint.new.y

    column = PostgresqlPoint.columns_hash["z"]
    assert_equal [14.4, 15.5], column.default
    assert_equal [14.4, 15.5], PostgresqlPoint.new.z
  end

  def test_schema_dumping
    output = dump_table_schema("postgresql_points")
    assert_match %r{t\.string\s+"x"$}, output
    assert_match %r{t\.string\s+"y",\s+default: \[12\.2, 13\.3\]$}, output
    assert_match %r{t\.string\s+"z",\s+default: \[14\.4, 15\.5\]$}, output
  end

  def test_roundtrip
    PostgresqlPoint.create! x: [10, 25.2]
    record = PostgresqlPoint.first
    assert_equal [10, 25.2], record.x

    record.x = [1.1, 2.2]
    record.save!
    assert record.reload
    assert_equal [1.1, 2.2], record.x
  end
end