Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rename client to ent, update docs annotations queries to only return document info #51

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion backends/ent/annotations.go
Original file line number Diff line number Diff line change
@@ -182,6 +182,7 @@ func (backend *Backend) GetDocumentAnnotations(documentID string, names ...strin

predicates := []predicate.Annotation{
annotation.HasDocumentWith(document.HasMetadataWith(metadata.NativeIDEQ(documentID))),
annotation.Not(annotation.HasNode()),
}

if len(names) > 0 {
@@ -203,7 +204,10 @@ func (backend *Backend) GetDocumentsByAnnotation(name string, values ...string)
return nil, errUninitializedClient
}

predicates := []predicate.Annotation{annotation.NameEQ(name)}
predicates := []predicate.Annotation{
annotation.NameEQ(name),
annotation.Not(annotation.HasNode()),
}

if len(values) > 0 {
predicates = append(predicates, annotation.ValueIn(values...))
@@ -241,6 +245,7 @@ func (backend *Backend) GetDocumentUniqueAnnotation(documentID, name string) (st
),
annotation.NameEQ(name),
annotation.IsUniqueEQ(true),
annotation.Not(annotation.HasNode()),
).
Only(backend.ctx)
if err != nil {
14 changes: 7 additions & 7 deletions backends/ent/annotations_test.go
Original file line number Diff line number Diff line change
@@ -243,7 +243,7 @@ func (as *annotationsSuite) TestBackend_GetDocumentsByAnnotation() {
"test-value-" + strconv.Itoa(idx+2),
"test-value-" + strconv.Itoa(idx+3),
} {
_, err = as.Backend.Client().ExecContext(as.Backend.Context(), query, uniqueID, false, annotationName, value)
_, err = as.Backend.Ent().ExecContext(as.Backend.Context(), query, uniqueID, false, annotationName, value)
as.Require().NoError(err)
}
}
@@ -307,7 +307,7 @@ func (as *annotationsSuite) TestBackend_GetNodesByAnnotation() {
"test-node-value-" + strconv.Itoa(idx+2),
"test-node-value-" + strconv.Itoa(idx+3),
} {
_, err := as.Backend.Client().ExecContext(
_, err := as.Backend.Ent().ExecContext(
as.Backend.Context(), query, uniqueID, nodeUUID, false, annotationName, value,
)
as.Require().NoError(err)
@@ -362,7 +362,7 @@ func (as *annotationsSuite) TestBackend_GetDocumentUniqueAnnotation() {
annotationValue := "unique-value"
query := "INSERT INTO annotations (document_id, is_unique, name, value) VALUES (?, ?, ?, ?)"

_, err = as.Backend.Client().ExecContext(as.Backend.Context(), query, id, true, annotationName, annotationValue)
_, err = as.Backend.Ent().ExecContext(as.Backend.Context(), query, id, true, annotationName, annotationValue)
as.Require().NoError(err)

got, err := as.Backend.GetDocumentUniqueAnnotation(as.documents[0].GetMetadata().GetId(), annotationName)
@@ -382,7 +382,7 @@ func (as *annotationsSuite) TestBackend_GetNodeUniqueAnnotation() {
annotationValue := "unique-value"
query := "INSERT INTO annotations (document_id, node_id, is_unique, name, value) VALUES (?, ?, ?, ?, ?)"

_, err = as.Backend.Client().ExecContext(
_, err = as.Backend.Ent().ExecContext(
as.Backend.Context(), query, docUUID, nodeUUID, true, annotationName, annotationValue,
)
as.Require().NoError(err)
@@ -423,7 +423,7 @@ func (as *annotationsSuite) TestBackend_RemoveDocumentAnnotations() {

as.Run(name, func() {
for _, value := range []string{"test-value-1", "test-value-2", "test-value-3"} {
_, err = as.Backend.Client().ExecContext(ctx, query, uniqueID, false, annotationName, value)
_, err = as.Backend.Ent().ExecContext(ctx, query, uniqueID, false, annotationName, value)
as.Require().NoError(err)
}

@@ -476,7 +476,7 @@ func (as *annotationsSuite) TestBackend_RemoveNodeAnnotations() {

as.Run(name, func() {
for _, value := range []string{"test-node-value-1", "test-node-value-2", "test-node-value-3"} {
_, err = as.Backend.Client().ExecContext(ctx, query, docUUID, nodeID, false, annotationName, value)
_, err = as.Backend.Ent().ExecContext(ctx, query, docUUID, nodeID, false, annotationName, value)
as.Require().NoError(err)
}

@@ -632,7 +632,7 @@ func (as *annotationsSuite) TestBackend_SetNodeUniqueAnnotation() { //nolint:dup
func (as *annotationsSuite) getTestResult(annotationName string) ent.Annotations {
as.T().Helper()

result, err := as.Backend.Client().QueryContext(
result, err := as.Backend.Ent().QueryContext(
as.Backend.Context(),
"SELECT * FROM annotations WHERE name == ?",
annotationName,
4 changes: 4 additions & 0 deletions backends/ent/backend.go
Original file line number Diff line number Diff line change
@@ -43,6 +43,10 @@ func NewBackend(opts ...Option) *Backend {
return backend
}

func (backend *Backend) Ent() *ent.Client {
return backend.client
}

func (backend *Backend) InitClient() error {
if backend.Options == nil {
backend.Options = NewBackendOptions()
10 changes: 1 addition & 9 deletions backends/ent/internal_test.go
Original file line number Diff line number Diff line change
@@ -6,15 +6,7 @@

package ent

import (
"context"

"github.com/protobom/storage/internal/backends/ent"
)

func (backend *Backend) Client() *ent.Client {
return backend.client
}
import "context"

func (backend *Backend) Context() context.Context {
return backend.ctx
2 changes: 1 addition & 1 deletion backends/ent/store_test.go
Original file line number Diff line number Diff line change
@@ -65,7 +65,7 @@ func (ss *storeSuite) TestBackend_Store() {
ss.Require().NoError(ss.Backend.Store(document, nil))
}

results, err := ss.Backend.Client().Document.
results, err := ss.Backend.Ent().Document.
Query().
WithMetadata().
WithNodeList().