2023-01-02 15:11:32 +01:00
|
|
|
package sqlite
|
|
|
|
|
|
|
|
import (
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2023-01-02 15:11:32 +01:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2023-04-03 15:09:35 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/management/server/activity"
|
2023-01-02 15:11:32 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewSQLiteStore(t *testing.T) {
|
|
|
|
dataDir := t.TempDir()
|
2023-09-19 18:08:40 +02:00
|
|
|
key, _ := GenerateKey()
|
2024-07-03 11:33:02 +02:00
|
|
|
store, err := NewSQLiteStore(context.Background(), dataDir, key)
|
2023-01-02 15:11:32 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
defer store.Close(context.Background()) //nolint
|
2023-01-02 15:11:32 +01:00
|
|
|
|
|
|
|
accountID := "account_1"
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
2024-07-03 11:33:02 +02:00
|
|
|
_, err = store.Save(context.Background(), &activity.Event{
|
2023-04-03 15:09:35 +02:00
|
|
|
Timestamp: time.Now().UTC(),
|
2023-01-02 15:11:32 +01:00
|
|
|
Activity: activity.PeerAddedByUser,
|
|
|
|
InitiatorID: "user_" + fmt.Sprint(i),
|
|
|
|
TargetID: "peer_" + fmt.Sprint(i),
|
|
|
|
AccountID: accountID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
result, err := store.Get(context.Background(), accountID, 0, 10, false)
|
2023-01-02 15:11:32 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Len(t, result, 10)
|
|
|
|
assert.True(t, result[0].Timestamp.Before(result[len(result)-1].Timestamp))
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
result, err = store.Get(context.Background(), accountID, 0, 5, true)
|
2023-01-02 15:11:32 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Len(t, result, 5)
|
|
|
|
assert.True(t, result[0].Timestamp.After(result[len(result)-1].Timestamp))
|
|
|
|
}
|