720a84be92
integration of background processor * integration part 1 * feature working * fix mimetype issue
353 lines
8.9 KiB
Go
353 lines
8.9 KiB
Go
package repository_test
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"queryorchestration/internal/database/repository"
|
|
"queryorchestration/internal/serviceconfig"
|
|
"queryorchestration/internal/test"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestDocument(t *testing.T) {
|
|
if testing.Short() {
|
|
t.SkipNow()
|
|
}
|
|
t.Run("List docs for a client", func(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := t.Context()
|
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
test.CreateDB(t, cfg)
|
|
|
|
queries := cfg.GetDBQueries()
|
|
|
|
clientId := fmt.Sprintf("EXAMPLE_%s", uuid.New().String()[:8])
|
|
err := queries.CreateClient(ctx, &repository.CreateClientParams{
|
|
Name: fmt.Sprintf("example_client_%s", clientId),
|
|
Clientid: clientId,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
docs, err := queries.ListDocumentsByClient(ctx, clientId)
|
|
require.NoError(t, err)
|
|
assert.Len(t, docs, 0)
|
|
})
|
|
t.Run("Create document", func(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := t.Context()
|
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
test.CreateDB(t, cfg)
|
|
|
|
queries := cfg.GetDBQueries()
|
|
|
|
clientId := fmt.Sprintf("EXAMPLE_%s", uuid.New().String()[:8])
|
|
err := queries.CreateClient(ctx, &repository.CreateClientParams{
|
|
Name: fmt.Sprintf("example_client_%s", clientId),
|
|
Clientid: clientId,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
hash := "example_hash"
|
|
id, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
Clientid: clientId,
|
|
Hash: hash,
|
|
BatchID: nil,
|
|
})
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, id)
|
|
|
|
docs, err := queries.ListDocumentsByClient(ctx, clientId)
|
|
require.NoError(t, err)
|
|
assert.Len(t, docs, 1)
|
|
assert.Equal(t, hash, docs[0].Hash)
|
|
})
|
|
|
|
t.Run("Create two documents", func(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := t.Context()
|
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
test.CreateDB(t, cfg)
|
|
|
|
queries := cfg.GetDBQueries()
|
|
|
|
clientId := fmt.Sprintf("EXAMPLE_%s", uuid.New().String()[:8])
|
|
err := queries.CreateClient(ctx, &repository.CreateClientParams{
|
|
Name: fmt.Sprintf("example_client_%s", clientId),
|
|
Clientid: clientId,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
hash := "example_hash"
|
|
id, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
Clientid: clientId,
|
|
Hash: hash,
|
|
BatchID: nil,
|
|
})
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, id)
|
|
|
|
documentTwoID, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
Clientid: clientId,
|
|
Hash: "example_hash_two",
|
|
BatchID: nil,
|
|
})
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, documentTwoID)
|
|
|
|
docs, err := queries.ListDocumentsByClient(ctx, clientId)
|
|
require.NoError(t, err)
|
|
assert.Len(t, docs, 2)
|
|
})
|
|
t.Run("Two clients", func(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := t.Context()
|
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
test.CreateDB(t, cfg)
|
|
|
|
queries := cfg.GetDBQueries()
|
|
|
|
clientId := fmt.Sprintf("EXAMPLE_%s", uuid.New().String()[:8])
|
|
err := queries.CreateClient(ctx, &repository.CreateClientParams{
|
|
Name: fmt.Sprintf("example_client_%s", clientId),
|
|
Clientid: clientId,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
hash := "example_hash"
|
|
id, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
Clientid: clientId,
|
|
Hash: hash,
|
|
BatchID: nil,
|
|
})
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, id)
|
|
|
|
clientTwoId := fmt.Sprintf("EXAMPLE_TWO_%s", uuid.New().String()[:8])
|
|
err = queries.CreateClient(ctx, &repository.CreateClientParams{
|
|
Name: "example_name_two",
|
|
Clientid: clientTwoId,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
hashTwo := "example_hash_two"
|
|
idTwo, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
Clientid: clientTwoId,
|
|
Hash: hashTwo,
|
|
BatchID: nil,
|
|
})
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, idTwo)
|
|
|
|
docs, err := queries.ListDocumentsByClient(ctx, clientId)
|
|
require.NoError(t, err)
|
|
assert.Len(t, docs, 1)
|
|
docs, err = queries.ListDocumentsByClient(ctx, clientTwoId)
|
|
require.NoError(t, err)
|
|
assert.Len(t, docs, 1)
|
|
})
|
|
t.Run("document summary", func(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := t.Context()
|
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
test.CreateDB(t, cfg)
|
|
|
|
queries := cfg.GetDBQueries()
|
|
|
|
clientId := fmt.Sprintf("EXAMPLE_%s", uuid.New().String()[:8])
|
|
err := queries.CreateClient(ctx, &repository.CreateClientParams{
|
|
Name: fmt.Sprintf("example_client_%s", clientId),
|
|
Clientid: clientId,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
hash := "example_hash"
|
|
id, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
Clientid: clientId,
|
|
Hash: hash,
|
|
BatchID: nil,
|
|
})
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, id)
|
|
|
|
doc, err := queries.GetDocumentSummary(ctx, id)
|
|
require.NoError(t, err)
|
|
assert.EqualExportedValues(t, &repository.GetDocumentSummaryRow{
|
|
ID: id,
|
|
Clientid: clientId,
|
|
Hash: hash,
|
|
}, doc)
|
|
})
|
|
t.Run("document external", func(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := t.Context()
|
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
test.CreateDB(t, cfg)
|
|
|
|
queries := cfg.GetDBQueries()
|
|
|
|
clientId := fmt.Sprintf("EXAMPLE_%s", uuid.New().String()[:8])
|
|
err := queries.CreateClient(ctx, &repository.CreateClientParams{
|
|
Name: fmt.Sprintf("example_client_%s", clientId),
|
|
Clientid: clientId,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
hash := "example_hash"
|
|
id, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
Clientid: clientId,
|
|
Hash: hash,
|
|
BatchID: nil,
|
|
})
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, id)
|
|
|
|
docext, err := queries.GetDocumentExternal(ctx, id)
|
|
require.NoError(t, err)
|
|
assert.EqualExportedValues(t, &repository.GetDocumentExternalRow{
|
|
ID: id,
|
|
Clientid: clientId,
|
|
Hash: hash,
|
|
Fields: []byte("{}"),
|
|
}, docext)
|
|
})
|
|
t.Run("doc id by hash", func(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := t.Context()
|
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
test.CreateDB(t, cfg)
|
|
|
|
queries := cfg.GetDBQueries()
|
|
|
|
clientId := fmt.Sprintf("EXAMPLE_%s", uuid.New().String()[:8])
|
|
err := queries.CreateClient(ctx, &repository.CreateClientParams{
|
|
Name: fmt.Sprintf("example_client_%s", clientId),
|
|
Clientid: clientId,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
hash := "example_hash"
|
|
id, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
Clientid: clientId,
|
|
Hash: hash,
|
|
BatchID: nil,
|
|
})
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, id)
|
|
|
|
docid, err := queries.GetDocumentIDByHash(ctx, &repository.GetDocumentIDByHashParams{
|
|
Hash: hash,
|
|
Clientid: clientId,
|
|
})
|
|
require.NoError(t, err)
|
|
assert.EqualExportedValues(t, id, docid)
|
|
})
|
|
t.Run("doc entry", func(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := t.Context()
|
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
test.CreateDB(t, cfg)
|
|
|
|
queries := cfg.GetDBQueries()
|
|
|
|
clientId := fmt.Sprintf("EXAMPLE_%s", uuid.New().String()[:8])
|
|
err := queries.CreateClient(ctx, &repository.CreateClientParams{
|
|
Name: fmt.Sprintf("example_client_%s", clientId),
|
|
Clientid: clientId,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
hash := "example_hash"
|
|
id, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
Clientid: clientId,
|
|
Hash: hash,
|
|
BatchID: nil,
|
|
})
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, id)
|
|
|
|
bucketone := "bucketone"
|
|
keyone := "keyone"
|
|
err = queries.AddDocumentEntry(ctx, &repository.AddDocumentEntryParams{
|
|
Documentid: id,
|
|
Bucket: bucketone,
|
|
Key: keyone,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
entry, err := queries.GetDocumentEntry(ctx, id)
|
|
require.NoError(t, err)
|
|
assert.EqualExportedValues(t, &repository.GetDocumentEntryRow{
|
|
Documentid: id,
|
|
Bucket: bucketone,
|
|
Key: keyone,
|
|
}, entry)
|
|
})
|
|
t.Run("doc latest entry", func(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := t.Context()
|
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
test.CreateDB(t, cfg)
|
|
|
|
queries := cfg.GetDBQueries()
|
|
|
|
clientId := fmt.Sprintf("EXAMPLE_%s", uuid.New().String()[:8])
|
|
err := queries.CreateClient(ctx, &repository.CreateClientParams{
|
|
Name: fmt.Sprintf("example_client_%s", clientId),
|
|
Clientid: clientId,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
hash := "example_hash"
|
|
id, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
Clientid: clientId,
|
|
Hash: hash,
|
|
BatchID: nil,
|
|
})
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, id)
|
|
|
|
bucketone := "bucketone"
|
|
keyone := "keyone"
|
|
err = queries.AddDocumentEntry(ctx, &repository.AddDocumentEntryParams{
|
|
Documentid: id,
|
|
Bucket: bucketone,
|
|
Key: keyone,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
// Add a small delay to ensure UUID v7 timestamps are different
|
|
time.Sleep(2 * time.Millisecond)
|
|
|
|
buckettwo := "buckettwo"
|
|
keytwo := "keytwo"
|
|
err = queries.AddDocumentEntry(ctx, &repository.AddDocumentEntryParams{
|
|
Documentid: id,
|
|
Bucket: buckettwo,
|
|
Key: keytwo,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
entry, err := queries.GetDocumentEntry(ctx, id)
|
|
require.NoError(t, err)
|
|
assert.EqualExportedValues(t, &repository.GetDocumentEntryRow{
|
|
Documentid: id,
|
|
Bucket: buckettwo,
|
|
Key: keytwo,
|
|
}, entry)
|
|
})
|
|
}
|