2020-04-07 09:41:12 +00:00
|
|
|
package dao
|
2019-12-13 15:43:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-11-23 11:09:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/random"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
2020-04-03 06:49:01 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-12-13 15:43:46 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-04-03 06:49:01 +00:00
|
|
|
func TestCachedCachedDao(t *testing.T) {
|
|
|
|
store := storage.NewMemoryStore()
|
|
|
|
// Persistent DAO to check for backing storage.
|
2021-03-25 19:32:16 +00:00
|
|
|
pdao := NewSimple(store, false)
|
2020-04-07 09:41:12 +00:00
|
|
|
assert.NotEqual(t, store, pdao.Store)
|
2020-04-03 06:49:01 +00:00
|
|
|
// Cached DAO.
|
2020-04-07 09:41:12 +00:00
|
|
|
cdao := NewCached(pdao)
|
|
|
|
cdaoDao := cdao.DAO.(*Simple)
|
|
|
|
assert.NotEqual(t, store, cdaoDao.Store)
|
|
|
|
assert.NotEqual(t, pdao.Store, cdaoDao.Store)
|
2020-04-03 06:49:01 +00:00
|
|
|
|
|
|
|
// Cached cached DAO.
|
2020-04-07 09:41:12 +00:00
|
|
|
ccdao := NewCached(cdao)
|
|
|
|
ccdaoDao := ccdao.DAO.(*Cached)
|
|
|
|
intDao := ccdaoDao.DAO.(*Simple)
|
|
|
|
assert.NotEqual(t, store, intDao.Store)
|
|
|
|
assert.NotEqual(t, pdao.Store, intDao.Store)
|
|
|
|
assert.NotEqual(t, cdaoDao.Store, intDao.Store)
|
2020-04-03 06:49:01 +00:00
|
|
|
|
2020-06-18 10:50:30 +00:00
|
|
|
id := int32(random.Int(0, 1024))
|
2020-04-03 06:49:01 +00:00
|
|
|
key := []byte("qwerty")
|
2021-03-05 14:06:54 +00:00
|
|
|
si := state.StorageItem("poiuyt")
|
2020-06-18 10:50:30 +00:00
|
|
|
require.NoError(t, ccdao.PutStorageItem(id, key, si))
|
|
|
|
resi := ccdao.GetStorageItem(id, key)
|
2020-04-03 06:49:01 +00:00
|
|
|
assert.Equal(t, si, resi)
|
|
|
|
|
2020-06-18 10:50:30 +00:00
|
|
|
resi = cdao.GetStorageItem(id, key)
|
2021-03-05 14:06:54 +00:00
|
|
|
assert.Equal(t, state.StorageItem(nil), resi)
|
2020-06-18 10:50:30 +00:00
|
|
|
resi = pdao.GetStorageItem(id, key)
|
2021-03-05 14:06:54 +00:00
|
|
|
assert.Equal(t, state.StorageItem(nil), resi)
|
2020-04-03 06:49:01 +00:00
|
|
|
|
|
|
|
cnt, err := ccdao.Persist()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, cnt)
|
2020-06-18 10:50:30 +00:00
|
|
|
resi = cdao.GetStorageItem(id, key)
|
2020-04-03 06:49:01 +00:00
|
|
|
assert.Equal(t, si, resi)
|
2020-06-18 10:50:30 +00:00
|
|
|
resi = pdao.GetStorageItem(id, key)
|
2021-03-05 14:06:54 +00:00
|
|
|
assert.Equal(t, state.StorageItem(nil), resi)
|
2020-04-03 06:49:01 +00:00
|
|
|
|
|
|
|
cnt, err = cdao.Persist()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, cnt)
|
2020-06-18 10:50:30 +00:00
|
|
|
resi = pdao.GetStorageItem(id, key)
|
2020-04-03 06:49:01 +00:00
|
|
|
assert.Equal(t, si, resi)
|
|
|
|
}
|