Module: Webhookdb::SpecHelpers::Whdb

Defined in:
lib/webhookdb/spec_helpers/whdb.rb

Overview

Set :whdbisolation metadatabase. Use the isolation mode, or :reset to just reset config before and after the spec.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.assign_connection_urls(o, **more) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/webhookdb/spec_helpers/whdb.rb', line 43

module_function def assign_connection_urls(o, **more)
  u = Webhookdb::Organization::DbBuilder.available_server_urls.sample
  raise "no server url?" if u.blank?
  o.update(
    readonly_connection_url_raw: u,
    admin_connection_url_raw: u,
    **more,
  )
end

.backfill(sint, **kw) ⇒ Object

Creates and returns a new backfill job for the given integration or replicator, and also runs the backfill.



122
123
124
125
126
127
# File 'lib/webhookdb/spec_helpers/whdb.rb', line 122

module_function def backfill(sint, **kw)
  sint = sint.service_integration if sint.respond_to?(:service_integration)
  bfjob = Webhookdb::Fixtures.backfill_job.for(sint).create(**kw)
  sint.replicator.backfill(bfjob)
  return bfjob
end

.create_all_dependencies(service_integration) ⇒ Object

If a service has dependencies, and those dependencies have dependencies, create them recursively until all requirements are satisfied.



69
70
71
72
73
74
75
76
77
78
# File 'lib/webhookdb/spec_helpers/whdb.rb', line 69

module_function def create_all_dependencies(service_integration)
  sint = service_integration
  created = []
  loop do
    sint = create_dependency(sint)
    break if sint.nil?
    created << sint
  end
  return created
end

.create_dependency(service_integration) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/webhookdb/spec_helpers/whdb.rb', line 53

module_function def create_dependency(service_integration)
  return service_integration.depends_on unless service_integration.depends_on.nil?
  dependency_descriptor = service_integration.replicator.descriptor.dependency_descriptor
  if dependency_descriptor.present?
    dependency = Webhookdb::Fixtures.service_integration.create(
      organization: service_integration.organization,
      service_name: dependency_descriptor.name,
    )
    service_integration.update(depends_on: dependency)
    return dependency
  end
  return nil
end

.included(context) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/webhookdb/spec_helpers/whdb.rb', line 8

def self.included(context)
  context.before(:each) do |example|
    if (isomode = example.[:whdbisolation])
      Webhookdb::Organization::DbBuilder.isolation_mode = isomode if isomode.is_a?(String)
      Webhookdb::Organization::DbBuilder.reset_configuration if isomode == :reset
    end
    if (regress = example.[:regression_mode])
      Webhookdb.regression_mode = regress
    end
  end

  context.after(:each) do |example|
    if (mode = example.[:whdbisolation])
      Webhookdb::Organization.dataset.each(&:remove_related_database) if mode != :reset
      Webhookdb::Organization::DbBuilder.reset_configuration
    end
    Webhookdb.regression_mode = false if example.[:regression_mode]
  end

  context.around(:each) do |example|
    if example.[:fake_replicator]
      Webhookdb::Replicator::Fake.reset
      begin
        example.run
      ensure
        Webhookdb::Replicator::Fake.reset
      end
    else
      example.run
    end
  end

  super
end

.setup_dependencies(service_integration, insert_required_data_callback = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/webhookdb/spec_helpers/whdb.rb', line 80

module_function def setup_dependencies(service_integration, insert_required_data_callback=nil)
  service_integration.replicator.create_table
  dep_sint = service_integration.depends_on
  return [] if dep_sint.nil?
  org = service_integration.organization
  dep_repls = []
  while dep_sint
    # Make sure all service integration orgs are the same instance, since it may be mutated
    # when we set up urls.
    raise Webhookdb::InvariantViolation, "service integration orgs must match" unless
      dep_sint.organization === org
    dep_sint.organization = org
    dep_repl = dep_sint.replicator
    dep_repl.create_table
    dep_repls << dep_repl
    dep_sint = dep_sint.depends_on
    break if dep_sint.nil?
  end
  insert_required_data_callback&.call(*dep_repls)
  return dep_repls
end

.setup_upsert_webhook_example(this) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/webhookdb/spec_helpers/whdb.rb', line 102

module_function def setup_upsert_webhook_example(this)
  this.let(:request_path) { nil }
  this.let(:request_method) { nil }
  this.let(:request_body) { nil }
  this.let(:request_headers) { nil }
  this.let(:rack_request) { nil }
  this.let(:webhook_request) do
    Webhookdb::Replicator::WebhookRequest.new(
      body: request_body, method: request_method, path: request_path, headers: request_headers, rack_request:,
    )
  end
  this.define_method(:upsert_webhook) do |svc, **kw|
    params = {body: request_body, headers: request_headers, method: request_method, path: request_path, rack_request:}
    params.merge!(**kw)
    svc.upsert_webhook(Webhookdb::Replicator::WebhookRequest.new(**params))
  end
end

Instance Method Details

#refresh_row(row, replicator: nil) ⇒ Object



129
130
131
132
133
# File 'lib/webhookdb/spec_helpers/whdb.rb', line 129

def refresh_row(row, replicator: nil)
  replicator ||= svc
  raise "Must provide :replicator or have :svc available" if replicator.nil?
  return replicator.readonly_dataset { |ds| ds[pk: row.fetch(:pk)] }
end

#update_row(row, replicator: nil, **fields) ⇒ Object



135
136
137
138
139
# File 'lib/webhookdb/spec_helpers/whdb.rb', line 135

def update_row(row, replicator: nil, **fields)
  replicator ||= svc
  raise "Must provide :replicator or have :svc available" if replicator.nil?
  return replicator.admin_dataset { |ds| ds.where(pk: row.fetch(:pk)).update(fields) }
end