2024-03-11 14:15:11 +05:30
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
2025-02-04 14:53:36 +05:30
|
|
|
"context"
|
2024-03-11 14:15:11 +05:30
|
|
|
"os"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
2025-01-10 18:43:35 +05:30
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2025-02-04 14:53:36 +05:30
|
|
|
"go.signoz.io/signoz/pkg/factory"
|
|
|
|
|
"go.signoz.io/signoz/pkg/factory/providertest"
|
2024-03-11 20:06:59 +05:30
|
|
|
"go.signoz.io/signoz/pkg/query-service/app/dashboards"
|
2024-03-11 14:15:11 +05:30
|
|
|
"go.signoz.io/signoz/pkg/query-service/dao"
|
2025-02-04 14:53:36 +05:30
|
|
|
"go.signoz.io/signoz/pkg/sqlmigration"
|
|
|
|
|
"go.signoz.io/signoz/pkg/sqlmigrator"
|
|
|
|
|
"go.signoz.io/signoz/pkg/sqlstore"
|
|
|
|
|
"go.signoz.io/signoz/pkg/sqlstore/sqlitesqlstore"
|
2024-03-11 14:15:11 +05:30
|
|
|
)
|
|
|
|
|
|
2025-01-10 18:43:35 +05:30
|
|
|
func NewTestSqliteDB(t *testing.T) (testDB *sqlx.DB, testDBFilePath string) {
|
2024-03-11 14:15:11 +05:30
|
|
|
testDBFile, err := os.CreateTemp("", "test-signoz-db-*")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("could not create temp file for test db: %v", err)
|
|
|
|
|
}
|
2025-01-10 18:43:35 +05:30
|
|
|
testDBFilePath = testDBFile.Name()
|
2024-03-11 14:15:11 +05:30
|
|
|
t.Cleanup(func() { os.Remove(testDBFilePath) })
|
|
|
|
|
testDBFile.Close()
|
|
|
|
|
|
2025-02-04 14:53:36 +05:30
|
|
|
sqlstore, err := sqlitesqlstore.New(context.Background(), providertest.NewSettings(), sqlstore.Config{Provider: "sqlite", Sqlite: sqlstore.SqliteConfig{Path: testDBFilePath}})
|
2024-03-11 14:15:11 +05:30
|
|
|
if err != nil {
|
2025-02-04 14:53:36 +05:30
|
|
|
t.Fatalf("could not create test db sqlite store: %v", err)
|
2024-03-11 14:15:11 +05:30
|
|
|
}
|
|
|
|
|
|
2025-02-04 14:53:36 +05:30
|
|
|
sqlmigrations, err := sqlmigration.New(
|
|
|
|
|
context.Background(),
|
|
|
|
|
providertest.NewSettings(),
|
|
|
|
|
sqlmigration.Config{},
|
|
|
|
|
factory.MustNewNamedMap(
|
|
|
|
|
sqlmigration.NewAddDataMigrationsFactory(),
|
|
|
|
|
sqlmigration.NewAddOrganizationFactory(),
|
|
|
|
|
sqlmigration.NewAddPreferencesFactory(),
|
|
|
|
|
sqlmigration.NewAddDashboardsFactory(),
|
|
|
|
|
sqlmigration.NewAddSavedViewsFactory(),
|
|
|
|
|
sqlmigration.NewAddAgentsFactory(),
|
|
|
|
|
sqlmigration.NewAddPipelinesFactory(),
|
|
|
|
|
sqlmigration.NewAddIntegrationsFactory(),
|
|
|
|
|
sqlmigration.NewAddLicensesFactory(),
|
|
|
|
|
sqlmigration.NewAddPatsFactory(),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("could not create test db sql migrations: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = sqlmigrator.New(context.Background(), providertest.NewSettings(), sqlstore, sqlmigrations, sqlmigrator.Config{}).Migrate(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("could not migrate test db sql migrations: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testDB = sqlstore.SQLxDB()
|
|
|
|
|
|
2025-01-10 18:43:35 +05:30
|
|
|
return testDB, testDBFilePath
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewQueryServiceDBForTests(t *testing.T) *sqlx.DB {
|
2025-01-22 12:49:38 +05:30
|
|
|
testDB, _ := NewTestSqliteDB(t)
|
2025-01-10 18:43:35 +05:30
|
|
|
|
2024-03-11 14:15:11 +05:30
|
|
|
// TODO(Raj): This should not require passing in the DB file path
|
2025-01-22 12:49:38 +05:30
|
|
|
dao.InitDao(testDB)
|
|
|
|
|
dashboards.InitDB(testDB)
|
2024-03-11 14:15:11 +05:30
|
|
|
|
|
|
|
|
return testDB
|
|
|
|
|
}
|