Skip to content

Commit

Permalink
Add specs for grouped_sum_precise_total_amount_cnets
Browse files Browse the repository at this point in the history
  • Loading branch information
nudded committed Sep 27, 2024
1 parent 115f614 commit 095f8ce
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 36 deletions.
42 changes: 42 additions & 0 deletions spec/services/events/stores/clickhouse_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,48 @@
end
end

describe '#grouped_sum_precise_total_amount_cents' do
let(:grouped_by) { %w[cloud] }

it 'returns the sum of values grouped by the provided group' do
result = event_store.grouped_sum_precise_total_amount_cents

expect(result.count).to eq(4)

null_group = result.find { |v| v[:groups]['cloud'].nil? }
expect(null_group[:value]).to eq(6)

result[...-1].each do |row|
next if row[:groups]['cloud'].nil?

expect(row[:groups]['cloud']).not_to be_nil
expect(row[:value]).not_to be_nil
end
end

context 'with multiple groups' do
let(:grouped_by) { %w[cloud region] }

it 'returns the sum of values grouped by the provided groups' do
result = event_store.grouped_sum_precise_total_amount_cents

expect(result.count).to eq(4)

null_group = result.find { |v| v[:groups]['cloud'].nil? }
expect(null_group[:groups]['region']).to be_nil
expect(null_group[:value]).to eq(6)

result[...-1].each do |row|
next if row[:groups]['cloud'].nil?

expect(row[:groups]['cloud']).not_to be_nil
expect(row[:groups]['region']).not_to be_nil
expect(row[:value]).not_to be_nil
end
end
end
end

describe '#active_unique_property?' do
before { event_store.aggregation_property = billable_metric.field_name }

Expand Down
113 changes: 77 additions & 36 deletions spec/services/events/stores/postgres_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@

expect(result.count).to eq(4)

null_group = result.last
null_group = result.find { |r| r[:groups]['cloud'].nil? }
expect(null_group[:groups]['cloud']).to be_nil
expect(null_group[:value]).to eq(2)

result[...-1].each do |row|
result.without(null_group).each do |row|
expect(row[:groups]['cloud']).not_to be_nil
expect(row[:value]).to eq(1)
end
Expand All @@ -147,12 +147,12 @@

expect(result.count).to eq(4)

null_group = result.last
null_group = result.find { |r| r[:groups]['cloud'].nil? }
expect(null_group[:groups]['cloud']).to be_nil
expect(null_group[:groups]['region']).to be_nil
expect(null_group[:value]).to eq(2)

result[...-1].each do |row|
result.without(null_group).each do |row|
expect(row[:groups]['cloud']).not_to be_nil
expect(row[:groups]['region']).not_to be_nil
expect(row[:value]).to eq(1)
Expand Down Expand Up @@ -359,12 +359,12 @@

expect(result.count).to eq(3)

null_group = result.last
null_group = result.find { |r| r[:groups]['agent_name'].nil? }
expect(null_group[:groups]['agent_name']).to be_nil
expect(null_group[:groups]['other']).to be_nil
expect(null_group[:value]).to eq(1)

expect(result[...-1].map { |r| r[:value] }).to contain_exactly(1, 0)
expect(result.without(null_group).map { |r| r[:value] }).to contain_exactly(1, 0)
end

context 'with no events' do
Expand Down Expand Up @@ -441,13 +441,13 @@

expect(result.count).to eq(3)

null_group = result.last
null_group = result.find { |r| r[:groups]['agent_name'].nil? }
expect(null_group[:groups]['agent_name']).to be_nil
expect(null_group[:groups]['other']).to be_nil
expect(null_group[:value].round(3)).to eq(0.935) # 29/31

# NOTE: Events calculation: [1/31, 30/31]
expect(result[...-1].map { |r| r[:value].round(3) }).to contain_exactly(0.032, 0.968)
expect(result.without(null_group).map { |r| r[:value].round(3) }).to contain_exactly(0.032, 0.968)
end

context 'with no events' do
Expand Down Expand Up @@ -593,12 +593,12 @@

expect(result.count).to eq(4)

null_group = result.last
null_group = result.find { |r| r[:groups]['cloud'].nil? }
expect(null_group[:groups]['cloud']).to be_nil
expect(null_group[:value]).to eq(4)
expect(null_group[:timestamp]).not_to be_nil

result[...-1].each do |row|
result.without(null_group).each do |row|
expect(row[:groups]['cloud']).not_to be_nil
expect(row[:value]).not_to be_nil
expect(row[:timestamp]).not_to be_nil
Expand All @@ -613,13 +613,13 @@

expect(result.count).to eq(4)

null_group = result.last
null_group = result.find { |r| r[:groups]['cloud'].nil? }
expect(null_group[:groups]['cloud']).to be_nil
expect(null_group[:groups]['region']).to be_nil
expect(null_group[:value]).to eq(4)
expect(null_group[:timestamp]).not_to be_nil

result[...-1].each do |row|
result.without(null_group).each do |row|
expect(row[:groups]['cloud']).not_to be_nil
expect(row[:groups]['region']).not_to be_nil
expect(row[:value]).not_to be_nil
Expand Down Expand Up @@ -662,11 +662,11 @@

expect(result.count).to eq(4)

null_group = result.last
null_group = result.find { |r| r[:groups]['cloud'].nil? }
expect(null_group[:groups]['cloud']).to be_nil
expect(null_group[:value]).to eq(4)

result[...-1].each do |row|
result.without(null_group).each do |row|
expect(row[:groups]['cloud']).not_to be_nil
expect(row[:value]).not_to be_nil
end
Expand All @@ -680,12 +680,12 @@

expect(result.count).to eq(4)

null_group = result.last
null_group = result.find { |r| r[:groups]['cloud'].nil? }
expect(null_group[:groups]['cloud']).to be_nil
expect(null_group[:groups]['region']).to be_nil
expect(null_group[:value]).to eq(4)

result[...-1].each do |row|
result.without(null_group).each do |row|
expect(row[:groups]['cloud']).not_to be_nil
expect(row[:groups]['region']).not_to be_nil
expect(row[:value]).not_to be_nil
Expand Down Expand Up @@ -716,11 +716,11 @@

expect(result.count).to eq(4)

null_group = result.last
null_group = result.find { |r| r[:groups]['cloud'].nil? }
expect(null_group[:groups]['cloud']).to be_nil
expect(null_group[:value]).to eq(4)

result[...-1].each do |row|
result.without(null_group).each do |row|
expect(row[:groups]['cloud']).not_to be_nil
expect(row[:value]).not_to be_nil
end
Expand All @@ -734,12 +734,13 @@

expect(result.count).to eq(4)

null_group = result.last
null_group = result.find { |r| r[:groups]['cloud'].nil? }

expect(null_group[:groups]['cloud']).to be_nil
expect(null_group[:groups]['region']).to be_nil
expect(null_group[:value]).to eq(4)

result[...-1].each do |row|
result.without(null_group).each do |row|
expect(row[:groups]['cloud']).not_to be_nil
expect(row[:groups]['region']).not_to be_nil
expect(row[:value]).not_to be_nil
Expand All @@ -754,6 +755,46 @@
end
end

describe '#grouped_sum_precise_total_amount_cents' do
let(:grouped_by) { %w[cloud] }

it 'returns the sum of values grouped by the provided group' do
result = event_store.grouped_sum_precise_total_amount_cents

expect(result.count).to eq(4)

null_group = result.find { |r| r[:groups]['cloud'].nil? }
expect(null_group[:groups]['cloud']).to be_nil
expect(null_group[:value]).to eq(6)

result.without(null_group).each do |row|
expect(row[:groups]['cloud']).not_to be_nil
expect(row[:value]).not_to be_nil
end
end

context 'with multiple groups' do
let(:grouped_by) { %w[cloud region] }

it 'returns the sum of values grouped by the provided groups' do
result = event_store.grouped_sum_precise_total_amount_cents

expect(result.count).to eq(4)

null_group = result.find { |r| r[:groups]['cloud'].nil? }
expect(null_group[:groups]['cloud']).to be_nil
expect(null_group[:groups]['region']).to be_nil
expect(null_group[:value]).to eq(6)

result.without(null_group).each do |row|
expect(row[:groups]['cloud']).not_to be_nil
expect(row[:groups]['region']).not_to be_nil
expect(row[:value]).not_to be_nil
end
end
end
end

describe '#sum' do
it 'returns the sum of event values' do
event_store.aggregation_property = billable_metric.field_name
Expand All @@ -776,11 +817,11 @@

