Skip to content

Commit

Permalink
fix account cause error (#3485)
Browse files Browse the repository at this point in the history
  • Loading branch information
bxy4543 committed Jul 10, 2023
1 parent 288c4f8 commit 1570909
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
8 changes: 6 additions & 2 deletions controllers/account/controllers/account_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,10 @@ func (r *AccountReconciler) syncAccount(ctx context.Context, name, accountNamesp
if err := r.syncRoleAndRoleBinding(ctx, name, userNamespace); err != nil {
return nil, fmt.Errorf("sync role and rolebinding failed: %v", err)
}

err := r.initBalance(&account)
if err != nil {
return nil, fmt.Errorf("sync init balance failed: %v", err)
}
// add account balance when account is new user
stringAmount := os.Getenv(NEWACCOUNTAMOUNTENV)
if stringAmount == "" {
Expand Down Expand Up @@ -497,6 +500,7 @@ const (
Threshold3 = 1999 * BaseUnit
Threshold4 = 4999 * BaseUnit
Threshold5 = 19999 * BaseUnit
Ratio0 int64 = 0
Ratio1 int64 = 10
Ratio2 int64 = 15
Ratio3 int64 = 20
Expand All @@ -508,7 +512,7 @@ func giveGift(amount int64) int64 {
var ratio int64
switch {
case amount < Threshold1:
return 0
ratio = Ratio0
case amount < Threshold2:
ratio = Ratio1
case amount < Threshold3:
Expand Down
25 changes: 13 additions & 12 deletions controllers/account/controllers/account_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,19 @@ func Test_giveGift(t *testing.T) {
want int64
}{
// [1-298] -> 0%, [299-598] -> 10%, [599-1998] -> 15%, [1999-4998] -> 20%, [4999-19998] -> 25%, [19999+] -> 30%
{name: "0% less than 299", args: args{amount: 100 * BaseUnit}, want: 0},
{name: "10% between 299 and 599", args: args{amount: 299 * BaseUnit}, want: 29_900_000},
{name: "10% between 299 and 599", args: args{amount: 300 * BaseUnit}, want: 30_000_000},
{name: "15% between 599 and 1999", args: args{amount: 599 * BaseUnit}, want: 89_850_000},
{name: "15% between 599 and 1999", args: args{amount: 600 * BaseUnit}, want: 90_000_000},
{name: "20% between 1999 and 4999", args: args{amount: 1999 * BaseUnit}, want: 399_800_000},
{name: "20% between 1999 and 4999", args: args{amount: 2000 * BaseUnit}, want: 400_000_000},
{name: "25% between 4999 and 19999", args: args{amount: 4999 * BaseUnit}, want: 1249_750_000},
{name: "25% between 4999 and 19999", args: args{amount: 5000 * BaseUnit}, want: 1250_000_000},
{name: "30% more than 19999", args: args{amount: 19999 * BaseUnit}, want: 5999_700_000},
{name: "30% more than 19999", args: args{amount: 20000 * BaseUnit}, want: 6000_000_000},
{name: "30% more than 19999", args: args{amount: 99999 * BaseUnit}, want: 29999_700_000},
{name: "0% less than 299", args: args{amount: 100 * BaseUnit}, want: 0 + 100*BaseUnit},
{name: "10% between 299 and 599", args: args{amount: 299 * BaseUnit}, want: 29_900_000 + 299*BaseUnit},
{name: "10% between 299 and 599", args: args{amount: 300 * BaseUnit}, want: 30_000_000 + 300*BaseUnit},
{name: "15% between 599 and 1999", args: args{amount: 599 * BaseUnit}, want: 89_850_000 + 599*BaseUnit},
{name: "15% between 599 and 1999", args: args{amount: 600 * BaseUnit}, want: 90_000_000 + 600*BaseUnit},
{name: "20% between 1999 and 4999", args: args{amount: 1999 * BaseUnit}, want: 399_800_000 + 1999*BaseUnit},
{name: "20% between 1999 and 4999", args: args{amount: 2000 * BaseUnit}, want: 400_000_000 + 2000*BaseUnit},
{name: "25% between 4999 and 19999", args: args{amount: 4999 * BaseUnit}, want: 1249_750_000 + 4999*BaseUnit},
{name: "25% between 4999 and 19999", args: args{amount: 5000 * BaseUnit}, want: 1250_000_000 + 5000*BaseUnit},
{name: "30% more than 19999", args: args{amount: 19999 * BaseUnit}, want: 5999_700_000 + 19999*BaseUnit},
{name: "30% more than 19999", args: args{amount: 20000 * BaseUnit}, want: 6000_000_000 + 20000*BaseUnit},
{name: "30% more than 19999", args: args{amount: 99999 * BaseUnit}, want: 29999_700_000 + 99999*BaseUnit},
{"0% less than 299", args{amount: 1 * BaseUnit}, 1 * BaseUnit},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 1570909

Please sign in to comment.