Class: Elasticsearch::Tests::Test

Inherits:
Object
  • Object
show all
Includes:
CodeRunner
Defined in:
lib/elasticsearch/tests/test.rb

Overview

Represents a test, which is initialized in the test runner when iterating through the YAML files. Each YAML file can have more than one test. When a test is executed it runs the setup, actions (and matches) and finally the teardown stage.

Constant Summary

Constants included from CodeRunner

CodeRunner::COMPARATORS

Instance Method Summary collapse

Methods included from CodeRunner

#compare, #do_action, #do_length, #do_match, #expected_exception?, #is_false, #is_true, #match_regexp, #set_variable

Methods included from Printer

display_errors, display_summary, #print_error, #print_failure, #print_match_failure, #print_success

Constructor Details

#initialize(yaml, file, client) ⇒ Test

Returns a new instance of Test.



31
32
33
34
35
36
37
38
39
40
# File 'lib/elasticsearch/tests/test.rb', line 31

def initialize(yaml, file, client)
  @setup = extract_setup!(yaml)
  @teardown = extract_teardown!(yaml)
  @title = yaml.first.keys.first
  @actions = yaml.first[@title]
  @file = file
  name = file.split('/')
  @short_name = "#{name[-2]}/#{name[-1]}"
  @client = client
end

Instance Method Details

#countObject



94
95
96
# File 'lib/elasticsearch/tests/test.rb', line 94

def count
  @actions.select { |a| a.keys.first == 'do' }.count
end

#executeObject



42
43
44
45
46
47
48
49
# File 'lib/elasticsearch/tests/test.rb', line 42

def execute
  begin
    run_setup
    run_actions
  ensure
    run_teardown
  end
end

#extract_setup!(yaml) ⇒ Object



98
99
100
101
102
# File 'lib/elasticsearch/tests/test.rb', line 98

def extract_setup!(yaml)
  yaml.map.with_index do |a, i|
    yaml.delete_at(i) if a.keys.first == 'setup'
  end.compact.first&.[]('setup')
end

#extract_teardown!(yaml) ⇒ Object



104
105
106
107
108
# File 'lib/elasticsearch/tests/test.rb', line 104

def extract_teardown!(yaml)
  yaml.map.with_index do |a, i|
    yaml.delete_at(i) if a.keys.first == 'teardown'
  end.compact.first
end

#run_action(action) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/elasticsearch/tests/test.rb', line 57

def run_action(action)
  definition = action.keys.first

  case definition
  when 'do'
    do_action(action['do'])
  when 'set'
    set_variable(action)
  when 'match'
    do_match(action)
  when 'length'
    do_length(action)
  when 'is_true'
    is_true(action)
  when 'is_false'
    is_false(action)
  when 'gt', 'gte', 'lt', 'lte'
    compare(action)
  end
rescue StandardError => e
  raise ActionError.new(e.message, @file, action)
end

#run_actionsObject



51
52
53
54
55
# File 'lib/elasticsearch/tests/test.rb', line 51

def run_actions
  return unless @actions

  @actions.map { |action| run_action(action) }
end

#run_setupObject



80
81
82
83
84
85
86
# File 'lib/elasticsearch/tests/test.rb', line 80

def run_setup
  return unless @setup

  @setup.map do |step|
    do_action(step['do']) if step['do']
  end
end

#run_teardownObject



88
89
90
91
92
# File 'lib/elasticsearch/tests/test.rb', line 88

def run_teardown
  return unless @teardown

  @teardown['teardown'].map { |step| do_action(step['do']) }
end