repos_test.go 2.1 KB

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