Convert model/channel_test.go validation calls into assert/require calls (#12605)

* Convert model/channel_test.go t.Fatal calls into assert/require calls

* Removed blank spaces

* Changes based on suggestions coming from related task https://github.com/mattermost/mattermost-server/pull/12602#pullrequestreview-296859312
Этот коммит содержится в:
George Felix
2019-10-13 21:38:58 +02:00
коммит произвёл Jesús Espino
родитель babbe087ff
Коммит b860355f30

Просмотреть файл

@@ -6,6 +6,8 @@ package model
import ( import (
"strings" "strings"
"testing" "testing"
"github.com/stretchr/testify/require"
) )
func TestChannelJson(t *testing.T) { func TestChannelJson(t *testing.T) {
@@ -13,27 +15,21 @@ func TestChannelJson(t *testing.T) {
json := o.ToJson() json := o.ToJson()
ro := ChannelFromJson(strings.NewReader(json)) ro := ChannelFromJson(strings.NewReader(json))
if o.Id != ro.Id { require.Equal(t, o.Id, ro.Id)
t.Fatal("Ids do not match")
}
p := ChannelPatch{Name: new(string)} p := ChannelPatch{Name: new(string)}
*p.Name = NewId() *p.Name = NewId()
json = p.ToJson() json = p.ToJson()
rp := ChannelPatchFromJson(strings.NewReader(json)) rp := ChannelPatchFromJson(strings.NewReader(json))
if *p.Name != *rp.Name { require.Equal(t, *p.Name, *rp.Name)
t.Fatal("names do not match")
}
} }
func TestChannelCopy(t *testing.T) { func TestChannelCopy(t *testing.T) {
o := Channel{Id: NewId(), Name: NewId()} o := Channel{Id: NewId(), Name: NewId()}
ro := o.DeepCopy() ro := o.DeepCopy()
if o.Id != ro.Id { require.Equal(t, o.Id, ro.Id, "Ids do not match")
t.Fatal("Ids do not match")
}
} }
func TestChannelPatch(t *testing.T) { func TestChannelPatch(t *testing.T) {
@@ -47,97 +43,57 @@ func TestChannelPatch(t *testing.T) {
o := Channel{Id: NewId(), Name: NewId()} o := Channel{Id: NewId(), Name: NewId()}
o.Patch(p) o.Patch(p)
if *p.Name != o.Name { require.Equal(t, *p.Name, o.Name)
t.Fatal("do not match") require.Equal(t, *p.DisplayName, o.DisplayName)
} require.Equal(t, *p.Header, o.Header)
if *p.DisplayName != o.DisplayName { require.Equal(t, *p.Purpose, o.Purpose)
t.Fatal("do not match") require.Equal(t, *p.GroupConstrained, *o.GroupConstrained)
}
if *p.Header != o.Header {
t.Fatal("do not match")
}
if *p.Purpose != o.Purpose {
t.Fatal("do not match")
}
if *p.GroupConstrained != *o.GroupConstrained {
t.Fatalf("expected %v got %v", *p.GroupConstrained, *o.GroupConstrained)
}
} }
func TestChannelIsValid(t *testing.T) { func TestChannelIsValid(t *testing.T) {
o := Channel{} o := Channel{}
if err := o.IsValid(); err == nil { require.Error(t, o.IsValid())
t.Fatal("should be invalid")
}
o.Id = NewId() o.Id = NewId()
if err := o.IsValid(); err == nil { require.Error(t, o.IsValid())
t.Fatal("should be invalid")
}
o.CreateAt = GetMillis() o.CreateAt = GetMillis()
if err := o.IsValid(); err == nil { require.Error(t, o.IsValid())
t.Fatal("should be invalid")
}
o.UpdateAt = GetMillis() o.UpdateAt = GetMillis()
if err := o.IsValid(); err == nil { require.Error(t, o.IsValid())
t.Fatal("should be invalid")
}
o.DisplayName = strings.Repeat("01234567890", 20) o.DisplayName = strings.Repeat("01234567890", 20)
if err := o.IsValid(); err == nil { require.Error(t, o.IsValid())
t.Fatal("should be invalid")
}
o.DisplayName = "1234" o.DisplayName = "1234"
o.Name = "ZZZZZZZ" o.Name = "ZZZZZZZ"
if err := o.IsValid(); err == nil { require.Error(t, o.IsValid())
t.Fatal("should be invalid")
}
o.Name = "zzzzz" o.Name = "zzzzz"
require.Error(t, o.IsValid())
if err := o.IsValid(); err == nil {
t.Fatal("should be invalid")
}
o.Type = "U" o.Type = "U"
if err := o.IsValid(); err == nil { require.Error(t, o.IsValid())
t.Fatal("should be invalid")
}
o.Type = "P" o.Type = "P"
require.Error(t, o.IsValid())
if err := o.IsValid(); err != nil {
t.Fatal(err)
}
o.Header = strings.Repeat("01234567890", 100) o.Header = strings.Repeat("01234567890", 100)
if err := o.IsValid(); err == nil { require.Error(t, o.IsValid())
t.Fatal("should be invalid")
}
o.Header = "1234" o.Header = "1234"
if err := o.IsValid(); err != nil { require.Nil(t, o.IsValid())
t.Fatal(err)
}
o.Purpose = strings.Repeat("01234567890", 30) o.Purpose = strings.Repeat("01234567890", 30)
if err := o.IsValid(); err == nil { require.Error(t, o.IsValid())
t.Fatal("should be invalid")
}
o.Purpose = "1234" o.Purpose = "1234"
if err := o.IsValid(); err != nil { require.Nil(t, o.IsValid())
t.Fatal(err)
}
o.Purpose = strings.Repeat("0123456789", 25) o.Purpose = strings.Repeat("0123456789", 25)
if err := o.IsValid(); err != nil { require.Nil(t, o.IsValid())
t.Fatal(err)
}
} }
func TestChannelPreSave(t *testing.T) { func TestChannelPreSave(t *testing.T) {
@@ -159,15 +115,11 @@ func TestGetGroupDisplayNameFromUsers(t *testing.T) {
users[3] = &User{Username: NewId()} users[3] = &User{Username: NewId()}
name := GetGroupDisplayNameFromUsers(users, true) name := GetGroupDisplayNameFromUsers(users, true)
if len(name) > CHANNEL_NAME_MAX_LENGTH { require.LessOrEqual(t, len(name), CHANNEL_NAME_MAX_LENGTH)
t.Fatal("name too long")
}
} }
func TestGetGroupNameFromUserIds(t *testing.T) { func TestGetGroupNameFromUserIds(t *testing.T) {
name := GetGroupNameFromUserIds([]string{NewId(), NewId(), NewId(), NewId(), NewId()}) name := GetGroupNameFromUserIds([]string{NewId(), NewId(), NewId(), NewId(), NewId()})
if len(name) > CHANNEL_NAME_MAX_LENGTH { require.LessOrEqual(t, len(name), CHANNEL_NAME_MAX_LENGTH)
t.Fatal("name too long")
}
} }