debian-mirror-gitlab/spec/support/helpers/snowplow_helpers.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

90 lines
2.5 KiB
Ruby
Raw Permalink Normal View History

2020-11-24 15:15:51 +05:30
# frozen_string_literal: true
module SnowplowHelpers
# Asserts call for one snowplow event from `Gitlab::Tracking#event`.
#
# @param [Hash]
#
# Examples:
#
2022-10-11 01:57:18 +05:30
# describe '#show' do
2020-11-24 15:15:51 +05:30
# it 'tracks snowplow events' do
# get :show
#
# expect_snowplow_event(category: 'Experiment', action: 'start')
# end
# end
#
2022-10-11 01:57:18 +05:30
# describe '#create' do
2020-11-24 15:15:51 +05:30
# it 'tracks snowplow events' do
# post :create
#
# expect_snowplow_event(
# category: 'Experiment',
# action: 'created',
# )
# expect_snowplow_event(
# category: 'Experiment',
# action: 'accepted',
# property: 'property',
# label: 'label'
# )
# end
# end
2021-02-22 17:27:13 +05:30
#
# Passing context:
#
# Simply provide a hash that has the schema and data expected.
#
# expect_snowplow_event(
# category: 'Experiment',
# action: 'created',
# context: [
# {
# schema: 'iglu:com.gitlab/.../0-3-0',
# data: { key: 'value' }
# }
# ]
# )
2023-06-20 00:43:36 +05:30
def expect_snowplow_event(category:, action:, context: nil, tracking_method: :event, **kwargs)
2021-02-22 17:27:13 +05:30
if context
2022-01-26 12:08:38 +05:30
if context.is_a?(Array)
kwargs[:context] = []
context.each do |c|
expect(SnowplowTracker::SelfDescribingJson).to have_received(:new)
.with(c[:schema], c[:data]).at_least(:once)
kwargs[:context] << an_instance_of(SnowplowTracker::SelfDescribingJson)
end
else
kwargs[:context] = context
2021-02-22 17:27:13 +05:30
end
end
2023-06-20 00:43:36 +05:30
expect(Gitlab::Tracking).to have_received(tracking_method) # rubocop:disable RSpec/ExpectGitlabTracking
2021-01-29 00:20:46 +05:30
.with(category, action, **kwargs).at_least(:once)
2020-11-24 15:15:51 +05:30
end
2021-06-08 01:23:25 +05:30
def match_snowplow_context_schema(schema_path:, context:)
expect(context).to match_snowplow_schema(schema_path)
end
2020-11-24 15:15:51 +05:30
# Asserts that no call to `Gitlab::Tracking#event` was made.
#
# Example:
#
# describe '#show', :snowplow do
# it 'does not track any snowplow events' do
# get :show
#
# expect_no_snowplow_event
# end
# end
2023-06-20 00:43:36 +05:30
def expect_no_snowplow_event(category: nil, action: nil, tracking_method: :event, **kwargs)
2021-06-08 01:23:25 +05:30
if category && action
2023-06-20 00:43:36 +05:30
expect(Gitlab::Tracking).not_to have_received(tracking_method).with(category, action, **kwargs) # rubocop:disable RSpec/ExpectGitlabTracking
2021-06-08 01:23:25 +05:30
else
2023-06-20 00:43:36 +05:30
expect(Gitlab::Tracking).not_to have_received(tracking_method) # rubocop:disable RSpec/ExpectGitlabTracking
2021-06-08 01:23:25 +05:30
end
2020-11-24 15:15:51 +05:30
end
end