gpu-compute-chain/x/colinearcore/memdb/biddb_test.go

100 lines
2.3 KiB
Go
Raw Normal View History

2022-08-28 17:42:43 -07:00
package memdb
import (
2022-09-06 15:18:09 -07:00
"colinear/x/colinearcore/types"
2022-08-30 14:43:42 -07:00
"fmt"
2022-08-28 17:42:43 -07:00
"testing"
2022-08-29 16:23:07 -07:00
"github.com/dgraph-io/badger/v3"
2022-08-28 17:42:43 -07:00
)
2022-09-13 14:56:38 -07:00
func TestBidFlow(t *testing.T) {
2022-08-28 17:42:43 -07:00
// this should panic if there's a problem
2022-09-13 14:56:38 -07:00
AuctionDB.Mount()
2022-08-28 17:42:43 -07:00
// add a bid
2022-09-13 14:56:38 -07:00
if err := AuctionDB.AddBid("0", &types.Bid{Owner: "cosmos123", Amount: "100"}); err != nil {
2022-08-28 17:42:43 -07:00
panic(err)
}
2022-08-29 16:23:07 -07:00
// add a higher bid (db funcs won't check amount, though)
2022-09-13 14:56:38 -07:00
if err := AuctionDB.AddBid("0", &types.Bid{Owner: "cosmos456", Amount: "200"}); err != nil {
2022-08-28 18:03:12 -07:00
panic(err)
}
2022-08-28 17:42:43 -07:00
2022-08-30 14:43:42 -07:00
// get all bids out
2022-09-13 14:56:38 -07:00
if bids, err := AuctionDB.GetBids("0"); err != nil {
2022-08-30 14:43:42 -07:00
panic(err)
} else {
if bids == nil {
panic("GetBids(0) should should not return nil")
}
if len(bids) != 2 {
panic(fmt.Sprintf("Length of GetBids(0) should be 2, instead got %d", len(bids)))
}
}
// check lowest bid
2022-09-13 14:56:38 -07:00
if bid, err := AuctionDB.GetLowestBid("0"); err != nil {
2022-08-28 17:42:43 -07:00
panic(err)
} else {
if bid.Owner != "cosmos123" || bid.Amount != "100" {
panic(fmt.Sprintf("Lowest auction item not selected. Amount %s with owner %s was chosen.", bid.Amount, bid.Owner))
2022-08-28 18:03:12 -07:00
}
2022-08-28 17:42:43 -07:00
}
2022-08-29 16:23:07 -07:00
// clear auction under id 0
2022-09-13 14:56:38 -07:00
if err := AuctionDB.ClearAuctionBids("0"); err != nil {
2022-08-29 16:23:07 -07:00
panic(err)
} else {
// expect auction key to be unknown
2022-09-13 14:56:38 -07:00
if err := AuctionDB.db.View(func(txn *badger.Txn) error {
2022-08-29 16:23:07 -07:00
_, err := txn.Get([]byte("0"))
return err
}); err != nil {
if err != badger.ErrKeyNotFound {
panic(err)
}
}
}
2022-08-28 17:42:43 -07:00
}
2022-09-13 14:56:38 -07:00
func TestVerifiedFlow(t *testing.T) {
// this should panic if there's a problem
AuctionDB.Mount()
providers := []string{
"colinear1...",
"colinear2...",
"colinear3...",
"colinear4...",
}
if err := AuctionDB.SetVerifiedProviders("0", providers); err != nil {
panic(err)
}
if prov, err := AuctionDB.GetVerifiedProviders("0"); err != nil {
panic(err)
} else {
if len(prov) != 4 {
panic(fmt.Sprintf("providers length should be 4, got %d instead", len(prov)))
}
if prov[0] != "colinear1..." {
panic(fmt.Sprintf("verified provider 1 should be `colinear1...`, instead got `%s`", prov[0]))
}
}
if err := AuctionDB.ClearVerifiedProviders("0"); err != nil {
panic(err)
}
if _, err := AuctionDB.GetVerifiedProviders("0"); err == nil {
panic("Should not return a provider")
}
// } else { // just a test - should exit with "key not found"
// panic(err)
// }
}