|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + snowflakeplugin "github.com/opencost/opencost-plugins/pkg/plugins/snowflake/plugin" |
| 9 | + "github.com/opencost/opencost/core/pkg/model/pb" |
| 10 | + "github.com/opencost/opencost/core/pkg/opencost" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "google.golang.org/protobuf/types/known/timestamppb" |
| 13 | + |
| 14 | + "github.com/DATA-DOG/go-sqlmock" // For mocking SQL database |
| 15 | +) |
| 16 | + |
| 17 | +// TestGetInvoices function |
| 18 | +func TestGetInvoices(t *testing.T) { |
| 19 | + // Create a mocked database connection |
| 20 | + db, mock, err := sqlmock.New() |
| 21 | + if err != nil { |
| 22 | + t.Fatalf("Error initializing sqlmock: %v", err) |
| 23 | + } |
| 24 | + defer db.Close() |
| 25 | + |
| 26 | + // Convert MockRows to *sql.Rows |
| 27 | + |
| 28 | + expectedQuery := `(?s)SELECT start_time, warehouse_name, credits_used_compute FROM snowflake\.account_usage\.warehouse_metering_history WHERE start_time >= DATEADD\(day, -m, CURRENT_TIMESTAMP\(\)\) AND warehouse_id > 0 -- Skip pseudo-VWs such as "CLOUD_SERVICES_ONLY" ORDER BY 1 DESC, 2;` |
| 29 | + |
| 30 | + rows := sqlmock.NewRows([]string{"Date", "WarehouseName", "Credits"}). |
| 31 | + AddRow("2025-01-23", "ANALYTICS_VW", 123.45). |
| 32 | + AddRow("2025-01-22", "LOADING_VW", 75) |
| 33 | + |
| 34 | + // Expect the query and provide the mock response |
| 35 | + mock.ExpectQuery(expectedQuery).WillReturnRows(rows) |
| 36 | + |
| 37 | + // Create the SnowflakeClient with the mocked db |
| 38 | + mockClient := &snowflakeClient{db: db} |
| 39 | + |
| 40 | + // Test the function |
| 41 | + invoices, err := GetInvoices(mockClient) |
| 42 | + |
| 43 | + // Assertions |
| 44 | + assert.NoError(t, err) |
| 45 | + assert.Len(t, invoices, 2) |
| 46 | + |
| 47 | + expectedInvoices := []snowflakeplugin.LineItem{ |
| 48 | + {WarehouseName: "ANALYTICS_VW", CreditUsed: 123.45, Date: "2025-01-23"}, |
| 49 | + {WarehouseName: "LOADING_VW", CreditUsed: 75, Date: "2025-01-22"}, |
| 50 | + } |
| 51 | + assert.Equal(t, expectedInvoices, invoices) |
| 52 | + |
| 53 | +} |
| 54 | + |
| 55 | +func TestExecuteQueryWithRowError(t *testing.T) { |
| 56 | + // Create a mocked database connection |
| 57 | + db, mock, err := sqlmock.New() |
| 58 | + if err != nil { |
| 59 | + t.Fatalf("Error initializing sqlmock: %v", err) |
| 60 | + } |
| 61 | + defer db.Close() |
| 62 | + |
| 63 | + // Define the query |
| 64 | + query := "SELECT start_time, warehouse_name, credits_used_compute FROM warehouse_metering_history" |
| 65 | + |
| 66 | + // Create mock rows with an error injected |
| 67 | + rows := sqlmock.NewRows([]string{"start_time", "warehouse_name", "credits_used_compute"}). |
| 68 | + AddRow("2025-01-28T16:00:00Z", "WH1", 123.45). |
| 69 | + AddRow("2025-01-28T15:00:00Z", "WH2", 678.90). |
| 70 | + RowError(1, errors.New("simulated row error")) // Inject error at row index 1 |
| 71 | + |
| 72 | + // Expect the query and provide the mock response |
| 73 | + mock.ExpectQuery(query).WillReturnRows(rows) |
| 74 | + |
| 75 | + // Create the SnowflakeClient with the mocked db |
| 76 | + client := &snowflakeClient{db: db} |
| 77 | + |
| 78 | + // Call ExecuteQuery |
| 79 | + result, err := client.ExecuteQuery(query) |
| 80 | + if err != nil { |
| 81 | + t.Fatalf("ExecuteQuery failed: %v", err) |
| 82 | + } |
| 83 | + defer result.Close() |
| 84 | + |
| 85 | + // Validate the results |
| 86 | + var ( |
| 87 | + startTime string |
| 88 | + warehouseName string |
| 89 | + credits float64 |
| 90 | + ) |
| 91 | + |
| 92 | + for result.Next() { |
| 93 | + if err := result.Scan(&startTime, &warehouseName, &credits); err != nil { |
| 94 | + t.Fatalf("Error scanning row: %v", err) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // Check for row errors |
| 99 | + if err := result.Err(); err == nil { |
| 100 | + t.Errorf("Expected an error from result.Err(), but got nil") |
| 101 | + } else if err.Error() != "simulated row error" { |
| 102 | + t.Errorf("Unexpected error message from result.Err(): %v", err) |
| 103 | + } |
| 104 | + |
| 105 | + // Verify that all expectations were met |
| 106 | + if err := mock.ExpectationsWereMet(); err != nil { |
| 107 | + t.Errorf("SQL expectations were not met: %v", err) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func TestGetCustomCosts(t *testing.T) { |
| 112 | + assert.Equal(t, 0, 0) |
| 113 | +} |
| 114 | + |
| 115 | +func TestFilterLineItemsByWindow(t *testing.T) { |
| 116 | + assert.Equal(t, 0, 0) |
| 117 | +} |
| 118 | + |
| 119 | +func TestGetSnowflakeCostsForWindow(t *testing.T) { |
| 120 | + |
| 121 | + // Create a mocked database connection |
| 122 | + db, _, err := sqlmock.New() |
| 123 | + if err != nil { |
| 124 | + t.Fatalf("Error initializing sqlmock: %v", err) |
| 125 | + } |
| 126 | + defer db.Close() |
| 127 | + |
| 128 | + // Create the SnowflakeClient with the mocked db |
| 129 | + mockClient := &snowflakeClient{db: db} |
| 130 | + // Create SnowflakeCostSource |
| 131 | + snowflakeCostSource := SnowflakeCostSource{ |
| 132 | + snowflakeClient: mockClient, |
| 133 | + } |
| 134 | + |
| 135 | + // Define the window |
| 136 | + startTime := time.Date(2025, 1, 20, 0, 0, 0, 0, time.UTC) |
| 137 | + endTime := time.Date(2025, 1, 25, 0, 0, 0, 0, time.UTC) |
| 138 | + window := opencost.NewWindow(&startTime, &endTime) |
| 139 | + |
| 140 | + // Fetch line items |
| 141 | + lineItems := []snowflakeplugin.LineItem{ |
| 142 | + {WarehouseName: "ANALYTICS_VW", CreditUsed: 123.45, Date: "2025-01-23"}, |
| 143 | + {WarehouseName: "LOADING_VW", CreditUsed: 75, Date: "2025-01-22"}, |
| 144 | + } |
| 145 | + |
| 146 | + // Call GetSnowflakeCostsForWindow |
| 147 | + result := snowflakeCostSource.GetSnowflakeCostsForWindow(&window, lineItems) |
| 148 | + |
| 149 | + // Assertions |
| 150 | + assert.NotNil(t, result) |
| 151 | + assert.Equal(t, "data_storage", result.CostSource) |
| 152 | + assert.Equal(t, "snowflake", result.Domain) |
| 153 | + assert.Equal(t, "v1", result.Version) |
| 154 | + assert.Equal(t, "USD", result.Currency) |
| 155 | + assert.Equal(t, timestamppb.New(startTime), result.Start) |
| 156 | + assert.Equal(t, timestamppb.New(endTime), result.End) |
| 157 | + assert.Empty(t, result.Errors) |
| 158 | + assert.Len(t, result.Costs, 2) |
| 159 | + |
| 160 | + expectedCosts := []*pb.CustomCost{ |
| 161 | + {UsageQuantity: 123.45, ResourceName: "ANALYTICS_VW"}, |
| 162 | + {UsageQuantity: 75, ResourceName: "LOADING_VW"}, |
| 163 | + } |
| 164 | + assert.Equal(t, expectedCosts, result.Costs) |
| 165 | + |
| 166 | +} |
0 commit comments