|
2 | 2 |
|
3 | 3 | require "rails_helper" |
4 | 4 |
|
5 | | -describe "Customer usage Scenario" do |
| 5 | +describe "Customer usage Scenario", cache: :redis do |
6 | 6 | let(:organization) { create(:organization, webhook_url: nil) } |
7 | 7 |
|
8 | 8 | let(:timezone) { "UTC" } |
9 | 9 | let(:customer) { create(:customer, organization:, timezone:, currency: "EUR") } |
10 | 10 |
|
11 | | - let(:plan) { create(:plan, organization:, amount_cents: 700, pay_in_advance: false, interval: "yearly") } |
| 11 | + let(:plan) { create(:plan, organization:, amount_cents: 5000, pay_in_advance: false, interval: "yearly") } |
| 12 | + let(:billable_metric) { create(:billable_metric, organization:, code: "image_generation", name: "Image generation") } |
| 13 | + let(:charge) { create(:standard_charge, plan:, billable_metric:, invoice_display_name: "Image generation") } |
| 14 | + |
| 15 | + before { charge } |
| 16 | + |
| 17 | + def customer_usage_json(units: 1.0, from_datetime: "2043-01-01T09:30:00Z", to_datetime: "2043-12-31T23:59:59Z") |
| 18 | + total_amount_cents = 1000 * units |
| 19 | + { |
| 20 | + customer_usage: { |
| 21 | + amount_cents: total_amount_cents, |
| 22 | + charges_usage: [ |
| 23 | + { |
| 24 | + amount_cents: total_amount_cents, |
| 25 | + amount_currency: "EUR", |
| 26 | + billable_metric: { |
| 27 | + aggregation_type: "count_agg", |
| 28 | + code: "image_generation", |
| 29 | + lago_id: billable_metric.id, |
| 30 | + name: billable_metric.name |
| 31 | + }, |
| 32 | + charge: { |
| 33 | + charge_model: "standard", |
| 34 | + invoice_display_name: "Image generation", |
| 35 | + lago_id: charge.id |
| 36 | + }, |
| 37 | + events_count: units.to_i, |
| 38 | + filters: [], |
| 39 | + grouped_usage: [], |
| 40 | + pricing_unit_details: nil, |
| 41 | + total_aggregated_units: units.to_d.to_s, |
| 42 | + units: units.to_d.to_s |
| 43 | + } |
| 44 | + ], |
| 45 | + currency: "EUR", |
| 46 | + from_datetime: from_datetime, |
| 47 | + issuing_date: to_datetime.slice(0, 10), |
| 48 | + lago_invoice_id: nil, |
| 49 | + taxes_amount_cents: 0, |
| 50 | + to_datetime: to_datetime, |
| 51 | + total_amount_cents: total_amount_cents |
| 52 | + } |
| 53 | + } |
| 54 | + end |
| 55 | + |
| 56 | + def fetch_and_assert_current_usage(units: 1.0, from_datetime: "2043-01-01T09:30:00Z", to_datetime: "2043-12-31T23:59:59Z") |
| 57 | + subscription = customer.subscriptions.first |
| 58 | + fetch_current_usage(customer:, subscription:) |
| 59 | + |
| 60 | + expect(json).to eq(customer_usage_json(units: units, from_datetime: from_datetime, to_datetime: to_datetime)) |
| 61 | + end |
12 | 62 |
|
13 | 63 | context "with start date in the past" do |
14 | 64 | it "retrieve the customer usage" do |
15 | | - travel_to(DateTime.new(2023, 8, 8, 9, 30)) do |
16 | | - create_subscription( |
| 65 | + travel_to(DateTime.new(2043, 8, 8, 9, 30)) do |
| 66 | + subscription = create_subscription( |
17 | 67 | { |
18 | 68 | external_customer_id: customer.external_id, |
19 | 69 | external_id: customer.external_id, |
20 | 70 | plan_code: plan.code, |
21 | | - subscription_at: DateTime.new(2023, 1, 1, 9, 30).iso8601 |
22 | | - } |
| 71 | + subscription_at: DateTime.new(2043, 1, 1, 9, 30).iso8601 |
| 72 | + }, |
| 73 | + as: :model |
23 | 74 | ) |
24 | 75 |
|
25 | | - subscription = customer.subscriptions.first |
26 | | - fetch_current_usage(customer:, subscription:) |
| 76 | + fetch_and_assert_current_usage(units: 0) |
| 77 | + |
| 78 | + create_event({ |
| 79 | + external_subscription_id: subscription.external_id, |
| 80 | + timestamp: Time.now.to_f, |
| 81 | + code: "image_generation", |
| 82 | + properties: {} |
| 83 | + }) |
27 | 84 |
|
28 | | - expect(json[:customer_usage][:from_datetime]).to eq("2023-01-01T09:30:00Z") |
29 | | - expect(json[:customer_usage][:to_datetime]).to eq("2023-12-31T23:59:59Z") |
| 85 | + fetch_and_assert_current_usage(units: 1) |
| 86 | + |
| 87 | + # test cache |
| 88 | + fetch_and_assert_current_usage(units: 1) |
| 89 | + |
| 90 | + create_event({ |
| 91 | + external_subscription_id: subscription.external_id, |
| 92 | + timestamp: Time.now.to_f, |
| 93 | + code: "image_generation", |
| 94 | + properties: {} |
| 95 | + }) |
| 96 | + |
| 97 | + fetch_and_assert_current_usage(units: 2) |
| 98 | + |
| 99 | + Event.last.destroy |
| 100 | + |
| 101 | + # test cache |
| 102 | + fetch_and_assert_current_usage(units: 2) |
| 103 | + |
| 104 | + Rails.cache.clear |
| 105 | + |
| 106 | + fetch_and_assert_current_usage(units: 1) |
30 | 107 | end |
31 | 108 | end |
32 | 109 |
|
33 | 110 | context "with Europe/Berlin timezone" do |
34 | 111 | let(:timezone) { "Europe/Berlin" } |
35 | 112 |
|
36 | 113 | it "retrieve the customer usage" do |
37 | | - travel_to(DateTime.new(2023, 8, 8, 9, 30)) do |
| 114 | + travel_to(DateTime.new(2043, 8, 8, 9, 30)) do |
38 | 115 | create_subscription( |
39 | 116 | { |
40 | 117 | external_customer_id: customer.external_id, |
41 | 118 | external_id: customer.external_id, |
42 | 119 | plan_code: plan.code, |
43 | | - subscription_at: DateTime.new(2023, 1, 1, 9, 30).iso8601 |
| 120 | + subscription_at: DateTime.new(2043, 1, 1, 9, 30).iso8601 |
44 | 121 | } |
45 | 122 | ) |
46 | 123 |
|
47 | | - subscription = customer.subscriptions.first |
48 | | - fetch_current_usage(customer:, subscription:) |
49 | | - |
50 | | - expect(json[:customer_usage][:from_datetime]).to eq("2023-01-01T09:30:00Z") |
51 | | - expect(json[:customer_usage][:to_datetime]).to eq("2023-12-31T22:59:59Z") |
| 124 | + fetch_and_assert_current_usage(units: 0, from_datetime: "2043-01-01T09:30:00Z", to_datetime: "2043-12-31T22:59:59Z") |
52 | 125 | end |
53 | 126 | end |
54 | 127 | end |
|
57 | 130 | let(:timezone) { "America/Los_Angeles" } |
58 | 131 |
|
59 | 132 | it "retrieve the customer usage" do |
60 | | - travel_to(DateTime.new(2023, 8, 8, 9, 30)) do |
| 133 | + travel_to(DateTime.new(2043, 8, 8, 9, 30)) do |
61 | 134 | create_subscription( |
62 | 135 | { |
63 | 136 | external_customer_id: customer.external_id, |
64 | 137 | external_id: customer.external_id, |
65 | 138 | plan_code: plan.code, |
66 | | - subscription_at: DateTime.new(2023, 1, 1, 9, 30).iso8601 |
| 139 | + subscription_at: DateTime.new(2043, 1, 1, 9, 30).iso8601 |
67 | 140 | } |
68 | 141 | ) |
69 | 142 |
|
70 | | - subscription = customer.subscriptions.first |
71 | | - fetch_current_usage(customer:, subscription:) |
72 | | - |
73 | | - expect(json[:customer_usage][:from_datetime]).to eq("2023-01-01T09:30:00Z") |
74 | | - expect(json[:customer_usage][:to_datetime]).to eq("2024-01-01T07:59:59Z") |
| 143 | + fetch_and_assert_current_usage(units: 0, from_datetime: "2043-01-01T09:30:00Z", to_datetime: "2044-01-01T07:59:59Z") |
75 | 144 | end |
76 | 145 | end |
77 | 146 | end |
|
0 commit comments