github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/session/pingpong/settlement_history_storage_test.go (about)

     1  /*
     2   * Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package pingpong
    19  
    20  import (
    21  	"math/big"
    22  	"os"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/ethereum/go-ethereum/common"
    27  	"github.com/mysteriumnetwork/node/core/storage/boltdb"
    28  	"github.com/mysteriumnetwork/node/identity"
    29  	"github.com/mysteriumnetwork/payments/crypto"
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  func TestSettlementHistoryStorage(t *testing.T) {
    34  	dir, err := os.MkdirTemp("", "providerInvoiceTest")
    35  	assert.NoError(t, err)
    36  	defer os.RemoveAll(dir)
    37  
    38  	bolt, err := boltdb.NewStorage(dir)
    39  	assert.NoError(t, err)
    40  	defer bolt.Close()
    41  
    42  	storage := NewSettlementHistoryStorage(bolt)
    43  
    44  	hermesAddress := common.HexToAddress("0x3313189b9b945DD38E7bfB6167F9909451582eE5")
    45  	providerID := identity.FromAddress("0x79bb2a1c5E0075005F084a66A44D5e930A88eC86")
    46  	entry1 := SettlementHistoryEntry{
    47  		TxHash:       common.BigToHash(big.NewInt(1)),
    48  		ProviderID:   providerID,
    49  		HermesID:     hermesAddress,
    50  		Time:         time.Date(2020, 1, 1, 1, 0, 0, 0, time.UTC),
    51  		Promise:      crypto.Promise{},
    52  		Beneficiary:  common.HexToAddress("0x4443189b9b945DD38E7bfB6167F9909451582eE5"),
    53  		Amount:       big.NewInt(123),
    54  		TotalSettled: big.NewInt(321),
    55  	}
    56  	entry2 := SettlementHistoryEntry{
    57  		TxHash:       common.BigToHash(big.NewInt(2)),
    58  		ProviderID:   providerID,
    59  		HermesID:     hermesAddress,
    60  		Time:         time.Date(2020, 1, 1, 2, 0, 0, 0, time.UTC),
    61  		Promise:      crypto.Promise{},
    62  		Beneficiary:  common.HexToAddress("0x4443189b9b945DD38E7bfB6167F9909451582eE5"),
    63  		Amount:       big.NewInt(456),
    64  		TotalSettled: big.NewInt(654),
    65  	}
    66  
    67  	t.Run("Returns empty list if no results exist", func(t *testing.T) {
    68  		entries, err := storage.List(SettlementHistoryFilter{})
    69  		assert.NoError(t, err)
    70  		assert.Len(t, entries, 0)
    71  		assert.EqualValues(t, []SettlementHistoryEntry{}, entries)
    72  	})
    73  
    74  	t.Run("Inserts a history entry successfully", func(t *testing.T) {
    75  		err := storage.Store(entry1)
    76  		assert.NoError(t, err)
    77  	})
    78  
    79  	t.Run("Fetches the inserted entry", func(t *testing.T) {
    80  		entries, err := storage.List(SettlementHistoryFilter{})
    81  		assert.NoError(t, err)
    82  		assert.Len(t, entries, 1)
    83  		assert.EqualValues(t, []SettlementHistoryEntry{entry1}, entries)
    84  	})
    85  
    86  	t.Run("Returns sorted results", func(t *testing.T) {
    87  		err := storage.Store(entry2)
    88  		assert.NoError(t, err)
    89  
    90  		entries, err := storage.List(SettlementHistoryFilter{})
    91  		assert.NoError(t, err)
    92  		assert.Len(t, entries, 2)
    93  		assert.EqualValues(t, []SettlementHistoryEntry{entry2, entry1}, entries)
    94  	})
    95  }