@@ -61,3 +61,74 @@ func TestAveragePrice(t *testing.T) {
6161 assert .Nil (t , price )
6262 })
6363}
64+
65+ func TestMedianPrice (t * testing.T ) {
66+ t .Run ("BTCUSDT median price" , func (t * testing.T ) {
67+ memory := storage .NewMemory [connector.TickerUpdate ]()
68+ api := & API {
69+ store : memory ,
70+ }
71+
72+ api .store .Store ("binance:BTCUSDT" , connector.TickerUpdate {
73+ Price : big .NewFloat (10000 ),
74+ })
75+ api .store .Store ("okx:BTCUSDT" , connector.TickerUpdate {
76+ Price : big .NewFloat (10010 ),
77+ })
78+ api .store .Store ("bybit:BTCUSDT" , connector.TickerUpdate {
79+ Price : big .NewFloat (10020 ),
80+ })
81+
82+ price , err := api .medianPrice ("BTCUSDT" , []string {"binance" , "okx" , "bybit" })
83+ assert .NoError (t , err )
84+ assert .Equal (t , big .NewFloat (10010 ), price )
85+ })
86+
87+ t .Run ("BTCUSDT median price with even number of exchanges" , func (t * testing.T ) {
88+ memory := storage .NewMemory [connector.TickerUpdate ]()
89+ api := & API {
90+ store : memory ,
91+ }
92+
93+ api .store .Store ("binance:BTCUSDT" , connector.TickerUpdate {
94+ Price : big .NewFloat (10000 ),
95+ })
96+ api .store .Store ("okx:BTCUSDT" , connector.TickerUpdate {
97+ Price : big .NewFloat (10010 ),
98+ })
99+
100+ price , err := api .medianPrice ("BTCUSDT" , []string {"binance" , "okx" })
101+ assert .NoError (t , err )
102+ assert .Equal (t , big .NewFloat (10005 ), price )
103+ })
104+
105+ t .Run ("Exchange not found" , func (t * testing.T ) {
106+ memory := storage .NewMemory [connector.TickerUpdate ]()
107+ api := & API {
108+ store : memory ,
109+ }
110+
111+ api .store .Store ("binance:BTCUSDT" , connector.TickerUpdate {
112+ Price : big .NewFloat (10000 ),
113+ })
114+
115+ price , err := api .medianPrice ("BTCUSDT" , []string {"binance" , "okx" })
116+ assert .Error (t , err )
117+ assert .Nil (t , price )
118+ })
119+
120+ t .Run ("Symbol not found" , func (t * testing.T ) {
121+ memory := storage .NewMemory [connector.TickerUpdate ]()
122+ api := & API {
123+ store : memory ,
124+ }
125+
126+ api .store .Store ("binance:BTCUSDT" , connector.TickerUpdate {
127+ Price : big .NewFloat (10000 ),
128+ })
129+
130+ price , err := api .medianPrice ("ETHUSDT" , []string {"binance" })
131+ assert .Error (t , err )
132+ assert .Nil (t , price )
133+ })
134+ }
0 commit comments