From e7827fecb399ee44f6a90aca87b93186c2f791d1 Mon Sep 17 00:00:00 2001 From: ChickenWhisky Date: Sat, 14 Dec 2024 00:16:12 +0530 Subject: [PATCH] Gonna scrap the idea of adding unit tests for now will come back to this later --- internals/orderbook/orderbook.go | 2 + internals/orderbook/orderbook_test.go | 109 ++++++++++++++++++++++++++ pkg/helpers/utils.go | 14 ++-- 3 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 internals/orderbook/orderbook_test.go diff --git a/internals/orderbook/orderbook.go b/internals/orderbook/orderbook.go index 679bda8..00b53f0 100644 --- a/internals/orderbook/orderbook.go +++ b/internals/orderbook/orderbook.go @@ -153,7 +153,9 @@ func (ob *OrderBook) CancelContract(contract models.Contract) error { // ModifyContract cancels a specific user's contract and then adds a new contract based on the updated modifications. func (ob *OrderBook) ModifyContract(contract models.Contract) { + contract.RequestType = "delete" ob.CancelContract(contract) + contract.RequestType = "add" ob.AddContract(contract) } diff --git a/internals/orderbook/orderbook_test.go b/internals/orderbook/orderbook_test.go new file mode 100644 index 0000000..7197ae6 --- /dev/null +++ b/internals/orderbook/orderbook_test.go @@ -0,0 +1,109 @@ +package orderbook + +import ( + "github.com/ChickenWhisky/makeItIntersting/pkg/models" + "testing" +) + +func TestAddContract(t *testing.T) { + ob := NewOrderBook() + contract := models.Contract{ + OrderType: "buy", + RequestType: "add", + Quantity: 10, + Price: 100, + } + + err := ob.AddContract(contract) + if err != nil { + t.Errorf("expected no error, got %v", err) + } + + if len(ob.Orders) != 1 { + t.Errorf("expected 1 contract, got %d", len(ob.Orders)) + } + +} + +func TestDeleteContract(t *testing.T) { + ob := NewOrderBook() + contract := models.Contract{ + ContractID: "123", + OrderType: "buy", + RequestType: "delete", + Quantity: 10, + Price: 100, + UserID: "user1", + } + + ob.AddContract(contract) + contract.RequestType = "delete" + err := ob.CancelContract(contract) + if err != nil { + t.Errorf("expected no error, got %v", err) + } + + if len(ob.Orders) != 0 { + t.Errorf("expected 0 contracts, got %d", len(ob.Orders)) + } +} + +func TestModifyContract(t *testing.T) { + ob := NewOrderBook() + contract := models.Contract{ + ContractID: "123", + OrderType: "buy", + RequestType: "add", + Quantity: 10, + Price: 100, + UserID: "user1", + } + + ob.AddContract(contract) + modifiedContract := models.Contract{ + ContractID: "123", + OrderType: "buy", + Quantity: 20, + Price: 150, + UserID: "user1", + } + + ob.ModifyContract(modifiedContract) + if ob.Orders["123"].Quantity != 20 { + t.Errorf("expected quantity 20, got %d", ob.Orders["123"].Quantity) + } + + if ob.Orders["123"].Price != 150 { + t.Errorf("expected price 150, got %f", ob.Orders["123"].Price) + } +} + +func TestMatchOrders(t *testing.T) { + ob := NewOrderBook() + askContract := models.Contract{ + ContractID: "ask1", + OrderType: "sell", + RequestType: "add", + Quantity: 10, + Price: 100, + } + bidContract := models.Contract{ + ContractID: "bid1", + OrderType: "buy", + RequestType: "add", + Quantity: 10, + Price: 100, + } + + ob.AddContract(askContract) + ob.AddContract(bidContract) + ob.MatchOrders() + + if len(ob.LastMatchedPrices) != 1 { + t.Errorf("expected 1 matched trade, got %d", len(ob.LastMatchedPrices)) + } + + if ob.LastMatchedPrices[0].Price != 100 { + t.Errorf("expected matched price 100, got %f", ob.LastMatchedPrices[0].Price) + } +} diff --git a/pkg/helpers/utils.go b/pkg/helpers/utils.go index 92705d1..5012448 100644 --- a/pkg/helpers/utils.go +++ b/pkg/helpers/utils.go @@ -2,19 +2,23 @@ package helpers import ( "crypto/rand" - "encoding/base64" "log" + "math/big" "os" "strconv" ) func GenerateRandomString(length int) string { + const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" b := make([]byte, length) - _, err := rand.Read(b) - if err != nil { - panic(err) + for i := range b { + num, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset)))) + if err != nil { + log.Fatal(err) + } + b[i] = charset[num.Int64()] } - return base64.StdEncoding.EncodeToString(b) + return string(b) } func ConvertStringToInt(s string) int { lengthFromEnv, err := strconv.Atoi(os.Getenv("CONTRACT_ID_LENGTH"))