mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-12 03:05:14 +00:00
Streams (#68)
This commit is contained in:
58
streams/handler/handler_test.go
Normal file
58
streams/handler/handler_test.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package handler_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/micro/micro/v3/service/events"
|
||||
"github.com/micro/services/streams/handler"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func testHandler(t *testing.T) *handler.Streams {
|
||||
// connect to the database
|
||||
db, err := gorm.Open(postgres.Open("postgresql://postgres@localhost:5432/postgres?sslmode=disable"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatalf("Error connecting to database: %v", err)
|
||||
}
|
||||
|
||||
// migrate the database
|
||||
if err := db.AutoMigrate(&handler.Token{}); err != nil {
|
||||
t.Fatalf("Error migrating database: %v", err)
|
||||
}
|
||||
|
||||
// clean any data from a previous run
|
||||
if err := db.Exec("TRUNCATE TABLE tokens CASCADE").Error; err != nil {
|
||||
t.Fatalf("Error cleaning database: %v", err)
|
||||
}
|
||||
|
||||
return &handler.Streams{
|
||||
DB: db,
|
||||
Events: new(eventsMock),
|
||||
Time: func() time.Time {
|
||||
return time.Unix(1612787045, 0)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type eventsMock struct {
|
||||
PublishCount int
|
||||
PublishTopic string
|
||||
PublishMessage interface{}
|
||||
|
||||
ConsumeTopic string
|
||||
ConsumeChan <-chan events.Event
|
||||
}
|
||||
|
||||
func (e *eventsMock) Publish(topic string, msg interface{}, opts ...events.PublishOption) error {
|
||||
e.PublishCount++
|
||||
e.PublishTopic = topic
|
||||
e.PublishMessage = msg
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *eventsMock) Consume(topic string, opts ...events.ConsumeOption) (<-chan events.Event, error) {
|
||||
e.ConsumeTopic = topic
|
||||
return e.ConsumeChan, nil
|
||||
}
|
||||
Reference in New Issue
Block a user