expect(result.count).to eq(4)

null_group = result.last
null_group = result.find { |r| r[:groups]['cloud'].nil? }
expect(null_group[:groups]['cloud']).to be_nil
expect(null_group[:value]).to eq(6)

result[...-1].each do |row|
result.without(null_group).each do |row|
expect(row[:groups]['cloud']).not_to be_nil
expect(row[:value]).not_to be_nil
end
Expand All @@ -794,12 +835,12 @@

expect(result.count).to eq(4)

null_group = result.last
null_group = result.find { |r| r[:groups]['cloud'].nil? }
expect(null_group[:groups]['cloud']).to be_nil
expect(null_group[:groups]['region']).to be_nil
expect(null_group[:value]).to eq(6)

result[...-1].each do |row|
result.without(null_group).each do |row|
expect(row[:groups]['cloud']).not_to be_nil
expect(row[:groups]['region']).not_to be_nil
expect(row[:value]).not_to be_nil
Expand Down Expand Up @@ -837,11 +878,11 @@

expect(result.count).to eq(4)

null_group = result.last
null_group = result.find { |r| r[:groups]['cloud'].nil? }
expect(null_group[:groups]['cloud']).to be_nil
expect(null_group[:value].round(5)).to eq(2.64516)

result[...-1].each do |row|
result.without(null_group).each do |row|
expect(row[:groups]['cloud']).not_to be_nil
expect(row[:value]).not_to be_nil
end
Expand All @@ -856,11 +897,11 @@

expect(result.count).to eq(4)

null_group = result.last
null_group = result.find { |r| r[:groups]['cloud'].nil? }
expect(null_group[:groups]['cloud']).to be_nil
expect(null_group[:value].round(5)).to eq(1.93548)

result[...-1].each do |row|
result.without(null_group).each do |row|
expect(row[:groups]['cloud']).not_to be_nil
expect(row[:value]).not_to be_nil
end
Expand All @@ -878,12 +919,12 @@

expect(result.count).to eq(4)

null_group = result.last
null_group = result.find { |r| r[:groups]['cloud'].nil? }
expect(null_group[:groups]['cloud']).to be_nil
expect(null_group[:groups]['region']).to be_nil
expect(null_group[:value].round(5)).to eq(2.64516)

result[...-1].each do |row|
result.without(null_group).each do |row|
expect(row[:groups]['cloud']).not_to be_nil
expect(row[:groups]['region']).not_to be_nil
expect(row[:value]).not_to be_nil
Expand Down Expand Up @@ -1086,12 +1127,12 @@

expect(result.count).to eq(3)

null_group = result.last
null_group = result.find { |r| r[:groups]['agent_name'].nil? }
expect(null_group[:groups]['agent_name']).to be_nil
expect(null_group[:groups]['other']).to be_nil
expect(null_group[:value].round(5)).to eq(0.02218)

result[...-1].each do |row|
result.without(null_group).each do |row|
expect(row[:groups]['agent_name']).not_to be_nil
expect(row[:groups]['other']).to be_nil
expect(row[:value].round(5)).to eq(0.02218)
Expand Down Expand Up @@ -1122,12 +1163,12 @@

expect(result.count).to eq(3)

null_group = result.last
null_group = result.find { |r| r[:groups]['agent_name'].nil? }
expect(null_group[:groups]['agent_name']).to be_nil
expect(null_group[:groups]['other']).to be_nil
expect(null_group[:value].round(5)).to eq(1000.02218)

result[...-1].each do |row|
result.without(null_group).each do |row|
expect(row[:groups]['agent_name']).not_to be_nil
expect(row[:groups]['other']).to be_nil
expect(row[:value].round(5)).to eq(1000.02218)
Expand All @@ -1142,12 +1183,12 @@

expect(result.count).to eq(3)

null_group = result.last
null_group = result.find { |r| r[:groups]['agent_name'].nil? }
expect(null_group[:groups]['agent_name']).to be_nil
expect(null_group[:groups]['other']).to be_nil
expect(null_group[:value].round(5)).to eq(1000)

result[...-1].each do |row|
result.without(null_group).each do |row|
expect(row[:groups]['agent_name']).not_to be_nil
expect(row[:groups]['other']).to be_nil
expect(row[:value].round(5)).to eq(1000)
Expand Down

0 comments on commit 095f8ce

Please sign in to comment.