diff --git a/internal/module/test/db-sqlite_test.go b/internal/module/test/db-sqlite_test.go new file mode 100644 index 0000000..b4e2f9d --- /dev/null +++ b/internal/module/test/db-sqlite_test.go @@ -0,0 +1,87 @@ +package module_test + +import ( + "testing" + + "github.com/jwetzell/showbridge-go/internal/config" + "github.com/jwetzell/showbridge-go/internal/module" +) + +func TestDbSqliteFromRegistry(t *testing.T) { + registration, ok := module.ModuleRegistry["db.sqlite"] + if !ok { + t.Fatalf("db.sqlite module not registered") + } + + moduleInstance, err := registration.New(config.ModuleConfig{ + Id: "test", + Type: "db.sqlite", + Params: map[string]any{ + "dsn": ":memory:", + }, + }) + + if err != nil { + t.Fatalf("failed to create db.sqlite module: %s", err) + } + + if moduleInstance.Id() != "test" { + t.Fatalf("db.sqlite module has wrong id: %s", moduleInstance.Id()) + } + + if moduleInstance.Type() != "db.sqlite" { + t.Fatalf("db.sqlite module has wrong type: %s", moduleInstance.Type()) + } +} + +func TestBadDbSqlite(t *testing.T) { + tests := []struct { + name string + params map[string]any + errorString string + }{ + { + name: "no dsn param", + params: map[string]any{}, + errorString: "db.sqlite dsn error: not found", + }, + { + name: "non-string dsn", + params: map[string]any{"dsn": 123}, + errorString: "db.sqlite dsn error: not a string", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + + registration, ok := module.ModuleRegistry["db.sqlite"] + if !ok { + t.Fatalf("db.sqlite module not registered") + } + + moduleInstance, err := registration.New(config.ModuleConfig{ + Id: "test", + Type: "db.sqlite", + Params: test.params, + }) + + if err != nil { + if test.errorString != err.Error() { + t.Fatalf("db.sqlite got error '%s', expected '%s'", err.Error(), test.errorString) + } + return + } + + err = moduleInstance.Start(t.Context()) + + if err == nil { + t.Fatalf("db.sqlite expected to fail") + } + + if err.Error() != test.errorString { + t.Fatalf("db.sqlite got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +} diff --git a/internal/module/test/redis-client_test.go b/internal/module/test/redis-client_test.go new file mode 100644 index 0000000..0f34247 --- /dev/null +++ b/internal/module/test/redis-client_test.go @@ -0,0 +1,108 @@ +package module_test + +import ( + "testing" + + "github.com/jwetzell/showbridge-go/internal/config" + "github.com/jwetzell/showbridge-go/internal/module" +) + +func TestRedisClientFromRegistry(t *testing.T) { + registration, ok := module.ModuleRegistry["redis.client"] + if !ok { + t.Fatalf("redis.client module not registered") + } + + moduleInstance, err := registration.New(config.ModuleConfig{ + Id: "test", + Type: "redis.client", + Params: map[string]any{ + "host": "localhost", + "port": 6379, + }, + }) + + if err != nil { + t.Fatalf("failed to create redis.client module: %s", err) + } + + if moduleInstance.Id() != "test" { + t.Fatalf("redis.client module has wrong id: %s", moduleInstance.Id()) + } + + if moduleInstance.Type() != "redis.client" { + t.Fatalf("redis.client module has wrong type: %s", moduleInstance.Type()) + } +} + +func TestBadRedisClient(t *testing.T) { + tests := []struct { + name string + params map[string]any + errorString string + }{ + { + name: "no host param", + params: map[string]any{ + "port": 6379, + }, + errorString: "redis.client host error: not found", + }, + { + name: "non-string host", + params: map[string]any{ + "host": 123, + "port": 6379, + }, + errorString: "redis.client host error: not a string", + }, + { + name: "no port param", + params: map[string]any{ + "host": "localhost", + }, + errorString: "redis.client port error: not found", + }, + { + name: "non-number port", + params: map[string]any{ + "host": "localhost", + "port": "6379", + }, + errorString: "redis.client port error: not a number", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + + registration, ok := module.ModuleRegistry["redis.client"] + if !ok { + t.Fatalf("redis.client module not registered") + } + + moduleInstance, err := registration.New(config.ModuleConfig{ + Id: "test", + Type: "redis.client", + Params: test.params, + }) + + if err != nil { + if test.errorString != err.Error() { + t.Fatalf("redis.client got error '%s', expected '%s'", err.Error(), test.errorString) + } + return + } + + err = moduleInstance.Start(t.Context()) + + if err == nil { + t.Fatalf("redis.client expected to fail") + } + + if err.Error() != test.errorString { + t.Fatalf("redis.client got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +}