feat(signoz): compile time check for dependency injection (#8033)

This commit is contained in:
Vibhu Pandey 2025-05-24 23:53:54 +05:30 committed by GitHub
parent 93ca3fee33
commit 403630ad31
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 98 additions and 0 deletions

View 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
}

View 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
View 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)
}
}