|
| 1 | +// Copyright 2021 The Gitea 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 | + |
| 5 | +package appstate |
| 6 | + |
| 7 | +import ( |
| 8 | + "path/filepath" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "code.gitea.io/gitea/models/appstate" |
| 12 | + "code.gitea.io/gitea/models/db" |
| 13 | + |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | +) |
| 16 | + |
| 17 | +func TestMain(m *testing.M) { |
| 18 | + db.MainTest(m, filepath.Join("..", ".."), "") |
| 19 | +} |
| 20 | + |
| 21 | +type testItem1 struct { |
| 22 | + Val1 string |
| 23 | + Val2 int |
| 24 | +} |
| 25 | + |
| 26 | +func (*testItem1) Name() string { |
| 27 | + return "test-item1" |
| 28 | +} |
| 29 | + |
| 30 | +type testItem2 struct { |
| 31 | + K string |
| 32 | +} |
| 33 | + |
| 34 | +func (*testItem2) Name() string { |
| 35 | + return "test-item2" |
| 36 | +} |
| 37 | + |
| 38 | +func TestAppStateDB(t *testing.T) { |
| 39 | + assert.NoError(t, db.PrepareTestDatabase()) |
| 40 | + _ = db.GetEngine(db.DefaultContext).Sync2(new(appstate.AppState)) |
| 41 | + |
| 42 | + as := &DBStore{} |
| 43 | + |
| 44 | + item1 := new(testItem1) |
| 45 | + assert.NoError(t, as.Get(item1)) |
| 46 | + assert.Equal(t, "", item1.Val1) |
| 47 | + assert.EqualValues(t, 0, item1.Val2) |
| 48 | + |
| 49 | + item1 = new(testItem1) |
| 50 | + item1.Val1 = "a" |
| 51 | + item1.Val2 = 2 |
| 52 | + assert.NoError(t, as.Set(item1)) |
| 53 | + |
| 54 | + item2 := new(testItem2) |
| 55 | + item2.K = "V" |
| 56 | + assert.NoError(t, as.Set(item2)) |
| 57 | + |
| 58 | + item1 = new(testItem1) |
| 59 | + assert.NoError(t, as.Get(item1)) |
| 60 | + assert.Equal(t, "a", item1.Val1) |
| 61 | + assert.EqualValues(t, 2, item1.Val2) |
| 62 | + |
| 63 | + item2 = new(testItem2) |
| 64 | + assert.NoError(t, as.Get(item2)) |
| 65 | + assert.Equal(t, "V", item2.K) |
| 66 | +} |
0 commit comments