Welcome to mirror list, hosted at ThFree Co, Russian Federation.

background_migrations_matchers.rb « matchers « support « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d3833a1e8e8ca746bc903937d4fed909f3cd4237 (plain)
1
2
3
4
5
6
7
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
42
43
44
45
46
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
84
85
86
87
88
89
90
91
92
93
94
95
96
# frozen_string_literal: true

RSpec::Matchers.define :be_background_migration_with_arguments do |arguments|
  define_method :matches? do |migration|
    expect do
      Gitlab::BackgroundMigration.perform(migration, arguments)
    end.not_to raise_error
  end
end

RSpec::Matchers.define :be_scheduled_delayed_migration do |delay, *expected|
  define_method :matches? do |migration|
    expect(migration).to be_background_migration_with_arguments(expected)

    BackgroundMigrationWorker.jobs.any? do |job|
      job['args'] == [migration, expected] &&
        job['at'].to_i == (delay.to_i + Time.now.to_i)
    end
  end

  failure_message do |migration|
    "Migration `#{migration}` with args `#{expected.inspect}` " \
      "not scheduled in expected time! Expected any of `#{BackgroundMigrationWorker.jobs.map { |j| j['args'] }}` to be `#{[migration, expected]}` " \
      "and any of `#{BackgroundMigrationWorker.jobs.map { |j| j['at'].to_i }}` to be `#{delay.to_i + Time.now.to_i}` (`#{delay.to_i}` + `#{Time.now.to_i}`)."
  end
end

RSpec::Matchers.define :be_scheduled_migration do |*expected|
  define_method :matches? do |migration|
    expect(migration).to be_background_migration_with_arguments(expected)

    BackgroundMigrationWorker.jobs.any? do |job|
      args = job['args'].size == 1 ? [BackgroundMigrationWorker.jobs[0]['args'][0], []] : job['args']
      args == [migration, expected]
    end
  end

  failure_message do |migration|
    "Migration `#{migration}` with args `#{expected.inspect}` not scheduled!"
  end
end

RSpec::Matchers.define :be_scheduled_migration_with_multiple_args do |*expected|
  define_method :matches? do |migration|
    expect(migration).to be_background_migration_with_arguments(expected)

    BackgroundMigrationWorker.jobs.any? do |job|
      args = job['args'].size == 1 ? [BackgroundMigrationWorker.jobs[0]['args'][0], []] : job['args']
      args[0] == migration && compare_args(args, expected)
    end
  end

  failure_message do |migration|
    "Migration `#{migration}` with args `#{expected.inspect}` not scheduled!"
  end

  def compare_args(args, expected)
    args[1].map.with_index do |arg, i|
      arg.is_a?(Array) ? same_arrays?(arg, expected[i]) : arg == expected[i]
    end.all?
  end

  def same_arrays?(arg, expected)
    arg.sort == expected.sort
  end
end

RSpec::Matchers.define :have_scheduled_batched_migration do |table_name: nil, column_name: nil, job_arguments: [], **attributes|
  define_method :matches? do |migration|
    # Default arguments passed by BatchedMigrationWrapper (values don't matter here)
    expect(migration).to be_background_migration_with_arguments([
      _start_id = 1,
      _stop_id = 2,
      table_name,
      column_name,
      _sub_batch_size = 10,
      _pause_ms = 100,
      *job_arguments
    ])

    batched_migrations =
      Gitlab::Database::BackgroundMigration::BatchedMigration
        .for_configuration(migration, table_name, column_name, job_arguments)

    expect(batched_migrations.count).to be(1)
    expect(batched_migrations).to all(have_attributes(attributes)) if attributes.present?
  end

  define_method :does_not_match? do |migration|
    batched_migrations =
      Gitlab::Database::BackgroundMigration::BatchedMigration
        .where(job_class_name: migration)

    expect(batched_migrations.count).to be(0)
  end
end