Skip to content

Commit

Permalink
fix unit test
Browse files Browse the repository at this point in the history
Signed-off-by: zwwhdls <zww@hdls.me>
  • Loading branch information
zwwhdls committed Dec 9, 2024
1 parent 828863a commit cc4e053
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 42 deletions.
52 changes: 26 additions & 26 deletions pkg/service/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
type Chain struct {
MeiliClient docstore.DocStoreInterface
Plugins []plugin.ChainPlugin
log *zap.SugaredLogger
Log *zap.SugaredLogger
}

var ChainPool *dispatch.Pool
Expand All @@ -51,29 +51,29 @@ func NewChain(conf config.Config) (*Chain, error) {
return &Chain{
MeiliClient: client,
Plugins: plugins,
log: log,
Log: log,
}, nil
}

func (c *Chain) Store(ctx context.Context, document *doc.Document) error {
document.Kind = "document"
return ChainPool.Run(ctx, func(ctx context.Context) error {
c.log.Debugf("store document: %+v", document.String())
c.Log.Debugf("store document: %+v", document.String())
if d, err := c.GetDocument(ctx, document.Namespace, document.EntryId); err != nil {
c.log.Errorf("get document error: %s", err)
c.Log.Errorf("get document error: %s", err)
return err
} else if d != nil {
c.log.Debugf("document already exists: %+v", d.String())
c.Log.Debugf("document already exists: %+v", d.String())
return nil
}
for _, plugin := range c.Plugins {
err := plugin.Run(ctx, document)
if err != nil {
c.log.Errorf("plugin error: %s", err)
c.Log.Errorf("plugin error: %s", err)
return err
}
}
c.log.Debugf("store document: %+v", document.String())
c.Log.Debugf("store document: %+v", document.String())
return c.MeiliClient.Store(ctx, document)
})
}
Expand Down Expand Up @@ -103,17 +103,17 @@ func (c *Chain) StoreAttr(ctx context.Context, docAttr *doc.DocumentAttr) error
Value: "attr",
}},
}); err != nil {
c.log.Errorf("delete document attr error: %s", err)
c.Log.Errorf("delete document attr error: %s", err)
return err
}
docAttr.Kind = "attr"
c.log.Debugf("store attr: %+v", docAttr.String())
c.Log.Debugf("store attr: %+v", docAttr.String())
return c.MeiliClient.Store(ctx, docAttr)
})
}

func (c *Chain) GetDocument(ctx context.Context, namespace, entryId string) (*doc.Document, error) {
c.log.Debugf("get document: namespace=%s, entryId=%s", namespace, entryId)
c.Log.Debugf("get document: namespace=%s, entryId=%s", namespace, entryId)
docs, err := c.MeiliClient.Search(ctx, &doc.DocumentQuery{
AttrQueries: []*doc.AttrQuery{
{
Expand All @@ -137,14 +137,14 @@ func (c *Chain) GetDocument(ctx context.Context, namespace, entryId string) (*do
Page: 1,
})
if err != nil {
c.log.Errorf("get document error: %s", err)
c.Log.Errorf("get document error: %s", err)
return nil, err
}
if len(docs) == 0 {
c.log.Debugf("document not found: namespace=%s, entryId=%s", namespace, entryId)
c.Log.Debugf("document not found: namespace=%s, entryId=%s", namespace, entryId)
return nil, nil
}
c.log.Debugf("get document: %+v", docs[0].String())
c.Log.Debugf("get document: %+v", docs[0].String())
return docs[0], nil
}

Expand All @@ -168,13 +168,13 @@ func (c *Chain) ListDocumentAttrs(ctx context.Context, namespace string, entryId
},
},
}
c.log.Debugf("list document attrs: %+v", docAttrQuery.String())
c.Log.Debugf("list document attrs: %+v", docAttrQuery.String())
attrs, err := c.MeiliClient.FilterAttr(ctx, docAttrQuery)
if err != nil {
c.log.Errorf("list document attrs error: %s", err)
c.Log.Errorf("list document attrs error: %s", err)
return nil, err
}
c.log.Debugf("list %d document attrs: %s", len(attrs), attrs.String())
c.Log.Debugf("list %d document attrs: %s", len(attrs), attrs.String())
return attrs, nil
}

Expand All @@ -198,13 +198,13 @@ func (c *Chain) GetDocumentAttrs(ctx context.Context, namespace, entryId string)
},
},
}
c.log.Debugf("get document attrs: %+v", docAttrQuery.String())
c.Log.Debugf("get document attrs: %+v", docAttrQuery.String())
attrs, err := c.MeiliClient.FilterAttr(ctx, docAttrQuery)
if err != nil {
c.log.Errorf("get document attrs error: %s", err)
c.Log.Errorf("get document attrs error: %s", err)
return nil, err
}
c.log.Debugf("get %d document attrs: %s", len(attrs), attrs.String())
c.Log.Debugf("get %d document attrs: %s", len(attrs), attrs.String())
return attrs, nil
}

