From f2546a76d98266039a729b85c39982391bf75af0 Mon Sep 17 00:00:00 2001 From: Alexey Terentyev Date: Sat, 16 Jun 2018 07:15:35 +0300 Subject: [PATCH] Added tests Signed-off-by: Alexey Terentyev --- models/topic_test.go | 25 +++++++++++++++++++++++++ routers/repo/topic.go | 6 +++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/models/topic_test.go b/models/topic_test.go index 472f4e52d..ecff6e083 100644 --- a/models/topic_test.go +++ b/models/topic_test.go @@ -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) +} diff --git a/routers/repo/topic.go b/routers/repo/topic.go index 2a5dc8fa5..013a5b769 100644 --- a/routers/repo/topic.go +++ b/routers/repo/topic.go @@ -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 }