refactor tests in model/reaction_test.go and move to testify t… (#12603)

Этот коммит содержится в:
Sheshagiri Rao Mallipedhi
2019-10-04 09:43:27 -07:00
коммит произвёл Ben Schumacher
родитель f97ed668ba
Коммит 8e86ec969e

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

@@ -6,79 +6,159 @@ package model
import ( import (
"strings" "strings"
"testing" "testing"
"github.com/stretchr/testify/require"
) )
func TestReactionIsValid(t *testing.T) { func TestReactionIsValid(t *testing.T) {
reaction := Reaction{ tests := []struct {
UserId: NewId(), // reaction
PostId: NewId(), reaction Reaction
EmojiName: "emoji", // error message to print
CreateAt: GetMillis(), errMsg string
// should there be an error
shouldErr bool
}{
{
reaction: Reaction{
UserId: NewId(),
PostId: NewId(),
EmojiName: "emoji",
CreateAt: GetMillis(),
},
errMsg: "",
shouldErr: false,
},
{
reaction: Reaction{
UserId: "",
PostId: NewId(),
EmojiName: "emoji",
CreateAt: GetMillis(),
},
errMsg: "user id should be invalid",
shouldErr: true,
},
{
reaction: Reaction{
UserId: "1234garbage",
PostId: NewId(),
EmojiName: "emoji",
CreateAt: GetMillis(),
},
errMsg: "user id should be invalid",
shouldErr: true,
},
{
reaction: Reaction{
UserId: NewId(),
PostId: "",
EmojiName: "emoji",
CreateAt: GetMillis(),
},
errMsg: "post id should be invalid",
shouldErr: true,
},
{
reaction: Reaction{
UserId: NewId(),
PostId: "1234garbage",
EmojiName: "emoji",
CreateAt: GetMillis(),
},
errMsg: "post id should be invalid",
shouldErr: true,
},
{
reaction: Reaction{
UserId: NewId(),
PostId: NewId(),
EmojiName: strings.Repeat("a", 64),
CreateAt: GetMillis(),
},
errMsg: "",
shouldErr: false,
},
{
reaction: Reaction{
UserId: NewId(),
PostId: NewId(),
EmojiName: "emoji-",
CreateAt: GetMillis(),
},
errMsg: "",
shouldErr: false,
},
{
reaction: Reaction{
UserId: NewId(),
PostId: NewId(),
EmojiName: "emoji_",
CreateAt: GetMillis(),
},
errMsg: "",
shouldErr: false,
},
{
reaction: Reaction{
UserId: NewId(),
PostId: NewId(),
EmojiName: "+1",
CreateAt: GetMillis(),
},
errMsg: "",
shouldErr: false,
},
{
reaction: Reaction{
UserId: NewId(),
PostId: NewId(),
EmojiName: "emoji:",
CreateAt: GetMillis(),
},
errMsg: "",
shouldErr: true,
},
{
reaction: Reaction{
UserId: NewId(),
PostId: NewId(),
EmojiName: "",
CreateAt: GetMillis(),
},
errMsg: "emoji name should be invalid",
shouldErr: true,
},
{
reaction: Reaction{
UserId: NewId(),
PostId: NewId(),
EmojiName: strings.Repeat("a", 65),
CreateAt: GetMillis(),
},
errMsg: "emoji name should be invalid",
shouldErr: true,
},
{
reaction: Reaction{
UserId: NewId(),
PostId: NewId(),
EmojiName: "emoji",
CreateAt: 0,
},
errMsg: "create at should be invalid",
shouldErr: true,
},
} }
if err := reaction.IsValid(); err != nil { for _, test := range tests {
t.Fatal(err) err := test.reaction.IsValid()
} if test.shouldErr {
// there should be an error here
reaction.UserId = "" require.NotNil(t, err, test.errMsg)
if err := reaction.IsValid(); err == nil { } else {
t.Fatal("user id should be invalid") // err should be nil here
} require.Nil(t, err, test.errMsg)
}
reaction.UserId = "1234garbage"
if err := reaction.IsValid(); err == nil {
t.Fatal("user id should be invalid")
}
reaction.UserId = NewId()
reaction.PostId = ""
if err := reaction.IsValid(); err == nil {
t.Fatal("post id should be invalid")
}
reaction.PostId = "1234garbage"
if err := reaction.IsValid(); err == nil {
t.Fatal("post id should be invalid")
}
reaction.PostId = NewId()
reaction.EmojiName = strings.Repeat("a", 64)
if err := reaction.IsValid(); err != nil {
t.Fatal(err)
}
reaction.EmojiName = "emoji-"
if err := reaction.IsValid(); err != nil {
t.Fatal(err)
}
reaction.EmojiName = "emoji_"
if err := reaction.IsValid(); err != nil {
t.Fatal(err)
}
reaction.EmojiName = "+1"
if err := reaction.IsValid(); err != nil {
t.Fatal(err)
}
reaction.EmojiName = "emoji:"
if err := reaction.IsValid(); err == nil {
t.Fatal(err)
}
reaction.EmojiName = ""
if err := reaction.IsValid(); err == nil {
t.Fatal("emoji name should be invalid")
}
reaction.EmojiName = strings.Repeat("a", 65)
if err := reaction.IsValid(); err == nil {
t.Fatal("emoji name should be invalid")
}
reaction.CreateAt = 0
if err := reaction.IsValid(); err == nil {
t.Fatal("create at should be invalid")
} }
} }