2
0

lfs_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. "gogs.io/gogs/internal/lfsutil"
  12. )
  13. func Test_lfs(t *testing.T) {
  14. if testing.Short() {
  15. t.Skip()
  16. }
  17. t.Parallel()
  18. tables := []interface{}{new(LFSObject)}
  19. db := &lfs{
  20. DB: initTestDB(t, "lfs", tables...),
  21. }
  22. for _, tc := range []struct {
  23. name string
  24. test func(*testing.T, *lfs)
  25. }{
  26. {"CreateObject", test_lfs_CreateObject},
  27. {"GetObjectByOID", test_lfs_GetObjectByOID},
  28. {"GetObjectsByOIDs", test_lfs_GetObjectsByOIDs},
  29. } {
  30. t.Run(tc.name, func(t *testing.T) {
  31. t.Cleanup(func() {
  32. err := clearTables(t, db.DB, tables...)
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. })
  37. tc.test(t, db)
  38. })
  39. }
  40. }
  41. func test_lfs_CreateObject(t *testing.T, db *lfs) {
  42. // Create first LFS object
  43. repoID := int64(1)
  44. oid := lfsutil.OID("ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f")
  45. err := db.CreateObject(repoID, oid, 12, lfsutil.StorageLocal)
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. // Get it back and check the CreatedAt field
  50. object, err := db.GetObjectByOID(repoID, oid)
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. assert.Equal(t, gorm.NowFunc().Format(time.RFC3339), object.CreatedAt.Format(time.RFC3339))
  55. // Try create second LFS object with same oid should fail
  56. err = db.CreateObject(repoID, oid, 12, lfsutil.StorageLocal)
  57. assert.Error(t, err)
  58. }
  59. func test_lfs_GetObjectByOID(t *testing.T, db *lfs) {
  60. // Create a LFS object
  61. repoID := int64(1)
  62. oid := lfsutil.OID("ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f")
  63. err := db.CreateObject(repoID, oid, 12, lfsutil.StorageLocal)
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. // We should be able to get it back
  68. _, err = db.GetObjectByOID(repoID, oid)
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. // Try to get a non-existent object
  73. _, err = db.GetObjectByOID(repoID, "bad_oid")
  74. expErr := ErrLFSObjectNotExist{args: errutil.Args{"repoID": repoID, "oid": lfsutil.OID("bad_oid")}}
  75. assert.Equal(t, expErr, err)
  76. }
  77. func test_lfs_GetObjectsByOIDs(t *testing.T, db *lfs) {
  78. // Create two LFS objects
  79. repoID := int64(1)
  80. oid1 := lfsutil.OID("ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f")
  81. oid2 := lfsutil.OID("ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64g")
  82. err := db.CreateObject(repoID, oid1, 12, lfsutil.StorageLocal)
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. err = db.CreateObject(repoID, oid2, 12, lfsutil.StorageLocal)
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. // We should be able to get them back and ignore non-existent ones
  91. objects, err := db.GetObjectsByOIDs(repoID, oid1, oid2, "bad_oid")
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. assert.Equal(t, 2, len(objects), "number of objects")
  96. assert.Equal(t, repoID, objects[0].RepoID)
  97. assert.Equal(t, oid1, objects[0].OID)
  98. assert.Equal(t, repoID, objects[1].RepoID)
  99. assert.Equal(t, oid2, objects[1].OID)
  100. }