@@ -148,6 +148,90 @@ defmodule KiteConnectExTest do
148
148
"""
149
149
end
150
150
151
+ describe "funds_and_margins/2" do
152
+ test "returns funds and margins for equity with valid response" , % { bypass: bypass } do
153
+ Bypass . expect_once ( bypass , "GET" , "/user/margins/equity" , fn conn ->
154
+ Plug.Conn . resp ( conn , 201 , funds_and_margins_response ( ) )
155
+ end )
156
+
157
+ assert KiteConnectEx . funds_and_margins ( "access-token" , "equity" ) ==
158
+ { :ok ,
159
+ % KiteConnectEx.User.FundAndMargin {
160
+ available: % KiteConnectEx.User.FundAndMargin.AvailableSegment {
161
+ adhoc_margin: 0 ,
162
+ cash: 622.32 ,
163
+ collateral: 0 ,
164
+ intraday_payin: 0
165
+ } ,
166
+ enabled: true ,
167
+ net: 622.32 ,
168
+ utilised: % KiteConnectEx.User.FundAndMargin.UtilisedSegment {
169
+ debits: 0 ,
170
+ exposure: 0 ,
171
+ holding_sales: 0 ,
172
+ m2m_realised: 0 ,
173
+ m2m_unrealised: 0 ,
174
+ option_premium: 0 ,
175
+ payout: 0 ,
176
+ span: 0 ,
177
+ turnover: 0
178
+ }
179
+ } }
180
+ end
181
+
182
+ test "returns error if API request fails" , % { bypass: bypass } do
183
+ Bypass . expect_once ( bypass , "GET" , "/user/margins/equity" , fn conn ->
184
+ Plug.Conn . resp (
185
+ conn ,
186
+ 403 ,
187
+ ~s< {"status": "error", "error_type": "TokenException", "message": "Invalid access_token"}>
188
+ )
189
+ end )
190
+
191
+ assert KiteConnectEx . funds_and_margins ( "access-token" , "equity" ) ==
192
+ { :error ,
193
+ % KiteConnectEx.Error {
194
+ code: 403 ,
195
+ error_type: "TokenException" ,
196
+ message: "Invalid access_token"
197
+ } }
198
+ end
199
+
200
+ defp funds_and_margins_response do
201
+ """
202
+ {
203
+ "status": "success",
204
+ "data": {
205
+ "available": {
206
+ "adhoc_margin": 0,
207
+ "cash": 622.32,
208
+ "collateral": 0,
209
+ "intraday_payin": 0,
210
+ "live_balance": 622.32,
211
+ "opening_balance": 622.32
212
+ },
213
+ "enabled": true,
214
+ "net": 622.32,
215
+ "utilised": {
216
+ "debits": 0,
217
+ "delivery": 0,
218
+ "exposure": 0,
219
+ "holding_sales": 0,
220
+ "liquid_collateral": 0,
221
+ "m2m_realised": 0,
222
+ "m2m_unrealised": 0,
223
+ "option_premium": 0,
224
+ "payout": 0,
225
+ "span": 0,
226
+ "stock_collateral": 0,
227
+ "turnover": 0
228
+ }
229
+ }
230
+ }
231
+ """
232
+ end
233
+ end
234
+
151
235
describe "holdings/1" do
152
236
test "returns portfolio holdings with valid access_token" , % { bypass: bypass } do
153
237
Bypass . expect_once ( bypass , "GET" , "/portfolio/holdings" , fn conn ->
0 commit comments