Class: MySQLExpectations::Matchers::HaveKey

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql_expectations/matchers/table_have_key.rb

Overview

A matcher that checks if a MySQLDumpExpectations::Table has a field. Optionally, checks the field type and nullability

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expected_key) ⇒ HaveKey

Returns a new instance of HaveKey.



13
14
15
16
# File 'lib/mysql_expectations/matchers/table_have_key.rb', line 13

def initialize(expected_key)
  @expected_key = expected_key
  @table = nil
end

Instance Attribute Details

#expected_keyObject (readonly)

Returns the value of attribute expected_key.



11
12
13
# File 'lib/mysql_expectations/matchers/table_have_key.rb', line 11

def expected_key
  @expected_key
end

#tableObject (readonly)

Returns the value of attribute table.



11
12
13
# File 'lib/mysql_expectations/matchers/table_have_key.rb', line 11

def table
  @table
end

Instance Method Details

#current_keys(keys) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/mysql_expectations/matchers/table_have_key.rb', line 29

def current_keys(keys)
  result = ''
  keys.each_with_index do |key, index|
    result << key.to_s
    result << ",\n" unless index == keys.size - 1
  end
  result
end

#descriptionObject



24
25
26
27
# File 'lib/mysql_expectations/matchers/table_have_key.rb', line 24

def description
  description = "have key #{expected_key}"
  description
end

#failure_messageObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mysql_expectations/matchers/table_have_key.rb', line 38

def failure_message
  result = "expected '#{table.name}' table to " \
    "have key #{expected_key}"
  keys = table.keys
  if keys.empty?
    result << ' but it has no keys.'
  else
    result << " but it has keys:\n"
    result << current_keys(keys)
  end
  result
end

#matches?(table) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
# File 'lib/mysql_expectations/matchers/table_have_key.rb', line 18

def matches?(table)
  @table = table
  @table.key?(expected_key.name) &&
    @table.key(expected_key.name) == expected_key
end