From 2c3695fa62ec453a8a1dd5a72b0b24ec7b2ab459 Mon Sep 17 00:00:00 2001 From: Siddhartha Basu Date: Mon, 6 Nov 2023 15:16:31 -0600 Subject: [PATCH] feat(testutils): add test utility functions for creating and parsing content data The `testutils` package now includes two utility functions. The `NewStoreContent` function creates a new `content.NewContentAttributes` object with predefined values for testing purposes. It takes in the name and namespace as parameters and returns the created object. The `ContentFromStore` function parses a JSON string representing content data and returns a `ContentJSON` object. This function is useful for testing scenarios where content data needs to be parsed and validated. --- internal/testutils/testutils.go | 39 +++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 internal/testutils/testutils.go diff --git a/internal/testutils/testutils.go b/internal/testutils/testutils.go new file mode 100644 index 0000000..22956ff --- /dev/null +++ b/internal/testutils/testutils.go @@ -0,0 +1,39 @@ +package testutils + +import ( + "encoding/json" + "fmt" + + "github.com/dictyBase/go-genproto/dictybaseapis/content" + "github.com/dictyBase/modware-content/internal/model" +) + +type ContentJSON struct { + Paragraph string `json:"paragraph"` + Text string `json:"text"` +} + +func NewStoreContent(name, namespace string) *content.NewContentAttributes { + cdata, _ := json.Marshal(&ContentJSON{ + Paragraph: "paragraph", + Text: "text", + }) + + return &content.NewContentAttributes{ + Name: name, + Namespace: namespace, + CreatedBy: "content@content.org", + Content: string(cdata), + Slug: model.Slugify(fmt.Sprintf("%s %s", name, namespace)), + } +} + +func ContentFromStore(jsctnt string) (*ContentJSON, error) { + ctnt := &ContentJSON{} + err := json.Unmarshal([]byte(jsctnt), ctnt) + if err != nil { + return ctnt, fmt.Errorf("error in unmarshing json %s", err) + } + + return ctnt, nil +}