|
| 1 | +package workflows_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "strconv" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/shopspring/decimal" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + "golang.org/x/exp/maps" |
| 11 | + |
| 12 | + "github.com/smartcontractkit/chainlink/v2/core/services/workflows" |
| 13 | +) |
| 14 | + |
| 15 | +func TestMeteringReport(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + |
| 18 | + testUnitA := workflows.MeteringSpendUnit("a") |
| 19 | + testUnitB := workflows.MeteringSpendUnit("b") |
| 20 | + |
| 21 | + t.Run("MedianSpend returns median for multiple spend units", func(t *testing.T) { |
| 22 | + t.Parallel() |
| 23 | + |
| 24 | + report := workflows.NewMeteringReport() |
| 25 | + steps := []workflows.MeteringReportStep{ |
| 26 | + {"abc", testUnitA, testUnitA.IntToSpendValue(1)}, |
| 27 | + {"xyz", testUnitA, testUnitA.IntToSpendValue(2)}, |
| 28 | + {"abc", testUnitA, testUnitA.IntToSpendValue(3)}, |
| 29 | + {"abc", testUnitB, testUnitB.DecimalToSpendValue(decimal.NewFromFloat(0.1))}, |
| 30 | + {"xyz", testUnitB, testUnitB.DecimalToSpendValue(decimal.NewFromFloat(0.2))}, |
| 31 | + {"abc", testUnitB, testUnitB.DecimalToSpendValue(decimal.NewFromFloat(0.3))}, |
| 32 | + } |
| 33 | + |
| 34 | + for idx, step := range steps { |
| 35 | + require.NoError(t, report.AddStep(workflows.MeteringReportStepRef(strconv.Itoa(idx)), step)) |
| 36 | + } |
| 37 | + |
| 38 | + expected := map[workflows.MeteringSpendUnit]workflows.MeteringSpendValue{ |
| 39 | + testUnitA: testUnitB.IntToSpendValue(2), |
| 40 | + testUnitB: testUnitB.DecimalToSpendValue(decimal.NewFromFloat(0.2)), |
| 41 | + } |
| 42 | + |
| 43 | + median := report.MedianSpend() |
| 44 | + |
| 45 | + require.Len(t, median, 2) |
| 46 | + require.Contains(t, maps.Keys(median), testUnitA) |
| 47 | + require.Contains(t, maps.Keys(median), testUnitB) |
| 48 | + |
| 49 | + assert.Equal(t, expected[testUnitA].String(), median[testUnitA].String()) |
| 50 | + assert.Equal(t, expected[testUnitB].String(), median[testUnitB].String()) |
| 51 | + }) |
| 52 | + |
| 53 | + t.Run("MedianSpend returns median single spend value", func(t *testing.T) { |
| 54 | + t.Parallel() |
| 55 | + |
| 56 | + report := workflows.NewMeteringReport() |
| 57 | + steps := []workflows.MeteringReportStep{ |
| 58 | + {"abc", testUnitA, testUnitA.IntToSpendValue(1)}, |
| 59 | + } |
| 60 | + |
| 61 | + for idx, step := range steps { |
| 62 | + require.NoError(t, report.AddStep(workflows.MeteringReportStepRef(strconv.Itoa(idx)), step)) |
| 63 | + } |
| 64 | + |
| 65 | + expected := map[workflows.MeteringSpendUnit]workflows.MeteringSpendValue{ |
| 66 | + testUnitA: testUnitA.IntToSpendValue(1), |
| 67 | + } |
| 68 | + |
| 69 | + median := report.MedianSpend() |
| 70 | + |
| 71 | + require.Len(t, median, 1) |
| 72 | + require.Contains(t, maps.Keys(median), testUnitA) |
| 73 | + |
| 74 | + assert.Equal(t, expected[testUnitA].String(), median[testUnitA].String()) |
| 75 | + }) |
| 76 | + |
| 77 | + t.Run("MedianSpend returns median odd number of spend values", func(t *testing.T) { |
| 78 | + t.Parallel() |
| 79 | + |
| 80 | + report := workflows.NewMeteringReport() |
| 81 | + steps := []workflows.MeteringReportStep{ |
| 82 | + {"abc", testUnitA, testUnitA.IntToSpendValue(1)}, |
| 83 | + {"abc", testUnitA, testUnitA.IntToSpendValue(3)}, |
| 84 | + {"xyz", testUnitA, testUnitA.IntToSpendValue(2)}, |
| 85 | + } |
| 86 | + |
| 87 | + for idx, step := range steps { |
| 88 | + require.NoError(t, report.AddStep(workflows.MeteringReportStepRef(strconv.Itoa(idx)), step)) |
| 89 | + } |
| 90 | + |
| 91 | + expected := map[workflows.MeteringSpendUnit]workflows.MeteringSpendValue{ |
| 92 | + testUnitA: testUnitA.IntToSpendValue(2), |
| 93 | + } |
| 94 | + |
| 95 | + median := report.MedianSpend() |
| 96 | + |
| 97 | + require.Len(t, median, 1) |
| 98 | + require.Contains(t, maps.Keys(median), testUnitA) |
| 99 | + |
| 100 | + assert.Equal(t, expected[testUnitA].String(), median[testUnitA].String()) |
| 101 | + }) |
| 102 | + |
| 103 | + t.Run("MedianSpend returns median as average for even number of spend values", func(t *testing.T) { |
| 104 | + t.Parallel() |
| 105 | + |
| 106 | + report := workflows.NewMeteringReport() |
| 107 | + steps := []workflows.MeteringReportStep{ |
| 108 | + {"xyz", testUnitA, testUnitA.IntToSpendValue(42)}, |
| 109 | + {"abc", testUnitA, testUnitA.IntToSpendValue(1)}, |
| 110 | + {"abc", testUnitA, testUnitA.IntToSpendValue(3)}, |
| 111 | + {"xyz", testUnitA, testUnitA.IntToSpendValue(2)}, |
| 112 | + } |
| 113 | + |
| 114 | + for idx, step := range steps { |
| 115 | + require.NoError(t, report.AddStep(workflows.MeteringReportStepRef(strconv.Itoa(idx)), step)) |
| 116 | + } |
| 117 | + |
| 118 | + expected := map[workflows.MeteringSpendUnit]workflows.MeteringSpendValue{ |
| 119 | + testUnitA: testUnitA.DecimalToSpendValue(decimal.NewFromFloat(2.5)), |
| 120 | + } |
| 121 | + |
| 122 | + median := report.MedianSpend() |
| 123 | + |
| 124 | + require.Len(t, median, 1) |
| 125 | + require.Contains(t, maps.Keys(median), testUnitA) |
| 126 | + |
| 127 | + assert.Equal(t, expected[testUnitA].String(), median[testUnitA].String()) |
| 128 | + }) |
| 129 | +} |
0 commit comments