lfs_test.go 3.1 KB

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