|
| 1 | +package graph_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/specterops/dawgs/graph" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +// stubDatabase is a no-op Database implementation suitable for exercising |
| 14 | +// behavior on *graph.DatabaseSwitch that does not depend on real driver |
| 15 | +// semantics. Methods return zero values; tests that need richer behavior |
| 16 | +// should embed this and override only what they need. |
| 17 | +type stubDatabase struct{} |
| 18 | + |
| 19 | +func (stubDatabase) SetWriteFlushSize(int) {} |
| 20 | +func (stubDatabase) SetBatchWriteSize(int) {} |
| 21 | +func (stubDatabase) ReadTransaction(context.Context, graph.TransactionDelegate, ...graph.TransactionOption) error { |
| 22 | + return nil |
| 23 | +} |
| 24 | +func (stubDatabase) WriteTransaction(context.Context, graph.TransactionDelegate, ...graph.TransactionOption) error { |
| 25 | + return nil |
| 26 | +} |
| 27 | +func (stubDatabase) BatchOperation(context.Context, graph.BatchDelegate, ...graph.BatchOption) error { |
| 28 | + return nil |
| 29 | +} |
| 30 | +func (stubDatabase) AssertSchema(context.Context, graph.Schema) error { return nil } |
| 31 | +func (stubDatabase) SetDefaultGraph(context.Context, graph.Graph) error { return nil } |
| 32 | +func (stubDatabase) Run(context.Context, string, map[string]any) error { return nil } |
| 33 | +func (stubDatabase) Close(context.Context) error { return nil } |
| 34 | +func (stubDatabase) FetchKinds(context.Context) (graph.Kinds, error) { return nil, nil } |
| 35 | +func (stubDatabase) RefreshKinds(context.Context) error { return nil } |
| 36 | + |
| 37 | +// optimizingStubDatabase is a stubDatabase that additionally satisfies |
| 38 | +// graph.Optimizer. Each call to Optimize increments calls and returns err. |
| 39 | +type optimizingStubDatabase struct { |
| 40 | + stubDatabase |
| 41 | + calls int |
| 42 | + err error |
| 43 | +} |
| 44 | + |
| 45 | +func (s *optimizingStubDatabase) Optimize(context.Context) error { |
| 46 | + s.calls++ |
| 47 | + return s.err |
| 48 | +} |
| 49 | + |
| 50 | +// TestDatabaseSwitch_Optimize_DelegatesWhenUnderlyingImplementsOptimizer |
| 51 | +// verifies that *graph.DatabaseSwitch forwards Optimize to the active driver |
| 52 | +// when that driver implements graph.Optimizer, and propagates its return. |
| 53 | +func TestDatabaseSwitch_Optimize_DelegatesWhenUnderlyingImplementsOptimizer(t *testing.T) { |
| 54 | + ctx := context.Background() |
| 55 | + |
| 56 | + driver := &optimizingStubDatabase{} |
| 57 | + dbSwitch := graph.NewDatabaseSwitch(ctx, driver) |
| 58 | + |
| 59 | + require.NoError(t, dbSwitch.Optimize(ctx)) |
| 60 | + assert.Equal(t, 1, driver.calls, "Optimize should be invoked exactly once on the underlying driver") |
| 61 | + |
| 62 | + driver.err = errors.New("optimizer reported failure") |
| 63 | + err := dbSwitch.Optimize(ctx) |
| 64 | + assert.ErrorIs(t, err, driver.err, "DatabaseSwitch must propagate the underlying optimizer error") |
| 65 | + assert.Equal(t, 2, driver.calls) |
| 66 | +} |
| 67 | + |
| 68 | +// TestDatabaseSwitch_Optimize_NoOpWhenUnderlyingDoesNotImplementOptimizer |
| 69 | +// verifies that the wrapper returns nil without panicking when the active |
| 70 | +// driver lacks an Optimize method. |
| 71 | +func TestDatabaseSwitch_Optimize_NoOpWhenUnderlyingDoesNotImplementOptimizer(t *testing.T) { |
| 72 | + ctx := context.Background() |
| 73 | + dbSwitch := graph.NewDatabaseSwitch(ctx, stubDatabase{}) |
| 74 | + |
| 75 | + require.NoError(t, dbSwitch.Optimize(ctx)) |
| 76 | +} |
| 77 | + |
| 78 | +// TestDatabaseSwitch_Optimize_FollowsActiveDriverAfterSwitch verifies that |
| 79 | +// after Switch reassigns the active driver, Optimize routes to the new one. |
| 80 | +func TestDatabaseSwitch_Optimize_FollowsActiveDriverAfterSwitch(t *testing.T) { |
| 81 | + ctx := context.Background() |
| 82 | + |
| 83 | + first := &optimizingStubDatabase{} |
| 84 | + second := &optimizingStubDatabase{} |
| 85 | + |
| 86 | + dbSwitch := graph.NewDatabaseSwitch(ctx, first) |
| 87 | + require.NoError(t, dbSwitch.Optimize(ctx)) |
| 88 | + assert.Equal(t, 1, first.calls) |
| 89 | + assert.Equal(t, 0, second.calls) |
| 90 | + |
| 91 | + dbSwitch.Switch(second) |
| 92 | + require.NoError(t, dbSwitch.Optimize(ctx)) |
| 93 | + assert.Equal(t, 1, first.calls, "first driver should not be invoked after Switch") |
| 94 | + assert.Equal(t, 1, second.calls, "Optimize should be routed to the new active driver") |
| 95 | +} |
| 96 | + |
| 97 | +// Compile-time assertion that *graph.DatabaseSwitch satisfies graph.Optimizer. |
| 98 | +// This keeps the wrapper's optional-capability contract enforced by the type |
| 99 | +// system rather than relying on test discovery alone. |
| 100 | +var _ graph.Optimizer = (*graph.DatabaseSwitch)(nil) |
0 commit comments