Expand All @@ -216,14 +216,14 @@ func (c *Chain) Search(ctx context.Context, query *doc.DocumentQuery, attrQuerie
Option: "=",
Value: "attr",
})
c.log.Debugf("filter attr query: %+v", attrQuery.String())
c.Log.Debugf("filter attr query: %+v", attrQuery.String())
attr, err := c.MeiliClient.FilterAttr(ctx, attrQuery)
if err != nil {
return nil, err
}
attrs = append(attrs, attr...)
}
c.log.Debugf("filter %d attrs: %s", len(attrs), attrs.String())
c.Log.Debugf("filter %d attrs: %s", len(attrs), attrs.String())
ids := []string{}
for _, attr := range attrs {
ids = append(ids, attr.EntryId)
Expand All @@ -244,22 +244,22 @@ func (c *Chain) Search(ctx context.Context, query *doc.DocumentQuery, attrQuerie
Value: ids,
})
}
c.log.Debugf("search document query: %+v", query.String())
c.Log.Debugf("search document query: %+v", query.String())
docs, err := c.MeiliClient.Search(ctx, query)
if err != nil {
c.log.Errorf("search document error: %s", err)
c.Log.Errorf("search document error: %s", err)
return nil, err
}
c.log.Debugf("search %d documents: %s", len(docs), docs.String())
c.Log.Debugf("search %d documents: %s", len(docs), docs.String())
return docs, nil
}

func (c *Chain) DeleteByFilter(ctx context.Context, queries doc.DocumentAttrQuery) error {
return ChainPool.Run(ctx, func(ctx context.Context) error {
c.log.Debugf("delete by filter: %+v", queries.String())
c.Log.Debugf("delete by filter: %+v", queries.String())
err := c.MeiliClient.DeleteByFilter(ctx, queries)
if err != nil {
c.log.Errorf("delete by filter error: %s", err)
c.Log.Errorf("delete by filter error: %s", err)
return err
}
return nil
Expand Down
37 changes: 21 additions & 16 deletions pkg/service/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/basenana/friday/pkg/models/doc"
"github.com/basenana/friday/pkg/service"
"github.com/basenana/friday/pkg/store/docstore"
"github.com/basenana/friday/pkg/utils/logger"
)

var _ = Describe("Chain", func() {
Expand All @@ -48,9 +49,11 @@ var _ = Describe("Chain", func() {

BeforeEach(func() {
service.ChainPool = dispatch.NewPool(10)
logger.InitLog()
Chain = &service.Chain{
MeiliClient: &docstore.MockClient{},
Plugins: []plugin.ChainPlugin{},
Log: logger.NewLog("test"),
}
for _, p := range plugin.DefaultRegisterer.Chains {
Chain.Plugins = append(Chain.Plugins, p)
Expand Down Expand Up @@ -137,7 +140,7 @@ var _ = Describe("Chain", func() {
Context("search document", func() {
It("search document should be successful", func() {
docs, err := Chain.Search(context.TODO(), &doc.DocumentQuery{
AttrQueries: []doc.AttrQuery{{
AttrQueries: []*doc.AttrQuery{{
Attr: "namespace",
Option: "=",
Value: "test-ns",
Expand All @@ -149,15 +152,15 @@ var _ = Describe("Chain", func() {
})
It("search document with attr should be successful", func() {
docs, err := Chain.Search(context.TODO(), &doc.DocumentQuery{
AttrQueries: []doc.AttrQuery{{
AttrQueries: []*doc.AttrQuery{{
Attr: "namespace",
Option: "=",
Value: "test-ns",
}},
Search: "test",
}, []*doc.DocumentAttrQuery{
{
AttrQueries: []doc.AttrQuery{
AttrQueries: []*doc.AttrQuery{
{
Attr: "parentId",
Option: "=",
Expand Down Expand Up @@ -206,21 +209,23 @@ var _ = Describe("Chain", func() {
EntryId: "10",
})
Expect(err).Should(BeNil())
err = Chain.DeleteByFilter(context.TODO(), []doc.AttrQuery{
{
Attr: "namespace",
Option: "=",
Value: "test-ns",
},
{
Attr: "entryId",
Option: "=",
Value: "10",
},
})
err = Chain.DeleteByFilter(context.TODO(), doc.DocumentAttrQuery{
AttrQueries: []*doc.AttrQuery{
{
Attr: "namespace",
Option: "=",
Value: "test-ns",
},
{
Attr: "entryId",
Option: "=",
Value: "10",
},
}},
)
Expect(err).Should(BeNil())
docs, err := Chain.Search(context.TODO(), &doc.DocumentQuery{
AttrQueries: []doc.AttrQuery{{
AttrQueries: []*doc.AttrQuery{{
Attr: "entryId",
Option: "=",
Value: "10",
Expand Down

0 comments on commit cc4e053

Please sign in to comment.