postgresql_geometric_types_test.rb 2.6 KB
Newer Older
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
require 'cases/helper'

module ActiveRecord
  class Migration
    class PostgreSQLGeometricTypesTest < ActiveRecord::TestCase
      attr_reader :connection, :table_name

      def setup
        super
        @connection = ActiveRecord::Base.connection
        @table_name = :testings
      end

      if current_adapter?(:PostgreSQLAdapter)
        def test_creating_column_with_point_type
          connection.create_table(table_name) do |t|
            t.point :foo_point
          end
          
          assert_column_exists(:foo_point)
          assert_type_correct(:foo_point, :point)
        end

        def test_creating_column_with_line_type
          connection.create_table(table_name) do |t|
            t.line :foo_line
          end
          
          assert_column_exists(:foo_line)
          assert_type_correct(:foo_line, :line)
        end

        def test_creating_column_with_lseg_type
          connection.create_table(table_name) do |t|
            t.lseg :foo_lseg
          end
          
          assert_column_exists(:foo_lseg)
          assert_type_correct(:foo_lseg, :lseg)
        end

        def test_creating_column_with_box_type
          connection.create_table(table_name) do |t|
            t.box :foo_box
          end
          
          assert_column_exists(:foo_box)
          assert_type_correct(:foo_box, :box)
        end

        def test_creating_column_with_path_type
          connection.create_table(table_name) do |t|
            t.path :foo_path
          end
          
          assert_column_exists(:foo_path)
          assert_type_correct(:foo_path, :path)
        end

        def test_creating_column_with_polygon_type
          connection.create_table(table_name) do |t|
            t.polygon :foo_polygon
          end
          
          assert_column_exists(:foo_polygon)
          assert_type_correct(:foo_polygon, :polygon)
        end

        def test_creating_column_with_circle_type
          connection.create_table(table_name) do |t|
            t.circle :foo_circle
          end
          
          assert_column_exists(:foo_circle)
          assert_type_correct(:foo_circle, :circle)
        end
      end

      private
        def assert_column_exists(column_name)
          columns = connection.columns(table_name)
          assert columns.map(&:name).include?(column_name.to_s)
        end

        def assert_type_correct(column_name, type)
          columns = connection.columns(table_name)
          column = columns.select{ |c| c.name == column_name.to_s }.first
          assert_equal type.to_s, column.sql_type
        end

    end
  end
end