mirror of
https://github.com/grafana/grafana.git
synced 2025-07-29 17:32:47 +08:00

* streamline initialization of test databases, support on-disk sqlite test db * clean up test databases * introduce testsuite helper * use testsuite everywhere we use a test db * update documentation * improve error handling * disable entity integration test until we can figure out locking error
106 lines
2.8 KiB
Go
106 lines
2.8 KiB
Go
package anonstore
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/db"
|
|
"github.com/grafana/grafana/pkg/tests/testsuite"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
testsuite.Run(m)
|
|
}
|
|
|
|
func TestIntegrationAnonStore_DeleteDevicesOlderThan(t *testing.T) {
|
|
store := db.InitTestDB(t)
|
|
anonDBStore := ProvideAnonDBStore(store, 0)
|
|
const keepFor = time.Hour * 24 * 61
|
|
|
|
anonDevice := &Device{
|
|
DeviceID: "32mdo31deeqwes",
|
|
ClientIP: "10.30.30.2",
|
|
UserAgent: "test",
|
|
UpdatedAt: time.Now().Add(-keepFor).Add(-time.Hour),
|
|
}
|
|
|
|
err := anonDBStore.CreateOrUpdateDevice(context.Background(), anonDevice)
|
|
require.NoError(t, err)
|
|
|
|
anonDevice.DeviceID = "keep"
|
|
anonDevice.UpdatedAt = time.Now().Add(-time.Hour)
|
|
|
|
err = anonDBStore.CreateOrUpdateDevice(context.Background(), anonDevice)
|
|
require.NoError(t, err)
|
|
|
|
from := time.Now().Add(-2 * keepFor)
|
|
to := time.Now()
|
|
|
|
count, err := anonDBStore.CountDevices(context.Background(), from, to)
|
|
require.NoError(t, err)
|
|
require.Equal(t, int64(2), count)
|
|
|
|
err = anonDBStore.DeleteDevicesOlderThan(context.Background(), time.Now().Add(-keepFor))
|
|
require.NoError(t, err)
|
|
|
|
devices, err := anonDBStore.ListDevices(context.Background(), &from, &to)
|
|
require.NoError(t, err)
|
|
require.Equal(t, 1, len(devices))
|
|
assert.Equal(t, "keep", devices[0].DeviceID)
|
|
}
|
|
|
|
func TestIntegrationBeyondDeviceLimit(t *testing.T) {
|
|
store := db.InitTestDB(t)
|
|
anonDBStore := ProvideAnonDBStore(store, 1)
|
|
|
|
anonDevice := &Device{
|
|
DeviceID: "32mdo31deeqwes",
|
|
ClientIP: "10.30.30.2",
|
|
UserAgent: "test",
|
|
UpdatedAt: time.Now().Add(-time.Hour),
|
|
}
|
|
|
|
err := anonDBStore.CreateOrUpdateDevice(context.Background(), anonDevice)
|
|
require.NoError(t, err)
|
|
|
|
anonDevice.DeviceID = "keep"
|
|
anonDevice.UpdatedAt = time.Now().Add(-time.Hour)
|
|
|
|
err = anonDBStore.CreateOrUpdateDevice(context.Background(), anonDevice)
|
|
require.ErrorIs(t, err, ErrDeviceLimitReached)
|
|
}
|
|
|
|
func TestIntegrationAnonStore_DeleteDevice(t *testing.T) {
|
|
store := db.InitTestDB(t)
|
|
anonDBStore := ProvideAnonDBStore(store, 0)
|
|
const keepFor = time.Hour * 24 * 61
|
|
|
|
anonDevice := &Device{
|
|
DeviceID: "32mdo31deeqwes",
|
|
ClientIP: "10.30.30.2",
|
|
UserAgent: "test",
|
|
UpdatedAt: time.Now().Add(-keepFor).Add(-time.Hour),
|
|
}
|
|
|
|
err := anonDBStore.CreateOrUpdateDevice(context.Background(), anonDevice)
|
|
require.NoError(t, err)
|
|
|
|
from := time.Now().Add(-2 * keepFor)
|
|
to := time.Now()
|
|
|
|
count, err := anonDBStore.CountDevices(context.Background(), from, to)
|
|
require.NoError(t, err)
|
|
require.Equal(t, int64(1), count)
|
|
|
|
err = anonDBStore.DeleteDevice(context.Background(), "32mdo31deeqwes")
|
|
require.NoError(t, err)
|
|
|
|
devices, err := anonDBStore.ListDevices(context.Background(), &from, &to)
|
|
require.NoError(t, err)
|
|
require.Equal(t, 0, len(devices))
|
|
}
|