2023-10-24 00:18:16 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
2024-01-28 02:02:08 +00:00
|
|
|
"fmt"
|
2023-10-24 00:18:16 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-06-17 00:00:41 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2024-01-11 01:23:36 +00:00
|
|
|
"reichard.io/antholume/config"
|
2024-01-28 02:02:08 +00:00
|
|
|
"reichard.io/antholume/utils"
|
2023-10-24 00:18:16 +00:00
|
|
|
)
|
|
|
|
|
2024-06-17 00:00:41 +00:00
|
|
|
var (
|
2024-08-10 13:26:30 +00:00
|
|
|
userID string = "testUser"
|
|
|
|
userPass string = "testPass"
|
|
|
|
deviceID string = "testDevice"
|
|
|
|
deviceName string = "testDeviceName"
|
|
|
|
documentID string = "testDocument"
|
|
|
|
documentTitle string = "testTitle"
|
|
|
|
documentAuthor string = "testAuthor"
|
|
|
|
documentFilepath string = "./testPath.epub"
|
|
|
|
documentWords int64 = 5000
|
2024-06-17 00:00:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type DatabaseTestSuite struct {
|
|
|
|
suite.Suite
|
2023-10-24 00:18:16 +00:00
|
|
|
dbm *DBManager
|
|
|
|
}
|
|
|
|
|
2024-06-17 00:00:41 +00:00
|
|
|
func TestDatabase(t *testing.T) {
|
|
|
|
suite.Run(t, new(DatabaseTestSuite))
|
|
|
|
}
|
2023-10-24 00:18:16 +00:00
|
|
|
|
2024-06-17 00:00:41 +00:00
|
|
|
// PROGRESS - TODO:
|
|
|
|
// - (q *Queries) GetProgress
|
|
|
|
// - (q *Queries) UpdateProgress
|
|
|
|
|
|
|
|
func (suite *DatabaseTestSuite) SetupTest() {
|
2023-10-24 00:18:16 +00:00
|
|
|
cfg := config.Config{
|
|
|
|
DBType: "memory",
|
|
|
|
}
|
|
|
|
|
2024-06-17 00:00:41 +00:00
|
|
|
suite.dbm = NewMgr(&cfg)
|
2023-10-24 00:18:16 +00:00
|
|
|
|
2024-06-17 00:00:41 +00:00
|
|
|
// Create User
|
|
|
|
rawAuthHash, _ := utils.GenerateToken(64)
|
|
|
|
authHash := fmt.Sprintf("%x", rawAuthHash)
|
|
|
|
_, err := suite.dbm.Queries.CreateUser(suite.dbm.Ctx, CreateUserParams{
|
|
|
|
ID: userID,
|
|
|
|
Pass: &userPass,
|
|
|
|
AuthHash: &authHash,
|
2023-10-24 00:18:16 +00:00
|
|
|
})
|
2024-06-17 00:00:41 +00:00
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
// Create Document
|
|
|
|
_, err = suite.dbm.Queries.UpsertDocument(suite.dbm.Ctx, UpsertDocumentParams{
|
2024-08-10 13:26:30 +00:00
|
|
|
ID: documentID,
|
|
|
|
Title: &documentTitle,
|
|
|
|
Author: &documentAuthor,
|
|
|
|
Filepath: &documentFilepath,
|
|
|
|
Words: &documentWords,
|
2024-06-17 00:00:41 +00:00
|
|
|
})
|
|
|
|
suite.NoError(err)
|
2023-10-24 00:18:16 +00:00
|
|
|
|
2024-06-17 00:00:41 +00:00
|
|
|
// Create Device
|
|
|
|
_, err = suite.dbm.Queries.UpsertDevice(suite.dbm.Ctx, UpsertDeviceParams{
|
|
|
|
ID: deviceID,
|
|
|
|
UserID: userID,
|
|
|
|
DeviceName: deviceName,
|
|
|
|
})
|
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
// Create Activity
|
|
|
|
end := time.Now()
|
|
|
|
start := end.AddDate(0, 0, -9)
|
|
|
|
var counter int64 = 0
|
|
|
|
|
|
|
|
for d := start; d.After(end) == false; d = d.AddDate(0, 0, 1) {
|
|
|
|
counter += 1
|
|
|
|
|
|
|
|
// Add Item
|
|
|
|
activity, err := suite.dbm.Queries.AddActivity(suite.dbm.Ctx, AddActivityParams{
|
|
|
|
DocumentID: documentID,
|
|
|
|
DeviceID: deviceID,
|
|
|
|
UserID: userID,
|
|
|
|
StartTime: d.UTC().Format(time.RFC3339),
|
|
|
|
Duration: 60,
|
|
|
|
StartPercentage: float64(counter) / 100.0,
|
|
|
|
EndPercentage: float64(counter+1) / 100.0,
|
2023-10-24 00:18:16 +00:00
|
|
|
})
|
|
|
|
|
2024-06-17 00:00:41 +00:00
|
|
|
suite.Nil(err, fmt.Sprintf("[%d] should have nil err for add activity", counter))
|
|
|
|
suite.Equal(counter, activity.ID, fmt.Sprintf("[%d] should have correct id for add activity", counter))
|
|
|
|
}
|
2024-02-25 01:45:26 +00:00
|
|
|
|
2024-06-17 00:00:41 +00:00
|
|
|
// Initiate Cache
|
|
|
|
err = suite.dbm.CacheTempTables()
|
|
|
|
suite.NoError(err)
|
2023-10-24 00:18:16 +00:00
|
|
|
}
|
|
|
|
|
2024-06-17 00:00:41 +00:00
|
|
|
// DEVICES - TODO:
|
|
|
|
// - (q *Queries) GetDevice
|
|
|
|
// - (q *Queries) GetDevices
|
|
|
|
// - (q *Queries) UpsertDevice
|
|
|
|
func (suite *DatabaseTestSuite) TestDevice() {
|
|
|
|
testDevice := "dev123"
|
|
|
|
device, err := suite.dbm.Queries.UpsertDevice(suite.dbm.Ctx, UpsertDeviceParams{
|
|
|
|
ID: testDevice,
|
|
|
|
UserID: userID,
|
|
|
|
DeviceName: deviceName,
|
2023-10-24 00:18:16 +00:00
|
|
|
})
|
|
|
|
|
2024-06-17 00:00:41 +00:00
|
|
|
suite.Nil(err, "should have nil err")
|
|
|
|
suite.Equal(testDevice, device.ID, "should have device id")
|
|
|
|
suite.Equal(userID, device.UserID, "should have user id")
|
|
|
|
suite.Equal(deviceName, device.DeviceName, "should have device name")
|
|
|
|
}
|
2023-10-24 00:18:16 +00:00
|
|
|
|
2024-06-17 00:00:41 +00:00
|
|
|
// ACTIVITY - TODO:
|
|
|
|
// - (q *Queries) AddActivity
|
|
|
|
// - (q *Queries) GetActivity
|
|
|
|
// - (q *Queries) GetLastActivity
|
|
|
|
func (suite *DatabaseTestSuite) TestActivity() {
|
|
|
|
// Validate Exists
|
|
|
|
existsRows, err := suite.dbm.Queries.GetActivity(suite.dbm.Ctx, GetActivityParams{
|
|
|
|
UserID: userID,
|
|
|
|
Offset: 0,
|
|
|
|
Limit: 50,
|
|
|
|
})
|
2023-10-24 00:18:16 +00:00
|
|
|
|
2024-06-17 00:00:41 +00:00
|
|
|
suite.Nil(err, "should have nil err for get activity")
|
|
|
|
suite.Len(existsRows, 10, "should have correct number of rows get activity")
|
2023-10-24 00:18:16 +00:00
|
|
|
|
2024-06-17 00:00:41 +00:00
|
|
|
// Validate Doesn't Exist
|
|
|
|
doesntExistsRows, err := suite.dbm.Queries.GetActivity(suite.dbm.Ctx, GetActivityParams{
|
|
|
|
UserID: userID,
|
|
|
|
DocumentID: "unknownDoc",
|
|
|
|
DocFilter: true,
|
|
|
|
Offset: 0,
|
|
|
|
Limit: 50,
|
2023-10-24 00:18:16 +00:00
|
|
|
})
|
|
|
|
|
2024-06-17 00:00:41 +00:00
|
|
|
suite.Nil(err, "should have nil err for get activity")
|
|
|
|
suite.Len(doesntExistsRows, 0, "should have no rows")
|
|
|
|
}
|
2023-10-24 00:18:16 +00:00
|
|
|
|
2024-06-17 00:00:41 +00:00
|
|
|
// MISC - TODO:
|
|
|
|
// - (q *Queries) AddMetadata
|
|
|
|
// - (q *Queries) GetDailyReadStats
|
|
|
|
// - (q *Queries) GetDatabaseInfo
|
|
|
|
// - (q *Queries) UpdateSettings
|
|
|
|
func (suite *DatabaseTestSuite) TestGetDailyReadStats() {
|
|
|
|
readStats, err := suite.dbm.Queries.GetDailyReadStats(suite.dbm.Ctx, userID)
|
|
|
|
|
|
|
|
suite.Nil(err, "should have nil err")
|
|
|
|
suite.Len(readStats, 30, "should have length of 30")
|
|
|
|
|
|
|
|
// Validate 1 Minute / Day - Last 10 Days
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
stat := readStats[i]
|
|
|
|
suite.Equal(int64(1), stat.MinutesRead, "should have one minute read")
|
|
|
|
}
|
2023-10-24 00:18:16 +00:00
|
|
|
|
2024-06-17 00:00:41 +00:00
|
|
|
// Validate 0 Minute / Day - Remaining 20 Days
|
|
|
|
for i := 10; i < 30; i++ {
|
|
|
|
stat := readStats[i]
|
|
|
|
suite.Equal(int64(0), stat.MinutesRead, "should have zero minutes read")
|
|
|
|
}
|
2023-10-24 00:18:16 +00:00
|
|
|
}
|