3
0

upd(messaging): tests import

This commit is contained in:
Tit Petric
2019-04-03 15:02:11 +02:00
parent 7e8cb4b04a
commit 8315c76b75
3 changed files with 30 additions and 76 deletions
+7 -25
View File
@@ -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")
}
+14 -24
View File
@@ -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
}
+9 -27
View File
@@ -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)
}
}
}