summaryrefslogtreecommitdiff
path: root/internal/controlplane/usage_analytics_test.go
blob: 2372be66dbb964bd0889fb56a904a81898f1348f (plain)
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
package controlplane

import "testing"

func TestChargeChange(t *testing.T) {
	tests := []struct {
		name              string
		current, previous int64
		want              *float64
	}{
		{name: "no activity", current: 0, previous: 0, want: nil},
		{name: "new spend has no finite percentage", current: 25, previous: 0, want: nil},
		{name: "increase", current: 125, previous: 25, want: floatPointer(400)},
		{name: "decrease", current: 25, previous: 100, want: floatPointer(-75)},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			got := chargeChange(test.current, test.previous)
			if test.want == nil {
				if got != nil {
					t.Fatalf("chargeChange() = %v, want nil", *got)
				}
				return
			}
			if got == nil || *got != *test.want {
				t.Fatalf("chargeChange() = %v, want %v", got, *test.want)
			}
		})
	}
}

func floatPointer(value float64) *float64 {
	return &value
}