@@ -66,6 +66,100 @@ func (suite *TestSuiteEnv) TestGetTransactions() {
6666 assert .Len (suite .T (), response .Data , 2 )
6767}
6868
69+ func (suite * TestSuiteEnv ) TestGetTransactionsInvalidQuery () {
70+ tests := []string {
71+ "budget=DefinitelyACat" ,
72+ "source=MaybeADog" ,
73+ "destination=OrARat?" ,
74+ "envelope=NopeDefinitelyAMole" ,
75+ "date=A long time ago" ,
76+ "amount=Seventeen Cents" ,
77+ "reconciled=I don't think so" ,
78+ }
79+
80+ for _ , tt := range tests {
81+ suite .T ().Run (tt , func (t * testing.T ) {
82+ recorder := test .Request (suite .T (), http .MethodGet , fmt .Sprintf ("http://example.com/v1/transactions?%s" , tt ), "" )
83+ test .AssertHTTPStatus (suite .T (), http .StatusBadRequest , & recorder )
84+ })
85+ }
86+ }
87+
88+ func (suite * TestSuiteEnv ) TestGetTransactionsFilter () {
89+ b := createTestBudget (suite .T (), models.BudgetCreate {})
90+
91+ a1 := createTestAccount (suite .T (), models.AccountCreate {BudgetID : b .Data .ID })
92+ a2 := createTestAccount (suite .T (), models.AccountCreate {BudgetID : b .Data .ID })
93+
94+ c := createTestCategory (suite .T (), models.CategoryCreate {BudgetID : b .Data .ID })
95+
96+ e1 := createTestEnvelope (suite .T (), models.EnvelopeCreate {CategoryID : c .Data .ID })
97+ e2 := createTestEnvelope (suite .T (), models.EnvelopeCreate {CategoryID : c .Data .ID })
98+
99+ e1ID := & e1 .Data .ID
100+ e2ID := & e2 .Data .ID
101+
102+ _ = createTestTransaction (suite .T (), models.TransactionCreate {
103+ Date : time .Date (2018 , 9 , 5 , 17 , 13 , 29 , 45256 , time .UTC ),
104+ Amount : decimal .NewFromFloat (2.718 ),
105+ Note : "This was an important expense" ,
106+ BudgetID : b .Data .ID ,
107+ EnvelopeID : e1ID ,
108+ SourceAccountID : a1 .Data .ID ,
109+ DestinationAccountID : a2 .Data .ID ,
110+ Reconciled : false ,
111+ })
112+
113+ _ = createTestTransaction (suite .T (), models.TransactionCreate {
114+ Date : time .Date (2016 , 5 , 1 , 14 , 13 , 25 , 584575 , time .UTC ),
115+ Amount : decimal .NewFromFloat (11235.813 ),
116+ Note : "Not important" ,
117+ BudgetID : b .Data .ID ,
118+ EnvelopeID : e2ID ,
119+ SourceAccountID : a2 .Data .ID ,
120+ DestinationAccountID : a1 .Data .ID ,
121+ Reconciled : false ,
122+ })
123+
124+ _ = createTestTransaction (suite .T (), models.TransactionCreate {
125+ Date : time .Date (2021 , 2 , 6 , 5 , 1 , 0 , 585 , time .UTC ),
126+ Amount : decimal .NewFromFloat (2.718 ),
127+ Note : "" ,
128+ BudgetID : b .Data .ID ,
129+ EnvelopeID : e1ID ,
130+ SourceAccountID : a1 .Data .ID ,
131+ DestinationAccountID : a2 .Data .ID ,
132+ Reconciled : true ,
133+ })
134+
135+ tests := []struct {
136+ name string
137+ query string
138+ len int
139+ }{
140+ {"Exact Date" , fmt .Sprintf ("date=%s" , time .Date (2021 , 2 , 6 , 5 , 1 , 0 , 585 , time .UTC ).Format (time .RFC3339Nano )), 1 },
141+ {"Exact Amount" , fmt .Sprintf ("amount=%s" , decimal .NewFromFloat (2.718 ).String ()), 2 },
142+ {"Note" , "note=Not important" , 1 },
143+ {"No note" , "note=" , 1 },
144+ {"Budget Match" , fmt .Sprintf ("budget=%s" , b .Data .ID ), 3 },
145+ {"Envelope 2" , fmt .Sprintf ("envelope=%s" , e2 .Data .ID ), 1 },
146+ {"Non-existing Source Account" , "source=3340a084-acf8-4cb4-8f86-9e7f88a86190" , 0 },
147+ {"Destination Account" , fmt .Sprintf ("destination=%s" , a2 .Data .ID ), 2 },
148+ {"Reconciled" , "reconciled=false" , 2 },
149+ }
150+
151+ for _ , tt := range tests {
152+ suite .T ().Run (tt .name , func (t * testing.T ) {
153+ var re controllers.TransactionListResponse
154+ r := test .Request (t , http .MethodGet , fmt .Sprintf ("/v1/transactions?%s" , tt .query ), "" )
155+ test .AssertHTTPStatus (t , http .StatusOK , & r )
156+ test .DecodeResponse (t , & r , & re )
157+
158+ assert .Equal (t , tt .len , len (re .Data ), "Request ID: %s" , r .Result ().Header .Get ("x-request-id" ))
159+ })
160+ }
161+ }
162+
69163func (suite * TestSuiteEnv ) TestNoTransactionNotFound () {
70164 recorder := test .Request (suite .T (), http .MethodGet , "http://example.com/v1/transactions/048b061f-3b6b-45ab-b0e9-0f38d2fff0c8" , "" )
71165
@@ -121,7 +215,9 @@ func (suite *TestSuiteEnv) TestTransactionSorting() {
121215 var transactions controllers.TransactionListResponse
122216 test .DecodeResponse (suite .T (), & r , & transactions )
123217
124- assert .Len (suite .T (), transactions .Data , 3 , "There are not exactly three transactions" )
218+ if ! assert .Len (suite .T (), transactions .Data , 3 , "There are not exactly three transactions" ) {
219+ assert .FailNow (suite .T (), "Number of transactions is wrong, aborting" )
220+ }
125221 assert .Equal (suite .T (), tMarch .Data .Date , transactions .Data [0 ].Date , "The first transaction is not the March transaction" )
126222 assert .Equal (suite .T (), tFebrurary .Data .Date , transactions .Data [1 ].Date , "The second transaction is not the February transaction" )
127223 assert .Equal (suite .T (), tJanuary .Data .Date , transactions .Data [2 ].Date , "The third transaction is not the January transaction" )
0 commit comments