-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathusage_thresholds_completion_service_spec.rb
More file actions
182 lines (144 loc) · 7.16 KB
/
Copy pathusage_thresholds_completion_service_spec.rb
File metadata and controls
182 lines (144 loc) · 7.16 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# frozen_string_literal: true
require "rails_helper"
RSpec.describe LifetimeUsages::UsageThresholdsCompletionService do
subject(:result) { described_class.call(lifetime_usage:) }
let(:lifetime_usage) { create(:lifetime_usage, subscription:, organization:, current_usage_amount_cents:) }
let(:plan) { create(:plan) }
let(:customer) { create(:customer, organization:) }
let(:subscription) { create(:subscription, plan:, customer:) }
let(:current_usage_amount_cents) { 0 }
let_it_be(:organization) { create_default(:organization) }
def create_threshold(attached_to:, **factory_args)
if attached_to == :subscription
create(:usage_threshold, :for_subscription, subscription:, **factory_args)
elsif attached_to == :plan
create(:usage_threshold, plan:, **factory_args)
end
end
it "computes the usage thresholds" do
expect(result.usage_thresholds).to be_empty
end
# TODO: usage_thresholds remove loop to always attach to sub
[:subscription, :plan].each do |attached_to|
context "with a usage threshold" do
let(:usage_threshold) { create_threshold(attached_to:, amount_cents: 100) }
before do
usage_threshold
end
it "computes the usage thresholds" do
thresholds = result.usage_thresholds
expect(thresholds.size).to eq(1)
threshold = thresholds.first
expect(threshold[:usage_threshold]).to eq(usage_threshold)
expect(threshold[:amount_cents]).to eq(usage_threshold.amount_cents)
expect(threshold[:completion_ratio]).to be_zero
expect(threshold[:reached_at]).to be_nil
end
context "with a lifetime_usage" do
let(:current_usage_amount_cents) { 23 }
it "computes the usage thresholds" do
thresholds = result.usage_thresholds
expect(thresholds.size).to eq(1)
threshold = thresholds.first
expect(threshold[:usage_threshold]).to eq(usage_threshold)
expect(threshold[:amount_cents]).to eq(usage_threshold.amount_cents)
expect(threshold[:completion_ratio]).to eq(0.23)
expect(threshold[:reached_at]).to be_nil
end
end
end
context "with a past threshold" do
let(:usage_threshold1) { create_threshold(attached_to:, amount_cents: 100) }
let(:usage_threshold2) { create_threshold(attached_to:, amount_cents: 200) }
let(:applied_usage_threshold) { create(:applied_usage_threshold, usage_threshold: usage_threshold1, invoice:) }
let(:invoice) { create(:invoice, organization:, customer:) }
let(:invoice_subscription) { create(:invoice_subscription, invoice:, subscription:) }
let(:current_usage_amount_cents) { 120 }
before do
usage_threshold1
usage_threshold2
invoice_subscription
applied_usage_threshold
end
it "computes the usage thresholds" do
thresholds = result.usage_thresholds
expect(thresholds.size).to eq(2)
threshold1 = thresholds.first
threshold2 = thresholds.last
expect(threshold1[:usage_threshold]).to eq(usage_threshold1)
expect(threshold1[:amount_cents]).to eq(usage_threshold1.amount_cents)
expect(threshold1[:completion_ratio]).to eq(1.0)
expect(threshold1[:reached_at]).to eq(applied_usage_threshold.created_at)
expect(threshold2[:usage_threshold]).to eq(usage_threshold2)
expect(threshold2[:amount_cents]).to eq(usage_threshold2.amount_cents)
expect(threshold2[:completion_ratio]).to eq(0.2)
expect(threshold2[:reached_at]).to be_nil
end
context "when lifetime_usage is above last threshold" do
let(:applied_usage_threshold2) { create(:applied_usage_threshold, usage_threshold: usage_threshold2, invoice:) }
let(:current_usage_amount_cents) { 223 }
before do
applied_usage_threshold2
end
it "computes the usage thresholds" do
thresholds = result.usage_thresholds
expect(thresholds.size).to eq(2)
threshold1 = thresholds.first
threshold2 = thresholds.last
expect(threshold1[:usage_threshold]).to eq(usage_threshold1)
expect(threshold1[:amount_cents]).to eq(usage_threshold1.amount_cents)
expect(threshold1[:completion_ratio]).to eq(1.0)
expect(threshold1[:reached_at]).to eq(applied_usage_threshold.created_at)
expect(threshold2[:usage_threshold]).to eq(usage_threshold2)
expect(threshold2[:amount_cents]).to eq(usage_threshold2.amount_cents)
expect(threshold2[:completion_ratio]).to eq(1)
expect(threshold2[:reached_at]).to eq(applied_usage_threshold2.created_at)
end
end
context "when next threshold is recurring" do
let(:usage_threshold2) { create_threshold(attached_to:, recurring: true, amount_cents: 200) }
it "computes the usage thresholds" do
thresholds = result.usage_thresholds.sort_by { it[:amount_cents] }
expect(thresholds.size).to eq(2)
threshold1 = thresholds.first
threshold2 = thresholds.last
expect(threshold1[:usage_threshold]).to eq(usage_threshold1)
expect(threshold1[:amount_cents]).to eq(usage_threshold1.amount_cents)
expect(threshold1[:completion_ratio]).to eq(1.0)
expect(threshold1[:reached_at]).to eq(applied_usage_threshold.created_at)
expect(threshold2[:usage_threshold]).to eq(usage_threshold2)
expect(threshold2[:amount_cents]).to eq(usage_threshold2.amount_cents + usage_threshold1.amount_cents)
expect(threshold2[:completion_ratio]).to eq(0.1) # 20/200
expect(threshold2[:reached_at]).to be_nil
end
context "when lifetime_usage is above next threshold" do
let(:applied_usage_threshold2) { create(:applied_usage_threshold, lifetime_usage_amount_cents: 700, usage_threshold: usage_threshold2, invoice:) }
let(:current_usage_amount_cents) { 723 }
before do
applied_usage_threshold2
end
it "computes the usage thresholds" do
thresholds = result.usage_thresholds
expect(thresholds.size).to eq(5)
threshold1 = thresholds.shift
expect(threshold1[:usage_threshold]).to eq(usage_threshold1)
expect(threshold1[:amount_cents]).to eq(usage_threshold1.amount_cents)
expect(threshold1[:completion_ratio]).to eq(1.0)
expect(threshold1[:reached_at]).to eq(applied_usage_threshold.created_at)
last_threshold = thresholds.pop
thresholds.each.with_index do |threshold, index|
expect(threshold[:usage_threshold]).to eq(usage_threshold2)
expect(threshold[:amount_cents]).to eq(100 + (index + 1) * 200)
expect(threshold[:completion_ratio]).to eq(1.0)
expect(threshold[:reached_at]).to eq(applied_usage_threshold2.created_at)
end
expect(last_threshold[:usage_threshold]).to eq(usage_threshold2)
expect(last_threshold[:amount_cents]).to eq(900)
expect(last_threshold[:completion_ratio]).to eq(0.115)
expect(last_threshold[:reached_at]).to be_nil
end
end
end
end
end
end