@@ -155,25 +155,25 @@ func (suite *TestSuiteStandard) TestYnabImportPreviewDuplicateDetection() {
155155 account := suite .createTestAccount (models.AccountCreate {})
156156
157157 // Get the import hash of the first transaction and create one with the same import hash
158- preview := parseComdirectTestCSV (suite , account .Data .ID )
158+ preview := parseCSV (suite , account .Data .ID , "comdirect-ynap.csv" )
159159
160160 transaction := suite .createTestTransaction (models.TransactionCreate {
161161 SourceAccountID : account .Data .ID ,
162162 ImportHash : preview .Data [0 ].Transaction .ImportHash ,
163163 Amount : decimal .NewFromFloat (1.13 ),
164164 })
165165
166- preview = parseComdirectTestCSV (suite , account .Data .ID )
166+ preview = parseCSV (suite , account .Data .ID , "comdirect-ynap.csv" )
167167
168168 suite .Assert ().Len (preview .Data [0 ].DuplicateTransactionIDs , 1 , "Duplicate transaction IDs field does not have the correct number of IDs" )
169169 suite .Assert ().Equal (transaction .Data .ID , preview .Data [0 ].DuplicateTransactionIDs [0 ], "Duplicate transaction ID is not ID of the transaction that is duplicated" )
170170}
171171
172- func parseComdirectTestCSV (suite * TestSuiteStandard , accountID uuid.UUID ) controllers.ImportPreviewList {
172+ func parseCSV (suite * TestSuiteStandard , accountID uuid.UUID , file string ) controllers.ImportPreviewList {
173173 path := fmt .Sprintf ("ynab-import-preview?accountId=%s" , accountID .String ())
174174
175175 // Parse the test CSV
176- body , headers := suite .loadTestFile ("importer/ynab-import/comdirect-ynap.csv" )
176+ body , headers := suite .loadTestFile (fmt . Sprintf ( "importer/ynab-import/%s" , file ) )
177177 recorder := test .Request (suite .controller , suite .T (), http .MethodPost , fmt .Sprintf ("http://example.com/v1/import/%s" , path ), body , headers )
178178 suite .Assert ().Equal (http .StatusOK , recorder .Code , "Request ID %s, response %s" , recorder .Header ().Get ("x-request-id" ), recorder .Body .String ())
179179
@@ -183,3 +183,55 @@ func parseComdirectTestCSV(suite *TestSuiteStandard, accountID uuid.UUID) contro
183183
184184 return response
185185}
186+
187+ func (suite * TestSuiteStandard ) TestYnabImportFindAccounts () {
188+ // Create a budget and two existing accounts to use
189+ budget := suite .createTestBudget (models.BudgetCreate {})
190+ edeka := suite .createTestAccount (models.AccountCreate {BudgetID : budget .Data .ID , Name : "Edeka" , External : true })
191+
192+ // Create an archived account named "Edeka" to ensure it is not found. If it were found, the tests for the non-archived
193+ // account being found would fail since we do not use an account if we find more than one with the same name
194+ _ = suite .createTestAccount (models.AccountCreate {BudgetID : budget .Data .ID , Name : "Edeka" , Hidden : true })
195+
196+ // Create an account named "Edeka" in another budget to ensure it is not found. If it were found, the tests for the non-archived
197+ // Edeka account being found would fail since we do not use an account if we find more than one with the same name
198+ _ = suite .createTestAccount (models.AccountCreate {Name : "Edeka" })
199+
200+ // Account we import to
201+ internalAccount := suite .createTestAccount (models.AccountCreate {BudgetID : budget .Data .ID , Name : "Envelope Zero Account" })
202+
203+ tests := []struct {
204+ name string // Name of the test
205+ sourceAccountIDs []uuid.UUID // The IDs of the source accounts
206+ sourceAccountNames []string // The sourceAccountName attribute after the find has been performed
207+ destinationAccountIDs []uuid.UUID // The IDs of the destination accounts
208+ destinationAccountNames []string // The destinationAccountName attribute after the find has been performed
209+ preTest func () // Function to execute before running tests
210+ }{
211+ {"No matching (Some Company) & 1 Matching (Edeka) accounts" , []uuid.UUID {internalAccount .Data .ID , internalAccount .Data .ID , uuid .Nil }, []string {"" , "" , "Some Company" }, []uuid.UUID {edeka .Data .ID , uuid .Nil , internalAccount .Data .ID }, []string {"" , "Deutsche Bahn" , "" }, func () {}},
212+ {"Two matching non-archived accounts" , []uuid.UUID {internalAccount .Data .ID , internalAccount .Data .ID , uuid .Nil }, []string {"" , "" , "Some Company" }, []uuid.UUID {uuid .Nil , uuid .Nil , internalAccount .Data .ID }, []string {"Edeka" , "Deutsche Bahn" , "" }, func () {
213+ _ = suite .createTestAccount (models.AccountCreate {BudgetID : budget .Data .ID , Name : "Edeka" })
214+ }},
215+ }
216+
217+ for _ , tt := range tests {
218+ suite .T ().Run (tt .name , func (t * testing.T ) {
219+ tt .preTest ()
220+ preview := parseCSV (suite , internalAccount .Data .ID , "account-find-test.csv" )
221+
222+ for i , transaction := range preview .Data {
223+ line := i + 1
224+ assert .Equal (t , tt .sourceAccountNames [i ], transaction .SourceAccountName , "sourceAccountName does not match in line %d" , line )
225+ assert .Equal (t , tt .destinationAccountNames [i ], transaction .DestinationAccountName , "destinationAccountName does not match in line %d" , line )
226+
227+ if tt .sourceAccountIDs [i ] != uuid .Nil {
228+ assert .Equal (t , tt .sourceAccountIDs [i ], transaction .Transaction .SourceAccountID , "sourceAccountID does not match in line %d" , line )
229+ }
230+
231+ if tt .destinationAccountIDs [i ] != uuid .Nil {
232+ assert .Equal (t , tt .destinationAccountIDs [i ], transaction .Transaction .DestinationAccountID , "destinationAccountID does not match in line %d" , line )
233+ }
234+ }
235+ })
236+ }
237+ }
0 commit comments