You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.8 KiB

  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestAddTopic(t *testing.T) {
  10. assert.NoError(t, PrepareTestDatabase())
  11. topics, err := FindTopics(&FindTopicOptions{})
  12. assert.NoError(t, err)
  13. assert.EqualValues(t, 4, len(topics))
  14. topics, err = FindTopics(&FindTopicOptions{
  15. Limit: 2,
  16. })
  17. assert.NoError(t, err)
  18. assert.EqualValues(t, 2, len(topics))
  19. topics, err = FindTopics(&FindTopicOptions{
  20. RepoID: 1,
  21. })
  22. assert.NoError(t, err)
  23. assert.EqualValues(t, 3, len(topics))
  24. assert.NoError(t, SaveTopics(2, "golang"))
  25. topics, err = FindTopics(&FindTopicOptions{})
  26. assert.NoError(t, err)
  27. assert.EqualValues(t, 4, len(topics))
  28. topics, err = FindTopics(&FindTopicOptions{
  29. RepoID: 2,
  30. })
  31. assert.NoError(t, err)
  32. assert.EqualValues(t, 1, len(topics))
  33. assert.NoError(t, SaveTopics(2, "golang", "gitea"))
  34. topic, err := GetTopicByName("gitea")
  35. assert.NoError(t, err)
  36. assert.EqualValues(t, 1, topic.RepoCount)
  37. topics, err = FindTopics(&FindTopicOptions{})
  38. assert.NoError(t, err)
  39. assert.EqualValues(t, 5, len(topics))
  40. topics, err = FindTopics(&FindTopicOptions{
  41. RepoID: 2,
  42. })
  43. assert.NoError(t, err)
  44. assert.EqualValues(t, 2, len(topics))
  45. }
  46. func TestTopicValidator(t *testing.T) {
  47. assert.True(t, ValidateTopic("12345"))
  48. assert.True(t, ValidateTopic("2-test"))
  49. assert.True(t, ValidateTopic("test-3"))
  50. assert.True(t, ValidateTopic("first"))
  51. assert.True(t, ValidateTopic("second-test-topic"))
  52. assert.True(t, ValidateTopic("third-project-topic-with-max-length"))
  53. assert.False(t, ValidateTopic("$fourth-test,topic"))
  54. assert.False(t, ValidateTopic("-fifth-test-topic"))
  55. assert.False(t, ValidateTopic("sixth-go-project-topic-with-excess-length"))
  56. }