Added tests
Signed-off-by: Alexey Terentyev <axifnx@gmail.com>
This commit is contained in:
parent
42096c63e8
commit
f2546a76d9
|
|
@ -5,6 +5,7 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
|
@ -55,3 +56,27 @@ func TestAddTopic(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 2, len(topics))
|
||||
}
|
||||
|
||||
func TestTopicValidator(t *testing.T) {
|
||||
assert.True(t, TopicValidator("first-topic"))
|
||||
assert.True(t, TopicValidator("#second-test_topic"))
|
||||
assert.True(t, TopicValidator("third+project+topic.with+max_length"))
|
||||
|
||||
assert.False(t, TopicValidator("$fourth-topic"))
|
||||
assert.False(t, TopicValidator("#fifth,test;topic"))
|
||||
assert.False(t, TopicValidator("#sixth-go+project.topic+with+excess_length"))
|
||||
}
|
||||
|
||||
func TestRemoveDuplicateTopics(t *testing.T) {
|
||||
topics := []string{"first", "second", "eleventh", "third", "fourth", "fifth", "sixth", "second", "seventh",
|
||||
"eleventh", "first", "eighth", "ninth", "sixth", "tenth", "eleventh"}
|
||||
|
||||
expectedSlice := []string{"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh"}
|
||||
|
||||
topics = RemoveDuplicateTopics(topics)
|
||||
assert.Len(t, topics, 11)
|
||||
|
||||
sort.Strings(topics)
|
||||
sort.Strings(expectedSlice)
|
||||
assert.EqualValues(t, expectedSlice, topics)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,10 +30,10 @@ func TopicsPost(ctx *context.Context) {
|
|||
topics = models.RemoveDuplicateTopics(topics)
|
||||
|
||||
if len(topics) > 25 {
|
||||
log.Error(2, "Incorrect number of topics(max 25): %v", )
|
||||
log.Error(2, "Incorrect number of topics(max 25)")
|
||||
ctx.JSON(422, map[string]interface{}{
|
||||
"invalidTopics": topics[:0],
|
||||
"message": ctx.Tr("repo.topic.count_error"),
|
||||
"message": ctx.Tr("repo.topic.count_error"),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
|
@ -49,7 +49,7 @@ func TopicsPost(ctx *context.Context) {
|
|||
log.Error(2, "Invalid topics: %v", invalidTopics)
|
||||
ctx.JSON(422, map[string]interface{}{
|
||||
"invalidTopics": invalidTopics,
|
||||
"message": ctx.Tr("repo.topic.pattern_error"),
|
||||
"message": ctx.Tr("repo.topic.pattern_error"),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user