From 61f21409a097501e5cb7c2031207ce926bc7edb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Garcia?= Date: Thu, 16 Sep 2021 12:22:31 -0300 Subject: [PATCH] Improve panic message when ksql.Mock methods are called but unimplemented --- mocks.go | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/mocks.go b/mocks.go index 97911cd..e8b251a 100644 --- a/mocks.go +++ b/mocks.go @@ -1,6 +1,9 @@ package ksql -import "context" +import ( + "context" + "fmt" +) var _ Provider = Mock{} @@ -20,40 +23,64 @@ type Mock struct { // Insert ... func (m Mock) Insert(ctx context.Context, table Table, record interface{}) error { + if m.InsertFn == nil { + panic(fmt.Errorf("Mock.Insert(ctx, %v, %v) called but the ksql.Mock.InsertFn() is not set", table, record)) + } return m.InsertFn(ctx, table, record) } // Update ... func (m Mock) Update(ctx context.Context, table Table, record interface{}) error { + if m.UpdateFn == nil { + panic(fmt.Errorf("Mock.Update(ctx, %v, %v) called but the ksql.Mock.UpdateFn() is not set", table, record)) + } return m.UpdateFn(ctx, table, record) } // Delete ... func (m Mock) Delete(ctx context.Context, table Table, ids ...interface{}) error { + if m.DeleteFn == nil { + panic(fmt.Errorf("Mock.Delete(ctx, %v, %v) called but the ksql.Mock.DeleteFn() is not set", table, ids)) + } return m.DeleteFn(ctx, table, ids...) } // Query ... func (m Mock) Query(ctx context.Context, records interface{}, query string, params ...interface{}) error { + if m.QueryFn == nil { + panic(fmt.Errorf("Mock.Query(ctx, %v, %s, %v) called but the ksql.Mock.QueryFn() is not set", records, query, params)) + } return m.QueryFn(ctx, records, query, params...) } // QueryOne ... func (m Mock) QueryOne(ctx context.Context, record interface{}, query string, params ...interface{}) error { + if m.QueryOneFn == nil { + panic(fmt.Errorf("Mock.QueryOne(ctx, %v, %s, %v) called but the ksql.Mock.QueryOneFn() is not set", record, query, params)) + } return m.QueryOneFn(ctx, record, query, params...) } // QueryChunks ... func (m Mock) QueryChunks(ctx context.Context, parser ChunkParser) error { + if m.QueryChunksFn == nil { + panic(fmt.Errorf("Mock.QueryChunks(ctx, %v) called but the ksql.Mock.QueryChunksFn() is not set", parser)) + } return m.QueryChunksFn(ctx, parser) } // Exec ... func (m Mock) Exec(ctx context.Context, query string, params ...interface{}) error { + if m.ExecFn == nil { + panic(fmt.Errorf("Mock.Exec(ctx, %s, %v) called but the ksql.Mock.ExecFn() is not set", query, params)) + } return m.ExecFn(ctx, query, params...) } // Transaction ... func (m Mock) Transaction(ctx context.Context, fn func(db Provider) error) error { + if m.TransactionFn == nil { + return fn(m) + } return m.TransactionFn(ctx, fn) }