aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/adapters/postgresql/geometric_test.rb
blob: 4b25381a830fdee2989908e3646e22c0d12a55ee (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
require "cases/helper"
require 'support/connection_helper'
require 'support/schema_dumping_helper'

class PostgresqlPointTest < ActiveRecord::TestCase
  include ConnectionHelper
  include SchemaDumpingHelper

  class PostgresqlPoint < ActiveRecord::Base; end

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

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

  def test_column
    column = PostgresqlPoint.columns_hash["x"]
    assert_equal :point, column.type
    assert_equal "point", column.sql_type
    assert_not column.array?

    type = PostgresqlPoint.type_for_attribute("x")
    assert_not type.binary?
  end

  def test_default
    assert_equal [12.2, 13.3], PostgresqlPoint.column_defaults['y']
    assert_equal [12.2, 13.3], PostgresqlPoint.new.y

    assert_equal [14.4, 15.5], PostgresqlPoint.column_defaults['z']
    assert_equal [14.4, 15.5], PostgresqlPoint.new.z
  end

  def test_schema_dumping
    output = dump_table_schema("postgresql_points")
    assert_match %r{t\.point\s+"x"$}, output
    assert_match %r{t\.point\s+"y",\s+default: \[12\.2, 13\.3\]$}, output
    assert_match %r{t\.point\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

  def test_mutation
    p = PostgresqlPoint.create! x: [10, 20]

    p.x[1] = 25
    p.save!
    p.reload

    assert_equal [10.0, 25.0], p.x
    assert_not p.changed?
  end
end

class PostgresqlGeometricTest < ActiveRecord::TestCase
  class PostgresqlGeometric < ActiveRecord::Base; end

  setup do
    @connection = ActiveRecord::Base.connection
    @connection.create_table("postgresql_geometrics") do |t|
      t.column :a_line_segment, :lseg
      t.column :a_box, :box
      t.column :a_path, :path
      t.column :a_polygon, :polygon
      t.column :a_circle, :circle
    end
  end

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

  def test_geometric_types
    g = PostgresqlGeometric.new(
      :a_line_segment => '(2.0, 3), (5.5, 7.0)',
      :a_box          => '2.0, 3, 5.5, 7.0',
      :a_path         => '[(2.0, 3), (5.5, 7.0), (8.5, 11.0)]',
      :a_polygon      => '((2.0, 3), (5.5, 7.0), (8.5, 11.0))',
      :a_circle       => '<(5.3, 10.4), 2>'
    )

    g.save!

    h = PostgresqlGeometric.find(g.id)

    assert_equal '[(2,3),(5.5,7)]', h.a_line_segment
    assert_equal '(5.5,7),(2,3)', h.a_box # reordered to store upper right corner then bottom left corner
    assert_equal '[(2,3),(5.5,7),(8.5,11)]', h.a_path
    assert_equal '((2,3),(5.5,7),(8.5,11))', h.a_polygon
    assert_equal '<(5.3,10.4),2>', h.a_circle
  end

  def test_alternative_format
    g = PostgresqlGeometric.new(
      :a_line_segment => '((2.0, 3), (5.5, 7.0))',
      :a_box          => '(2.0, 3), (5.5, 7.0)',
      :a_path         => '((2.0, 3), (5.5, 7.0), (8.5, 11.0))',
      :a_polygon      => '2.0, 3, 5.5, 7.0, 8.5, 11.0',
      :a_circle       => '((5.3, 10.4), 2)'
    )

    g.save!

    h = PostgresqlGeometric.find(g.id)
    assert_equal '[(2,3),(5.5,7)]', h.a_line_segment
    assert_equal '(5.5,7),(2,3)', h.a_box   # reordered to store upper right corner then bottom left corner
    assert_equal '((2,3),(5.5,7),(8.5,11))', h.a_path
    assert_equal '((2,3),(5.5,7),(8.5,11))', h.a_polygon
    assert_equal '<(5.3,10.4),2>', h.a_circle
  end

  def test_geometric_function
    PostgresqlGeometric.create! a_path: '[(2.0, 3), (5.5, 7.0), (8.5, 11.0)]'  # [ ] is an open path
    PostgresqlGeometric.create! a_path: '((2.0, 3), (5.5, 7.0), (8.5, 11.0))'  # ( ) is a closed path

    objs = PostgresqlGeometric.find_by_sql "SELECT isopen(a_path) FROM postgresql_geometrics ORDER BY id ASC"
    assert_equal [true, false], objs.map(&:isopen)

    objs = PostgresqlGeometric.find_by_sql "SELECT isclosed(a_path) FROM postgresql_geometrics ORDER BY id ASC"
    assert_equal [false, true], objs.map(&:isclosed)
  end
end