efe87321b2
Testing Tweaks * parallel * slowestquery * split * cleanup * health * dynamiccores * go * profile * timeout * commitmychanges * taskfile * sqlcheckstart * client * cost * ctxtimeout
427 lines
10 KiB
Go
427 lines
10 KiB
Go
package repository_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"queryorchestration/internal/database/repository"
|
|
"queryorchestration/internal/serviceconfig"
|
|
"queryorchestration/internal/test"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestTextExtraction(t *testing.T) {
|
|
t.Parallel()
|
|
if testing.Short() {
|
|
t.SkipNow()
|
|
}
|
|
ctx := context.Background()
|
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
net := test.GetNetwork(t)
|
|
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
|
|
|
|
queries := cfg.GetDBQueries()
|
|
|
|
clientId := "EXAMPLE"
|
|
err := queries.CreateClient(ctx, &repository.CreateClientParams{
|
|
Name: "example_client",
|
|
Clientid: clientId,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
hash := "example_hash"
|
|
id, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
Clientid: clientId,
|
|
Hash: hash,
|
|
})
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, id)
|
|
|
|
bucket := "example_bucket"
|
|
key := "example_key"
|
|
cleanid, err := queries.AddDocumentClean(ctx, &repository.AddDocumentCleanParams{
|
|
Documentid: id,
|
|
Bucket: &bucket,
|
|
Key: &key,
|
|
Hash: &hash,
|
|
Mimetype: repository.NullCleanmimetype{
|
|
Valid: true,
|
|
Cleanmimetype: repository.CleanmimetypeApplicationPdf,
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
err = queries.AddDocumentCleanEntry(ctx, &repository.AddDocumentCleanEntryParams{
|
|
Cleanid: cleanid,
|
|
Version: 1,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
isextract, err := queries.IsDocumentTextExtracted(ctx, id)
|
|
require.NoError(t, err)
|
|
assert.False(t, isextract)
|
|
|
|
textId, err := queries.AddDocumentText(ctx, &repository.AddDocumentTextParams{
|
|
Cleanid: cleanid,
|
|
Part: 0,
|
|
Bucket: bucket,
|
|
Key: key,
|
|
Hash: "example",
|
|
Createdat: pgtype.Timestamp{
|
|
Time: time.Now().UTC(),
|
|
Valid: true,
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
assert.NotEqual(t, uuid.UUID{}, textId)
|
|
|
|
isextract, err = queries.IsDocumentTextExtracted(ctx, id)
|
|
require.NoError(t, err)
|
|
assert.False(t, isextract)
|
|
|
|
isextract, err = queries.IsDocumentTextExtracted(ctx, id)
|
|
require.NoError(t, err)
|
|
assert.False(t, isextract)
|
|
|
|
err = queries.AddDocumentTextEntry(ctx, &repository.AddDocumentTextEntryParams{
|
|
Textid: textId,
|
|
Version: 543,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
isextract, err = queries.IsDocumentTextExtracted(ctx, id)
|
|
require.NoError(t, err)
|
|
assert.True(t, isextract)
|
|
|
|
text, err := queries.GetTextEntryByDocId(ctx, id)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, textId, text.ID)
|
|
assert.Equal(t, id, text.Documentid)
|
|
assert.Equal(t, bucket, text.Bucket)
|
|
assert.Equal(t, key, text.Key)
|
|
assert.Equal(t, "example", text.Hash)
|
|
assert.Equal(t, cleanid, text.Cleanid)
|
|
assert.Equal(t, int64(543), text.Extractionversion)
|
|
assert.Equal(t, textId, text.ID)
|
|
|
|
uniqueEntry, err := queries.GetDocumentTextExtractionByHash(ctx, &repository.GetDocumentTextExtractionByHashParams{
|
|
Hash: "example",
|
|
Cleanentryid: cleanid,
|
|
})
|
|
require.NoError(t, err)
|
|
assert.EqualExportedValues(t, text.ID, uniqueEntry)
|
|
|
|
_, err = queries.GetDocumentTextExtractionByHash(ctx, &repository.GetDocumentTextExtractionByHashParams{
|
|
Hash: "definitely invalid",
|
|
Cleanentryid: cleanid,
|
|
})
|
|
require.EqualError(t, err, "no rows in result set")
|
|
}
|
|
|
|
func TestTextTextractPart(t *testing.T) {
|
|
t.Parallel()
|
|
if testing.Short() {
|
|
t.SkipNow()
|
|
}
|
|
ctx := context.Background()
|
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
net := test.GetNetwork(t)
|
|
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
|
|
|
|
queries := cfg.GetDBQueries()
|
|
|
|
clientId := "EXAMPLE"
|
|
err := queries.CreateClient(ctx, &repository.CreateClientParams{
|
|
Name: "example_client",
|
|
Clientid: clientId,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
hash := "example_hash"
|
|
id, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
Clientid: clientId,
|
|
Hash: hash,
|
|
})
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, id)
|
|
|
|
bucket := "example_bucket"
|
|
key := "example_key"
|
|
cleanid, err := queries.AddDocumentClean(ctx, &repository.AddDocumentCleanParams{
|
|
Documentid: id,
|
|
Bucket: &bucket,
|
|
Key: &key,
|
|
Hash: &hash,
|
|
Mimetype: repository.NullCleanmimetype{
|
|
Valid: true,
|
|
Cleanmimetype: repository.CleanmimetypeApplicationPdf,
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
err = queries.AddDocumentCleanEntry(ctx, &repository.AddDocumentCleanEntryParams{
|
|
Cleanid: cleanid,
|
|
Version: 1,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
part, err := queries.GetTextractOutputCurrentPart(ctx, &repository.GetTextractOutputCurrentPartParams{
|
|
Clientid: &clientId,
|
|
Querydate: pgtype.Timestamptz{
|
|
Time: time.Now().UTC(),
|
|
Valid: true,
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, &repository.GetTextractOutputCurrentPartRow{
|
|
Count: 0,
|
|
Part: 0,
|
|
}, part)
|
|
|
|
_, err = queries.AddDocumentText(ctx, &repository.AddDocumentTextParams{
|
|
Cleanid: cleanid,
|
|
Part: 0,
|
|
Createdat: pgtype.Timestamp{
|
|
Time: time.Now().UTC(),
|
|
Valid: true,
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
part, err = queries.GetTextractOutputCurrentPart(ctx, &repository.GetTextractOutputCurrentPartParams{
|
|
Clientid: &clientId,
|
|
Querydate: pgtype.Timestamptz{
|
|
Time: time.Now().UTC(),
|
|
Valid: true,
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, &repository.GetTextractOutputCurrentPartRow{
|
|
Count: 1,
|
|
Part: 0,
|
|
}, part)
|
|
|
|
_, err = queries.AddDocumentText(ctx, &repository.AddDocumentTextParams{
|
|
Cleanid: cleanid,
|
|
Part: 0,
|
|
Createdat: pgtype.Timestamp{
|
|
Time: time.Now().UTC(),
|
|
Valid: true,
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
part, err = queries.GetTextractOutputCurrentPart(ctx, &repository.GetTextractOutputCurrentPartParams{
|
|
Clientid: &clientId,
|
|
Querydate: pgtype.Timestamptz{
|
|
Time: time.Now().UTC(),
|
|
Valid: true,
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, &repository.GetTextractOutputCurrentPartRow{
|
|
Count: 2,
|
|
Part: 0,
|
|
}, part)
|
|
|
|
_, err = queries.AddDocumentText(ctx, &repository.AddDocumentTextParams{
|
|
Cleanid: cleanid,
|
|
Part: 1,
|
|
Createdat: pgtype.Timestamp{
|
|
Time: time.Now().UTC(),
|
|
Valid: true,
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
part, err = queries.GetTextractOutputCurrentPart(ctx, &repository.GetTextractOutputCurrentPartParams{
|
|
Clientid: &clientId,
|
|
Querydate: pgtype.Timestamptz{
|
|
Time: time.Now().UTC(),
|
|
Valid: true,
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, &repository.GetTextractOutputCurrentPartRow{
|
|
Count: 1,
|
|
Part: 1,
|
|
}, part)
|
|
}
|
|
|
|
func TestTextOutPart(t *testing.T) {
|
|
t.Parallel()
|
|
if testing.Short() {
|
|
t.SkipNow()
|
|
}
|
|
ctx := context.Background()
|
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
net := test.GetNetwork(t)
|
|
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
|
|
|
|
queries := cfg.GetDBQueries()
|
|
|
|
clientId := "EXAMPLE"
|
|
err := queries.CreateClient(ctx, &repository.CreateClientParams{
|
|
Name: "example_client",
|
|
Clientid: clientId,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
hash := "example_hash"
|
|
id, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
Clientid: clientId,
|
|
Hash: hash,
|
|
})
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, id)
|
|
|
|
bucket := "example_bucket"
|
|
key := "example_key"
|
|
cleanid, err := queries.AddDocumentClean(ctx, &repository.AddDocumentCleanParams{
|
|
Documentid: id,
|
|
Bucket: &bucket,
|
|
Key: &key,
|
|
Hash: &hash,
|
|
Mimetype: repository.NullCleanmimetype{
|
|
Valid: true,
|
|
Cleanmimetype: repository.CleanmimetypeApplicationPdf,
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
err = queries.AddDocumentCleanEntry(ctx, &repository.AddDocumentCleanEntryParams{
|
|
Cleanid: cleanid,
|
|
Version: 1,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
textId, err := queries.AddDocumentText(ctx, &repository.AddDocumentTextParams{
|
|
Cleanid: cleanid,
|
|
Bucket: "hi",
|
|
Key: "hi",
|
|
Hash: "hi",
|
|
Part: 0,
|
|
Createdat: pgtype.Timestamp{
|
|
Time: time.Now().UTC(),
|
|
Valid: true,
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
err = queries.AddDocumentTextEntry(ctx, &repository.AddDocumentTextEntryParams{
|
|
Textid: textId,
|
|
Version: 123,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
part, err := queries.GetTextOutCurrentPart(ctx, &repository.GetTextOutCurrentPartParams{
|
|
Clientid: &clientId,
|
|
Querydate: pgtype.Timestamptz{
|
|
Time: time.Now().UTC(),
|
|
Valid: true,
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, &repository.GetTextOutCurrentPartRow{
|
|
Count: 1,
|
|
Part: 0,
|
|
}, part)
|
|
|
|
textId, err = queries.AddDocumentText(ctx, &repository.AddDocumentTextParams{
|
|
Cleanid: cleanid,
|
|
Bucket: "hi",
|
|
Key: "hi",
|
|
Hash: "hi",
|
|
Part: 0,
|
|
Createdat: pgtype.Timestamp{
|
|
Time: time.Now().UTC(),
|
|
Valid: true,
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
err = queries.AddDocumentTextEntry(ctx, &repository.AddDocumentTextEntryParams{
|
|
Textid: textId,
|
|
Version: 123,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
part, err = queries.GetTextOutCurrentPart(ctx, &repository.GetTextOutCurrentPartParams{
|
|
Clientid: &clientId,
|
|
Querydate: pgtype.Timestamptz{
|
|
Time: time.Now().UTC(),
|
|
Valid: true,
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, &repository.GetTextOutCurrentPartRow{
|
|
Count: 2,
|
|
Part: 0,
|
|
}, part)
|
|
|
|
textId, err = queries.AddDocumentText(ctx, &repository.AddDocumentTextParams{
|
|
Cleanid: cleanid,
|
|
Bucket: "hi",
|
|
Key: "hi",
|
|
Hash: "hi",
|
|
Part: 0,
|
|
Createdat: pgtype.Timestamp{
|
|
Time: time.Now().UTC(),
|
|
Valid: true,
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
err = queries.AddDocumentTextEntry(ctx, &repository.AddDocumentTextEntryParams{
|
|
Textid: textId,
|
|
Version: 123,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
part, err = queries.GetTextOutCurrentPart(ctx, &repository.GetTextOutCurrentPartParams{
|
|
Clientid: &clientId,
|
|
Querydate: pgtype.Timestamptz{
|
|
Time: time.Now().UTC(),
|
|
Valid: true,
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, &repository.GetTextOutCurrentPartRow{
|
|
Count: 3,
|
|
Part: 0,
|
|
}, part)
|
|
|
|
textId, err = queries.AddDocumentText(ctx, &repository.AddDocumentTextParams{
|
|
Cleanid: cleanid,
|
|
Bucket: "hi",
|
|
Key: "hi",
|
|
Hash: "hi",
|
|
Part: 1,
|
|
Createdat: pgtype.Timestamp{
|
|
Time: time.Now().UTC(),
|
|
Valid: true,
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
err = queries.AddDocumentTextEntry(ctx, &repository.AddDocumentTextEntryParams{
|
|
Textid: textId,
|
|
Version: 123,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
part, err = queries.GetTextOutCurrentPart(ctx, &repository.GetTextOutCurrentPartParams{
|
|
Clientid: &clientId,
|
|
Querydate: pgtype.Timestamptz{
|
|
Time: time.Now().UTC(),
|
|
Valid: true,
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, &repository.GetTextOutCurrentPartRow{
|
|
Count: 1,
|
|
Part: 1,
|
|
}, part)
|
|
}
|