Module: AeEasy::Core::Mock::FakeExecutor

Defined in:
lib/ae_easy_override/core/mock/fake_executor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#input_dirObject

Current assigned input directory.



9
10
11
# File 'lib/ae_easy_override/core/mock/fake_executor.rb', line 9

def input_dir
  @input_dir
end

#root_input_dirObject

Root input directory.



6
7
8
# File 'lib/ae_easy_override/core/mock/fake_executor.rb', line 6

def root_input_dir
  @root_input_dir
end

Instance Method Details

#expand_relative_input(dir) ⇒ String

Expand a relative input directory.

Parameters:

  • dir (String, nil)

    Relative input directory

Returns:

  • (String)

    Absolute path



16
17
18
19
# File 'lib/ae_easy_override/core/mock/fake_executor.rb', line 16

def expand_relative_input dir
  return nil if dir.nil?
  File.expand_path File.join(root_input_dir, dir)
end

#load_expected_outputs(opts = {}) ⇒ FakeExecutor

Load expected outputs into executor from options or input files.

Parameters:

  • opts (Hash) (defaults to: {})

    ({}) Configuration options.

Options Hash (opts):

  • :input_dir (String, nil) — default: nil

    Will load files from this directory. The files map as follows (file_name -> variable): “` expected_outputs.json -> saved_outputs “`

  • :rel_dir (String, nil) — default: nil

    Same as :input_dir option but relative to root input directory (see #root_input_dir).

  • :outputs (Hash, nil)

    Outputs to load. It will override `expected_outputs.json` from input directory.

Returns:



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/ae_easy_override/core/mock/fake_executor.rb', line 166

def load_expected_outputs opts = {}
  opts = {
    rel_dir: nil,
    input_dir: nil,
    outputs: nil
  }.merge opts
  dir = opts[:input_dir] || expand_relative_input(opts[:rel_dir]) || self.input_dir

  # Load overrides
  save_outputs opts[:outputs] unless opts[:outputs].nil?

  # Load input files
  unless dir.nil?
    expected_outputs = AeEasy::Test::Helper.load_json_file(File.join(dir, 'expected_outputs.json'))
    save_outputs expected_outputs unless expected_outputs.nil?
  end

  self
end

#load_expected_pages(opts = {}) ⇒ FakeExecutor

Load expected pages into executor from options or input files.

Parameters:

  • opts (Hash) (defaults to: {})

    ({}) Configuration options.

Options Hash (opts):

  • :input_dir (String, nil) — default: nil

    Will load files from this directory. The files map as follows (file_name -> variable): “` expected_pages.json -> saved_pages “`

  • :rel_dir (String, nil) — default: nil

    Same as :input_dir option but relative to root input directory (see #root_input_dir).

  • :pages (Hash, nil)

    Pages to load. It will override `expected_pages.json` from input directory.

Returns:



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/ae_easy_override/core/mock/fake_executor.rb', line 132

def load_expected_pages opts = {}
  opts = {
    rel_dir: nil,
    input_dir: nil,
    pages: nil
  }.merge opts
  dir = opts[:input_dir] || expand_relative_input(opts[:rel_dir]) || self.input_dir

  # Load overrides
  save_pages opts[:pages] unless opts[:pages].nil?

  # Load input files
  unless dir.nil?
    expected_pages = AeEasy::Test::Helper.load_json_file(File.join(dir, 'expected_pages.json'))
    save_pages expected_pages unless expected_pages.nil?
  end

  self
end

#load_failed_content(opts = {}) ⇒ FakeExecutor

Load failed content into executor from options or input files.

Parameters:

  • opts (Hash) (defaults to: {})

    ({}) Configuration options.

Options Hash (opts):

  • :input_dir (String, nil) — default: nil

    Will load files from this directory. The files map as follows (file_name -> variable): “` failed_content.json -> failed_content “`

  • :rel_dir (String, nil) — default: nil

    Same as :input_dir option but relative to root input directory (see #root_input_dir).

  • :failed_content (Hash, nil)

    Failed content to load. It will override `failed_content.json` from input directory.

Returns:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ae_easy_override/core/mock/fake_executor.rb', line 99

def load_failed_content opts = {}
  opts = {
    rel_dir: nil,
    input_dir: nil,
    failed_content: nil
  }.merge opts
  dir = opts[:input_dir] || expand_relative_input(opts[:rel_dir]) || self.input_dir

  # Load overrides
  self.failed_content = opts[:failed_content]

  # Load input files
  unless dir.nil?
    self.failed_content ||= AeEasy::Test::Helper.load_file(File.join(dir, 'failed_content.json'))
  end

  self
end

#load_input(opts = {}) ⇒ FakeExecutor

Load data into executor from options or input files.

Parameters:

  • opts (Hash) (defaults to: {})

    ({}) Configuration options.

Options Hash (opts):

  • :input_dir (String, nil) — default: nil

    Will load files from this directory. The files map as follows (file_name -> variable): “` content -> content page.json -> page vars.json -> page pages.json -> saved_pages outputs.json -> saved_outputs “`

  • :rel_dir (String, nil) — default: nil

    Same as :input_dir option but relative to root input directory (see #root_input_dir).

  • :content (String, nil)

    Content to load. It will override `content` file from input directory.

  • :page (Hash, nil)

    Page to load. It will override `page.json` from input directory.

  • :vars (Hash, nil)

    Variables to load. It will override `vars.json` from input directory.

  • :pages (Hash, nil)

    Pages to load. It will override `pages.json` from input directory.

  • :outputs (Hash, nil)

    Outputs to load. It will override `outputs.json` from input directory.

Returns:



47
48
49
50
51
52
53
54
55
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
83
# File 'lib/ae_easy_override/core/mock/fake_executor.rb', line 47

def load_input opts = {}
  opts = {
    rel_dir: nil,
    input_dir: nil,
    content: nil,
    page: nil,
    vars: nil,
    pages: nil,
    outputs: nil
  }.merge opts
  dir = self.input_dir = opts[:input_dir] || expand_relative_input(opts[:rel_dir]) || self.input_dir

  # Load overrides
  self.content = opts[:content]
  new_page = AeEasy::Core.deep_stringify_keys(opts[:page]) unless opts[:page].nil?
  save_pages opts[:pages] unless opts[:pages].nil?
  save_outputs opts[:outputs] unless opts[:outputs].nil?
  vars = nil
  vars = AeEasy::Core.deep_stringify_keys(opts[:vars]) unless opts[:vars]

  # Load input files
  unless dir.nil?
    self.content ||= AeEasy::Test::Helper.load_file(File.join(dir, 'content'))
    new_page ||= AeEasy::Test::Helper.load_json_file(File.join(dir, 'page.json'))
    input_pages = AeEasy::Test::Helper.load_json_file(File.join(dir, 'pages.json'))
    save_pages input_pages unless input_pages.nil?
    input_outputs = AeEasy::Test::Helper.load_json_file(File.join(dir, 'outputs.json'))
    save_outputs outputs unless input_outputs.nil?
    input_vars = AeEasy::Test::Helper.load_json_file(File.join(dir, 'vars.json'))
    vars ||= input_vars if opts[:page].nil?
  end

  # Load vars only when no page override and not nil
  self.page = new_page unless new_page.nil?
  page['vars'] = vars unless vars.nil?
  self
end

#match_expected_outputs(opts = {}) ⇒ Hash

Match expected outputs.

Parameters:

  • opts (Hash) (defaults to: {})

    ({}) Configuration options.

Options Hash (opts):

  • :input_dir (String, nil) — default: nil

    Will load files from this directory. The files map as follows (file_name -> description): “` expected_outputs.json -> expected outputs to compare with saved_outputs. “`

  • :rel_dir (String, nil) — default: nil

    Same as :input_dir option but relative to root input directory (see #root_input_dir).

  • :outputs (Hash, nil)

    Expected outputs to load. It will override `expected_outputs.json` from input directory.

  • :skip_fields (Array) — default: nil

    Fields to skip on match.

  • :default_skip_fields (Boolean) — default: true

    Add `_gid`, `_job_id` and `_created_at` to the `:skip_fields` list when `true`.

Returns:

  • (Hash)

    A hash with the following structure:

    • `[Boolean] match` `true` when match, `false` when diff.

    • `[Hash] expected` Non matching expected outputs.

    • `[Hash] saved` Non matching saved outputs.



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/ae_easy_override/core/mock/fake_executor.rb', line 285

def match_expected_outputs opts = {}
  opts = {
    rel_dir: nil,
    input_dir: nil,
    outputs: nil,
    skip_fields: [],
    default_skip_fields: true,
  }.merge opts

  # Expected context
  expected_opts = {}.merge opts
  expected_opts[:input_dir] ||= input_dir
  expected = new_executor
  expected.root_input_dir = root_input_dir
  expected.load_expected_outputs expected_opts

  # Config skip fields
  skip_fields = opts[:skip_fields]
  skip_fields += ['_created_at', '_gid', '_job_id'] if opts[:default_skip_fields]
  skip_fields.uniq!

  # Diff
  diff = AeEasy::Test::Helper.match_collections(
    saved_outputs,
    expected.saved_outputs,
    skip: skip_fields,
    compare_way: :left
  )
  {
    match: diff[:match],
    saved: diff[:diff][:items_a],
    expected: diff[:diff][:items_b]
  }
end

#match_expected_pages(opts = {}) ⇒ Hash

Match expected pages.

Parameters:

  • opts (Hash) (defaults to: {})

    ({}) Configuration options.

Options Hash (opts):

  • :input_dir (String, nil) — default: nil

    Will load files from this directory. The files map as follows (file_name -> description): “` expected_pages.json -> expected pages to compare with saved_pages. “`

  • :rel_dir (String, nil) — default: nil

    Same as :input_dir option but relative to root input directory (see #root_input_dir).

  • :pages (Hash, nil)

    Expected pages to load. It will override `expected_pages.json` from input directory.

  • :skip_fields (Array) — default: nil

    Fields to skip on match.

  • :default_skip_fields (Boolean) — default: true

    Add `gid` and `job_id` to the `:skip_fields` list when `true`.

Returns:

  • (Hash)

    A hash with the following fields:

    • `[Boolean] match` `true` when match, `false` when diff.

    • `[Hash] saved` Non matching saved pages.

    • `[Hash] expected` Non matching expected pages.



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/ae_easy_override/core/mock/fake_executor.rb', line 213

def match_expected_pages opts = {}
  opts = {
    rel_dir: nil,
    input_dir: nil,
    pages: nil,
    skip_fields: [],
    default_skip_fields: true,
  }.merge opts
  opts[:input_dir] ||= input_dir

  # Expected context
  expected_opts = {}.merge opts
  expected_opts[:input_dir] ||= input_dir
  expected = new_executor
  expected.root_input_dir = root_input_dir
  expected.load_expected_pages expected_opts

  # Config skip fields
  skip_fields = opts[:skip_fields]
  skip_fields += ['gid', 'job_id'] if opts[:default_skip_fields]
  skip_fields.uniq!

  # Diff
  diff = AeEasy::Test::Helper.match_collections(
    saved_pages,
    expected.saved_pages,
    skip: skip_fields,
    compare_way: :left
  )
  {
    match: diff[:match],
    saved: diff[:diff][:items_a],
    expected: diff[:diff][:items_b]
  }
end

#new_executorAeEasy::Core::Mock::FakeExecutor

Create an executor based on the current executor type.



189
190
191
# File 'lib/ae_easy_override/core/mock/fake_executor.rb', line 189

def new_executor
  self.class.new
end

#should_match_outputs(opts = {}) ⇒ Boolean

Match saved outputs with expected and verbose diff. Test::Helper#match_expected_outputs

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :log_caller (Array) — default: nil

    Log caller. Defaults to method `caller`.

Returns:

  • (Boolean)

    `true` when pass, else `false`.



326
327
328
329
330
331
332
333
334
# File 'lib/ae_easy_override/core/mock/fake_executor.rb', line 326

def should_match_outputs opts = {}
  flush
  diff = match_expected_outputs opts
  log_caller = opts[:log_caller] || ([] + caller)
  unless diff[:match]
    AeEasy::Test.verbose_match_diff 'outputs', diff, log_caller
  end
  diff[:match]
end

#should_match_pages(opts = {}) ⇒ Boolean

Match saved pages with expected and verbose diff. Test::Helper#match_expected

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :log_caller (Array) — default: nil

    Log caller. Defaults to method `caller`.

Returns:

  • (Boolean)

    `true` when pass, else `false`.



255
256
257
258
259
260
261
262
263
# File 'lib/ae_easy_override/core/mock/fake_executor.rb', line 255

def should_match_pages opts = {}
  flush
  diff = match_expected_pages opts
  log_caller = opts[:log_caller] || ([] + caller)
  unless diff[:match]
    AeEasy::Test.verbose_match_diff 'pages', diff, log_caller
  end
  diff[:match]
end