2025-01-23 14:56:20 +00:00
|
|
|
package repository_test
|
|
|
|
|
|
|
|
|
|
import (
|
2025-08-04 10:54:22 -07:00
|
|
|
"fmt"
|
2025-03-05 12:05:46 +00:00
|
|
|
"testing"
|
2025-08-20 19:01:13 +00:00
|
|
|
"time"
|
2025-03-05 12:05:46 +00:00
|
|
|
|
2025-01-23 14:56:20 +00:00
|
|
|
"queryorchestration/internal/database/repository"
|
2025-02-05 12:52:41 +00:00
|
|
|
"queryorchestration/internal/serviceconfig"
|
2025-01-23 14:56:20 +00:00
|
|
|
"queryorchestration/internal/test"
|
|
|
|
|
|
2025-08-04 10:54:22 -07:00
|
|
|
"github.com/google/uuid"
|
2025-01-23 14:56:20 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2025-03-19 11:54:14 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2025-01-23 14:56:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestDocument(t *testing.T) {
|
2025-02-07 14:15:06 +00:00
|
|
|
if testing.Short() {
|
2025-05-03 01:16:53 +00:00
|
|
|
t.SkipNow()
|
2025-02-07 14:15:06 +00:00
|
|
|
}
|
2025-05-12 20:54:16 +00:00
|
|
|
t.Run("List docs for a client", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2025-06-03 13:52:10 +00:00
|
|
|
ctx := t.Context()
|
2025-01-31 13:43:55 +00:00
|
|
|
|
2025-05-12 20:54:16 +00:00
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
2025-05-23 00:20:01 +00:00
|
|
|
test.CreateDB(t, cfg)
|
2025-01-23 14:56:20 +00:00
|
|
|
|
2025-05-12 20:54:16 +00:00
|
|
|
queries := cfg.GetDBQueries()
|
2025-01-23 14:56:20 +00:00
|
|
|
|
2025-08-04 10:54:22 -07:00
|
|
|
clientId := fmt.Sprintf("EXAMPLE_%s", uuid.New().String()[:8])
|
2025-05-12 20:54:16 +00:00
|
|
|
err := queries.CreateClient(ctx, &repository.CreateClientParams{
|
2025-08-20 19:01:13 +00:00
|
|
|
Name: fmt.Sprintf("example_client_%s", clientId),
|
2025-05-12 20:54:16 +00:00
|
|
|
Clientid: clientId,
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
docs, err := queries.ListDocumentsByClient(ctx, clientId)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.Len(t, docs, 0)
|
2025-03-11 16:31:06 +00:00
|
|
|
})
|
2025-05-12 20:54:16 +00:00
|
|
|
t.Run("Create document", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2025-06-03 13:52:10 +00:00
|
|
|
ctx := t.Context()
|
2025-05-12 20:54:16 +00:00
|
|
|
|
|
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
2025-05-23 00:20:01 +00:00
|
|
|
test.CreateDB(t, cfg)
|
2025-05-12 20:54:16 +00:00
|
|
|
|
|
|
|
|
queries := cfg.GetDBQueries()
|
2025-01-23 14:56:20 +00:00
|
|
|
|
2025-08-04 10:54:22 -07:00
|
|
|
clientId := fmt.Sprintf("EXAMPLE_%s", uuid.New().String()[:8])
|
2025-05-12 20:54:16 +00:00
|
|
|
err := queries.CreateClient(ctx, &repository.CreateClientParams{
|
2025-08-20 19:01:13 +00:00
|
|
|
Name: fmt.Sprintf("example_client_%s", clientId),
|
2025-05-12 20:54:16 +00:00
|
|
|
Clientid: clientId,
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
2025-02-21 17:05:37 +00:00
|
|
|
|
2025-05-12 20:54:16 +00:00
|
|
|
hash := "example_hash"
|
|
|
|
|
id, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
|
|
|
Clientid: clientId,
|
|
|
|
|
Hash: hash,
|
2025-08-04 10:54:22 -07:00
|
|
|
BatchID: nil,
|
2025-05-12 20:54:16 +00:00
|
|
|
})
|
|
|
|
|
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)
|
2025-01-24 16:12:25 +00:00
|
|
|
})
|
2025-05-12 20:54:16 +00:00
|
|
|
|
|
|
|
|
t.Run("Create two documents", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2025-06-03 13:52:10 +00:00
|
|
|
ctx := t.Context()
|
2025-05-12 20:54:16 +00:00
|
|
|
|
|
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
2025-05-23 00:20:01 +00:00
|
|
|
test.CreateDB(t, cfg)
|
2025-05-12 20:54:16 +00:00
|
|
|
|
|
|
|
|
queries := cfg.GetDBQueries()
|
|
|
|
|
|
2025-08-04 10:54:22 -07:00
|
|
|
clientId := fmt.Sprintf("EXAMPLE_%s", uuid.New().String()[:8])
|
2025-05-12 20:54:16 +00:00
|
|
|
err := queries.CreateClient(ctx, &repository.CreateClientParams{
|
2025-08-20 19:01:13 +00:00
|
|
|
Name: fmt.Sprintf("example_client_%s", clientId),
|
2025-05-12 20:54:16 +00:00
|
|
|
Clientid: clientId,
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
hash := "example_hash"
|
|
|
|
|
id, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
|
|
|
Clientid: clientId,
|
|
|
|
|
Hash: hash,
|
2025-08-04 10:54:22 -07:00
|
|
|
BatchID: nil,
|
2025-05-12 20:54:16 +00:00
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.NotEmpty(t, id)
|
|
|
|
|
|
|
|
|
|
documentTwoID, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
|
|
|
Clientid: clientId,
|
|
|
|
|
Hash: "example_hash_two",
|
2025-08-04 10:54:22 -07:00
|
|
|
BatchID: nil,
|
2025-05-12 20:54:16 +00:00
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.NotEmpty(t, documentTwoID)
|
|
|
|
|
|
|
|
|
|
docs, err := queries.ListDocumentsByClient(ctx, clientId)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.Len(t, docs, 2)
|
2025-02-21 17:05:37 +00:00
|
|
|
})
|
2025-05-12 20:54:16 +00:00
|
|
|
t.Run("Two clients", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2025-06-03 13:52:10 +00:00
|
|
|
ctx := t.Context()
|
2025-02-21 17:05:37 +00:00
|
|
|
|
2025-05-12 20:54:16 +00:00
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
2025-05-23 00:20:01 +00:00
|
|
|
test.CreateDB(t, cfg)
|
2025-02-21 17:05:37 +00:00
|
|
|
|
2025-05-12 20:54:16 +00:00
|
|
|
queries := cfg.GetDBQueries()
|
|
|
|
|
|
2025-08-04 10:54:22 -07:00
|
|
|
clientId := fmt.Sprintf("EXAMPLE_%s", uuid.New().String()[:8])
|
2025-05-12 20:54:16 +00:00
|
|
|
err := queries.CreateClient(ctx, &repository.CreateClientParams{
|
2025-08-20 19:01:13 +00:00
|
|
|
Name: fmt.Sprintf("example_client_%s", clientId),
|
2025-05-12 20:54:16 +00:00
|
|
|
Clientid: clientId,
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
hash := "example_hash"
|
|
|
|
|
id, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
|
|
|
Clientid: clientId,
|
|
|
|
|
Hash: hash,
|
2025-08-04 10:54:22 -07:00
|
|
|
BatchID: nil,
|
2025-05-12 20:54:16 +00:00
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.NotEmpty(t, id)
|
|
|
|
|
|
2025-08-04 10:54:22 -07:00
|
|
|
clientTwoId := fmt.Sprintf("EXAMPLE_TWO_%s", uuid.New().String()[:8])
|
2025-05-12 20:54:16 +00:00
|
|
|
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,
|
2025-08-04 10:54:22 -07:00
|
|
|
BatchID: nil,
|
2025-05-12 20:54:16 +00:00
|
|
|
})
|
|
|
|
|
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)
|
2025-02-11 15:22:59 +00:00
|
|
|
})
|
2025-05-12 20:54:16 +00:00
|
|
|
t.Run("document summary", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2025-06-03 13:52:10 +00:00
|
|
|
ctx := t.Context()
|
2025-05-12 20:54:16 +00:00
|
|
|
|
|
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
2025-05-23 00:20:01 +00:00
|
|
|
test.CreateDB(t, cfg)
|
2025-05-12 20:54:16 +00:00
|
|
|
|
|
|
|
|
queries := cfg.GetDBQueries()
|
|
|
|
|
|
2025-08-04 10:54:22 -07:00
|
|
|
clientId := fmt.Sprintf("EXAMPLE_%s", uuid.New().String()[:8])
|
2025-05-12 20:54:16 +00:00
|
|
|
err := queries.CreateClient(ctx, &repository.CreateClientParams{
|
2025-08-20 19:01:13 +00:00
|
|
|
Name: fmt.Sprintf("example_client_%s", clientId),
|
2025-05-12 20:54:16 +00:00
|
|
|
Clientid: clientId,
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
hash := "example_hash"
|
|
|
|
|
id, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
|
|
|
Clientid: clientId,
|
|
|
|
|
Hash: hash,
|
2025-08-04 10:54:22 -07:00
|
|
|
BatchID: nil,
|
2025-05-12 20:54:16 +00:00
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.NotEmpty(t, id)
|
|
|
|
|
|
|
|
|
|
doc, err := queries.GetDocumentSummary(ctx, id)
|
|
|
|
|
require.NoError(t, err)
|
2025-08-04 10:54:22 -07:00
|
|
|
assert.EqualExportedValues(t, &repository.GetDocumentSummaryRow{
|
2025-05-12 20:54:16 +00:00
|
|
|
ID: id,
|
|
|
|
|
Clientid: clientId,
|
|
|
|
|
Hash: hash,
|
|
|
|
|
}, doc)
|
2025-02-21 17:05:37 +00:00
|
|
|
})
|
2025-05-12 20:54:16 +00:00
|
|
|
t.Run("document external", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2025-06-03 13:52:10 +00:00
|
|
|
ctx := t.Context()
|
2025-02-21 17:05:37 +00:00
|
|
|
|
2025-05-12 20:54:16 +00:00
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
2025-05-23 00:20:01 +00:00
|
|
|
test.CreateDB(t, cfg)
|
2025-02-21 17:05:37 +00:00
|
|
|
|
2025-05-12 20:54:16 +00:00
|
|
|
queries := cfg.GetDBQueries()
|
|
|
|
|
|
2025-08-04 10:54:22 -07:00
|
|
|
clientId := fmt.Sprintf("EXAMPLE_%s", uuid.New().String()[:8])
|
2025-05-12 20:54:16 +00:00
|
|
|
err := queries.CreateClient(ctx, &repository.CreateClientParams{
|
2025-08-20 19:01:13 +00:00
|
|
|
Name: fmt.Sprintf("example_client_%s", clientId),
|
2025-05-12 20:54:16 +00:00
|
|
|
Clientid: clientId,
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
hash := "example_hash"
|
|
|
|
|
id, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
|
|
|
Clientid: clientId,
|
|
|
|
|
Hash: hash,
|
2025-08-04 10:54:22 -07:00
|
|
|
BatchID: nil,
|
2025-05-12 20:54:16 +00:00
|
|
|
})
|
|
|
|
|
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)
|
2025-03-11 16:31:06 +00:00
|
|
|
})
|
2025-05-12 20:54:16 +00:00
|
|
|
t.Run("doc id by hash", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2025-06-03 13:52:10 +00:00
|
|
|
ctx := t.Context()
|
2025-05-12 20:54:16 +00:00
|
|
|
|
|
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
2025-05-23 00:20:01 +00:00
|
|
|
test.CreateDB(t, cfg)
|
2025-05-12 20:54:16 +00:00
|
|
|
|
|
|
|
|
queries := cfg.GetDBQueries()
|
|
|
|
|
|
2025-08-04 10:54:22 -07:00
|
|
|
clientId := fmt.Sprintf("EXAMPLE_%s", uuid.New().String()[:8])
|
2025-05-12 20:54:16 +00:00
|
|
|
err := queries.CreateClient(ctx, &repository.CreateClientParams{
|
2025-08-20 19:01:13 +00:00
|
|
|
Name: fmt.Sprintf("example_client_%s", clientId),
|
2025-05-12 20:54:16 +00:00
|
|
|
Clientid: clientId,
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
hash := "example_hash"
|
|
|
|
|
id, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
|
|
|
Clientid: clientId,
|
|
|
|
|
Hash: hash,
|
2025-08-04 10:54:22 -07:00
|
|
|
BatchID: nil,
|
2025-05-12 20:54:16 +00:00
|
|
|
})
|
|
|
|
|
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)
|
2025-02-11 15:22:59 +00:00
|
|
|
})
|
2025-05-12 20:54:16 +00:00
|
|
|
t.Run("doc entry", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2025-06-03 13:52:10 +00:00
|
|
|
ctx := t.Context()
|
2025-05-12 20:54:16 +00:00
|
|
|
|
|
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
2025-05-23 00:20:01 +00:00
|
|
|
test.CreateDB(t, cfg)
|
2025-05-12 20:54:16 +00:00
|
|
|
|
|
|
|
|
queries := cfg.GetDBQueries()
|
|
|
|
|
|
2025-08-04 10:54:22 -07:00
|
|
|
clientId := fmt.Sprintf("EXAMPLE_%s", uuid.New().String()[:8])
|
2025-05-12 20:54:16 +00:00
|
|
|
err := queries.CreateClient(ctx, &repository.CreateClientParams{
|
2025-08-20 19:01:13 +00:00
|
|
|
Name: fmt.Sprintf("example_client_%s", clientId),
|
2025-05-12 20:54:16 +00:00
|
|
|
Clientid: clientId,
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
hash := "example_hash"
|
|
|
|
|
id, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
|
|
|
Clientid: clientId,
|
|
|
|
|
Hash: hash,
|
2025-08-04 10:54:22 -07:00
|
|
|
BatchID: nil,
|
2025-05-12 20:54:16 +00:00
|
|
|
})
|
|
|
|
|
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)
|
2025-02-21 17:05:37 +00:00
|
|
|
})
|
2025-05-12 20:54:16 +00:00
|
|
|
t.Run("doc latest entry", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2025-06-03 13:52:10 +00:00
|
|
|
ctx := t.Context()
|
2025-05-12 20:54:16 +00:00
|
|
|
|
|
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
2025-05-23 00:20:01 +00:00
|
|
|
test.CreateDB(t, cfg)
|
2025-05-12 20:54:16 +00:00
|
|
|
|
|
|
|
|
queries := cfg.GetDBQueries()
|
|
|
|
|
|
2025-08-04 10:54:22 -07:00
|
|
|
clientId := fmt.Sprintf("EXAMPLE_%s", uuid.New().String()[:8])
|
2025-05-12 20:54:16 +00:00
|
|
|
err := queries.CreateClient(ctx, &repository.CreateClientParams{
|
2025-08-20 19:01:13 +00:00
|
|
|
Name: fmt.Sprintf("example_client_%s", clientId),
|
2025-05-12 20:54:16 +00:00
|
|
|
Clientid: clientId,
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
hash := "example_hash"
|
|
|
|
|
id, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
|
|
|
Clientid: clientId,
|
|
|
|
|
Hash: hash,
|
2025-08-04 10:54:22 -07:00
|
|
|
BatchID: nil,
|
2025-05-12 20:54:16 +00:00
|
|
|
})
|
|
|
|
|
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)
|
|
|
|
|
|
2025-08-20 19:01:13 +00:00
|
|
|
// Add a small delay to ensure UUID v7 timestamps are different
|
|
|
|
|
time.Sleep(2 * time.Millisecond)
|
|
|
|
|
|
2025-05-12 20:54:16 +00:00
|
|
|
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)
|
2025-02-05 17:44:01 +00:00
|
|
|
})
|
2025-01-23 14:56:20 +00:00
|
|
|
}
|