code.gitea.io/gitea@v1.19.3/modules/system/appstate_test.go (about) 1 // Copyright 2021 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package system 5 6 import ( 7 "path/filepath" 8 "testing" 9 10 "code.gitea.io/gitea/models/unittest" 11 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestMain(m *testing.M) { 16 unittest.MainTest(m, &unittest.TestOptions{ 17 GiteaRootPath: filepath.Join("..", ".."), 18 FixtureFiles: []string{""}, // load nothing 19 }) 20 } 21 22 type testItem1 struct { 23 Val1 string 24 Val2 int 25 } 26 27 func (*testItem1) Name() string { 28 return "test-item1" 29 } 30 31 type testItem2 struct { 32 K string 33 } 34 35 func (*testItem2) Name() string { 36 return "test-item2" 37 } 38 39 func TestAppStateDB(t *testing.T) { 40 assert.NoError(t, unittest.PrepareTestDatabase()) 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 }