|
| 1 | +package event_processors |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/DATA-DOG/go-sqlmock" |
| 8 | + "github.com/getlago/lago/events-processor/models" |
| 9 | + "github.com/getlago/lago/events-processor/tests" |
| 10 | + "github.com/getlago/lago/events-processor/utils" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "gorm.io/gorm" |
| 13 | +) |
| 14 | + |
| 15 | +var processor *EventEnrichmentService |
| 16 | + |
| 17 | +func setupTestEnv(t *testing.T) (sqlmock.Sqlmock, func()) { |
| 18 | + db, mock, delete := tests.SetupMockStore(t) |
| 19 | + apiStore := models.NewApiStore(db) |
| 20 | + |
| 21 | + processor = &EventEnrichmentService{ |
| 22 | + apiStore: apiStore, |
| 23 | + } |
| 24 | + |
| 25 | + return mock, delete |
| 26 | +} |
| 27 | + |
| 28 | +func mockBmLookup(sqlmock sqlmock.Sqlmock, bm *models.BillableMetric) { |
| 29 | + columns := []string{"id", "organization_id", "code", "aggregation_type", "field_name", "expression", "created_at", "updated_at", "deleted_at"} |
| 30 | + |
| 31 | + rows := sqlmock.NewRows(columns). |
| 32 | + AddRow(bm.ID, bm.OrganizationID, bm.Code, bm.AggregationType, bm.FieldName, bm.Expression, bm.CreatedAt, bm.UpdatedAt, bm.DeletedAt) |
| 33 | + |
| 34 | + sqlmock.ExpectQuery("SELECT \\* FROM \"billable_metrics\".*").WillReturnRows(rows) |
| 35 | +} |
| 36 | + |
| 37 | +func mockSubscriptionLookup(sqlmock sqlmock.Sqlmock, sub *models.Subscription) { |
| 38 | + columns := []string{"id", "external_id", "plan_id", "created_at", "updated_at", "terminated_at"} |
| 39 | + |
| 40 | + rows := sqlmock.NewRows(columns). |
| 41 | + AddRow(sub.ID, sub.ExternalID, sub.PlanID, sub.CreatedAt, sub.UpdatedAt, sub.TerminatedAt) |
| 42 | + |
| 43 | + sqlmock.ExpectQuery(".* FROM \"subscriptions\".*").WillReturnRows(rows) |
| 44 | +} |
| 45 | + |
| 46 | +func TestEnrichEvent(t *testing.T) { |
| 47 | + t.Run("Without Billable Metric", func(t *testing.T) { |
| 48 | + sqlmock, delete := setupTestEnv(t) |
| 49 | + defer delete() |
| 50 | + |
| 51 | + event := models.Event{ |
| 52 | + OrganizationID: "1a901a90-1a90-1a90-1a90-1a901a901a90", |
| 53 | + ExternalSubscriptionID: "sub_id", |
| 54 | + Code: "api_calls", |
| 55 | + Timestamp: 1741007009, |
| 56 | + } |
| 57 | + |
| 58 | + sqlmock.ExpectQuery(".*").WillReturnError(gorm.ErrRecordNotFound) |
| 59 | + |
| 60 | + result := processor.EnrichEvent(&event) |
| 61 | + assert.False(t, result.Success()) |
| 62 | + assert.Equal(t, "record not found", result.ErrorMsg()) |
| 63 | + assert.Equal(t, "fetch_billable_metric", result.ErrorCode()) |
| 64 | + assert.Equal(t, "Error fetching billable metric", result.ErrorMessage()) |
| 65 | + }) |
| 66 | + |
| 67 | + t.Run("When event source is post processed on API and the result is successful", func(t *testing.T) { |
| 68 | + sqlmock, delete := setupTestEnv(t) |
| 69 | + defer delete() |
| 70 | + |
| 71 | + properties := map[string]any{ |
| 72 | + "api_requests": "12.0", |
| 73 | + } |
| 74 | + |
| 75 | + event := models.Event{ |
| 76 | + OrganizationID: "1a901a90-1a90-1a90-1a90-1a901a901a90", |
| 77 | + ExternalSubscriptionID: "sub_id", |
| 78 | + Code: "api_calls", |
| 79 | + Timestamp: 1741007009, |
| 80 | + Source: models.HTTP_RUBY, |
| 81 | + Properties: properties, |
| 82 | + SourceMetadata: &models.SourceMetadata{ |
| 83 | + ApiPostProcess: true, |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + bm := models.BillableMetric{ |
| 88 | + ID: "bm123", |
| 89 | + OrganizationID: event.OrganizationID, |
| 90 | + Code: event.Code, |
| 91 | + AggregationType: models.AggregationTypeSum, |
| 92 | + FieldName: "api_requests", |
| 93 | + Expression: "", |
| 94 | + CreatedAt: time.Now(), |
| 95 | + UpdatedAt: time.Now(), |
| 96 | + } |
| 97 | + mockBmLookup(sqlmock, &bm) |
| 98 | + |
| 99 | + sub := models.Subscription{ID: "sub123", PlanID: "plan123"} |
| 100 | + mockSubscriptionLookup(sqlmock, &sub) |
| 101 | + |
| 102 | + result := processor.EnrichEvent(&event) |
| 103 | + |
| 104 | + assert.True(t, result.Success()) |
| 105 | + assert.Equal(t, "12.0", *result.Value().Value) |
| 106 | + assert.Equal(t, "sum", result.Value().AggregationType) |
| 107 | + assert.Equal(t, "sub123", result.Value().SubscriptionID) |
| 108 | + assert.Equal(t, "plan123", result.Value().PlanID) |
| 109 | + }) |
| 110 | + |
| 111 | + t.Run("When timestamp is invalid", func(t *testing.T) { |
| 112 | + sqlmock, delete := setupTestEnv(t) |
| 113 | + defer delete() |
| 114 | + |
| 115 | + event := models.Event{ |
| 116 | + OrganizationID: "1a901a90-1a90-1a90-1a90-1a901a901a90", |
| 117 | + ExternalSubscriptionID: "sub_id", |
| 118 | + Code: "api_calls", |
| 119 | + Timestamp: "2025-03-06T12:00:00Z", |
| 120 | + Source: "SQS", |
| 121 | + } |
| 122 | + |
| 123 | + bm := models.BillableMetric{ |
| 124 | + ID: "bm123", |
| 125 | + OrganizationID: event.OrganizationID, |
| 126 | + Code: event.Code, |
| 127 | + AggregationType: models.AggregationTypeWeightedSum, |
| 128 | + FieldName: "api_requests", |
| 129 | + Expression: "", |
| 130 | + CreatedAt: time.Now(), |
| 131 | + UpdatedAt: time.Now(), |
| 132 | + } |
| 133 | + mockBmLookup(sqlmock, &bm) |
| 134 | + |
| 135 | + result := processor.EnrichEvent(&event) |
| 136 | + assert.False(t, result.Success()) |
| 137 | + assert.Equal(t, "strconv.ParseFloat: parsing \"2025-03-06T12:00:00Z\": invalid syntax", result.ErrorMsg()) |
| 138 | + assert.Equal(t, "build_enriched_event", result.ErrorCode()) |
| 139 | + assert.Equal(t, "Error while converting event to enriched event", result.ErrorMessage()) |
| 140 | + }) |
| 141 | + |
| 142 | + t.Run("When expression failed to evaluate", func(t *testing.T) { |
| 143 | + sqlmock, delete := setupTestEnv(t) |
| 144 | + defer delete() |
| 145 | + |
| 146 | + event := models.Event{ |
| 147 | + OrganizationID: "1a901a90-1a90-1a90-1a90-1a901a901a90", |
| 148 | + ExternalSubscriptionID: "sub_id", |
| 149 | + Code: "api_calls", |
| 150 | + Timestamp: "1741007009.123", |
| 151 | + Source: "SQS", |
| 152 | + } |
| 153 | + |
| 154 | + bm := models.BillableMetric{ |
| 155 | + ID: "bm123", |
| 156 | + OrganizationID: event.OrganizationID, |
| 157 | + Code: event.Code, |
| 158 | + AggregationType: models.AggregationTypeWeightedSum, |
| 159 | + FieldName: "api_requests", |
| 160 | + Expression: "round(event.properties.value)", |
| 161 | + CreatedAt: time.Now(), |
| 162 | + UpdatedAt: time.Now(), |
| 163 | + } |
| 164 | + mockBmLookup(sqlmock, &bm) |
| 165 | + |
| 166 | + result := processor.EnrichEvent(&event) |
| 167 | + assert.False(t, result.Success()) |
| 168 | + assert.Contains(t, result.ErrorMsg(), "Failed to evaluate expr: round(event.properties.value)") |
| 169 | + assert.Equal(t, "evaluate_expression", result.ErrorCode()) |
| 170 | + assert.Equal(t, "Error evaluating custom expression", result.ErrorMessage()) |
| 171 | + }) |
| 172 | +} |
| 173 | + |
| 174 | +func TestEvaluateExpression(t *testing.T) { |
| 175 | + _, delete := setupTestEnv(t) |
| 176 | + defer delete() |
| 177 | + |
| 178 | + bm := models.BillableMetric{} |
| 179 | + event := models.EnrichedEvent{Timestamp: 1741007009.0, Code: "foo"} |
| 180 | + var result utils.Result[bool] |
| 181 | + |
| 182 | + t.Run("Without expression", func(t *testing.T) { |
| 183 | + result = processor.evaluateExpression(&event, &bm) |
| 184 | + assert.True(t, result.Success(), "It should succeed when Billable metric does not have a custom expression") |
| 185 | + }) |
| 186 | + |
| 187 | + t.Run("With an expression but witout required fields", func(t *testing.T) { |
| 188 | + bm.Expression = "round(event.properties.value * event.properties.units)" |
| 189 | + bm.FieldName = "total_value" |
| 190 | + result = processor.evaluateExpression(&event, &bm) |
| 191 | + assert.False(t, result.Success()) |
| 192 | + assert.Contains( |
| 193 | + t, |
| 194 | + result.ErrorMsg(), |
| 195 | + "Failed to evaluate expr:", |
| 196 | + "It should fail when the event does not hold the required fields", |
| 197 | + ) |
| 198 | + }) |
| 199 | + |
| 200 | + t.Run("With an expression and with required fields", func(t *testing.T) { |
| 201 | + properties := map[string]any{ |
| 202 | + "value": "12.0", |
| 203 | + "units": 3, |
| 204 | + } |
| 205 | + event.Properties = properties |
| 206 | + result = processor.evaluateExpression(&event, &bm) |
| 207 | + assert.True(t, result.Success()) |
| 208 | + assert.Equal(t, "36", event.Properties["total_value"]) |
| 209 | + }) |
| 210 | + |
| 211 | + t.Run("With a float timestamp", func(t *testing.T) { |
| 212 | + event.Timestamp = 1741007009.123 |
| 213 | + |
| 214 | + result = processor.evaluateExpression(&event, &bm) |
| 215 | + assert.True(t, result.Success()) |
| 216 | + assert.Equal(t, "36", event.Properties["total_value"]) |
| 217 | + }) |
| 218 | +} |
0 commit comments