2020-04-07 20:09:30 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe UsersStatistics do
|
2020-04-08 05:09:43 -04:00
|
|
|
let(:users_statistics) { build(:users_statistics) }
|
|
|
|
|
|
|
|
describe 'scopes' do
|
|
|
|
describe '.order_created_at_desc' do
|
|
|
|
it 'returns the entries ordered by created at descending' do
|
|
|
|
users_statistics1 = create(:users_statistics, created_at: Time.current)
|
|
|
|
users_statistics2 = create(:users_statistics, created_at: Time.current - 2.days)
|
|
|
|
users_statistics3 = create(:users_statistics, created_at: Time.current - 5.hours)
|
|
|
|
|
|
|
|
expect(described_class.order_created_at_desc).to eq(
|
|
|
|
[
|
|
|
|
users_statistics1,
|
|
|
|
users_statistics3,
|
|
|
|
users_statistics2
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.latest' do
|
|
|
|
it 'returns the latest entry' do
|
|
|
|
create(:users_statistics, created_at: Time.current - 1.day)
|
|
|
|
users_statistics = create(:users_statistics, created_at: Time.current)
|
|
|
|
|
|
|
|
expect(described_class.latest).to eq(users_statistics)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-07 20:09:30 -04:00
|
|
|
describe '.create_current_stats!' do
|
|
|
|
before do
|
2021-11-11 10:10:57 -05:00
|
|
|
create_list(:user_highest_role, 1)
|
2020-04-07 20:09:30 -04:00
|
|
|
create_list(:user_highest_role, 2, :guest)
|
2021-11-11 10:10:57 -05:00
|
|
|
create_list(:user_highest_role, 2, :reporter)
|
|
|
|
create_list(:user_highest_role, 2, :developer)
|
|
|
|
create_list(:user_highest_role, 2, :maintainer)
|
2020-04-07 20:09:30 -04:00
|
|
|
create_list(:user_highest_role, 2, :owner)
|
|
|
|
create_list(:user, 2, :bot)
|
|
|
|
create_list(:user, 1, :blocked)
|
|
|
|
|
2022-01-14 07:18:55 -05:00
|
|
|
allow(described_class.connection).to receive(:transaction_open?).and_return(false)
|
2020-04-07 20:09:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when successful' do
|
|
|
|
it 'creates an entry with the current statistics values' do
|
|
|
|
expect(described_class.create_current_stats!).to have_attributes(
|
2021-11-11 10:10:57 -05:00
|
|
|
without_groups_and_projects: 1,
|
2020-04-07 20:09:30 -04:00
|
|
|
with_highest_role_guest: 2,
|
2021-11-11 10:10:57 -05:00
|
|
|
with_highest_role_reporter: 2,
|
|
|
|
with_highest_role_developer: 2,
|
|
|
|
with_highest_role_maintainer: 2,
|
2020-04-07 20:09:30 -04:00
|
|
|
with_highest_role_owner: 2,
|
|
|
|
bots: 2,
|
|
|
|
blocked: 1
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when unsuccessful' do
|
|
|
|
it 'raises an ActiveRecord::RecordInvalid exception' do
|
|
|
|
allow(UsersStatistics).to receive(:create!).and_raise(ActiveRecord::RecordInvalid)
|
|
|
|
|
|
|
|
expect { described_class.create_current_stats! }.to raise_error(ActiveRecord::RecordInvalid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-04-08 05:09:43 -04:00
|
|
|
|
|
|
|
describe '#active' do
|
|
|
|
it 'sums users statistics values without the value for blocked' do
|
|
|
|
expect(users_statistics.active).to eq(71)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#total' do
|
|
|
|
it 'sums all users statistics values' do
|
|
|
|
expect(users_statistics.total).to eq(78)
|
|
|
|
end
|
|
|
|
end
|
2020-04-07 20:09:30 -04:00
|
|
|
end
|