2025-01-23 14:56:20 +00:00
|
|
|
package repository_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-03-05 12:05:46 +00:00
|
|
|
"testing"
|
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
|
|
"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-04-22 14:40:16 +00:00
|
|
|
t.Parallel()
|
2025-02-07 14:15:06 +00:00
|
|
|
if testing.Short() {
|
|
|
|
|
t.Skip("Skipping long test in short mode")
|
|
|
|
|
}
|
2025-01-23 14:56:20 +00:00
|
|
|
ctx := context.Background()
|
2025-01-31 13:43:55 +00:00
|
|
|
|
2025-02-05 12:52:41 +00:00
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
2025-04-22 14:40:16 +00:00
|
|
|
net := test.DepNetwork.Get(t, ctx)
|
|
|
|
|
_ = test.CreateDB(t, ctx, cfg, net, &test.CreateDatabaseConfig{
|
2025-01-31 13:43:55 +00:00
|
|
|
RunMigrations: true,
|
|
|
|
|
})
|
2025-01-23 14:56:20 +00:00
|
|
|
|
2025-02-05 12:52:41 +00:00
|
|
|
queries := cfg.GetDBQueries()
|
2025-01-23 14:56:20 +00:00
|
|
|
|
2025-04-02 18:50:03 +00:00
|
|
|
clientId := "EXAMPLE"
|
|
|
|
|
err := queries.CreateClient(ctx, &repository.CreateClientParams{
|
|
|
|
|
Name: "example_client",
|
|
|
|
|
ID: clientId,
|
2025-03-11 16:31:06 +00:00
|
|
|
})
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-01-23 14:56:20 +00:00
|
|
|
|
2025-04-02 18:50:03 +00:00
|
|
|
docs, err := queries.ListDocumentsByClient(ctx, clientId)
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-02-21 17:05:37 +00:00
|
|
|
assert.Len(t, docs, 0)
|
|
|
|
|
|
2025-01-24 16:12:25 +00:00
|
|
|
hash := "example_hash"
|
|
|
|
|
id, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
|
2025-03-10 11:03:00 +00:00
|
|
|
Clientid: clientId,
|
|
|
|
|
Hash: hash,
|
2025-01-24 16:12:25 +00:00
|
|
|
})
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-01-23 14:56:20 +00:00
|
|
|
assert.NotEmpty(t, id)
|
2025-02-21 17:05:37 +00:00
|
|
|
|
|
|
|
|
bucketone := "bucketone"
|
|
|
|
|
keyone := "keyone"
|
|
|
|
|
err = queries.AddDocumentEntry(ctx, &repository.AddDocumentEntryParams{
|
|
|
|
|
Documentid: id,
|
|
|
|
|
Bucket: bucketone,
|
|
|
|
|
Key: keyone,
|
|
|
|
|
})
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-02-21 17:05:37 +00:00
|
|
|
|
2025-04-02 18:50:03 +00:00
|
|
|
docs, err = queries.ListDocumentsByClient(ctx, clientId)
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-02-21 17:05:37 +00:00
|
|
|
assert.Len(t, docs, 1)
|
2025-03-17 18:14:15 +00:00
|
|
|
assert.Equal(t, hash, docs[0].Hash)
|
2025-02-21 17:05:37 +00:00
|
|
|
|
2025-02-11 15:22:59 +00:00
|
|
|
documentTwoID, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
|
2025-03-10 11:03:00 +00:00
|
|
|
Clientid: clientId,
|
|
|
|
|
Hash: "example_hash_two",
|
2025-02-11 15:22:59 +00:00
|
|
|
})
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-02-11 15:22:59 +00:00
|
|
|
assert.NotEmpty(t, documentTwoID)
|
2025-02-21 17:05:37 +00:00
|
|
|
|
|
|
|
|
buckettwo := "buckettwo"
|
|
|
|
|
keytwo := "keytwo"
|
|
|
|
|
err = queries.AddDocumentEntry(ctx, &repository.AddDocumentEntryParams{
|
|
|
|
|
Documentid: documentTwoID,
|
|
|
|
|
Bucket: buckettwo,
|
|
|
|
|
Key: keytwo,
|
|
|
|
|
})
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-02-21 17:05:37 +00:00
|
|
|
|
2025-04-02 18:50:03 +00:00
|
|
|
docs, err = queries.ListDocumentsByClient(ctx, clientId)
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-02-21 17:05:37 +00:00
|
|
|
assert.Len(t, docs, 2)
|
|
|
|
|
|
2025-04-02 18:50:03 +00:00
|
|
|
clientTwoId := "EXAMPLE TWO"
|
|
|
|
|
err = queries.CreateClient(ctx, &repository.CreateClientParams{
|
|
|
|
|
Name: "example_name_two",
|
|
|
|
|
ID: clientTwoId,
|
2025-03-11 16:31:06 +00:00
|
|
|
})
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-02-21 17:05:37 +00:00
|
|
|
|
2025-04-02 18:50:03 +00:00
|
|
|
docs, err = queries.ListDocumentsByClient(ctx, clientId)
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-02-21 17:05:37 +00:00
|
|
|
assert.Len(t, docs, 2)
|
2025-04-02 18:50:03 +00:00
|
|
|
docs, err = queries.ListDocumentsByClient(ctx, clientTwoId)
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-02-21 17:05:37 +00:00
|
|
|
assert.Len(t, docs, 0)
|
|
|
|
|
|
|
|
|
|
documentThreeId, err := queries.CreateDocument(ctx, &repository.CreateDocumentParams{
|
2025-03-10 11:03:00 +00:00
|
|
|
Clientid: clientTwoId,
|
2025-03-17 18:14:15 +00:00
|
|
|
Hash: hash,
|
2025-02-11 15:22:59 +00:00
|
|
|
})
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-01-23 14:56:20 +00:00
|
|
|
|
2025-02-21 17:05:37 +00:00
|
|
|
bucketthree := "buckettwo"
|
|
|
|
|
keythree := "keytwo"
|
|
|
|
|
err = queries.AddDocumentEntry(ctx, &repository.AddDocumentEntryParams{
|
|
|
|
|
Documentid: documentThreeId,
|
|
|
|
|
Bucket: bucketthree,
|
|
|
|
|
Key: keythree,
|
|
|
|
|
})
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-02-21 17:05:37 +00:00
|
|
|
|
2025-04-02 18:50:03 +00:00
|
|
|
docs, err = queries.ListDocumentsByClient(ctx, clientId)
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-02-21 17:05:37 +00:00
|
|
|
assert.Len(t, docs, 2)
|
2025-04-02 18:50:03 +00:00
|
|
|
docs, err = queries.ListDocumentsByClient(ctx, clientTwoId)
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-02-21 17:05:37 +00:00
|
|
|
assert.Len(t, docs, 1)
|
2025-03-17 18:14:15 +00:00
|
|
|
assert.Equal(t, hash, docs[0].Hash)
|
2025-02-21 17:05:37 +00:00
|
|
|
|
2025-03-17 18:14:15 +00:00
|
|
|
doc, err := queries.GetDocumentSummary(ctx, id)
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-01-23 14:56:20 +00:00
|
|
|
assert.EqualExportedValues(t, &repository.Document{
|
2025-03-10 11:03:00 +00:00
|
|
|
ID: id,
|
|
|
|
|
Clientid: clientId,
|
|
|
|
|
Hash: hash,
|
2025-01-23 14:56:20 +00:00
|
|
|
}, doc)
|
2025-02-05 17:44:01 +00:00
|
|
|
|
2025-04-02 18:50:03 +00:00
|
|
|
docext, err := queries.GetDocumentExternal(ctx, id)
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-03-17 18:14:15 +00:00
|
|
|
assert.EqualExportedValues(t, &repository.GetDocumentExternalRow{
|
|
|
|
|
ID: id,
|
2025-04-02 18:50:03 +00:00
|
|
|
Clientid: clientId,
|
2025-03-17 18:14:15 +00:00
|
|
|
Hash: hash,
|
|
|
|
|
Fields: []byte("{}"),
|
|
|
|
|
}, docext)
|
|
|
|
|
|
2025-02-05 17:44:01 +00:00
|
|
|
docid, err := queries.GetDocumentIDByHash(ctx, &repository.GetDocumentIDByHashParams{
|
2025-03-10 11:03:00 +00:00
|
|
|
Hash: hash,
|
|
|
|
|
Clientid: clientId,
|
2025-02-05 17:44:01 +00:00
|
|
|
})
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-02-05 17:44:01 +00:00
|
|
|
assert.EqualExportedValues(t, id, docid)
|
2025-02-07 12:12:51 +00:00
|
|
|
|
|
|
|
|
entry, err := queries.GetDocumentEntry(ctx, id)
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-02-07 12:12:51 +00:00
|
|
|
assert.EqualExportedValues(t, &repository.GetDocumentEntryRow{
|
|
|
|
|
Documentid: id,
|
2025-02-21 17:05:37 +00:00
|
|
|
Bucket: bucketone,
|
|
|
|
|
Key: keyone,
|
2025-02-07 12:12:51 +00:00
|
|
|
}, entry)
|
|
|
|
|
|
2025-02-21 17:05:37 +00:00
|
|
|
entry, err = queries.GetDocumentEntry(ctx, documentTwoID)
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-02-07 12:12:51 +00:00
|
|
|
assert.EqualExportedValues(t, &repository.GetDocumentEntryRow{
|
2025-02-21 17:05:37 +00:00
|
|
|
Documentid: documentTwoID,
|
|
|
|
|
Bucket: buckettwo,
|
|
|
|
|
Key: keytwo,
|
2025-02-07 12:12:51 +00:00
|
|
|
}, entry)
|
|
|
|
|
|
2025-02-21 17:05:37 +00:00
|
|
|
entry, err = queries.GetDocumentEntry(ctx, documentThreeId)
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-02-07 12:12:51 +00:00
|
|
|
assert.EqualExportedValues(t, &repository.GetDocumentEntryRow{
|
2025-02-21 17:05:37 +00:00
|
|
|
Documentid: documentThreeId,
|
|
|
|
|
Bucket: bucketthree,
|
|
|
|
|
Key: keythree,
|
2025-02-07 12:12:51 +00:00
|
|
|
}, entry)
|
2025-01-23 14:56:20 +00:00
|
|
|
}
|