forked from GoCodeAlone/modular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbdd_migrations_test.go
More file actions
200 lines (168 loc) · 5.92 KB
/
bdd_migrations_test.go
File metadata and controls
200 lines (168 loc) · 5.92 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
package database
import (
"context"
"fmt"
"time"
)
// Database migration events and handling
func (ctx *DatabaseBDDTestContext) aDatabaseMigrationIsInitiated() error {
// Reset event observer to capture only this scenario's events
ctx.eventObserver.Reset()
// Create a simple test migration
migration := Migration{
ID: "test-migration-001",
Version: "1.0.0",
SQL: "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY, name TEXT)",
Up: true,
}
// Get the database service and set up event emission
if ctx.service != nil {
// Set the database module as the event emitter for the service
ctx.service.SetEventEmitter(ctx.module)
// Create migrations table first
err := ctx.service.CreateMigrationsTable(context.Background())
if err != nil {
return fmt.Errorf("failed to create migrations table: %w", err)
}
// Run the migration - this should emit the migration started event
err = ctx.service.RunMigration(context.Background(), migration)
if err != nil {
ctx.lastError = err
return fmt.Errorf("migration failed: %w", err)
}
}
return nil
}
func (ctx *DatabaseBDDTestContext) aMigrationStartedEventShouldBeEmitted() error {
events := ctx.eventObserver.GetEvents()
for _, event := range events {
if event.Type() == EventTypeMigrationStarted {
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", EventTypeMigrationStarted, eventTypes)
}
func (ctx *DatabaseBDDTestContext) theEventShouldContainMigrationMetadata() error {
events := ctx.eventObserver.GetEvents()
for _, event := range events {
if event.Type() == EventTypeMigrationStarted {
// Check that the event has migration metadata
data := event.Data()
if data == nil {
return fmt.Errorf("migration started event should contain metadata but data is nil")
}
return nil
}
}
return fmt.Errorf("migration started event not found to validate metadata")
}
func (ctx *DatabaseBDDTestContext) aDatabaseMigrationCompletesSuccessfully() error {
// Reset event observer to capture only this scenario's events
ctx.eventObserver.Reset()
// Create a test migration that will complete successfully
migration := Migration{
ID: "test-migration-002",
Version: "1.1.0",
SQL: "CREATE TABLE IF NOT EXISTS completed_table (id INTEGER PRIMARY KEY, status TEXT DEFAULT 'completed')",
Up: true,
}
// Get the database service and set up event emission
if ctx.service != nil {
// Set the database module as the event emitter for the service
ctx.service.SetEventEmitter(ctx.module)
// Create migrations table first
err := ctx.service.CreateMigrationsTable(context.Background())
if err != nil {
return fmt.Errorf("failed to create migrations table: %w", err)
}
// Run the migration - this should emit migration started and completed events
err = ctx.service.RunMigration(context.Background(), migration)
if err != nil {
ctx.lastError = err
return fmt.Errorf("migration failed: %w", err)
}
}
return nil
}
func (ctx *DatabaseBDDTestContext) aMigrationCompletedEventShouldBeEmitted() error {
time.Sleep(100 * time.Millisecond) // Give time for async event emission
events := ctx.eventObserver.GetEvents()
for _, event := range events {
if event.Type() == EventTypeMigrationCompleted {
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", EventTypeMigrationCompleted, eventTypes)
}
func (ctx *DatabaseBDDTestContext) theEventShouldContainMigrationResults() error {
events := ctx.eventObserver.GetEvents()
for _, event := range events {
if event.Type() == EventTypeMigrationCompleted {
// Check that the event has migration results
data := event.Data()
if data == nil {
return fmt.Errorf("migration completed event should contain results but data is nil")
}
return nil
}
}
return fmt.Errorf("migration completed event not found to validate results")
}
func (ctx *DatabaseBDDTestContext) aDatabaseMigrationFailsWithErrors() error {
// Reset event observer to capture only this scenario's events
ctx.eventObserver.Reset()
// Create a migration with invalid SQL that will fail
migration := Migration{
ID: "test-migration-fail",
Version: "1.2.0",
SQL: "CREATE TABLE duplicate_table (id INTEGER PRIMARY KEY); CREATE TABLE duplicate_table (name TEXT);", // This will fail due to duplicate table
Up: true,
}
// Get the database service and set up event emission
if ctx.service != nil {
// Set the database module as the event emitter for the service
ctx.service.SetEventEmitter(ctx.module)
// Run the migration - this should fail and emit migration failed event
err := ctx.service.RunMigration(context.Background(), migration)
if err != nil {
// This is expected - the migration should fail
ctx.lastError = err
}
}
return nil
}
func (ctx *DatabaseBDDTestContext) aMigrationFailedEventShouldBeEmitted() error {
events := ctx.eventObserver.GetEvents()
for _, event := range events {
if event.Type() == EventTypeMigrationFailed {
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", EventTypeMigrationFailed, eventTypes)
}
func (ctx *DatabaseBDDTestContext) theEventShouldContainFailureDetails() error {
events := ctx.eventObserver.GetEvents()
for _, event := range events {
if event.Type() == EventTypeMigrationFailed {
// Check that the event has failure details
data := event.Data()
if data == nil {
return fmt.Errorf("migration failed event should contain failure details but data is nil")
}
return nil
}
}
return fmt.Errorf("migration failed event not found to validate failure details")
}