gitlab.com/SkynetLabs/skyd@v1.6.9/skymodules/accounting/accounting_helpers_test.go (about)

     1  package accounting
     2  
     3  import (
     4  	"math"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"gitlab.com/NebulousLabs/fastrand"
     9  	"gitlab.com/SkynetLabs/skyd/build"
    10  	"gitlab.com/SkynetLabs/skyd/skymodules"
    11  	"gitlab.com/SkynetLabs/skyd/skymodules/renter"
    12  	"go.sia.tech/siad/modules"
    13  	"go.sia.tech/siad/modules/host"
    14  	"go.sia.tech/siad/modules/miner"
    15  	"go.sia.tech/siad/modules/wallet"
    16  	"go.sia.tech/siad/persist"
    17  	"go.sia.tech/siad/types"
    18  )
    19  
    20  // accountingTestDir joins the provided directories and prefixes them with the
    21  // Sia testing directory, removing any files or directories that previously
    22  // existed at that location.
    23  func accountingTestDir(dirs ...string) string {
    24  	path := build.TempDir("accounting", filepath.Join(dirs...))
    25  	err := os.RemoveAll(path)
    26  	if err != nil {
    27  		panic(err)
    28  	}
    29  	err = os.MkdirAll(path, persist.DefaultDiskPermissionsTest)
    30  	if err != nil {
    31  		panic(err)
    32  	}
    33  	return path
    34  }
    35  
    36  // newTestAccounting creates a new Accounting module for testing
    37  func newTestAccounting(testDir string) (*Accounting, error) {
    38  	h, m, r, w, deps := testingParams()
    39  	a, err := NewCustomAccounting(h, m, r, w, testDir, deps)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	return a, nil
    44  }
    45  
    46  // randomCurrency is a helper that returns a random currency value
    47  func randomCurrency() types.Currency {
    48  	return types.NewCurrency64(fastrand.Uint64n(math.MaxUint64))
    49  }
    50  
    51  // testingParams returns the minimum required parameters for creating an
    52  // Accounting module for testing.
    53  func testingParams() (modules.Host, modules.Miner, skymodules.Renter, modules.Wallet, modules.Dependencies) {
    54  	h := &host.Host{}
    55  	m := &miner.Miner{}
    56  	r := &mockRenter{}
    57  	w := &mockWallet{}
    58  	deps := &modules.ProductionDependencies{}
    59  	return h, m, r, w, deps
    60  }
    61  
    62  // mockRenter is a helper for Accounting unit tests
    63  type mockRenter struct {
    64  	*renter.Renter
    65  }
    66  
    67  // PeriodSpending mocks the Renter's PeriodSpending
    68  func (mr *mockRenter) PeriodSpending() (skymodules.FinancialMetrics, error) {
    69  	return skymodules.FinancialMetrics{
    70  		ContractorSpending: skymodules.ContractorSpending{
    71  			DownloadSpending: randomCurrency(),
    72  			Fees: skymodules.Fees{
    73  				ContractFees:    randomCurrency(),
    74  				SiafundFees:     randomCurrency(),
    75  				TransactionFees: randomCurrency(),
    76  			},
    77  			StorageSpending: randomCurrency(),
    78  			TotalAllocated:  randomCurrency(),
    79  			UploadSpending:  randomCurrency(),
    80  			Unspent:         randomCurrency(),
    81  			WithheldFunds:   randomCurrency(),
    82  		},
    83  		EphemeralAccountSpending: []skymodules.EphemeralAccountSpending{
    84  			{
    85  				AccountSpending: skymodules.AccountSpending{
    86  					AccountBalanceCost:    randomCurrency(),
    87  					DownloadsCost:         randomCurrency(),
    88  					RegistryReadsCost:     randomCurrency(),
    89  					RegistryWritesCost:    randomCurrency(),
    90  					RepairDownloadsCost:   randomCurrency(),
    91  					RepairUploadsCost:     randomCurrency(),
    92  					SnapshotDownloadsCost: randomCurrency(),
    93  					SnapshotUploadsCost:   randomCurrency(),
    94  					SubscriptionsCost:     randomCurrency(),
    95  					UpdatePriceTableCost:  randomCurrency(),
    96  					UploadsCost:           randomCurrency(),
    97  
    98  					Balance: randomCurrency(),
    99  					Residue: randomCurrency(),
   100  				},
   101  				HostKey: types.SiaPublicKey{},
   102  			},
   103  		},
   104  	}, nil
   105  }
   106  
   107  // mockWallet is a helper for Accounting unit tests
   108  type mockWallet struct {
   109  	*wallet.Wallet
   110  }
   111  
   112  // ConfirmedBalance mocks the Wallet's ConfirmedBalance
   113  func (mw *mockWallet) ConfirmedBalance() (types.Currency, types.Currency, types.Currency, error) {
   114  	sc := randomCurrency()
   115  	sf := randomCurrency()
   116  	return sc, sf, types.ZeroCurrency, nil
   117  }