From 93729aea843772fde1886023771fce67568e5ae1 Mon Sep 17 00:00:00 2001 From: Anatoly Ostrovsky Date: Thu, 7 Dec 2023 01:27:20 +0200 Subject: [PATCH] Unify db functions --- pkg/db/db.go | 13 +++++++ pkg/models/application_entity_test.go | 6 ++- .../matching_service_stop_limit_order_test.go | 38 +++++++++---------- .../matching_service_stop_loss_order_test.go | 38 +++++++++---------- pkg/services/payment_service_test.go | 8 ++-- pkg/services/price_level_test.go | 19 +++++----- pkg/services/registration_service_test.go | 8 ++-- pkg/services/service_suite_test.go | 2 +- pkg/utils/db_test_utils.go | 20 ---------- 9 files changed, 73 insertions(+), 79 deletions(-) delete mode 100644 pkg/utils/db_test_utils.go diff --git a/pkg/db/db.go b/pkg/db/db.go index 69b2655..01a5ed6 100644 --- a/pkg/db/db.go +++ b/pkg/db/db.go @@ -1,6 +1,8 @@ package db import ( + "context" + log2 "log" "open-outcry/pkg/conf" "reflect" @@ -67,3 +69,14 @@ func QueryList[T interface{}](query string, args ...any) []T { } } + +func GetCount(tableName string) int { + return QueryVal[int]("SELECT COUNT(*) FROM " + tableName) +} + +func DeleteAll(tableName string) { + _, err := Instance().ExecContext(context.Background(), "DELETE FROM "+tableName) + if err != nil { + log2.Fatal(err) + } +} diff --git a/pkg/models/application_entity_test.go b/pkg/models/application_entity_test.go index 910c799..e319686 100644 --- a/pkg/models/application_entity_test.go +++ b/pkg/models/application_entity_test.go @@ -1,9 +1,11 @@ package models -import "open-outcry/pkg/utils" +import ( + "open-outcry/pkg/db" +) func (assert *ModelsTestSuite) TestMasterEntity() { // expect master entity to exist - assert.Equal(1, utils.GetCount("app_entity")) + assert.Equal(1, db.GetCount("app_entity")) assert.Equal(AppEntityId(Master), FindAppEntityExternalId("MASTER")) } diff --git a/pkg/services/matching_service_stop_limit_order_test.go b/pkg/services/matching_service_stop_limit_order_test.go index 7696ef7..11dce2e 100644 --- a/pkg/services/matching_service_stop_limit_order_test.go +++ b/pkg/services/matching_service_stop_limit_order_test.go @@ -1,8 +1,8 @@ package services import ( + "open-outcry/pkg/db" "open-outcry/pkg/models" - "open-outcry/pkg/utils" ) func (assert *ServiceTestSuite) TestCreateStopLimitSellOrderSave() { @@ -11,7 +11,7 @@ func (assert *ServiceTestSuite) TestCreateStopLimitSellOrderSave() { // then: assert.NotNil(res) - assert.Equal(1, utils.GetCount("stop_order")) + assert.Equal(1, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -27,7 +27,7 @@ func (assert *ServiceTestSuite) TestCreateStopLimitBuyOrderBuy() { // then: - assert.Equal(1, utils.GetCount("stop_order")) + assert.Equal(1, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -43,7 +43,7 @@ func (assert *ServiceTestSuite) TestCreateStopLimitSellOrderActivate() { ProcessTradeOrder(assert.tradingAccount2, "BTC_EUR", "LIMIT", models.Buy, 10, 1, "GTC") // then: the order becomes activated - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(1, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal([]PriceVolume{{10.0, 2.0}}, GetVolumes("BTC_EUR", models.Sell)) @@ -57,7 +57,7 @@ func (assert *ServiceTestSuite) TestCreateStopLimitBuyOrderActivate() { ProcessTradeOrder(assert.tradingAccount1, "BTC_EUR", "LIMIT", models.Sell, 10, 1, "GTC") // then: the order becomes activated - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(1, GetBuyBookOrderCount()) assert.Equal([]PriceVolume{{10.0, 2.0}}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -71,7 +71,7 @@ func (assert *ServiceTestSuite) TestCreateStopLimitSellOrderNonCrossing() { ProcessTradeOrder(assert.tradingAccount2, "BTC_EUR", "LIMIT", models.Buy, 11, 1, "GTC") // then: the order remains deactivated - assert.Equal(1, utils.GetCount("stop_order")) + assert.Equal(1, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -81,7 +81,7 @@ func (assert *ServiceTestSuite) TestCreateStopLimitSellOrderNonCrossing() { ProcessTradeOrder(assert.tradingAccount2, "BTC_EUR", "LIMIT", models.Buy, 9, 1, "GTC") // then: it becomes activated - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(1, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal([]PriceVolume{{10.0, 2.0}}, GetVolumes("BTC_EUR", models.Sell)) @@ -95,7 +95,7 @@ func (assert *ServiceTestSuite) TestCreateStopLimitBuyOrderNonCrossing() { ProcessTradeOrder(assert.tradingAccount1, "BTC_EUR", "LIMIT", models.Sell, 11, 1, "GTC") // then: the order remains deactivated - assert.Equal(1, utils.GetCount("stop_order")) + assert.Equal(1, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -105,7 +105,7 @@ func (assert *ServiceTestSuite) TestCreateStopLimitBuyOrderNonCrossing() { ProcessTradeOrder(assert.tradingAccount1, "BTC_EUR", "LIMIT", models.Sell, 9, 1, "GTC") // then: it becomes activated - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(1, GetBuyBookOrderCount()) assert.Equal([]PriceVolume{{10.0, 2.0}}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -119,7 +119,7 @@ func (assert *ServiceTestSuite) TestCreateStopLimitSellOrderActivateAndSettle() ProcessTradeOrder(assert.tradingAccount2, "BTC_EUR", "LIMIT", models.Buy, 10, 3, "GTC") // then: the order becomes activated and settled - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -134,7 +134,7 @@ func (assert *ServiceTestSuite) TestCreateStopLimitBuyOrderActivateAndSettle() { ProcessTradeOrder(assert.tradingAccount2, "BTC_EUR", "LIMIT", models.Sell, 10, 3, "GTC") // then: the order becomes activated and settled - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -148,7 +148,7 @@ func (assert *ServiceTestSuite) TestCreateStopLimitSellOrderActivateAndSettleOpp ProcessTradeOrder(assert.tradingAccount1, "BTC_EUR", "LIMIT", models.Sell, 10, 1, "GTC") // then: the order becomes activated and settled - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -162,7 +162,7 @@ func (assert *ServiceTestSuite) TestCreateStopLimitBuyOrderActivateAndSettleOppo ProcessTradeOrder(assert.tradingAccount1, "BTC_EUR", "LIMIT", models.Buy, 10, 1, "GTC") // then: the order becomes activated and settled - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -178,7 +178,7 @@ func (assert *ServiceTestSuite) TestCreateStopLimitSellOrderActivateAndSettleBef // then: the order becomes activated and settled assert.Equal([]float64{10.0, 10.0, 11.0}, GetTradePrices()) - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -194,7 +194,7 @@ func (assert *ServiceTestSuite) TestCreateStopLimitBuyOrderActivateAndSettleBefo // // then: the order becomes activated and settled assert.Equal([]float64{10.0, 10.0, 9.0}, GetTradePrices()) - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -208,7 +208,7 @@ func (assert *ServiceTestSuite) TestCreateStopLimitSellOrderActivateByMarketAndS ProcessTradeOrder(assert.tradingAccount2, "BTC_EUR", "LIMIT", models.Buy, 10, 3, "GTC") // then: the order becomes activated and settled - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -219,7 +219,7 @@ func (assert *ServiceTestSuite) TestCreateStopLimitSellOrderActivateByMarketAndS ProcessTradeOrder(assert.tradingAccount1, "BTC_EUR", models.Market, models.Sell, 0, 1, "GTC") // then: the order becomes activated and settled - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -233,7 +233,7 @@ func (assert *ServiceTestSuite) TestCreateStopLimitBuyOrderActivateByMarketAndSe ProcessTradeOrder(assert.tradingAccount2, "BTC_EUR", "LIMIT", models.Sell, 10, 3, "GTC") // // then: the order becomes activated and settled - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -244,7 +244,7 @@ func (assert *ServiceTestSuite) TestCreateStopLimitBuyOrderActivateByMarketAndSe ProcessTradeOrder(assert.tradingAccount1, "BTC_EUR", models.Market, models.Buy, 0, 10, "GTC") // // then: the order becomes activated and settled - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) diff --git a/pkg/services/matching_service_stop_loss_order_test.go b/pkg/services/matching_service_stop_loss_order_test.go index 7b09596..1fc3a86 100644 --- a/pkg/services/matching_service_stop_loss_order_test.go +++ b/pkg/services/matching_service_stop_loss_order_test.go @@ -1,8 +1,8 @@ package services import ( + "open-outcry/pkg/db" "open-outcry/pkg/models" - "open-outcry/pkg/utils" ) func (assert *ServiceTestSuite) TestCreateStopLossSellOrderSave() { @@ -12,7 +12,7 @@ func (assert *ServiceTestSuite) TestCreateStopLossSellOrderSave() { // then: assert.NotNil(res) - assert.Equal(1, utils.GetCount("stop_order")) + assert.Equal(1, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -26,7 +26,7 @@ func (assert *ServiceTestSuite) TestCreateStoLossBuyOrderBuy() { // then: assert.NotNil(res) - assert.Equal(1, utils.GetCount("stop_order")) + assert.Equal(1, db.GetCount("stop_order")) assert.Equal(0, GetBuyBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal(1000.00, models.FindPaymentAccountByAppEntityIdAndCurrencyName(assert.appEntity1, "EUR").AmountReserved) @@ -43,7 +43,7 @@ func (assert *ServiceTestSuite) TestCreateStopLossSellOrderActivate() { ProcessTradeOrder(assert.tradingAccount2, "BTC_EUR", "LIMIT", models.Buy, 10, 1, "GTC") // then: the order becomes activated as a market order which is invisible to the order book - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(1, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -61,7 +61,7 @@ func (assert *ServiceTestSuite) TestCreateStopLosBuyOrderActivate() { assert.Equal(20.0, models.FindPaymentAccountByAppEntityIdAndCurrencyName(assert.appEntity1, "EUR").AmountReserved) // then: the order becomes activated as a market order which is invisible to the order book - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(1, GetBuyBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) @@ -76,7 +76,7 @@ func (assert *ServiceTestSuite) TestCreateStopLossSellOrderNonCrossing() { ProcessTradeOrder(assert.tradingAccount2, "BTC_EUR", "LIMIT", models.Buy, 11, 1, "GTC") // then: the order remains deactivated - assert.Equal(1, utils.GetCount("stop_order")) + assert.Equal(1, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -85,7 +85,7 @@ func (assert *ServiceTestSuite) TestCreateStopLossSellOrderNonCrossing() { ProcessTradeOrder(assert.tradingAccount2, "BTC_EUR", "LIMIT", models.Buy, 9, 1, "GTC") // then: it becomes activated - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(1, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) } @@ -98,7 +98,7 @@ func (assert *ServiceTestSuite) TestCreateStopLossBuyOrderNonCrossing() { ProcessTradeOrder(assert.tradingAccount1, "BTC_EUR", "LIMIT", models.Sell, 11, 1, "GTC") // then: the order remains deactivated - assert.Equal(1, utils.GetCount("stop_order")) + assert.Equal(1, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -107,7 +107,7 @@ func (assert *ServiceTestSuite) TestCreateStopLossBuyOrderNonCrossing() { ProcessTradeOrder(assert.tradingAccount1, "BTC_EUR", "LIMIT", models.Sell, 9, 1, "GTC") // then: it becomes activated - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(1, GetBuyBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) } @@ -120,7 +120,7 @@ func (assert *ServiceTestSuite) TestCreateStopLossSellOrderActivateAndSettle() { ProcessTradeOrder(assert.tradingAccount2, "BTC_EUR", "LIMIT", models.Buy, 10, 3, "GTC") // then: the order becomes activated and settled - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -134,7 +134,7 @@ func (assert *ServiceTestSuite) TestCreateStopLossBuyOrderActivateAndSettle() { ProcessTradeOrder(assert.tradingAccount2, "BTC_EUR", "LIMIT", models.Sell, 10, 3, "GTC") // then: the order becomes activated and settled - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -148,7 +148,7 @@ func (assert *ServiceTestSuite) TestCreateStopLossSellOrderActivateAndSettleOppo ProcessTradeOrder(assert.tradingAccount1, "BTC_EUR", "LIMIT", models.Sell, 10, 1, "GTC") // then: the order becomes activated and settled - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -162,7 +162,7 @@ func (assert *ServiceTestSuite) TestCreateStopLossBuyOrderActivateAndSettleOppos ProcessTradeOrder(assert.tradingAccount1, "BTC_EUR", "LIMIT", models.Buy, 10, 1, "GTC") // then: the order becomes activated and settled - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -178,7 +178,7 @@ func (assert *ServiceTestSuite) TestCreateStopLossSellOrderActivateAndSettleBefo // then: the order becomes activated and settled at last trade price which is 10 assert.Equal([]float64{10.0, 10.0, 11.0}, GetTradePrices()) - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -194,7 +194,7 @@ func (assert *ServiceTestSuite) TestCreateStopLossBuyOrderActivateAndSettleBefor // then: the order becomes activated and settled assert.Equal([]float64{10.0, 10.0, 9.0}, GetTradePrices()) - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -208,7 +208,7 @@ func (assert *ServiceTestSuite) TestCreateStopLossSellOrderActivateByMarketAndSe ProcessTradeOrder(assert.tradingAccount2, "BTC_EUR", "LIMIT", models.Buy, 10, 3, "GTC") // then: the order becomes activated and settled - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -219,7 +219,7 @@ func (assert *ServiceTestSuite) TestCreateStopLossSellOrderActivateByMarketAndSe ProcessTradeOrder(assert.tradingAccount1, "BTC_EUR", models.Market, models.Sell, 0, 1, "GTC") // then: the order becomes activated and settled - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -233,7 +233,7 @@ func (assert *ServiceTestSuite) TestCreateStopLossBuyOrderActivateByMarketAndSet ProcessTradeOrder(assert.tradingAccount2, "BTC_EUR", "LIMIT", models.Sell, 10, 3, "GTC") // then: the order becomes activated and settled - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) @@ -244,7 +244,7 @@ func (assert *ServiceTestSuite) TestCreateStopLossBuyOrderActivateByMarketAndSet ProcessTradeOrder(assert.tradingAccount1, "BTC_EUR", models.Market, models.Buy, 0, 10, "GTC") // then: the order becomes activated and settled - assert.Equal(0, utils.GetCount("stop_order")) + assert.Equal(0, db.GetCount("stop_order")) assert.Equal(0, GetSellBookOrderCount()) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Buy)) assert.Equal([]PriceVolume{}, GetVolumes("BTC_EUR", models.Sell)) diff --git a/pkg/services/payment_service_test.go b/pkg/services/payment_service_test.go index 80e5758..e31c2fc 100644 --- a/pkg/services/payment_service_test.go +++ b/pkg/services/payment_service_test.go @@ -1,13 +1,13 @@ package services import ( + "open-outcry/pkg/db" "open-outcry/pkg/models" - "open-outcry/pkg/utils" ) func (assert *ServiceTestSuite) TestDepositPayment() { // given a customer - utils.DeleteAll("payment") + db.DeleteAll("payment") appEntity1, _ := Acc("test3") // when amount is deposited @@ -18,7 +18,7 @@ func (assert *ServiceTestSuite) TestDepositPayment() { assert.Equal(1010.00, acc.Amount) assert.Equal(1010.00, acc.AmountAvailable) assert.Equal(0.00, acc.AmountReserved) - assert.Equal(3, utils.GetCount("payment")) + assert.Equal(3, db.GetCount("payment")) // when amount is deposited CreatePaymentDeposit(appEntity1, 10.00, "EUR", "BANK", "REF125") @@ -28,5 +28,5 @@ func (assert *ServiceTestSuite) TestDepositPayment() { assert.Equal(1020.00, acc.Amount) assert.Equal(1020.00, acc.AmountAvailable) assert.Equal(0.00, acc.AmountReserved) - assert.Equal(4, utils.GetCount("payment")) + assert.Equal(4, db.GetCount("payment")) } diff --git a/pkg/services/price_level_test.go b/pkg/services/price_level_test.go index 7a9406e..127101f 100644 --- a/pkg/services/price_level_test.go +++ b/pkg/services/price_level_test.go @@ -3,7 +3,6 @@ package services import ( "open-outcry/pkg/db" "open-outcry/pkg/models" - "open-outcry/pkg/utils" ) func (assert *ServiceTestSuite) TestCreatePriceLevel() { @@ -21,7 +20,7 @@ func (assert *ServiceTestSuite) TestCreatePriceLevel() { assert.Nil(err) // then a price level is created - assert.Equal(1, utils.GetCount("price_level")) + assert.Equal(1, db.GetCount("price_level")) assert.Equal(10.0, db.QueryVal[float64]("SELECT volume FROM price_level LIMIT 1")) // when give another order for smaller amount @@ -35,7 +34,7 @@ func (assert *ServiceTestSuite) TestCreatePriceLevel() { ) // then price level is updated - assert.Equal(1, utils.GetCount("price_level")) + assert.Equal(1, db.GetCount("price_level")) assert.Equal(15.0, db.QueryVal[float64]("SELECT volume FROM price_level LIMIT 1")) // when give another order for different price @@ -48,7 +47,7 @@ func (assert *ServiceTestSuite) TestCreatePriceLevel() { ) // then another price level is created - assert.Equal(2, utils.GetCount("price_level")) + assert.Equal(2, db.GetCount("price_level")) } func (assert *ServiceTestSuite) TestCancelWithSingle() { @@ -65,14 +64,14 @@ func (assert *ServiceTestSuite) TestCancelWithSingle() { ) // then a price level is created - assert.Equal(1, utils.GetCount("price_level")) + assert.Equal(1, db.GetCount("price_level")) assert.Equal(10.0, db.QueryVal[float64]("SELECT volume FROM price_level LIMIT 1")) // when the order is deleted CancelTradeOrder(id) // then price level is deleted also - assert.Equal(0, utils.GetCount("price_level")) + assert.Equal(0, db.GetCount("price_level")) } func (assert *ServiceTestSuite) TestCancelWithTwoOrdersOfSameSize() { @@ -99,14 +98,14 @@ func (assert *ServiceTestSuite) TestCancelWithTwoOrdersOfSameSize() { ) // then a price level is created - assert.Equal(1, utils.GetCount("price_level")) + assert.Equal(1, db.GetCount("price_level")) assert.Equal(20.0, db.QueryVal[float64]("SELECT volume FROM price_level LIMIT 1")) // when the order is deleted CancelTradeOrder(id) // then price level updated - assert.Equal(1, utils.GetCount("price_level")) + assert.Equal(1, db.GetCount("price_level")) assert.Equal(10.0, db.QueryVal[float64]("SELECT volume FROM price_level LIMIT 1")) } @@ -134,12 +133,12 @@ func (assert *ServiceTestSuite) TestCancelWithTwoOrdersWithDiffPrice() { ) // then a price level is created - assert.Equal(2, utils.GetCount("price_level")) + assert.Equal(2, db.GetCount("price_level")) // when the order is deleted CancelTradeOrder(id) // then price levels are updated - assert.Equal(1, utils.GetCount("price_level")) + assert.Equal(1, db.GetCount("price_level")) assert.Equal(10.0, db.QueryVal[float64]("SELECT volume FROM price_level LIMIT 1")) } diff --git a/pkg/services/registration_service_test.go b/pkg/services/registration_service_test.go index dc605cd..23b62b7 100644 --- a/pkg/services/registration_service_test.go +++ b/pkg/services/registration_service_test.go @@ -1,27 +1,27 @@ package services import ( + "open-outcry/pkg/db" "open-outcry/pkg/models" - "open-outcry/pkg/utils" "github.com/google/uuid" ) func (assert *ServiceTestSuite) TestCreateAppEntity() { // when - count := utils.GetCount("app_entity") + count := db.GetCount("app_entity") res := CreateAppEntity(models.AppEntityExternalId("test")) // then assert.NotNil(res) _, err := uuid.Parse(string(res)) assert.Nil(err) - assert.Equal(count+1, utils.GetCount("app_entity")) + assert.Equal(count+1, db.GetCount("app_entity")) // when res2 := CreateAppEntity(models.AppEntityExternalId("test")) // then assert.NotEqual(res, res2) - assert.Equal(count+2, utils.GetCount("app_entity")) + assert.Equal(count+2, db.GetCount("app_entity")) } diff --git a/pkg/services/service_suite_test.go b/pkg/services/service_suite_test.go index 2486f22..4b8b5b3 100644 --- a/pkg/services/service_suite_test.go +++ b/pkg/services/service_suite_test.go @@ -46,7 +46,7 @@ func (suite *ServiceTestSuite) TearDownTest() { "payment_account WHERE app_entity_id != 1", "trading_account", "app_entity WHERE pub_id != 'MASTER'", - }, utils.DeleteAll) + }, db.DeleteAll) } func (suite *ServiceTestSuite) TearDownAllSuite() { diff --git a/pkg/utils/db_test_utils.go b/pkg/utils/db_test_utils.go deleted file mode 100644 index 7f37753..0000000 --- a/pkg/utils/db_test_utils.go +++ /dev/null @@ -1,20 +0,0 @@ -package utils - -import ( - "context" - "log" - "open-outcry/pkg/db" -) - -func GetCount(tableName string) int { - var count int - db.Instance().QueryRow("SELECT COUNT(*) FROM " + tableName).Scan(&count) - return count -} - -func DeleteAll(tableName string) { - _, err := db.Instance().ExecContext(context.Background(), "DELETE FROM "+tableName) - if err != nil { - log.Fatal(err) - } -}