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

61 lines
1.3 KiB
Go
Raw Normal View History

2022-08-28 17:42:43 -07:00
package memdb
import (
"cosmos-test/x/cosmostest/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
)
func TestBasicFlow(t *testing.T) {
// this should panic if there's a problem
BidDB.Mount()
// add a bid
2022-08-28 18:03:12 -07:00
if err := BidDB.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-08-28 18:03:12 -07:00
if err := BidDB.AddBid("0", &types.Bid{Owner: "cosmos456", Amount: "200"}); err != nil {
panic(err)
}
2022-08-28 17:42:43 -07:00
2022-08-30 14:43:42 -07:00
// get all bids out
if bids, err := BidDB.GetBids("0"); err != nil {
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)))
}
}
2022-08-29 16:23:07 -07:00
// check highest bid
if bid, err := BidDB.GetLowestBid("0"); err != nil {
2022-08-28 17:42:43 -07:00
panic(err)
} else {
2022-08-28 18:03:12 -07:00
if bid.Owner != "cosmos456" || bid.Amount != "200" {
panic("Highest auction item not selected")
}
2022-08-28 17:42:43 -07:00
}
2022-08-29 16:23:07 -07:00
// clear auction under id 0
if err := BidDB.ClearAuction("0"); err != nil {
panic(err)
} else {
// expect auction key to be unknown
if err := BidDB.db.View(func(txn *badger.Txn) error {
_, err := txn.Get([]byte("0"))
return err
}); err != nil {
if err != badger.ErrKeyNotFound {
panic(err)
}
}
}
2022-08-28 17:42:43 -07:00
}