repos_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2020 The Gogs 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 db
  5. import (
  6. "testing"
  7. "time"
  8. "github.com/jinzhu/gorm"
  9. "github.com/stretchr/testify/assert"
  10. "gogs.io/gogs/internal/errutil"
  11. )
  12. func Test_repos(t *testing.T) {
  13. if testing.Short() {
  14. t.Skip()
  15. }
  16. t.Parallel()
  17. tables := []interface{}{new(Repository)}
  18. db := &repos{
  19. DB: initTestDB(t, "repos", tables...),
  20. }
  21. for _, tc := range []struct {
  22. name string
  23. test func(*testing.T, *repos)
  24. }{
  25. {"create", test_repos_create},
  26. {"GetByName", test_repos_GetByName},
  27. } {
  28. t.Run(tc.name, func(t *testing.T) {
  29. t.Cleanup(func() {
  30. err := clearTables(t, db.DB, tables...)
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. })
  35. tc.test(t, db)
  36. })
  37. }
  38. }
  39. func test_repos_create(t *testing.T, db *repos) {
  40. t.Run("name not allowed", func(t *testing.T) {
  41. _, err := db.create(1, createRepoOpts{
  42. Name: "my.git",
  43. })
  44. expErr := ErrNameNotAllowed{args: errutil.Args{"reason": "reserved", "pattern": "*.git"}}
  45. assert.Equal(t, expErr, err)
  46. })
  47. t.Run("already exists", func(t *testing.T) {
  48. _, err := db.create(1, createRepoOpts{
  49. Name: "repo1",
  50. })
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. _, err = db.create(1, createRepoOpts{
  55. Name: "repo1",
  56. })
  57. expErr := ErrRepoAlreadyExist{args: errutil.Args{"ownerID": int64(1), "name": "repo1"}}
  58. assert.Equal(t, expErr, err)
  59. })
  60. repo, err := db.create(1, createRepoOpts{
  61. Name: "repo2",
  62. })
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. repo, err = db.GetByName(repo.OwnerID, repo.Name)
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. assert.Equal(t, gorm.NowFunc().Format(time.RFC3339), repo.Created.UTC().Format(time.RFC3339))
  71. }
  72. func test_repos_GetByName(t *testing.T, db *repos) {
  73. repo, err := db.create(1, createRepoOpts{
  74. Name: "repo1",
  75. })
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. _, err = db.GetByName(repo.OwnerID, repo.Name)
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. _, err = db.GetByName(1, "bad_name")
  84. expErr := ErrRepoNotExist{args: errutil.Args{"ownerID": int64(1), "name": "bad_name"}}
  85. assert.Equal(t, expErr, err)
  86. }