@@ -304,3 +304,99 @@ func (suite *TestSuiteStandard) TestAccountDuplicateNames() {
304304
305305 suite .Assert ().Contains (err .Error (), "UNIQUE constraint failed: accounts.name, accounts.budget_id" , "Error message for account creation fail does not match expected message" )
306306}
307+
308+ func (suite * TestSuiteStandard ) TestAccountOnBudgetToOnBudgetTransactionsNoEnvelopes () {
309+ budget := suite .createTestBudget (models.BudgetCreate {
310+ Name : "TestAccountOnBudgetToOnBudgetTransactionsNoEnvelopes" ,
311+ })
312+
313+ account := suite .createTestAccount (models.AccountCreate {
314+ BudgetID : budget .ID ,
315+ OnBudget : true ,
316+ External : false ,
317+ Name : "TestAccountOnBudgetToOnBudgetTransactionsNoEnvelopes" ,
318+ })
319+
320+ transferTargetAccount := suite .createTestAccount (models.AccountCreate {
321+ BudgetID : budget .ID ,
322+ OnBudget : false ,
323+ External : false ,
324+ Name : "TestAccountOnBudgetToOnBudgetTransactionsNoEnvelopes:Target" ,
325+ })
326+
327+ category := suite .createTestCategory (models.CategoryCreate {
328+ BudgetID : budget .ID ,
329+ })
330+
331+ envelope := suite .createTestEnvelope (models.EnvelopeCreate {
332+ CategoryID : category .ID ,
333+ })
334+
335+ t := suite .createTestTransaction (models.TransactionCreate {
336+ Amount : decimal .NewFromFloat (17.23 ),
337+ BudgetID : budget .ID ,
338+ SourceAccountID : account .ID ,
339+ DestinationAccountID : transferTargetAccount .ID ,
340+ EnvelopeID : & envelope .ID ,
341+ })
342+
343+ // Try saving the account, which must fail
344+ data := models.Account {AccountCreate : models.AccountCreate {OnBudget : true }}
345+ err := suite .db .Model (& transferTargetAccount ).Select ("OnBudget" ).Updates (data ).Error
346+
347+ if ! assert .NotNil (suite .T (), err , "Target account could be updated to be on budget while having transactions with envelopes being set" ) {
348+ assert .FailNow (suite .T (), "Exiting because assertion was not met" )
349+ }
350+ assert .Contains (suite .T (), err .Error (), "the account cannot be set to on budget because the following transactions have an envelope set: " )
351+
352+ // Update the envelope for the transaction
353+ t .EnvelopeID = nil
354+ err = suite .db .Save (& t ).Error
355+ assert .Nil (suite .T (), err , "Transaction could not be updated" )
356+
357+ // Save again
358+ err = suite .db .Save (& transferTargetAccount ).Error
359+ assert .Nil (suite .T (), err , "Target account could not be updated despite transaction having its envelope removed" )
360+ }
361+
362+ func (suite * TestSuiteStandard ) TestAccountOffBudgetToOnBudgetTransactionsNoEnvelopes () {
363+ budget := suite .createTestBudget (models.BudgetCreate {
364+ Name : "TestAccountOffBudgetToOnBudgetTransactionsNoEnvelopes" ,
365+ })
366+
367+ account := suite .createTestAccount (models.AccountCreate {
368+ BudgetID : budget .ID ,
369+ OnBudget : false ,
370+ External : false ,
371+ Name : "TestAccountOffBudgetToOnBudgetTransactionsNoEnvelopes" ,
372+ })
373+
374+ transferTargetAccount := suite .createTestAccount (models.AccountCreate {
375+ BudgetID : budget .ID ,
376+ OnBudget : false ,
377+ External : false ,
378+ Name : "TestAccountOffBudgetToOnBudgetTransactionsNoEnvelopes:Target" ,
379+ })
380+
381+ category := suite .createTestCategory (models.CategoryCreate {
382+ BudgetID : budget .ID ,
383+ })
384+
385+ envelope := suite .createTestEnvelope (models.EnvelopeCreate {
386+ CategoryID : category .ID ,
387+ })
388+
389+ _ = suite .createTestTransaction (models.TransactionCreate {
390+ Amount : decimal .NewFromFloat (17.23 ),
391+ BudgetID : budget .ID ,
392+ SourceAccountID : account .ID ,
393+ DestinationAccountID : transferTargetAccount .ID ,
394+ EnvelopeID : & envelope .ID ,
395+ })
396+
397+ // Try saving the account, which must work
398+ data := models.Account {AccountCreate : models.AccountCreate {OnBudget : true }}
399+ err := suite .db .Model (& transferTargetAccount ).Select ("OnBudget" ).Updates (data ).Error
400+
401+ assert .Nil (suite .T (), err , "Target account could not be updated to be on budget, but it does not have transactions with envelopes being set" )
402+ }
0 commit comments