mirror of
https://git.mirrors.martin98.com/https://github.com/SigNoz/signoz
synced 2025-08-16 14:16:02 +08:00
feat(signoz): compile time check for dependency injection (#8033)
This commit is contained in:
parent
93ca3fee33
commit
403630ad31
28
pkg/emailing/emailingtest/provider.go
Normal file
28
pkg/emailing/emailingtest/provider.go
Normal file
@ -0,0 +1,28 @@
|
||||
package emailingtest
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/SigNoz/signoz/pkg/emailing"
|
||||
"github.com/SigNoz/signoz/pkg/types/emailtypes"
|
||||
)
|
||||
|
||||
var _ emailing.Emailing = (*Provider)(nil)
|
||||
|
||||
type Provider struct {
|
||||
SentEmailCountByTo map[string]int
|
||||
SentEmailCountByTemplateName map[emailtypes.TemplateName]int
|
||||
}
|
||||
|
||||
func New() *Provider {
|
||||
return &Provider{
|
||||
SentEmailCountByTo: make(map[string]int),
|
||||
SentEmailCountByTemplateName: make(map[emailtypes.TemplateName]int),
|
||||
}
|
||||
}
|
||||
|
||||
func (provider *Provider) SendHTML(ctx context.Context, to string, subject string, templateName emailtypes.TemplateName, data map[string]any) error {
|
||||
provider.SentEmailCountByTo[to]++
|
||||
provider.SentEmailCountByTemplateName[templateName]++
|
||||
return nil
|
||||
}
|
36
pkg/signoz/handler_test.go
Normal file
36
pkg/signoz/handler_test.go
Normal file
@ -0,0 +1,36 @@
|
||||
package signoz
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/SigNoz/signoz/pkg/emailing/emailingtest"
|
||||
"github.com/SigNoz/signoz/pkg/factory/factorytest"
|
||||
"github.com/SigNoz/signoz/pkg/modules/user/impluser"
|
||||
"github.com/SigNoz/signoz/pkg/sqlstore"
|
||||
"github.com/SigNoz/signoz/pkg/sqlstore/sqlstoretest"
|
||||
"github.com/SigNoz/signoz/pkg/types/authtypes"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// This is a test to ensure that all fields of the handlers are initialized.
|
||||
// It also helps us catch these errors at compile time instead of runtime.
|
||||
func TestNewHandlers(t *testing.T) {
|
||||
sqlstore := sqlstoretest.New(sqlstore.Config{Provider: "sqlite"}, sqlmock.QueryMatcherEqual)
|
||||
jwt := authtypes.NewJWT("", 1*time.Hour, 1*time.Hour)
|
||||
emailing := emailingtest.New()
|
||||
providerSettings := factorytest.NewSettings()
|
||||
userModule := impluser.NewModule(impluser.NewStore(sqlstore), jwt, emailing, providerSettings)
|
||||
userHandler := impluser.NewHandler(userModule)
|
||||
|
||||
modules := NewModules(sqlstore, userModule)
|
||||
handlers := NewHandlers(modules, userHandler)
|
||||
|
||||
reflectVal := reflect.ValueOf(handlers)
|
||||
for i := 0; i < reflectVal.NumField(); i++ {
|
||||
f := reflectVal.Field(i)
|
||||
assert.False(t, f.IsZero(), "%s handler has not been initialized", reflectVal.Type().Field(i).Name)
|
||||
}
|
||||
}
|
34
pkg/signoz/module_test.go
Normal file
34
pkg/signoz/module_test.go
Normal file
@ -0,0 +1,34 @@
|
||||
package signoz
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/SigNoz/signoz/pkg/emailing/emailingtest"
|
||||
"github.com/SigNoz/signoz/pkg/factory/factorytest"
|
||||
"github.com/SigNoz/signoz/pkg/modules/user/impluser"
|
||||
"github.com/SigNoz/signoz/pkg/sqlstore"
|
||||
"github.com/SigNoz/signoz/pkg/sqlstore/sqlstoretest"
|
||||
"github.com/SigNoz/signoz/pkg/types/authtypes"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// This is a test to ensure that all fields of the modules are initialized.
|
||||
// It also helps us catch these errors at compile time instead of runtime.
|
||||
func TestNewModules(t *testing.T) {
|
||||
sqlstore := sqlstoretest.New(sqlstore.Config{Provider: "sqlite"}, sqlmock.QueryMatcherEqual)
|
||||
jwt := authtypes.NewJWT("", 1*time.Hour, 1*time.Hour)
|
||||
emailing := emailingtest.New()
|
||||
providerSettings := factorytest.NewSettings()
|
||||
userModule := impluser.NewModule(impluser.NewStore(sqlstore), jwt, emailing, providerSettings)
|
||||
|
||||
modules := NewModules(sqlstore, userModule)
|
||||
|
||||
reflectVal := reflect.ValueOf(modules)
|
||||
for i := 0; i < reflectVal.NumField(); i++ {
|
||||
f := reflectVal.Field(i)
|
||||
assert.False(t, f.IsZero(), "%s module has not been initialized", reflectVal.Type().Field(i).Name)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user