From 89737724188fd9465e50b16cdf9e3282e1c2362d Mon Sep 17 00:00:00 2001 From: zwwhdls Date: Sun, 29 Oct 2023 22:06:45 +0800 Subject: [PATCH] add unit test for documrnt manager Signed-off-by: zwwhdls --- pkg/document/manager_test.go | 67 ++++++++++++++++++++++++++++++++++++ pkg/document/suite_test.go | 65 ++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 pkg/document/manager_test.go create mode 100644 pkg/document/suite_test.go diff --git a/pkg/document/manager_test.go b/pkg/document/manager_test.go new file mode 100644 index 00000000..2635bc1f --- /dev/null +++ b/pkg/document/manager_test.go @@ -0,0 +1,67 @@ +/* + Copyright 2023 NanaFS Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package document + +import ( + "context" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + "github.com/basenana/nanafs/pkg/types" +) + +var _ = Describe("testDocumentManage", func() { + var docId string + Context("create document", func() { + It("create should be succeed", func() { + doc := &types.Document{ + Name: "test_create_doc", + } + err := docManager.CreateDocument(context.TODO(), doc) + Expect(err).Should(BeNil()) + docId = doc.ID + }) + It("query should be succeed", func() { + doc, err := docManager.GetDocument(context.TODO(), docId) + Expect(err).Should(BeNil()) + Expect(doc.Name).Should(Equal("test_create_doc")) + }) + }) + Context("update document", func() { + It("update should be succeed", func() { + err := docManager.UpdateDocument(context.TODO(), &types.Document{ + ID: docId, + Name: "test_create_doc", + Content: "abc", + }) + Expect(err).Should(BeNil()) + doc, err := docManager.GetDocument(context.TODO(), docId) + Expect(err).Should(BeNil()) + Expect(doc.Content).Should(Equal("abc")) + }) + }) + Context("delete document", func() { + It("delete should be succeed", func() { + err := docManager.DeleteDocument(context.TODO(), docId) + Expect(err).Should(BeNil()) + doc, err := docManager.GetDocument(context.TODO(), docId) + Expect(err).ShouldNot(BeNil()) + Expect(doc).Should(BeNil()) + }) + }) +}) diff --git a/pkg/document/suite_test.go b/pkg/document/suite_test.go new file mode 100644 index 00000000..08bfab32 --- /dev/null +++ b/pkg/document/suite_test.go @@ -0,0 +1,65 @@ +/* + Copyright 2023 NanaFS Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package document + +import ( + "os" + "testing" + + "github.com/basenana/nanafs/config" + "github.com/basenana/nanafs/pkg/metastore" + "github.com/basenana/nanafs/pkg/plugin" + "github.com/basenana/nanafs/pkg/storage" + "github.com/basenana/nanafs/pkg/types" + "github.com/basenana/nanafs/utils/logger" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var ( + docRecorder metastore.DocumentRecorder + docManager Manager + root *types.Metadata + + workdir string +) + +func TestDEntry(t *testing.T) { + logger.InitLogger() + defer logger.Sync() + RegisterFailHandler(Fail) + + var err error + workdir, err = os.MkdirTemp(os.TempDir(), "ut-nanafs-dentry-") + Expect(err).Should(BeNil()) + t.Logf("unit test workdir on: %s", workdir) + storage.InitLocalCache(config.Config{CacheDir: workdir, CacheSize: 1}) + + RunSpecs(t, "DEntry Suite") +} + +var _ = BeforeSuite(func() { + memMeta, err := metastore.NewMetaStorage(storage.MemoryStorage, config.Meta{}) + Expect(err).Should(BeNil()) + docRecorder = memMeta + docManager, _ = NewManager(docRecorder) + + // init plugin + err = plugin.Init(&config.Plugin{}, memMeta) + Expect(err).Should(BeNil()) +})