aboutsummaryrefslogtreecommitdiff
path: root/pkg/database/sql/main_test.go
blob: c373740d66ac05e765ab5a4bb0e51ae9df8928e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//go:build integration

package sql

import (
	"context"
	"os"
	"testing"

	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
)

var (
	ctx context.Context
)

func setup(t *testing.T) (*gorm.DB, func()) {
	t.Helper()

	ctx = context.Background()

	file, err := os.CreateTemp("", "img_user_*.db")
	if err != nil {
		t.Fatalf("Error creating tmp error: %s", err.Error())
	}

	db, err := gorm.Open(sqlite.Open(file.Name()), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Info),
	})
	if err != nil {
		t.Fatalf("Error openning db, error %s", err.Error())
	}

	err = Migrate(db)
	if err != nil {
		t.Fatalf("Error migrating db, error %s", err.Error())
	}

	return db, func() {
		//nolint:errcheck
		os.Remove(file.Name())
	}
}