Merged in bugfix/cleanup (pull request #10)
Clean Up Testing and Linting * somescriptcleanup * mostgolangci * deplatest * imageversions * usingscratch * movedqueuefrtesting * finishedunittesting * linting * taskfilecontext
This commit is contained in:
@@ -14,8 +14,8 @@ import (
|
||||
|
||||
func TestDBConn(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
_, container := createDB(t, ctx)
|
||||
defer container.Terminate(ctx)
|
||||
_, cleanup := createDB(t, ctx)
|
||||
defer cleanup()
|
||||
|
||||
conn := database.GetDBConn(ctx)
|
||||
assert.NotNil(t, conn)
|
||||
@@ -34,8 +34,8 @@ func TestDBConnNoDB(t *testing.T) {
|
||||
|
||||
func TestDBPool(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
_, container := createDB(t, ctx)
|
||||
defer container.Terminate(ctx)
|
||||
_, cleanup := createDB(t, ctx)
|
||||
defer cleanup()
|
||||
|
||||
pool := database.GetDBPool(ctx)
|
||||
assert.NotNil(t, pool)
|
||||
@@ -49,7 +49,12 @@ type dbConfig struct {
|
||||
Password string
|
||||
}
|
||||
|
||||
func createDB(t *testing.T, ctx context.Context) (*dbConfig, testcontainers.Container) {
|
||||
type db struct {
|
||||
config *dbConfig
|
||||
container *testcontainers.Container
|
||||
}
|
||||
|
||||
func createDB(t *testing.T, ctx context.Context) (*db, func()) {
|
||||
alias := "postgres"
|
||||
port, err := nat.NewPort("tcp", "5432")
|
||||
if err != nil {
|
||||
@@ -65,7 +70,7 @@ func createDB(t *testing.T, ctx context.Context) (*dbConfig, testcontainers.Cont
|
||||
}
|
||||
|
||||
req := testcontainers.ContainerRequest{
|
||||
Image: "postgres:latest",
|
||||
Image: "postgres:17.2-alpine3.21",
|
||||
Env: map[string]string{
|
||||
"POSTGRES_DB": config.Name,
|
||||
"POSTGRES_USER": config.User,
|
||||
@@ -102,5 +107,13 @@ func createDB(t *testing.T, ctx context.Context) (*dbConfig, testcontainers.Cont
|
||||
t.Setenv("DB_PORT", fmt.Sprint(config.Port))
|
||||
t.Setenv("DB_NAME", config.Name)
|
||||
|
||||
return &config, container
|
||||
return &db{
|
||||
config: &config,
|
||||
container: &container,
|
||||
}, func() {
|
||||
err := container.Terminate(ctx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,8 +12,8 @@ import (
|
||||
|
||||
func TestRunMigrations(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
_, container := createDB(t, ctx)
|
||||
defer container.Terminate(ctx)
|
||||
_, cleanup := createDB(t, ctx)
|
||||
defer cleanup()
|
||||
|
||||
t.Setenv("DB_NOSSL", "1")
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
@@ -16,7 +18,11 @@ func MustToDBUUIDArray(ids []uuid.UUID) []pgtype.UUID {
|
||||
|
||||
func MustToDBUUID(id uuid.UUID) pgtype.UUID {
|
||||
var dbID pgtype.UUID
|
||||
dbID.Scan(id.String())
|
||||
err := dbID.Scan(id.String())
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
return dbID
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ func TestMustToDBUUIDArray(t *testing.T) {
|
||||
|
||||
dbIDs := database.MustToDBUUIDArray(ids)
|
||||
|
||||
assert.Equal(t, len(ids), len(dbIDs))
|
||||
assert.Len(t, dbIDs, len(ids))
|
||||
for index, id := range dbIDs {
|
||||
assert.Equal(t, database.MustToDBUUID(ids[index]), id)
|
||||
}
|
||||
@@ -42,7 +42,7 @@ func TestMustToUUIDArray(t *testing.T) {
|
||||
|
||||
ids := database.MustToUUIDArray(dbIDs)
|
||||
|
||||
assert.Equal(t, len(ids), len(dbIDs))
|
||||
assert.Len(t, ids, len(dbIDs))
|
||||
for index, id := range dbIDs {
|
||||
assert.Equal(t, database.MustToDBUUID(ids[index]), id)
|
||||
}
|
||||
|
||||
@@ -12,15 +12,15 @@ import (
|
||||
|
||||
func TestCollector(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
pool, container := createDB(t, ctx)
|
||||
defer container.Terminate(ctx)
|
||||
queries := repository.New(pool)
|
||||
db, cleanup := createDB(t, ctx)
|
||||
defer cleanup()
|
||||
queries := repository.New(db.pool)
|
||||
|
||||
collectorID := database.MustToDBUUID(uuid.New())
|
||||
|
||||
collectorQueries, err := queries.GetCollectorQueries(ctx, collectorID)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 0, len(collectorQueries))
|
||||
assert.Len(t, collectorQueries, 0)
|
||||
assert.ElementsMatch(t, []repository.GetCollectorQueriesRow{}, collectorQueries)
|
||||
|
||||
jobID := database.MustToDBUUID(uuid.New())
|
||||
|
||||
@@ -12,9 +12,9 @@ import (
|
||||
|
||||
func TestQueries(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
pool, container := createDB(t, ctx)
|
||||
defer container.Terminate(ctx)
|
||||
queries := repository.New(pool)
|
||||
db, cleanup := createDB(t, ctx)
|
||||
defer cleanup()
|
||||
queries := repository.New(db.pool)
|
||||
|
||||
contextQueryID, err := queries.CreateQuery(ctx, repository.Querytype(repository.QuerytypeContextFull))
|
||||
assert.Nil(t, err)
|
||||
@@ -60,7 +60,7 @@ func TestQueries(t *testing.T) {
|
||||
|
||||
qs, err := queries.ListQueries(ctx)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 2, len(qs))
|
||||
assert.Len(t, qs, 2)
|
||||
log.Print(qs)
|
||||
assert.ElementsMatch(t, []repository.Fullactivequery{
|
||||
{ID: jsonQueryID,
|
||||
|
||||
@@ -13,9 +13,9 @@ import (
|
||||
|
||||
func TestResults(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
pool, container := createDB(t, ctx)
|
||||
defer container.Terminate(ctx)
|
||||
queries := repository.New(pool)
|
||||
db, cleanup := createDB(t, ctx)
|
||||
defer cleanup()
|
||||
queries := repository.New(db.pool)
|
||||
|
||||
jsonQueryID, err := queries.CreateQuery(ctx, repository.Querytype(repository.QuerytypeJsonExtractor))
|
||||
assert.Nil(t, err)
|
||||
@@ -46,7 +46,7 @@ func TestResults(t *testing.T) {
|
||||
Textversion: textVersion,
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 1, len(resultsByDoc))
|
||||
assert.Len(t, resultsByDoc, 1)
|
||||
assert.ElementsMatch(t, []repository.ListResultsByDocumentIDRow{
|
||||
{
|
||||
ID: jsonResultID,
|
||||
@@ -57,7 +57,7 @@ func TestResults(t *testing.T) {
|
||||
|
||||
results, err := queries.ListResultValuesByID(ctx, []pgtype.UUID{jsonResultID})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 1, len(results))
|
||||
assert.Len(t, results, 1)
|
||||
assert.ElementsMatch(t, []repository.ListResultValuesByIDRow{
|
||||
{
|
||||
ID: jsonResultID,
|
||||
|
||||
@@ -14,7 +14,12 @@ import (
|
||||
"github.com/testcontainers/testcontainers-go/wait"
|
||||
)
|
||||
|
||||
func createDB(t *testing.T, ctx context.Context) (*pgxpool.Pool, testcontainers.Container) {
|
||||
type db struct {
|
||||
pool *pgxpool.Pool
|
||||
container *testcontainers.Container
|
||||
}
|
||||
|
||||
func createDB(t *testing.T, ctx context.Context) (*db, func()) {
|
||||
port, err := nat.NewPort("tcp", "5432")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create port: %v", err)
|
||||
@@ -25,7 +30,7 @@ func createDB(t *testing.T, ctx context.Context) (*pgxpool.Pool, testcontainers.
|
||||
user := "postgres"
|
||||
|
||||
req := testcontainers.ContainerRequest{
|
||||
Image: "postgres:latest",
|
||||
Image: "postgres:17.2-alpine3.21",
|
||||
Env: map[string]string{
|
||||
"POSTGRES_DB": name,
|
||||
"POSTGRES_USER": user,
|
||||
@@ -65,5 +70,13 @@ func createDB(t *testing.T, ctx context.Context) (*pgxpool.Pool, testcontainers.
|
||||
|
||||
pool := database.GetDBPool(ctx)
|
||||
|
||||
return pool, container
|
||||
return &db{
|
||||
pool: pool,
|
||||
container: &container,
|
||||
}, func() {
|
||||
err := container.Terminate(ctx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user