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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Projects::DataTransfer, feature_category: :source_code_management do
let_it_be(:project) { create(:project) }
it { expect(subject).to be_valid }
# tests DataTransferCounterAttribute with the appropiate attributes
it_behaves_like CounterAttribute,
%i[repository_egress artifacts_egress packages_egress registry_egress] do
let(:model) { create(:project_data_transfer, project: project) }
end
describe 'associations' do
it { is_expected.to belong_to(:project) }
it { is_expected.to belong_to(:namespace) }
end
describe 'scopes' do
let(:dates) { %w[2023-01-01 2023-02-01 2023-03-01] }
before do
dates.each { |date| create(:project_data_transfer, project: project, date: date) }
end
describe '.current_month' do
subject { described_class.current_month }
it 'returns data transfer for the current month' do
travel_to(Time.utc(2022, 5, 2)) do
_past_month = create(:project_data_transfer, project: project, date: '2022-04-01')
current_month = create(:project_data_transfer, project: project, date: '2022-05-01')
is_expected.to match_array([current_month])
end
end
end
describe '.with_project_between_dates' do
subject do
described_class.with_project_between_dates(project, Date.new(2023, 2, 1), Date.new(2023, 3, 1))
end
it 'returns the correct number of results' do
expect(subject.size).to eq(2)
end
end
describe '.with_namespace_between_dates' do
subject do
described_class.with_namespace_between_dates(project.namespace, Date.new(2023, 2, 1), Date.new(2023, 3, 1))
end
it 'returns the correct number of results' do
expect(subject.select(:namespace_id).to_a.size).to eq(2)
end
end
end
describe '.beginning_of_month' do
subject { described_class.beginning_of_month(time) }
let(:time) { Time.utc(2022, 5, 2) }
it { is_expected.to eq(Time.utc(2022, 5, 1)) }
end
describe 'unique index' do
before do
create(:project_data_transfer, project: project, date: '2022-05-01')
end
it 'raises unique index violation' do
expect { create(:project_data_transfer, project: project, namespace: project.root_namespace, date: '2022-05-01') }
.to raise_error(ActiveRecord::RecordNotUnique)
end
context 'when project was moved from one namespace to another' do
it 'creates a new record' do
expect { create(:project_data_transfer, project: project, namespace: create(:namespace), date: '2022-05-01') }
.to change { described_class.count }.by(1)
end
end
context 'when a different project is created' do
it 'creates a new record' do
expect { create(:project_data_transfer, project: build(:project), date: '2022-05-01') }
.to change { described_class.count }.by(1)
end
end
end
end
|