aboutsummaryrefslogtreecommitdiff
path: root/internal/db/lfs_test.go
blob: 49d94406779ed3331303c45ff489ade80f050dd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2020 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package db

import (
	"testing"
	"time"

	"github.com/jinzhu/gorm"
	"github.com/stretchr/testify/assert"

	"gogs.io/gogs/internal/errutil"
	"gogs.io/gogs/internal/lfsutil"
)

func Test_lfs(t *testing.T) {
	if testing.Short() {
		t.Skip()
	}

	t.Parallel()

	tables := []interface{}{new(LFSObject)}
	db := &lfs{
		DB: initTestDB(t, "lfs", tables...),
	}

	for _, tc := range []struct {
		name string
		test func(*testing.T, *lfs)
	}{
		{"CreateObject", test_lfs_CreateObject},
		{"GetObjectByOID", test_lfs_GetObjectByOID},
		{"GetObjectsByOIDs", test_lfs_GetObjectsByOIDs},
	} {
		t.Run(tc.name, func(t *testing.T) {
			t.Cleanup(func() {
				err := clearTables(t, db.DB, tables...)
				if err != nil {
					t.Fatal(err)
				}
			})
			tc.test(t, db)
		})
	}
}

func test_lfs_CreateObject(t *testing.T, db *lfs) {
	// Create first LFS object
	repoID := int64(1)
	oid := lfsutil.OID("ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f")
	err := db.CreateObject(repoID, oid, 12, lfsutil.StorageLocal)
	if err != nil {
		t.Fatal(err)
	}

	// Get it back and check the CreatedAt field
	object, err := db.GetObjectByOID(repoID, oid)
	if err != nil {
		t.Fatal(err)
	}
	assert.Equal(t, gorm.NowFunc().Format(time.RFC3339), object.CreatedAt.Format(time.RFC3339))

	// Try create second LFS object with same oid should fail
	err = db.CreateObject(repoID, oid, 12, lfsutil.StorageLocal)
	assert.Error(t, err)
}

func test_lfs_GetObjectByOID(t *testing.T, db *lfs) {
	// Create a LFS object
	repoID := int64(1)
	oid := lfsutil.OID("ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f")
	err := db.CreateObject(repoID, oid, 12, lfsutil.StorageLocal)
	if err != nil {
		t.Fatal(err)
	}

	// We should be able to get it back
	_, err = db.GetObjectByOID(repoID, oid)
	if err != nil {
		t.Fatal(err)
	}

	// Try to get a non-existent object
	_, err = db.GetObjectByOID(repoID, "bad_oid")
	expErr := ErrLFSObjectNotExist{args: errutil.Args{"repoID": repoID, "oid": lfsutil.OID("bad_oid")}}
	assert.Equal(t, expErr, err)
}

func test_lfs_GetObjectsByOIDs(t *testing.T, db *lfs) {
	// Create two LFS objects
	repoID := int64(1)
	oid1 := lfsutil.OID("ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f")
	oid2 := lfsutil.OID("ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64g")
	err := db.CreateObject(repoID, oid1, 12, lfsutil.StorageLocal)
	if err != nil {
		t.Fatal(err)
	}
	err = db.CreateObject(repoID, oid2, 12, lfsutil.StorageLocal)
	if err != nil {
		t.Fatal(err)
	}

	// We should be able to get them back and ignore non-existent ones
	objects, err := db.GetObjectsByOIDs(repoID, oid1, oid2, "bad_oid")
	if err != nil {
		t.Fatal(err)
	}
	assert.Equal(t, 2, len(objects), "number of objects")

	assert.Equal(t, repoID, objects[0].RepoID)
	assert.Equal(t, oid1, objects[0].OID)

	assert.Equal(t, repoID, objects[1].RepoID)
	assert.Equal(t, oid2, objects[1].OID)
}