debian-mirror-gitlab/spec/services/cohorts_service_spec.rb

102 lines
2.9 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
describe CohortsService do
describe '#execute' do
def month_start(months_ago)
months_ago.months.ago.beginning_of_month.to_date
end
# In the interests of speed and clarity, this example has minimal data.
it 'returns a list of user cohorts' do
6.times do |months_ago|
months_ago_time = (months_ago * 2).months.ago
2020-05-24 23:13:21 +05:30
create(:user, created_at: months_ago_time, last_activity_on: Time.current)
2017-08-17 22:00:37 +05:30
create(:user, created_at: months_ago_time, last_activity_on: months_ago_time)
end
create(:user) # this user is inactive and belongs to the current month
expected_cohorts = [
{
registration_month: month_start(11),
2020-01-01 13:55:28 +05:30
activity_months: Array.new(11) { { total: 0, percentage: 0 } },
2017-08-17 22:00:37 +05:30
total: 0,
inactive: 0
},
{
registration_month: month_start(10),
2020-01-01 13:55:28 +05:30
activity_months: Array.new(10) { { total: 1, percentage: 50 } },
2017-08-17 22:00:37 +05:30
total: 2,
inactive: 0
},
{
registration_month: month_start(9),
2020-01-01 13:55:28 +05:30
activity_months: Array.new(9) { { total: 0, percentage: 0 } },
2017-08-17 22:00:37 +05:30
total: 0,
inactive: 0
},
{
registration_month: month_start(8),
2020-01-01 13:55:28 +05:30
activity_months: Array.new(8) { { total: 1, percentage: 50 } },
2017-08-17 22:00:37 +05:30
total: 2,
inactive: 0
},
{
registration_month: month_start(7),
2020-01-01 13:55:28 +05:30
activity_months: Array.new(7) { { total: 0, percentage: 0 } },
2017-08-17 22:00:37 +05:30
total: 0,
inactive: 0
},
{
registration_month: month_start(6),
2020-01-01 13:55:28 +05:30
activity_months: Array.new(6) { { total: 1, percentage: 50 } },
2017-08-17 22:00:37 +05:30
total: 2,
inactive: 0
},
{
registration_month: month_start(5),
2020-01-01 13:55:28 +05:30
activity_months: Array.new(5) { { total: 0, percentage: 0 } },
2017-08-17 22:00:37 +05:30
total: 0,
inactive: 0
},
{
registration_month: month_start(4),
2020-01-01 13:55:28 +05:30
activity_months: Array.new(4) { { total: 1, percentage: 50 } },
2017-08-17 22:00:37 +05:30
total: 2,
inactive: 0
},
{
registration_month: month_start(3),
2020-01-01 13:55:28 +05:30
activity_months: Array.new(3) { { total: 0, percentage: 0 } },
2017-08-17 22:00:37 +05:30
total: 0,
inactive: 0
},
{
registration_month: month_start(2),
2020-01-01 13:55:28 +05:30
activity_months: Array.new(2) { { total: 1, percentage: 50 } },
2017-08-17 22:00:37 +05:30
total: 2,
inactive: 0
},
{
registration_month: month_start(1),
2020-01-01 13:55:28 +05:30
activity_months: Array.new(1) { { total: 0, percentage: 0 } },
2017-08-17 22:00:37 +05:30
total: 0,
inactive: 0
},
{
registration_month: month_start(0),
2020-01-01 13:55:28 +05:30
activity_months: [],
2017-08-17 22:00:37 +05:30
total: 2,
inactive: 1
2017-09-10 17:25:29 +05:30
}
2017-08-17 22:00:37 +05:30
]
expect(described_class.new.execute).to eq(months_included: 12,
cohorts: expected_cohorts)
end
end
end