decred.org/dcrdex@v1.0.5/server/book/accounts_test.go (about)

     1  package book
     2  
     3  import (
     4  	"testing"
     5  
     6  	"decred.org/dcrdex/dex/order"
     7  )
     8  
     9  func TestAccounts(t *testing.T) {
    10  	sellerTo := "sellerTo"
    11  	sellerFrom := "sellerFrom"
    12  	loSell := newLimitOrder(true, 1e8, 10, order.StandingTiF, 0)
    13  	loSell.Coins = []order.CoinID{[]byte(sellerFrom)}
    14  	loSell.Address = sellerTo
    15  
    16  	seller2To := "seller2To"
    17  	seller2From := "seller2From"
    18  	loSell2 := newLimitOrder(true, 1e8, 10, order.StandingTiF, 0)
    19  	loSell2.Coins = []order.CoinID{[]byte(seller2From)}
    20  	loSell2.Address = seller2To
    21  
    22  	buyerTo := "buyerTo"
    23  	buyerFrom := "buyerFrom"
    24  	loBuy := newLimitOrder(false, 1e8, 10, order.StandingTiF, 0)
    25  	loBuy.Coins = []order.CoinID{[]byte(buyerFrom)}
    26  	loBuy.Address = buyerTo
    27  
    28  	// Same buyer, other order
    29  	loBuy2 := newLimitOrder(false, 2e8, 10, order.StandingTiF, 0)
    30  	loBuy2.Coins = []order.CoinID{[]byte(buyerFrom)}
    31  	loBuy2.Address = buyerTo
    32  
    33  	allAddrs := []string{sellerTo, sellerFrom, buyerTo, buyerFrom, seller2From, seller2To}
    34  	allOrds := []*order.LimitOrder{loSell, loSell2, loBuy, loBuy2}
    35  
    36  	ensureCount := func(tag string, addr string, n int, counter func(string, func(*order.LimitOrder))) {
    37  		t.Helper()
    38  		var count int
    39  		counter(addr, func(*order.LimitOrder) {
    40  			count++
    41  		})
    42  		if n != count {
    43  			t.Fatalf("%s: wrong %s base asset count: wanted %d, got %d", tag, addr, n, count)
    44  		}
    45  	}
    46  
    47  	ensureBaseCount := func(tag string, tracker *accountTracker, addr string, n int) {
    48  		t.Helper()
    49  		ensureCount(tag, addr, n, tracker.iterateBaseAccount)
    50  	}
    51  
    52  	ensureQuoteCount := func(tag string, tracker *accountTracker, addr string, n int) {
    53  		t.Helper()
    54  		ensureCount(tag, addr, n, tracker.iterateQuoteAccount)
    55  	}
    56  
    57  	type test struct {
    58  		name           string
    59  		tracker        *accountTracker
    60  		expBaseCounts  map[string]int
    61  		expQuoteCounts map[string]int
    62  	}
    63  
    64  	runTests := func(tests []*test, prep func(tracker *accountTracker)) {
    65  		for _, tt := range tests {
    66  			if tt.expBaseCounts == nil {
    67  				tt.expBaseCounts = make(map[string]int)
    68  			}
    69  			if tt.expQuoteCounts == nil {
    70  				tt.expQuoteCounts = make(map[string]int)
    71  			}
    72  			prep(tt.tracker)
    73  			for _, acctAddr := range allAddrs {
    74  				ensureBaseCount(tt.name, tt.tracker, acctAddr, tt.expBaseCounts[acctAddr])
    75  				ensureQuoteCount(tt.name, tt.tracker, acctAddr, tt.expQuoteCounts[acctAddr])
    76  			}
    77  		}
    78  	}
    79  
    80  	tests := []*test{
    81  		{
    82  			name:    "non-tracker",
    83  			tracker: newAccountTracker(0),
    84  		},
    85  		{
    86  			name:    "base-tracker",
    87  			tracker: newAccountTracker(AccountTrackingBase),
    88  			expBaseCounts: map[string]int{
    89  				buyerTo:     2,
    90  				sellerFrom:  1,
    91  				seller2From: 1,
    92  			},
    93  		},
    94  		{
    95  			name:    "quote-tracker",
    96  			tracker: newAccountTracker(AccountTrackingQuote),
    97  			expQuoteCounts: map[string]int{
    98  				buyerFrom: 2,
    99  				sellerTo:  1,
   100  				seller2To: 1,
   101  			},
   102  		},
   103  		{
   104  			name:    "both-tracker",
   105  			tracker: newAccountTracker(AccountTrackingQuote | AccountTrackingBase),
   106  			expQuoteCounts: map[string]int{
   107  				buyerFrom: 2,
   108  				sellerTo:  1,
   109  				seller2To: 1,
   110  			},
   111  			expBaseCounts: map[string]int{
   112  				buyerTo:     2,
   113  				sellerFrom:  1,
   114  				seller2From: 1,
   115  			},
   116  		},
   117  	}
   118  
   119  	runTests(tests, func(tracker *accountTracker) {
   120  		for _, ord := range allOrds {
   121  			tracker.add(ord)
   122  		}
   123  	})
   124  
   125  	// For the next tests, we'll add all of the orders, but then remove one of
   126  	// the buyer's orders.
   127  	tests = []*test{
   128  		{
   129  			name:    "remove-a-buy",
   130  			tracker: newAccountTracker(AccountTrackingBase | AccountTrackingQuote),
   131  			expQuoteCounts: map[string]int{
   132  				buyerFrom: 1,
   133  				sellerTo:  1,
   134  				seller2To: 1,
   135  			},
   136  			expBaseCounts: map[string]int{
   137  				buyerTo:     1,
   138  				sellerFrom:  1,
   139  				seller2From: 1,
   140  			},
   141  		},
   142  	}
   143  
   144  	runTests(tests, func(tracker *accountTracker) {
   145  		for _, ord := range allOrds {
   146  			tracker.add(ord)
   147  		}
   148  		tracker.remove(loBuy2)
   149  	})
   150  
   151  	// Add all, remove all, and make sure the book is empty.
   152  	tests = []*test{
   153  		{
   154  			name:    "remove-all",
   155  			tracker: newAccountTracker(AccountTrackingBase | AccountTrackingQuote),
   156  		},
   157  	}
   158  	runTests(tests, func(tracker *accountTracker) {
   159  		for _, ord := range allOrds {
   160  			tracker.add(ord)
   161  		}
   162  		for _, ord := range allOrds {
   163  			tracker.remove(ord)
   164  		}
   165  	})
   166  }