From 8315c76b75d2a70e84b88a87d76f516695e7c8d5 Mon Sep 17 00:00:00 2001 From: Tit Petric Date: Wed, 3 Apr 2019 15:02:11 +0200 Subject: [PATCH] upd(messaging): tests import --- messaging/internal/service/channel_test.go | 32 ++++-------------- messaging/internal/service/main_test.go | 38 ++++++++-------------- messaging/internal/service/message_test.go | 36 +++++--------------- 3 files changed, 30 insertions(+), 76 deletions(-) diff --git a/messaging/internal/service/channel_test.go b/messaging/internal/service/channel_test.go index e0747f077..d6450780d 100644 --- a/messaging/internal/service/channel_test.go +++ b/messaging/internal/service/channel_test.go @@ -1,3 +1,5 @@ +// +build unit + package service import ( @@ -6,40 +8,20 @@ import ( "testing" "github.com/crusttech/crust/internal/auth" + "github.com/crusttech/crust/internal/test" "github.com/crusttech/crust/messaging/types" systemTypes "github.com/crusttech/crust/system/types" ) -// func TestChannelCreation(t *testing.T) { -// mockCtrl := gomock.NewController(t) -// defer mockCtrl.Finish() -// -// chRpoMock := NewMockRepository(mockCtrl) -// chRpoMock.EXPECT().WithCtx(gomock.Any()).AnyTimes().Return(chRpoMock) -// chRpoMock.EXPECT(). -// FindUserByID(usr.ID). -// Times(1). -// Return(usr, nil) -// -// svc := channel{ -// channel: -// } -// -// svc.Create() -// } - func TestChannelNameTooShort(t *testing.T) { - // mockCtrl := gomock.NewController(t) - // defer mockCtrl.Finish() - - ctx := context.TODO() - auth.SetIdentityToContext(ctx, &systemTypes.User{}) + ctx := context.Background() + ctx = auth.SetIdentityToContext(ctx, &systemTypes.User{}) svc := channel{db: &mockDB{}, ctx: ctx} e := func(out *types.Channel, err error) error { return err } longName := strings.Repeat("X", settingsChannelNameLength+1) - assert(t, e(svc.Create(&types.Channel{})) != nil, "Should not allow to create unnamed channels") - assert(t, e(svc.Create(&types.Channel{Name: longName})) != nil, "Should not allow to create channel with really long name") + test.Assert(t, e(svc.Create(&types.Channel{})) != nil, "Should not allow to create unnamed channels") + test.Assert(t, e(svc.Create(&types.Channel{Name: longName})) != nil, "Should not allow to create channel with really long name") } diff --git a/messaging/internal/service/main_test.go b/messaging/internal/service/main_test.go index eb2cf71be..114a2dbdf 100644 --- a/messaging/internal/service/main_test.go +++ b/messaging/internal/service/main_test.go @@ -1,18 +1,19 @@ +// +build integration external + package service import ( - "fmt" "log" "os" - "runtime" "testing" - "github.com/joho/godotenv" "github.com/namsral/flag" "github.com/titpetric/factory" + messagingMigrate "github.com/crusttech/crust/messaging/db" + "github.com/crusttech/crust/messaging/internal/repository" systemMigrate "github.com/crusttech/crust/system/db" - _ "github.com/crusttech/crust/system/service" + systemService "github.com/crusttech/crust/system/service" ) type mockDB struct{} @@ -20,17 +21,9 @@ type mockDB struct{} func (mockDB) Transaction(callback func() error) error { return callback() } func TestMain(m *testing.M) { - // @todo this is a very optimistic initialization, make it more robust - godotenv.Load("../../.env") - - prefix := "messaging" dsn := "" - - p := func(s string) string { - return prefix + "-" + s - } - - flag.StringVar(&dsn, p("db-dsn"), "crust:crust@tcp(db1:3306)/crust?collation=utf8mb4_general_ci", "DSN for database connection") + new(repository.Flags).Init("messaging") + flag.StringVar(&dsn, "db-dsn", "crust:crust@tcp(crust-db:3306)/crust?collation=utf8mb4_general_ci", "DSN for database connection") flag.Parse() factory.Database.Add("default", dsn) @@ -45,6 +38,10 @@ func TestMain(m *testing.M) { log.Printf("Error running migrations: %+v\n", err) return } + if err := messagingMigrate.Migrate(db); err != nil { + log.Printf("Error running migrations: %+v\n", err) + return + } // clean up tables { @@ -56,15 +53,8 @@ func TestMain(m *testing.M) { } } + systemService.Init() + Init() + os.Exit(m.Run()) } - -func assert(t *testing.T, ok bool, format string, args ...interface{}) bool { - if !ok { - _, file, line, _ := runtime.Caller(1) - caller := fmt.Sprintf("\nAsserted at:%s:%d", file, line) - - t.Fatalf(format+caller, args...) - } - return ok -} diff --git a/messaging/internal/service/message_test.go b/messaging/internal/service/message_test.go index 40996110c..5b5532bf8 100644 --- a/messaging/internal/service/message_test.go +++ b/messaging/internal/service/message_test.go @@ -1,3 +1,5 @@ +// +build unit + package service import ( @@ -6,42 +8,22 @@ import ( "testing" "github.com/crusttech/crust/internal/auth" + "github.com/crusttech/crust/internal/test" "github.com/crusttech/crust/messaging/types" systemTypes "github.com/crusttech/crust/system/types" ) -// func TestChannelCreation(t *testing.T) { -// mockCtrl := gomock.NewController(t) -// defer mockCtrl.Finish() -// -// chRpoMock := NewMockRepository(mockCtrl) -// chRpoMock.EXPECT().WithCtx(gomock.Any()).AnyTimes().Return(chRpoMock) -// chRpoMock.EXPECT(). -// FindUserByID(usr.ID). -// Times(1). -// Return(usr, nil) -// -// svc := channel{ -// channel: -// } -// -// svc.Create() -// } - func TestMessageLength(t *testing.T) { - // mockCtrl := gomock.NewController(t) - // defer mockCtrl.Finish() - - ctx := context.TODO() - auth.SetIdentityToContext(ctx, &systemTypes.User{}) + ctx := context.Background() + ctx = auth.SetIdentityToContext(ctx, &systemTypes.User{}) svc := message{db: &mockDB{}, ctx: ctx} e := func(out *types.Message, err error) error { return err } longText := strings.Repeat("X", settingsMessageBodyLength+1) - assert(t, e(svc.Create(&types.Message{})) != nil, "Should not allow to create unnamed channels") - assert(t, e(svc.Create(&types.Message{Message: longText})) != nil, "Should not allow to create channel with really long name") + test.Assert(t, e(svc.Create(&types.Message{})) != nil, "Should not allow to create empty message") + test.Assert(t, e(svc.Create(&types.Message{Message: longText})) != nil, "Should not allow to create message with really long text") } func TestMentionsExtraction(t *testing.T) { @@ -72,10 +54,10 @@ func TestMentionsExtraction(t *testing.T) { for _, c := range cases { mm = svc.extractMentions(&types.Message{Message: c.text}) - assert(t, len(mm) == len(c.ids), "Number of extracted (%d) and expected (%d) user IDs do not match (%s)", len(mm), len(c.ids), c.text) + test.Assert(t, len(mm) == len(c.ids), "Number of extracted (%d) and expected (%d) user IDs do not match (%s)", len(mm), len(c.ids), c.text) for _, id := range c.ids { - assert(t, len(mm.FindByUserID(id)) == 1, "Owner ID (%d) was not extracted (%s)", id, c.text) + test.Assert(t, len(mm.FindByUserID(id)) == 1, "Owner ID (%d) was not extracted (%s)", id, c.text) } } }