|
6 | 6 |
|
7 | 7 | "github.com/stretchr/testify/assert" |
8 | 8 |
|
| 9 | + "github.com/openmeterio/openmeter/openmeter/customer" |
9 | 10 | "github.com/openmeterio/openmeter/openmeter/streaming" |
| 11 | + "github.com/openmeterio/openmeter/pkg/models" |
10 | 12 | ) |
11 | 13 |
|
12 | 14 | func TestCreateEventsTable(t *testing.T) { |
@@ -91,6 +93,42 @@ func TestQueryEventsTable(t *testing.T) { |
91 | 93 | wantSQL: "SELECT id, type, subject, source, time, data, ingested_at, stored_at, store_row_id FROM openmeter.om_events WHERE namespace = ? AND time >= ? AND time < ? AND id LIKE ? ORDER BY time DESC LIMIT ?", |
92 | 94 | wantArgs: []interface{}{"my_namespace", from.Unix(), to.Unix(), "%event-id-1%", 100}, |
93 | 95 | }, |
| 96 | + // Customer filter |
| 97 | + { |
| 98 | + query: queryEventsTable{ |
| 99 | + Database: "openmeter", |
| 100 | + EventsTableName: "om_events", |
| 101 | + Namespace: "my_namespace", |
| 102 | + From: from, |
| 103 | + Limit: 100, |
| 104 | + Customers: &[]streaming.Customer{ |
| 105 | + customer.Customer{ |
| 106 | + ManagedResource: models.ManagedResource{ |
| 107 | + NamespacedModel: models.NamespacedModel{ |
| 108 | + Namespace: "my_namespace", |
| 109 | + }, |
| 110 | + ID: "customer1", |
| 111 | + }, |
| 112 | + UsageAttribution: customer.CustomerUsageAttribution{ |
| 113 | + SubjectKeys: []string{"subject1", "subject2"}, |
| 114 | + }, |
| 115 | + }, |
| 116 | + customer.Customer{ |
| 117 | + ManagedResource: models.ManagedResource{ |
| 118 | + NamespacedModel: models.NamespacedModel{ |
| 119 | + Namespace: "my_namespace", |
| 120 | + }, |
| 121 | + ID: "customer2", |
| 122 | + }, |
| 123 | + UsageAttribution: customer.CustomerUsageAttribution{ |
| 124 | + SubjectKeys: []string{"subject3"}, |
| 125 | + }, |
| 126 | + }, |
| 127 | + }, |
| 128 | + }, |
| 129 | + wantSQL: "WITH map('subject1', 'customer1', 'subject2', 'customer1', 'subject3', 'customer2') as subject_to_customer_id SELECT id, type, subject, source, time, data, ingested_at, stored_at, store_row_id, subject_to_customer_id[om_events.subject] AS customer_id FROM openmeter.om_events WHERE namespace = ? AND time >= ? AND om_events.subject IN (?) ORDER BY time DESC LIMIT ?", |
| 130 | + wantArgs: []interface{}{"my_namespace", from.Unix(), []string{"subject1", "subject2", "subject3"}, 100}, |
| 131 | + }, |
94 | 132 | } |
95 | 133 |
|
96 | 134 | for _, tt := range tests { |
|
0 commit comments