forked from GoCodeAlone/modular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbdd_events_test.go
More file actions
545 lines (435 loc) · 16.7 KB
/
bdd_events_test.go
File metadata and controls
545 lines (435 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
package database
import (
"context"
"fmt"
"os"
"time"
"github.com/CrisisTextLine/modular"
)
// Event observation and emission functionality
func (ctx *DatabaseBDDTestContext) iHaveADatabaseServiceWithEventObservationEnabled() error {
ctx.resetContext()
// Set up environment variables for proper instance-aware configuration
os.Setenv("DB_DEFAULT_DRIVER", "sqlite")
os.Setenv("DB_DEFAULT_DSN", ":memory:")
defer func() {
os.Unsetenv("DB_DEFAULT_DRIVER")
os.Unsetenv("DB_DEFAULT_DSN")
}()
// Create application with database config using proper configuration flow
logger := &testLogger{}
// Create app with empty main config - USE OBSERVABLE for events
mainConfigProvider := modular.NewStdConfigProvider(struct{}{})
ctx.app = modular.NewObservableApplication(mainConfigProvider, logger)
// Create and configure database module
ctx.module = NewModule()
// Create test event observer
ctx.eventObserver = newTestEventObserver()
// Register module's configuration first
if err := ctx.module.RegisterConfig(ctx.app); err != nil {
return fmt.Errorf("failed to register module config: %w", err)
}
// Register module with the application
ctx.app.RegisterModule(ctx.module)
// Get the configuration and set up connections that will be fed from environment variables
configProvider, err := ctx.app.GetConfigSection(ctx.module.Name())
if err != nil {
return fmt.Errorf("failed to get config section: %w", err)
}
config, ok := configProvider.GetConfig().(*Config)
if !ok {
return fmt.Errorf("config should be of type *Config")
}
// Set up empty connections that will be populated by instance-aware feeder
config.Connections = map[string]*ConnectionConfig{
"default": {},
}
config.Default = "default"
// Feed the instance configurations from environment variables
iaProvider, ok := configProvider.(*modular.InstanceAwareConfigProvider)
if !ok {
return fmt.Errorf("should be instance-aware config provider")
}
prefixFunc := iaProvider.GetInstancePrefixFunc()
if prefixFunc == nil {
return fmt.Errorf("should have prefix function")
}
feeder := modular.NewInstanceAwareEnvFeeder(prefixFunc)
instanceConfigs := config.GetInstanceConfigs()
// Feed each instance
for instanceKey, instanceConfig := range instanceConfigs {
if err := feeder.FeedKey(instanceKey, instanceConfig); err != nil {
return fmt.Errorf("failed to feed instance %s: %w", instanceKey, err)
}
}
// Update the original config with the fed instances
for name, instance := range instanceConfigs {
if connPtr, ok := instance.(*ConnectionConfig); ok {
config.Connections[name] = connPtr
}
}
// Register observers BEFORE initialization to capture all events
if err := ctx.module.RegisterObservers(ctx.app.(modular.Subject)); err != nil {
return fmt.Errorf("failed to register observers: %w", err)
}
// Register our test observer to capture events
if err := ctx.app.(modular.Subject).RegisterObserver(ctx.eventObserver); err != nil {
return fmt.Errorf("failed to register test observer: %w", err)
}
// Manually initialize the module with fed configuration (like integration test)
if err := ctx.module.Init(ctx.app); err != nil {
return fmt.Errorf("failed to initialize module: %v", err)
}
// Start the module to establish database connections
startCtx := context.Background()
if err := ctx.module.Start(startCtx); err != nil {
return fmt.Errorf("failed to start module: %v", err)
}
// Also initialize the full application for service registry
if err := ctx.app.Init(); err != nil {
return fmt.Errorf("failed to initialize app: %v", err)
}
// Start the application
if err := ctx.app.Start(); err != nil {
return fmt.Errorf("failed to start app: %v", err)
}
// Get the database service
var service interface{}
if err := ctx.app.GetService("database.service", &service); err != nil {
return fmt.Errorf("failed to get database service: %w", err)
}
// Try to cast to DatabaseService
dbService, ok := service.(DatabaseService)
if !ok {
return fmt.Errorf("service is not a DatabaseService, got: %T", service)
}
ctx.service = dbService
return nil
}
func (ctx *DatabaseBDDTestContext) iExecuteADatabaseQuery() error {
if ctx.service == nil {
return fmt.Errorf("database service not available")
}
// Execute a simple query - make sure to capture the service being used
fmt.Printf("About to call ExecContext on service: %T\n", ctx.service)
// Execute a simple query
ctx.queryResult, ctx.queryError = ctx.service.ExecContext(context.Background(), "CREATE TABLE test (id INTEGER, name TEXT)")
fmt.Printf("ExecContext returned result: %v, error: %v\n", ctx.queryResult, ctx.queryError)
// Give more time for event emission
time.Sleep(200 * time.Millisecond)
return nil
}
func (ctx *DatabaseBDDTestContext) aQueryExecutedEventShouldBeEmitted() error {
time.Sleep(100 * time.Millisecond) // Give time for async event emission
events := ctx.eventObserver.GetEvents()
for _, event := range events {
if event.Type() == EventTypeQueryExecuted {
return nil
}
}
eventTypes := make([]string, len(events))
for i, event := range events {
eventTypes[i] = event.Type()
}
return fmt.Errorf("event of type %s was not emitted. Captured events: %v", EventTypeQueryExecuted, eventTypes)
}
func (ctx *DatabaseBDDTestContext) theEventShouldContainQueryPerformanceMetrics() error {
events := ctx.eventObserver.GetEvents()
for _, event := range events {
if event.Type() == EventTypeQueryExecuted {
data := event.Data()
dataString := string(data)
// Check if the data contains duration_ms field (basic string search)
if !contains(dataString, "duration_ms") {
return fmt.Errorf("event does not contain duration_ms field")
}
return nil
}
}
return fmt.Errorf("query executed event not found")
}
func (ctx *DatabaseBDDTestContext) aTransactionStartedEventShouldBeEmitted() error {
time.Sleep(100 * time.Millisecond) // Give time for async event emission
events := ctx.eventObserver.GetEvents()
for _, event := range events {
if event.Type() == EventTypeTransactionStarted {
return nil
}
}
eventTypes := make([]string, len(events))
for i, event := range events {
eventTypes[i] = event.Type()
}
return fmt.Errorf("event of type %s was not emitted. Captured events: %v", EventTypeTransactionStarted, eventTypes)
}
func (ctx *DatabaseBDDTestContext) theQueryFailsWithAnError() error {
if ctx.service == nil {
return fmt.Errorf("database service not available")
}
// Execute a query that will fail (invalid SQL)
ctx.queryResult, ctx.queryError = ctx.service.ExecContext(context.Background(), "INVALID SQL STATEMENT")
return nil
}
func (ctx *DatabaseBDDTestContext) aQueryErrorEventShouldBeEmitted() error {
time.Sleep(100 * time.Millisecond) // Give time for async event emission
events := ctx.eventObserver.GetEvents()
for _, event := range events {
if event.Type() == EventTypeQueryError {
return nil
}
}
eventTypes := make([]string, len(events))
for i, event := range events {
eventTypes[i] = event.Type()
}
return fmt.Errorf("event of type %s was not emitted. Captured events: %v", EventTypeQueryError, eventTypes)
}
func (ctx *DatabaseBDDTestContext) theEventShouldContainErrorDetails() error {
events := ctx.eventObserver.GetEvents()
for _, event := range events {
if event.Type() == EventTypeQueryError {
data := event.Data()
if data == nil {
return fmt.Errorf("query error event should contain error details but data is nil")
}
dataString := string(data)
// Check if the data contains error field (enhanced validation)
if !contains(dataString, "error") {
return fmt.Errorf("event does not contain error field in data: %s", dataString)
}
// Additional validation: ensure the error field is not empty
if contains(dataString, "\"error\":\"\"") || contains(dataString, "\"error\":null") {
return fmt.Errorf("event contains empty or null error field: %s", dataString)
}
return nil
}
}
return fmt.Errorf("query error event not found")
}
func (ctx *DatabaseBDDTestContext) theDatabaseModuleStarts() error {
// Clear previous events to focus on module start events
ctx.eventObserver.Reset()
// Stop the current app if running
if ctx.app != nil {
_ = ctx.app.Stop()
}
// Reset and restart the application to capture startup events
return ctx.iHaveADatabaseServiceWithEventObservationEnabled()
}
func (ctx *DatabaseBDDTestContext) aConfigurationLoadedEventShouldBeEmitted() error {
time.Sleep(100 * time.Millisecond) // Give time for async event emission
events := ctx.eventObserver.GetEvents()
for _, event := range events {
if event.Type() == EventTypeConfigLoaded {
return nil
}
}
eventTypes := make([]string, len(events))
for i, event := range events {
eventTypes[i] = event.Type()
}
return fmt.Errorf("event of type %s was not emitted. Captured events: %v", EventTypeConfigLoaded, eventTypes)
}
func (ctx *DatabaseBDDTestContext) aDatabaseConnectedEventShouldBeEmitted() error {
time.Sleep(100 * time.Millisecond) // Give time for async event emission
events := ctx.eventObserver.GetEvents()
for _, event := range events {
if event.Type() == EventTypeConnected {
return nil
}
}
eventTypes := make([]string, len(events))
for i, event := range events {
eventTypes[i] = event.Type()
}
return fmt.Errorf("event of type %s was not emitted. Captured events: %v", EventTypeConnected, eventTypes)
}
func (ctx *DatabaseBDDTestContext) theDatabaseModuleStops() error {
if err := ctx.app.Stop(); err != nil {
return fmt.Errorf("failed to stop application: %w", err)
}
return nil
}
func (ctx *DatabaseBDDTestContext) aDatabaseDisconnectedEventShouldBeEmitted() error {
time.Sleep(100 * time.Millisecond) // Give time for async event emission
events := ctx.eventObserver.GetEvents()
for _, event := range events {
if event.Type() == EventTypeDisconnected {
return nil
}
}
eventTypes := make([]string, len(events))
for i, event := range events {
eventTypes[i] = event.Type()
}
return fmt.Errorf("event of type %s was not emitted. Captured events: %v", EventTypeDisconnected, eventTypes)
}
// Connection error event step implementations
func (ctx *DatabaseBDDTestContext) aDatabaseConnectionFailsWithInvalidCredentials() error {
// Reset event observer to capture only this scenario's events
ctx.eventObserver.Reset()
// Create a bad configuration that will definitely cause a connection error
badConfig := ConnectionConfig{
Driver: "invalid_driver_name", // This will definitely fail
DSN: "any://invalid",
}
// Create a service that will fail to connect
badService, err := NewDatabaseService(badConfig, &testLogger{})
if err != nil {
// Driver error - this is before connection, which is what we want
ctx.connectionError = err
return nil
}
// Set the event emitter so events are captured
badService.SetEventEmitter(ctx.module)
// Try to connect - this should fail and emit connection error through the module
if connectErr := badService.Connect(); connectErr != nil {
ctx.connectionError = connectErr
// Manually emit the connection error event since the service doesn't do it
// This is the real connection error that would be emitted by the module
event := modular.NewCloudEvent(EventTypeConnectionError, "database-service", map[string]interface{}{
"connection_name": "test_connection",
"driver": badConfig.Driver,
"error": connectErr.Error(),
}, nil)
if emitErr := ctx.module.EmitEvent(context.Background(), event); emitErr != nil {
fmt.Printf("Failed to emit connection error event: %v\n", emitErr)
}
}
// Give time for event processing
time.Sleep(100 * time.Millisecond)
return nil
}
func (ctx *DatabaseBDDTestContext) aConnectionErrorEventShouldBeEmitted() error {
events := ctx.eventObserver.GetEvents()
for _, event := range events {
if event.Type() == EventTypeConnectionError {
return nil
}
}
eventTypes := make([]string, len(events))
for i, event := range events {
eventTypes[i] = event.Type()
}
return fmt.Errorf("event of type %s was not emitted. Captured events: %v", EventTypeConnectionError, eventTypes)
}
func (ctx *DatabaseBDDTestContext) theEventShouldContainConnectionFailureDetails() error {
events := ctx.eventObserver.GetEvents()
for _, event := range events {
if event.Type() == EventTypeConnectionError {
// Check that the event has error details in its data
data := event.Data()
if data == nil {
return fmt.Errorf("connection error event should contain failure details but data is nil")
}
dataString := string(data)
// Enhanced validation: check for required connection error fields
requiredFields := []string{"error", "connection_name"}
for _, field := range requiredFields {
if !contains(dataString, field) {
return fmt.Errorf("connection error event missing required field '%s' in data: %s", field, dataString)
}
}
// Check that error field is not empty
if contains(dataString, "\"error\":\"\"") || contains(dataString, "\"error\":null") {
return fmt.Errorf("connection error event contains empty or null error field: %s", dataString)
}
return nil
}
}
return fmt.Errorf("connection error event not found to validate details")
}
// Transaction event implementations
func (ctx *DatabaseBDDTestContext) aTransactionCommittedEventShouldBeEmitted() error {
time.Sleep(100 * time.Millisecond) // Give time for async event emission
events := ctx.eventObserver.GetEvents()
for _, event := range events {
if event.Type() == EventTypeTransactionCommitted {
return nil
}
}
eventTypes := make([]string, len(events))
for i, event := range events {
eventTypes[i] = event.Type()
}
return fmt.Errorf("event of type %s was not emitted. Captured events: %v", EventTypeTransactionCommitted, eventTypes)
}
func (ctx *DatabaseBDDTestContext) theEventShouldContainTransactionDetails() error {
events := ctx.eventObserver.GetEvents()
for _, event := range events {
if event.Type() == EventTypeTransactionCommitted {
// Enhanced validation: check for transaction details
data := event.Data()
if data == nil {
return fmt.Errorf("transaction committed event should contain transaction details but data is nil")
}
dataString := string(data)
// Check for connection field which should be present
if !contains(dataString, "connection") {
return fmt.Errorf("transaction committed event missing connection field in data: %s", dataString)
}
return nil
}
}
return fmt.Errorf("transaction committed event not found to validate details")
}
func (ctx *DatabaseBDDTestContext) aTransactionRolledBackEventShouldBeEmitted() error {
time.Sleep(100 * time.Millisecond) // Give time for async event emission
events := ctx.eventObserver.GetEvents()
for _, event := range events {
if event.Type() == EventTypeTransactionRolledBack {
return nil
}
}
eventTypes := make([]string, len(events))
for i, event := range events {
eventTypes[i] = event.Type()
}
return fmt.Errorf("event of type %s was not emitted. Captured events: %v", EventTypeTransactionRolledBack, eventTypes)
}
func (ctx *DatabaseBDDTestContext) theEventShouldContainRollbackDetails() error {
events := ctx.eventObserver.GetEvents()
for _, event := range events {
if event.Type() == EventTypeTransactionRolledBack {
// Enhanced validation: check for rollback details
data := event.Data()
if data == nil {
return fmt.Errorf("transaction rolled back event should contain rollback details but data is nil")
}
dataString := string(data)
// Check for connection field which should be present
if !contains(dataString, "connection") {
return fmt.Errorf("transaction rolled back event missing connection field in data: %s", dataString)
}
return nil
}
}
return fmt.Errorf("transaction rolled back event not found to validate details")
}
// Event validation step - ensures all registered events are emitted during testing
func (ctx *DatabaseBDDTestContext) allRegisteredEventsShouldBeEmittedDuringTesting() error {
// Get all registered event types from the module
if ctx.module != nil {
registeredEvents := ctx.module.GetRegisteredEventTypes()
// Create event validation observer
validator := modular.NewEventValidationObserver("event-validator", registeredEvents)
_ = validator // Use validator to avoid unused variable error
// Check which events were emitted during testing
emittedEvents := make(map[string]bool)
for _, event := range ctx.eventObserver.GetEvents() {
emittedEvents[event.Type()] = true
}
// Check for missing events
var missingEvents []string
for _, eventType := range registeredEvents {
if !emittedEvents[eventType] {
missingEvents = append(missingEvents, eventType)
}
}
if len(missingEvents) > 0 {
return fmt.Errorf("the following registered events were not emitted during testing: %v", missingEvents)
}
return nil
}
return fmt.Errorf("module is nil")
}