PageRenderTime 21ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/lib/gitlab/analytics/cycle_analytics/aggregated/records_fetcher_spec.rb

https://gitlab.com/abuhazim/gitlab-foss
Ruby | 146 lines | 108 code | 35 blank | 3 comment | 0 complexity | f14dc28bf61c6ba26491a18dba773371 MD5 | raw file
  1. # frozen_string_literal: true
  2. require 'spec_helper'
  3. RSpec.describe Gitlab::Analytics::CycleAnalytics::Aggregated::RecordsFetcher do
  4. let_it_be(:project) { create(:project) }
  5. let_it_be(:issue_1) { create(:issue, project: project) }
  6. let_it_be(:issue_2) { create(:issue, project: project) }
  7. let_it_be(:issue_3) { create(:issue, project: project) }
  8. let_it_be(:stage) { create(:cycle_analytics_project_stage, start_event_identifier: :issue_created, end_event_identifier: :issue_deployed_to_production, project: project) }
  9. let_it_be(:stage_event_1) { create(:cycle_analytics_issue_stage_event, stage_event_hash_id: stage.stage_event_hash_id, project_id: project.id, issue_id: issue_1.id, start_event_timestamp: 2.years.ago, end_event_timestamp: 1.year.ago) } # duration: 1 year
  10. let_it_be(:stage_event_2) { create(:cycle_analytics_issue_stage_event, stage_event_hash_id: stage.stage_event_hash_id, project_id: project.id, issue_id: issue_2.id, start_event_timestamp: 5.years.ago, end_event_timestamp: 2.years.ago) } # duration: 3 years
  11. let_it_be(:stage_event_3) { create(:cycle_analytics_issue_stage_event, stage_event_hash_id: stage.stage_event_hash_id, project_id: project.id, issue_id: issue_3.id, start_event_timestamp: 6.years.ago, end_event_timestamp: 3.months.ago) } # duration: 5+ years
  12. let(:params) { { from: 10.years.ago, to: Date.today } }
  13. subject(:records_fetcher) do
  14. query_builder = Gitlab::Analytics::CycleAnalytics::Aggregated::BaseQueryBuilder.new(stage: stage, params: params)
  15. described_class.new(stage: stage, query: query_builder.build_sorted_query, params: params)
  16. end
  17. shared_examples 'match returned records' do
  18. it 'returns issues in the correct order' do
  19. returned_iids = records_fetcher.serialized_records.pluck(:iid).map(&:to_i)
  20. expect(returned_iids).to eq(expected_issue_ids)
  21. end
  22. end
  23. describe '#serialized_records' do
  24. describe 'sorting' do
  25. context 'when sorting by end event DESC' do
  26. let(:expected_issue_ids) { [issue_3.iid, issue_1.iid, issue_2.iid] }
  27. before do
  28. params[:sort] = :end_event
  29. params[:direction] = :desc
  30. end
  31. it_behaves_like 'match returned records'
  32. end
  33. context 'when intervalstyle setting is configured to "postgres"' do
  34. it 'avoids nil durations' do
  35. # ActiveRecord cannot parse the 'postgres' intervalstyle, it returns nil
  36. # The setting is rolled back after the test case.
  37. Analytics::CycleAnalytics::IssueStageEvent.connection.execute("SET LOCAL intervalstyle='postgres'")
  38. records_fetcher.serialized_records do |relation|
  39. durations = relation.map(&:total_time)
  40. expect(durations).to all(be > 0)
  41. end
  42. end
  43. end
  44. context 'when sorting by end event ASC' do
  45. let(:expected_issue_ids) { [issue_2.iid, issue_1.iid, issue_3.iid] }
  46. before do
  47. params[:sort] = :end_event
  48. params[:direction] = :asc
  49. end
  50. it_behaves_like 'match returned records'
  51. end
  52. context 'when sorting by duration DESC' do
  53. let(:expected_issue_ids) { [issue_3.iid, issue_2.iid, issue_1.iid] }
  54. before do
  55. params[:sort] = :duration
  56. params[:direction] = :desc
  57. end
  58. it_behaves_like 'match returned records'
  59. end
  60. context 'when sorting by duration ASC' do
  61. let(:expected_issue_ids) { [issue_1.iid, issue_2.iid, issue_3.iid] }
  62. before do
  63. params[:sort] = :duration
  64. params[:direction] = :asc
  65. end
  66. it_behaves_like 'match returned records'
  67. end
  68. end
  69. describe 'pagination' do
  70. let(:expected_issue_ids) { [issue_3.iid] }
  71. before do
  72. params[:sort] = :duration
  73. params[:direction] = :asc
  74. params[:page] = 2
  75. stub_const('Gitlab::Analytics::CycleAnalytics::Aggregated::RecordsFetcher::MAX_RECORDS', 2)
  76. end
  77. it_behaves_like 'match returned records'
  78. end
  79. context 'when passing a block to serialized_records method' do
  80. before do
  81. params[:sort] = :duration
  82. params[:direction] = :asc
  83. end
  84. it 'yields the underlying stage event scope' do
  85. stage_event_records = []
  86. records_fetcher.serialized_records do |scope|
  87. stage_event_records.concat(scope.to_a)
  88. end
  89. expect(stage_event_records.map(&:issue_id)).to eq([issue_1.id, issue_2.id, issue_3.id])
  90. end
  91. end
  92. context 'when the issue record no longer exists' do
  93. it 'skips non-existing issue records' do
  94. create(:cycle_analytics_issue_stage_event, {
  95. issue_id: 0, # non-existing id
  96. stage_event_hash_id: stage.stage_event_hash_id,
  97. project_id: project.id,
  98. start_event_timestamp: 5.months.ago,
  99. end_event_timestamp: 3.months.ago
  100. })
  101. stage_event_count = nil
  102. records_fetcher.serialized_records do |scope|
  103. stage_event_count = scope.to_a.size
  104. end
  105. issue_count = records_fetcher.serialized_records.to_a.size
  106. expect(stage_event_count).to eq(4)
  107. expect(issue_count).to eq(3)
  108. end
  109. end
  110. end
  111. end