class RSpec::Matchers::Sequel::HaveColumn

Public Class Methods

new(column_name) click to toggle source
# File lib/rspec/sequel_expectations/matchers/have_column.rb, line 52
def initialize(column_name)
  @name    = column_name
  @type    = nil
  @null    = nil
  @default = false
  @table   = nil
  @error   = nil
end

Public Instance Methods

allow_null() click to toggle source
# File lib/rspec/sequel_expectations/matchers/have_column.rb, line 23
def allow_null
  @null = true
  self
end
description() click to toggle source
# File lib/rspec/sequel_expectations/matchers/have_column.rb, line 33
def description
  text = [%(have column named "#{@name}")]
  text << "of type #{@type}" if @type
  text << %(with default value "#{@default}") unless @default == false
  text << 'allowing null' if @null == true
  text << 'not allowing null' if @null == false
  text.join(' ')
end
failure_message() click to toggle source
# File lib/rspec/sequel_expectations/matchers/have_column.rb, line 42
def failure_message
  %(expected #{@table} to #{description} but #{@error})
end
failure_message_when_negated() click to toggle source
# File lib/rspec/sequel_expectations/matchers/have_column.rb, line 46
def failure_message_when_negated
  %(did not expect #{@table} to #{description})
end
matches?(subject) click to toggle source
# File lib/rspec/sequel_expectations/matchers/have_column.rb, line 7
def matches?(subject)
  get_column_from(subject)

  have_column? && correct_type? && correct_default? && correct_null?
end
not_null() click to toggle source
# File lib/rspec/sequel_expectations/matchers/have_column.rb, line 28
def not_null
  @null = false
  self
end
of_type(type) click to toggle source
# File lib/rspec/sequel_expectations/matchers/have_column.rb, line 13
def of_type(type)
  @type = type
  self
end
with_default(val) click to toggle source
# File lib/rspec/sequel_expectations/matchers/have_column.rb, line 18
def with_default(val)
  @default = val
  self
end

Private Instance Methods

correct_default?() click to toggle source
# File lib/rspec/sequel_expectations/matchers/have_column.rb, line 102
def correct_default?
  return true if @default == false

  if [@column[:default], @column[:ruby_default]].include?(@default)
    true
  else
    @error = %(it has default value "#{@column[:ruby_default] || @column[:default]}")
    false
  end
end
correct_null?() click to toggle source
# File lib/rspec/sequel_expectations/matchers/have_column.rb, line 91
def correct_null?
  return true if @null.nil?

  if @column[:allow_null] == @null
    true
  else
    @error = %(it #{"does not " if @null == true}allow null)
    false
  end
end
correct_type?() click to toggle source
# File lib/rspec/sequel_expectations/matchers/have_column.rb, line 77
def correct_type?
  return true unless @type

  expected = DB.send(:type_literal, { type: @type }).to_s
  actual   = [@column[:type].to_s, @column[:db_type].to_s]

  if actual.include?(expected)
    true
  else
    @error = %(it have type [#{actual.join(', ')}])
    false
  end
end
get_column_from(table) click to toggle source
# File lib/rspec/sequel_expectations/matchers/have_column.rb, line 61
def get_column_from(table)
  column  = DB.schema(table.to_sym).detect { |tuple| tuple.first == @name }

  @table  = table
  @column = column ? column.last : nil
end
have_column?() click to toggle source
# File lib/rspec/sequel_expectations/matchers/have_column.rb, line 68
def have_column?
  if @column.nil?
    @error = %(#{@table} does not have a column named "#{@name}")
    false
  else
    true
  end
end