Skip to content

Commit 886cbb8

Browse files
committed
fix duplication
1 parent 4913a71 commit 886cbb8

1 file changed

Lines changed: 16 additions & 34 deletions

File tree

test/log_struct/integrations/good_job/log_subscriber_test.rb

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -278,23 +278,8 @@ def self.log_arguments?
278278
Object.const_set(:NoLogArgsJob, job_class)
279279

280280
begin
281-
# Create a mock job where job.class returns the actual job class
282-
# rubocop:disable Sorbet/ConstantsFromStrings
283-
klass = Object.const_get(:NoLogArgsJob)
284-
# rubocop:enable Sorbet/ConstantsFromStrings
285-
job = create_mock_job_with_class(klass, "job_123", "default")
286-
event_data = create_test_event({
287-
job: job,
288-
duration: 0.1
289-
})
290-
291-
@subscriber.enqueue(event_data)
292-
::SemanticLogger.flush
293-
294-
output = @log_output.string
295-
log = JSON.parse(output.lines.first.strip)
296-
297-
# Arguments should not be present when log_arguments? returns false
281+
log = run_enqueue_with_job_class(:NoLogArgsJob)
282+
298283
refute log.key?("arguments")
299284
ensure
300285
Object.send(:remove_const, :NoLogArgsJob)
@@ -333,23 +318,8 @@ def self.log_arguments?
333318
Object.const_set(:LogArgsJob, job_class)
334319

335320
begin
336-
# Create a mock job where job.class returns the actual job class
337-
# rubocop:disable Sorbet/ConstantsFromStrings
338-
klass = Object.const_get(:LogArgsJob)
339-
# rubocop:enable Sorbet/ConstantsFromStrings
340-
job = create_mock_job_with_class(klass, "job_123", "default")
341-
event_data = create_test_event({
342-
job: job,
343-
duration: 0.1
344-
})
345-
346-
@subscriber.enqueue(event_data)
347-
::SemanticLogger.flush
348-
349-
output = @log_output.string
350-
log = JSON.parse(output.lines.first.strip)
351-
352-
# Arguments should be present when log_arguments? returns true
321+
log = run_enqueue_with_job_class(:LogArgsJob)
322+
353323
assert log.key?("arguments")
354324
assert_equal ["arg1", "arg2"], log["arguments"]
355325
ensure
@@ -401,6 +371,18 @@ def create_mock_job_with_class(klass, job_id, queue_name, extra_attributes = {})
401371
mock
402372
end
403373

374+
# Run an enqueue event with a job class that has log_arguments? method and return the parsed log
375+
def run_enqueue_with_job_class(job_class)
376+
# rubocop:disable Sorbet/ConstantsFromStrings
377+
klass = Object.const_get(job_class)
378+
# rubocop:enable Sorbet/ConstantsFromStrings
379+
job = create_mock_job_with_class(klass, "job_123", "default")
380+
event_data = create_test_event({job: job, duration: 0.1})
381+
@subscriber.enqueue(event_data)
382+
::SemanticLogger.flush
383+
JSON.parse(@log_output.string.lines.first.strip)
384+
end
385+
404386
def create_mock_execution(attributes = {})
405387
OpenStruct.new({
406388
executions: 1,

0 commit comments

Comments
 (0)