Added tests

Signed-off-by: Alexey Terentyev <axifnx@gmail.com>
This commit is contained in:
Alexey Terentyev 2018-06-16 07:15:35 +03:00
parent 42096c63e8
commit f2546a76d9
No known key found for this signature in database
GPG Key ID: 60D6C550AEEBB467
2 changed files with 28 additions and 3 deletions

View File

@ -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)
}

View File

@ -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
}