Class: TestData

Inherits:
Object show all
Defined in:
lib/test_data.rb

Overview

The TestData class holds all the shared state for test cases

Constant Summary collapse

RW_DELAY_MS =
250

Instance Method Summary collapse

Constructor Details

#initialize(test_case, job: nil, data: nil) ⇒ TestData

Public: Initializes a TestData object.

test_case - TestCase (subclass) instance. job - Integer or String job (default: nil). data - String name of file to use for test data (default: nil).

Returns nothing.



15
16
17
18
19
20
21
22
23
# File 'lib/test_data.rb', line 15

def initialize(test_case, job: nil, data: nil)
  @seq = 0
  @data = {}
  @test_case = test_case
  @job = job
  @data_file = data
  @promiscuous = true  #TODO: DEPRECATED
  @w_lock_obj = "test-data-#{@job}"
end

Instance Method Details

#[](key) ⇒ Object

Public: Gets a data attribute.

key - Symbol data attribute to get.

Returns Object value, or nil.



80
81
82
83
84
# File 'lib/test_data.rb', line 80

def [](key)
  value = current_data[key.to_sym]
  logger.debug("Retrieved test data: #{key} => #{value.inspect}")
  value
end

#[]=(key, value) ⇒ Object

Public: Updates a data attribute.

key - Symbol data attribute to update. value - Object value to set.

Returns Object value that was set.



92
93
94
95
96
97
98
99
# File 'lib/test_data.rb', line 92

def []=(key, value)
  @test_case.lock(@w_lock_obj, 1.min) do
    logger.debug("Assigning test data: #{key} => #{value.inspect}")
    current_data[key.to_sym] = value
    update(nil)
    value
  end
end

#delete(key) ⇒ Object

Public: Deletes a data attribute.

key - Symbol data attribute to delete.

Returns Object value of deleted attribute.



123
124
125
126
127
128
129
130
# File 'lib/test_data.rb', line 123

def delete(key)
  @test_case.lock(@w_lock_obj, 1.min) do
    value = current_data.delete(key.to_sym)
    logger.debug("Deleted test data: #{key} => #{value.inspect}")
    update(nil)
    value
  end
end

#dig(*keys) ⇒ Object

Public: Digs a data attribute.

keys - Splat Array of Symbol data attribute hierarchy to dig.

Returns Object value, or nil.



57
58
59
60
61
62
# File 'lib/test_data.rb', line 57

def dig(*keys)
  sym_keys = keys.map {|k| k.to_sym}
  value = current_data.dig(*sym_keys)
  logger.debug("Retrieved test data: #{keys.join('/')} => #{value.inspect}")
  value
end

#empty?Boolean

Public: Checks if test data is empty.

Returns a Boolean true if test data is empty, otherwise false.

Returns:

  • (Boolean)


48
49
50
# File 'lib/test_data.rb', line 48

def empty?
  current_data.empty?
end

#fetch(key, *opts) ⇒ Object

Public: Fetches a data attribute.

key - Symbol data attribute to fetch. opts - Splat Array of additional options to ‘fetch’. Normally just a default to return if nil.

Returns Object value, or returns the specified default if not exists. Raises an Exception if the value did not exist and no default specified.



108
109
110
111
112
113
114
115
116
# File 'lib/test_data.rb', line 108

def fetch(key, *opts)
  begin
    value = current_data.fetch(key.to_sym, *opts)
    logger.debug("Fetched test data: #{key} => #{value.inspect}")
  rescue IndexError
    raise "Key not found in test data: #{key}"
  end
  value
end

#key?(key) ⇒ Boolean

Public: Checks if a data attribute is present.

key - Symbol data attribute to check.

Returns Boolean true or false.

Returns:

  • (Boolean)


69
70
71
72
73
# File 'lib/test_data.rb', line 69

def key?(key)
  ret = current_data.key?(key.to_sym)
  logger.debug("Key #{ret ? '' : 'not '}found in test data: #{key}")
  ret
end

#promiscuous=(val) ⇒ Object

Public: DEPRECATED

val - Boolean indicating whether test data should be promiscuous; if true, test data will be refreshed on every

access.

Returns nothing.



31
32
33
34
35
36
# File 'lib/test_data.rb', line 31

def promiscuous=(val)
  unless val == true || val == false
    raise "Invalid promiscuous value: #{val}"
  end
  @promiscuous = val
end

#promiscuous?Boolean

Public: DEPRECATED

Returns a Boolean true if test data is promiscuous, otherwise false.

Returns:

  • (Boolean)


41
42
43
# File 'lib/test_data.rb', line 41

def promiscuous?
  @promiscuous
end

#refreshObject

Public: Refreshes test data.

Returns refreshed test data Hash.



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/test_data.rb', line 165

def refresh
  json = delay(RW_DELAY_MS) do
    @test_case.send(:tmc_get, "/api/jobs/#{@job}/data?seq=#{@seq}")
  end
  no_update = false
  begin
    if @data_file.nil?
      @test_case.refute_nil(json)
      json = json['GetTestDataResult'] unless json['GetTestDataResult'].nil?  # server bug
      no_update = json['testData'].nil?
      unless no_update
        @seq = json['seq'] unless json['seq'].nil?
        json = json['testData']
      end
    elsif json.nil?
      # Fall back to local data file, if provided
      path = File.join(Dir.pwd, 'conf', @data_file)
      json = JSON.parse(File.read(path))
    end
    unless no_update
      @data.clear
      @data.update(@test_case.rubify(json, snake_case: false))
    end
  rescue => e
    raise "Failed to refresh test data! #{e.message}\n\t#{e.backtrace.join("\n\t")}"
  end
  @data
end

#update(obj) ⇒ Object

Public: Updates test data.

obj - Hash from which to update test data.

Returns updated test data Hash.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/test_data.rb', line 137

def update(obj)
  @test_case.lock(@w_lock_obj, 1.min) do
    unless obj.nil?
      logger.debug("Updating test data: #{obj.inspect}")
      current_data.update(@test_case.rubify(obj, snake_case: false))
    end
    data = @test_case.jsonify(@data, camel_case: false)
    resp = delay(RW_DELAY_MS) do
      @test_case.send(:tmc_put, "/api/jobs/#{@job}/data", json: data)
    end
    begin
      if @data_file.nil?
        @test_case.refute_nil(resp)
      elsif resp.nil?
        # Fall back to local data file, if provided
        path = File.join(Dir.pwd, 'conf', @data_file)
        File.write(path, JSON.generate(data, {indent: '  ', space: ' ', object_nl: "\n", array_nl: "\n"}))
      end
    rescue => e
      raise "Failed to update test data! #{e.message}\n\t#{e.backtrace.join("\n\t")}"
    end
    @data
  end
end