code.vegaprotocol.io/vega@v0.79.0/datanode/sqlsubscribers/margin_levels_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package sqlsubscribers_test
    17  
    18  import (
    19  	"context"
    20  	"testing"
    21  	"time"
    22  
    23  	"code.vegaprotocol.io/vega/core/events"
    24  	"code.vegaprotocol.io/vega/core/types"
    25  	"code.vegaprotocol.io/vega/datanode/entities"
    26  	"code.vegaprotocol.io/vega/datanode/sqlsubscribers"
    27  	"code.vegaprotocol.io/vega/datanode/sqlsubscribers/mocks"
    28  	"code.vegaprotocol.io/vega/libs/num"
    29  
    30  	"github.com/golang/mock/gomock"
    31  )
    32  
    33  func TestMarginLevels_Push(t *testing.T) {
    34  	ctrl := gomock.NewController(t)
    35  
    36  	accountSource := TestNullAccountSource{}
    37  
    38  	store := mocks.NewMockMarginLevelsStore(ctrl)
    39  
    40  	store.EXPECT().Add(gomock.Any()).Times(1)
    41  	store.EXPECT().Flush(gomock.Any()).Times(2)
    42  	subscriber := sqlsubscribers.NewMarginLevels(store, accountSource)
    43  	subscriber.Flush(context.Background())
    44  	subscriber.Push(context.Background(), events.NewMarginLevelsEvent(context.Background(), types.MarginLevels{
    45  		MaintenanceMargin:      num.NewUint(1000),
    46  		SearchLevel:            num.NewUint(1000),
    47  		InitialMargin:          num.NewUint(1000),
    48  		CollateralReleaseLevel: num.NewUint(1000),
    49  		OrderMargin:            num.UintZero(),
    50  		Party:                  "DEADBEEF",
    51  		MarketID:               "DEADBEEF",
    52  		Asset:                  "DEADBEEF",
    53  		Timestamp:              time.Now().UnixNano(),
    54  	}))
    55  
    56  	subscriber.Flush(context.Background())
    57  }
    58  
    59  type TestAccountSource struct{}
    60  
    61  func (TestAccountSource) Obtain(ctx context.Context, a *entities.Account) error {
    62  	a.ID = "1"
    63  	return nil
    64  }
    65  
    66  func (TestAccountSource) GetByID(id entities.AccountID) (entities.Account, error) {
    67  	panic("implement me")
    68  }
    69  
    70  type TestNullAccountSource struct{}
    71  
    72  func (TestNullAccountSource) Obtain(ctx context.Context, a *entities.Account) error {
    73  	return nil
    74  }
    75  
    76  func (TestNullAccountSource) GetByID(ctx context.Context, id entities.AccountID) (entities.Account, error) {
    77  	panic("implement me")
    78  }