本站源代码
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.

78 lines
2.0KB

  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. totalNrOfTopics := 6
  11. repo1NrOfTopics := 3
  12. repo2NrOfTopics := 2
  13. assert.NoError(t, PrepareTestDatabase())
  14. topics, err := FindTopics(&FindTopicOptions{})
  15. assert.NoError(t, err)
  16. assert.EqualValues(t, totalNrOfTopics, len(topics))
  17. topics, err = FindTopics(&FindTopicOptions{
  18. Limit: 2,
  19. })
  20. assert.NoError(t, err)
  21. assert.EqualValues(t, 2, len(topics))
  22. topics, err = FindTopics(&FindTopicOptions{
  23. RepoID: 1,
  24. })
  25. assert.NoError(t, err)
  26. assert.EqualValues(t, repo1NrOfTopics, len(topics))
  27. assert.NoError(t, SaveTopics(2, "golang"))
  28. repo2NrOfTopics = 1
  29. topics, err = FindTopics(&FindTopicOptions{})
  30. assert.NoError(t, err)
  31. assert.EqualValues(t, totalNrOfTopics, len(topics))
  32. topics, err = FindTopics(&FindTopicOptions{
  33. RepoID: 2,
  34. })
  35. assert.NoError(t, err)
  36. assert.EqualValues(t, repo2NrOfTopics, len(topics))
  37. assert.NoError(t, SaveTopics(2, "golang", "gitea"))
  38. repo2NrOfTopics = 2
  39. totalNrOfTopics++
  40. topic, err := GetTopicByName("gitea")
  41. assert.NoError(t, err)
  42. assert.EqualValues(t, 1, topic.RepoCount)
  43. topics, err = FindTopics(&FindTopicOptions{})
  44. assert.NoError(t, err)
  45. assert.EqualValues(t, totalNrOfTopics, len(topics))
  46. topics, err = FindTopics(&FindTopicOptions{
  47. RepoID: 2,
  48. })
  49. assert.NoError(t, err)
  50. assert.EqualValues(t, repo2NrOfTopics, len(topics))
  51. }
  52. func TestTopicValidator(t *testing.T) {
  53. assert.True(t, ValidateTopic("12345"))
  54. assert.True(t, ValidateTopic("2-test"))
  55. assert.True(t, ValidateTopic("test-3"))
  56. assert.True(t, ValidateTopic("first"))
  57. assert.True(t, ValidateTopic("second-test-topic"))
  58. assert.True(t, ValidateTopic("third-project-topic-with-max-length"))
  59. assert.False(t, ValidateTopic("$fourth-test,topic"))
  60. assert.False(t, ValidateTopic("-fifth-test-topic"))
  61. assert.False(t, ValidateTopic("sixth-go-project-topic-with-excess-length"))
  62. }
上海开阖软件有限公司 沪ICP备12045867号-1