Module: MatcherHelpers

Defined in:
lib/squcumber-postgres/support/matchers.rb

Instance Method Summary collapse

Instance Method Details

#convert_mock_value(value) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/squcumber-postgres/support/matchers.rb', line 84

def convert_mock_value(value)
  value_parser_regexp = /\s*((?<modifier>(beginning|end))\s+of\s+(?<modifier_base>day|month|year))?\s*(?<placeholder>[^\(\)]+)\s*(\(as (?<format>day|month|year|(custom '[^']+'))\))?\s*/

  parsed_value = value.match(value_parser_regexp)
  placeholder = parsed_value[:placeholder]
  format = parsed_value[:format]
  modifier = parsed_value[:modifier]
  modifier_base = parsed_value[:modifier_base]

  new_value = case placeholder
    when /today/
      Date.today
    when /yesterday/
      Date.today.prev_day
    when /tomorrow/
      Date.today.next_day
    when /last month/
      Date.today.prev_month
    when /next month/
      Date.today.next_month
    when /last year/
      Date.today.prev_year
    when /next year/
      Date.today.next_year
    when /\s*\d+\s+minute(s)?\s+ago\s*?/
      number_of_minutes = value.match(/\d+/)[0].to_i
      DateTime.now - (number_of_minutes/24.0/60.0)
    when /\s*\d+\s+hours(s)?\s+ago\s*?/
      number_of_hours = value.match(/\d+/)[0].to_i
      DateTime.now - (number_of_hours/24.0)
    when /\s*\d+\s+month(s)?\s+ago\s*?/
      number_of_months = value.match(/\d+/)[0].to_i
      Date.today.prev_month(number_of_months)
    when /\s*\d+\s+day(s)?\s+ago\s*/
      number_of_days = value.match(/\d+/)[0].to_i
      Date.today.prev_day(number_of_days)
    when /\s*\d+\s+year(s)?\s+ago\s*/
      number_of_years = value.match(/\d+/)[0].to_i
      Date.today.prev_year(number_of_years)
    when /\s*\d+\s+minute(s)?\s+from now\s*?/
      number_of_minutes = value.match(/\d+/)[0].to_i
      DateTime.now + (number_of_minutes/24.0/60.0)
    when /\s*\d+\s+hours(s)?\s+from now\s*?/
      number_of_hours = value.match(/\d+/)[0].to_i
      DateTime.now + (number_of_hours/24.0)
    when /\s*\d+\s+month(s)?\s+from now\s*?/
      number_of_months = value.match(/\d+/)[0].to_i
      Date.today.next_month(number_of_months)
    when /\s*\d+\s+day(s)?\s+from now\s*/
      number_of_days = value.match(/\d+/)[0].to_i
      Date.today.next_day(number_of_days)
    when /\s*\d+\s+year(s)?\s+from now\s*/
      number_of_years = value.match(/\d+/)[0].to_i
      Date.today.next_year(number_of_years)
    else
      placeholder
  end

  if new_value.is_a?(Date)
    modified_new_value = case modifier
      when nil
        new_value
      when 'beginning'
        case modifier_base
          when 'day'
            new_value
          when 'month'
            Date.new(new_value.year, new_value.month, 1)
          when 'year'
            Date.new(new_value.year, 1, 1)
          else
            raise "Invalid date modifier provided: #{modifier} #{modifier_base}"
        end
      when 'end'
        case modifier_base
          when 'day'
            new_value
          when 'month'
            Date.new(new_value.next_month.year, new_value.next_month.month, 1).prev_day
          when 'year'
            Date.new(new_value.next_year.year, 1, 1).prev_day
          else
            raise "Invalid date modifier provided: #{modifier} #{modifier_base}"
        end
      else
        raise "Invalid date modifier provided: #{modifier} #{modifier_base}"
    end

    formatted_new_value = case format
      when nil
        modified_new_value.to_s
      when 'day', 'month', 'year'
        modified_new_value.send(format.to_sym)
      when /custom '[^']+'/
        parsed_format = format.match(/custom '(?<format_string>[^']+)'/)
        modified_new_value.strftime(parsed_format[:format_string])
      else
        raise "Invalid date format provided: #{format}"
    end

    formatted_new_value.to_s
  else
    value
  end
end

#convert_mock_values(mock_data) ⇒ Object



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
# File 'lib/squcumber-postgres/support/matchers.rb', line 56

def convert_mock_values(mock_data)
  mock_data.map do |entry|
    entry.each do |key, value|
      # Examples of valid values; all examples assume that today is 30th July 2017
      #
      #    1 month ago
      #    => '2017-06-30'
      #
      #    40 days ago (as month)
      #    => '6'
      #
      #    2 years ago (as year)
      #    => '2015'
      #
      #    beginning of month last month
      #    => '2017-06-01'
      #
      #    end of month last year
      #    => '2016-12-31'
      #
      #    today (as custom '%Y-%m')
      #    => '2017-07'
      #
      entry[key] = convert_mock_value(value)
    end
  end
end

#convert_null_value(value, null) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/squcumber-postgres/support/matchers.rb', line 46

def convert_null_value(value, null)
  if null.nil?
    value
  elsif value.eql?(null)
    nil
  else
    value
  end
end

#convert_null_values(mock_data, null) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/squcumber-postgres/support/matchers.rb', line 38

def convert_null_values(mock_data, null)
  mock_data.map do |entry|
    entry.each do |key, value|
      entry[key] = convert_null_value(value, null)
    end
  end
end

#sanity_check_result(actual, expected) ⇒ Object



2
3
4
5
6
7
8
9
10
11
# File 'lib/squcumber-postgres/support/matchers.rb', line 2

def sanity_check_result(actual, expected)
  raise("The returned result is empty") if actual.empty?
  raise("No data provided for comparison") if expected.empty?

  expected[0].keys.each do |expected_key|
    unless actual[0].keys.include?(expected_key)
      raise("Column name '#{expected_key}' does not exist in result.\nAvailable column names are #{actual[0].keys.join(', ')}")
    end
  end
end

#values_match(actual, expected, null = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/squcumber-postgres/support/matchers.rb', line 13

def values_match(actual, expected, null=nil)
  if expected.eql?('today')
    actual.match(/#{Regexp.quote(Date.today.to_s)}/)
  elsif expected.eql?('yesterday')
    actual.match(/#{Regexp.quote((Date.today - 1).to_s)}/)
  elsif expected.eql?('any_date')
    actual.match(/^\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}$/)
  elsif expected.eql?('sometime today')
    actual.match(/^#{Regexp.quote(Date.today.to_s)} \d{2}:\d{2}:\d{2}$/)
  elsif expected.eql?('sometime yesterday')
    actual.match(/^#{Regexp.quote((Date.today - 1).to_s)} \d{2}:\d{2}:\d{2}$/)
  elsif !null.nil? and !expected.nil? and expected.eql?(null)
    actual.nil?
  elsif expected.eql?('any_string')
    true if actual.is_a?(String) or actual.nil?
  elsif expected.eql?('false') or expected.eql?('true')
    true if actual.eql?(expected[0])
  elsif !expected.nil?
    actual ||= ''
    actual.eql?(expected)
  else  # we have not mocked this, so ignore it
    true
  end
end