package db import ( "database/sql" "time" ) // Now is a variable that holds the time.Now…
- User
package db import ( "database/sql" "time" ) // Now is a variable that holds the time.Now function. // This allows us to replace it in tests to control the "current" time. var Now = time.Now // CategorySummary holds the aggregated total for a single category. type CategorySummary struct { Category string Total float64 } // GetMonthlyExpenseReport calculates and returns the total expenses for each // category within the current calendar month. // // It returns a non-nil, empty slice if no expenses are found for the month. // An error is returned if the database query fails. func GetMonthlyExpenseReport(db *sql.DB) ([]CategorySummary, error) { return nil, nil } package db_test import ( "slices" "testing" "time" "github.com/google/go-cmp/cmp" "github.com/slinden210489/pft-cli/internal/db" "github.com/slinden210489/pft-cli/internal/transaction" ) func TestGetMonthlyExpenseReport(t *testing.T) { // For stable tests, we define a fixed "current" time. // We'll use September 2025. now := time.Date(2025, time.September, 15, 10, 0, 0, 0, time.UTC) startOfMonth := time.Date(2025, time.September, 1, 0, 0, 0, 0, time.UTC) endOfMonth := startOfMonth.AddDate(0, 1, -1) lastMonth := startOfMonth.AddDate(0, -1, 0) // Define test cases tests := []struct { name string seedTxs []transaction.Transaction // Transactions to insert into the DB wantReport []db.CategorySummary // Expected result from the function mockTimeFunc func() time.Time // A function to mock time.Now() }{ { name: "with mixed transactions in and out of month", seedTxs: []transaction.Transaction{ // === Relevant Expenses (Current Month) === {Amount: 100.00, Type: transaction.TypeExpense, Category: "Groceries", Date: startOfMonth.Add(2 * 24 * time.Hour)}, {Amount: 25.50, Type: transaction.TypeExpense, Category: "Groceries", Date: startOfMonth.Add(5 * 24 * time.Hour)}, // Sum for Groceries: 125.50 {Amount: 75.00, Type: transaction.TypeExpense, Category: "Software", Date: startOfMonth.Add(10 * 24 * time.Hour)}, // Sum for Software: 75.00 // === Irrelevant Transactions (should be ignored) === {Amount: 2500.00, Type: transaction.TypeIncome, Category: "Salary", Date: startOfMonth.Add(1 * 24 * time.Hour)}, // Wrong type {Amount: 50.00, Type: transaction.TypeExpense, Category: "Groceries", Date: lastMonth}, // Wrong month (past) {Amount: 10.00, Type: transaction.TypeExpense, Category: "Software", Date: endOfMonth.AddDate(0, 1, 0)}, // Wrong month (future) }, // The expected result MUST be pre-sorted by Total descending. wantReport: []db.CategorySummary{ {Category: "Groceries", Total: 125.50}, {Category: "Software", Total: 75.00}, }, mockTimeFunc: func() time.Time { return now }, }, { name: "with no expenses in current month", seedTxs: []transaction.Transaction{ {Amount: 2500.00, Type: transaction.TypeIncome, Category: "Salary", Date: startOfMonth}, {Amount: 50.00, Type: transaction.TypeExpense, Category: "Groceries", Date: lastMonth}, }, wantReport: []db.CategorySummary{}, // Expect a non-nil, empty slice mockTimeFunc: func() time.Time { return now }, }, { name: "with no transactions at all", seedTxs: []transaction.Transaction{}, wantReport: []db.CategorySummary{}, // Expect a non-nil, empty slice mockTimeFunc: func() time.Time { return now }, }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { t.Parallel() dbConn := initTestDB(t) defer dbConn.Close() // Replace the time.Now function for this test run. // This is an advanced technique called "monkey patching". db.Now = tc.mockTimeFunc // Restore the original function at the end of the test. defer func() { db.Now = time.Now }() // Seed the database with the initial set of transactions. for _, tx := range tc.seedTxs { if _, err := db.AddTransaction(dbConn, tx); err != nil { t.Fatalf("Failed to add setup transaction: %v", err) } } // Execute the function under test. gotReport, err := db.GetMonthlyExpenseReport(dbConn) if err != nil { t.Fatalf("GetMonthlyExpenseReport() returned an unexpected error: %v", err) } // The function is expected to sort the results. We must sort our want slice // the same way before comparing to be safe, although it's pre-sorted. slices.SortFunc(tc.wantReport, func(a, b db.CategorySummary) int { // Descending order if a.Total > b.Total { return -1 } if a.Total < b.Total { return 1 } return 0 }) // Compare the actual result with the expected result. if diff := cmp.Diff(tc.wantReport, gotReport); diff != "" { t.Errorf("GetMonthlyExpenseReport() mismatch (-want +got):\n%s", diff) } }) } }
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.