Class: MigrationTestHelper::Table

Inherits:
Object
  • Object
show all
Includes:
Connection, Test::Unit::Assertions
Defined in:
lib/migration_test_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Connection

#conn

Constructor Details

#initialize(name) ⇒ Table

:nodoc:



128
129
130
131
132
133
# File 'lib/migration_test_helper.rb', line 128

def initialize(name) #:nodoc:
  @name = name.to_s
  @columns = []
  @indexes = []
  assert conn.tables.include?(@name), "table <#{@name}> not found in schema"
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



126
127
128
# File 'lib/migration_test_helper.rb', line 126

def name
  @name
end

Instance Method Details

#column(colname, type, options = {}) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/migration_test_helper.rb', line 135

def column(colname,type,options={})
  colname = colname.to_s
  @columns << colname
  col = conn.columns(name).find {|c| c.name == colname }
  assert_not_nil col, "column <#{colname}> not found in table <#{self.name}>"
  assert_equal type, col.type, "wrong type for column <#{colname}> in table <#{name}>"
  options.each do |k,v|
    k = k.to_sym; actual = col.send(k); actual = actual.is_a?(String) ? actual.sub(/'$/,'').sub(/^'/,'') : actual
    assert_equal v, actual, "column <#{colname}> in table <#{name}> has wrong :#{k}"
  end
end

#index(column_name, options = {}) ⇒ Object



147
148
149
# File 'lib/migration_test_helper.rb', line 147

def index(column_name, options = {})
  @indexes << "name <#{options[:name]}> columns <#{Array(column_name).join(",")}> unique <#{options[:unique] == true}>" 
end

#verifyObject

:nodoc:



151
152
153
154
155
156
157
# File 'lib/migration_test_helper.rb', line 151

def verify #:nodoc:
  actual_columns = conn.columns(name).map {|c| c.name }
  assert_equal @columns.sort, actual_columns.sort, "wrong columns for table: <#{name}>"

  actual_indexes = conn.indexes(@name).collect { |i| "name <#{i.name}> columns <#{i.columns.join(",")}> unique <#{i.unique}>" }
  assert_equal @indexes.sort, actual_indexes.sort, "wrong indexes for table: <#{name}>"
end