Class: MySQLExpectations::Matchers::HaveField

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql_expectations/matchers/table_have_field.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_field_name) ⇒ HaveField

Returns a new instance of HaveField.



14
15
16
17
18
19
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 14

def initialize(expected_field_name)
  @expected_field_name = expected_field_name
  @expected_type = nil
  @expected_nullable = nil
  @table = nil
end

Instance Attribute Details

#expected_field_nameObject (readonly)

Returns the value of attribute expected_field_name.



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

def expected_field_name
  @expected_field_name
end

#expected_nullableObject (readonly)

Returns the value of attribute expected_nullable.



12
13
14
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 12

def expected_nullable
  @expected_nullable
end

#expected_typeObject (readonly)

Returns the value of attribute expected_type.



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

def expected_type
  @expected_type
end

#tableObject (readonly)

Returns the value of attribute table.



12
13
14
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 12

def table
  @table
end

Instance Method Details

#actual_nullableObject



36
37
38
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 36

def actual_nullable
  table.field(expected_field_name).nullable?
end

#actual_typeObject



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

def actual_type
  table.field(expected_field_name).type
end

#descriptionObject



52
53
54
55
56
57
58
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 52

def description
  description = "have field '#{expected_field_name}'"
  description << " of type '#{expected_type}'" if expected_type
  description << ' that is nullable' if expected_nullable == true
  description << ' that is not nullable' if expected_nullable == false
  description
end

#failure_messageObject



81
82
83
84
85
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 81

def failure_message
  return field_error unless field_match?
  return type_error unless type_match?
  return nullable_error unless nullable_match?
end

#failure_message_when_negatedObject



87
88
89
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 87

def failure_message_when_negated
  field_error(negated: true)
end

#field_error(negated: false) ⇒ Object



60
61
62
63
64
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 60

def field_error(negated: false)
  "expected '#{table.name}' table" \
    " #{negated ? 'not ' : ''}to "\
    "have field '#{expected_field_name}'."
end

#field_match?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 21

def field_match?
  table.field?(expected_field_name)
end

#matches?(table) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 47

def matches?(table)
  @table = table
  field_match? && type_match? && nullable_match?
end

#nullable_errorObject



71
72
73
74
75
76
77
78
79
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 71

def nullable_error
  message = "expected '#{table.name}.#{expected_field_name}' field "
  message << 'to be '
  message << 'not ' unless expected_nullable
  message << 'nullable but it was '
  message << 'not ' unless actual_nullable
  message << 'nullable.'
  message
end

#nullable_match?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 40

def nullable_match?
  if field_match? && !expected_nullable.nil?
    return actual_nullable == expected_nullable
  end
  true
end

#of_type(expected_type) ⇒ Object



91
92
93
94
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 91

def of_type(expected_type)
  @expected_type = expected_type
  self
end

#that_is_not_nullableObject



101
102
103
104
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 101

def that_is_not_nullable
  @expected_nullable = false
  self
end

#that_is_nullableObject



96
97
98
99
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 96

def that_is_nullable
  @expected_nullable = true
  self
end

#type_errorObject



66
67
68
69
# File 'lib/mysql_expectations/matchers/table_have_field.rb', line 66

def type_error
  "expected '#{table.name}.#{expected_field_name}' field " \
    "to be of type '#{expected_type}' but it was '#{actual_type}'"
end

#type_match?Boolean

Returns:

  • (Boolean)


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

def type_match?
  if field_match? && !expected_type.nil?
    return actual_type == expected_type
  end
  true
end