|
| 1 | +//go:build integration |
| 2 | + |
| 3 | +package config |
| 4 | + |
| 5 | +// Amend-path behavior per stored amount basis (PR A). Commitment rows are |
| 6 | +// seeded directly: no writer produces them yet. |
| 7 | + |
| 8 | +import ( |
| 9 | + "math" |
| 10 | + "math/big" |
| 11 | + "strconv" |
| 12 | + "testing" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/LeanerCloud/cloud-commitments-go/pkg/ladder" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | +) |
| 18 | + |
| 19 | +func mustOnDemand(t *testing.T, rows []LadderInFlightRow) float64 { |
| 20 | + t.Helper() |
| 21 | + v, err := LadderInFlightOnDemand(rows) |
| 22 | + require.NoError(t, err) |
| 23 | + return v |
| 24 | +} |
| 25 | + |
| 26 | +func (f *ceilingFixture) setBasis(id, basis, rate string) { |
| 27 | + f.t.Helper() |
| 28 | + _, err := f.store.db.Exec(f.ctx, "UPDATE ladder_tranches SET amount_basis=NULLIF($2,''),commitment_rate=NULLIF($3,'') WHERE id=$1", id, basis, rate) |
| 29 | + require.NoError(f.t, err) |
| 30 | +} |
| 31 | + |
| 32 | +func seedNettingRows(t *testing.T, f *ceilingFixture, basis LadderAmountBasis, rate string, amounts ...string) { |
| 33 | + t.Helper() |
| 34 | + for _, a := range amounts { |
| 35 | + v, err := strconv.ParseFloat(a, 64) |
| 36 | + require.NoError(t, err) |
| 37 | + tr := lifecycleTranche(f.cfgID, v, ladder.TrancheStatusFired, lockTestAsOf.Add(-time.Hour)) |
| 38 | + tr.AmountBasis = &basis |
| 39 | + if rate != "" { |
| 40 | + tr.CommitmentRate = &rate |
| 41 | + } |
| 42 | + require.NoError(t, f.store.SaveLadderTranches(f.ctx, []LadderTrancheDB{tr})) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func plannedRows(t *testing.T, f *ceilingFixture) []LadderInFlightRow { |
| 47 | + t.Helper() |
| 48 | + var got []LadderInFlightRow |
| 49 | + _, err := f.store.PlanAndSaveLadderRun(f.ctx, f.cfgID, lockTestAsOf, func(rows []LadderInFlightRow) (*LadderRunDB, []LadderTrancheDB, error) { |
| 50 | + got = rows |
| 51 | + return lockTestRun(f.cfgID), nil, nil |
| 52 | + }) |
| 53 | + require.NoError(t, err) |
| 54 | + return got |
| 55 | +} |
| 56 | + |
| 57 | +// The reference is the pre-change algorithm: sum the decimals exactly, then |
| 58 | +// round up to a float only when the nearest float is below the sum. |
| 59 | +func TestLadderNetting_LegacyOnlyRowsKeepTheExactTotalBitForBit(t *testing.T) { |
| 60 | + f := newCeilingFixture(t) |
| 61 | + amounts := []string{"0.1", "0.2", "0.3", "1234.567891", "0.000001", "0.000001", "7.7", "19.999999"} |
| 62 | + seedNettingRows(t, f, LadderAmountOnDemand, "", amounts...) |
| 63 | + exact := new(big.Rat) |
| 64 | + for _, a := range amounts { |
| 65 | + v, ok := new(big.Rat).SetString(a) |
| 66 | + require.True(t, ok) |
| 67 | + exact.Add(exact, v) |
| 68 | + } |
| 69 | + want, err := nettingFloat(exact) |
| 70 | + require.NoError(t, err) |
| 71 | + got := mustOnDemand(t, plannedRows(t, f)) |
| 72 | + require.Equal(t, math.Float64bits(want), math.Float64bits(got)) |
| 73 | +} |
| 74 | + |
| 75 | +func TestLadderNetting_ThirdRateRowNeverNetsBelowTheExactRational(t *testing.T) { |
| 76 | + f := newCeilingFixture(t) |
| 77 | + seedNettingRows(t, f, LadderAmountCommitment, "1/3", "1.000001") |
| 78 | + rows := plannedRows(t, f) |
| 79 | + require.Len(t, rows, 1) |
| 80 | + require.LessOrEqual(t, new(big.Rat).SetFloat64(rows[0].Rate).Cmp(big.NewRat(1, 3)), 0) |
| 81 | + got := mustOnDemand(t, rows) |
| 82 | + require.GreaterOrEqual(t, new(big.Rat).SetFloat64(got).Cmp(big.NewRat(3000003, 1000000)), 0) |
| 83 | +} |
| 84 | + |
| 85 | +func TestLadderNetting_NullBasisStillFailsLoudBeforeBuilder(t *testing.T) { |
| 86 | + f := newCeilingFixture(t) |
| 87 | + _, ids := f.plan(fptr(10), fptr(0), fptr(100), 5) |
| 88 | + f.setBasis(ids[0], "", "") |
| 89 | + built := false |
| 90 | + _, err := f.store.PlanAndSaveLadderRun(f.ctx, f.cfgID, time.Now().UTC().Truncate(time.Microsecond), func([]LadderInFlightRow) (*LadderRunDB, []LadderTrancheDB, error) { |
| 91 | + built = true |
| 92 | + return nil, nil, nil |
| 93 | + }) |
| 94 | + require.Error(t, err) |
| 95 | + require.False(t, built) |
| 96 | +} |
0 commit comments