lfs_test.go 3.0 KB

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