Module: Webhookdb::SpecHelpers

Defined in:
lib/webhookdb/spec_helpers.rb

Defined Under Namespace

Modules: Async, Citest, Message, Postgres, Service, Whdb

Constant Summary collapse

TEST_DATA_DIR =

The directory to look in for fixture data

Pathname("spec/data").expand_path

Class Method Summary collapse

Class Method Details

.included(context) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/webhookdb/spec_helpers.rb', line 17

def self.included(context)
  context.before(:all) do
    Webhookdb::Customer.password_hash_cost = 1
  end
  context.before(:each) do
    allow(Kernel).to receive(:sleep) do |n|
      raise "Never sleep with > 0 during tests" if n.positive?
    end
  end
  super
end

.json_headers(**more) ⇒ Object



33
34
35
# File 'lib/webhookdb/spec_helpers.rb', line 33

module_function def json_headers(**more)
  return {"Content-Type" => "application/json"}.merge(**more)
end

.json_response(body, status: 200, headers: {}) ⇒ Object



37
38
39
# File 'lib/webhookdb/spec_helpers.rb', line 37

module_function def json_response(body, status: 200, headers: {})
  return {status:, body: body.to_json, headers: json_headers(**headers)}
end

.load_fixture_data(name, raw: false) ⇒ Object

Load data from the spec/data directory with the specified name, deserializing it if it’s YAML or JSON, and returning it.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/webhookdb/spec_helpers.rb', line 43

module_function def load_fixture_data(name, raw: false)
  name = name.to_s
  path = TEST_DATA_DIR + name
  path = TEST_DATA_DIR + "#{name}.json" unless path.exist? || !File.extname(name).empty?
  path = TEST_DATA_DIR + "#{name}.yaml" unless path.exist? || !File.extname(name).empty?
  path = TEST_DATA_DIR + "#{name}.yml" unless path.exist? || !File.extname(name).empty?

  rawdata = path.read(encoding: "utf-8")

  return rawdata if raw

  return case path.extname
    when ".json"
      Oj.load(rawdata)
    when ".yml", ".yaml"
      YAML.safe_load(rawdata)
    else
      rawdata
  end
end

.test_data_dirObject



29
30
31
# File 'lib/webhookdb/spec_helpers.rb', line 29

module_function def test_data_dir
  return TEST_DATA_DIR
end