Skip to content

Commit

Permalink
test(arangodb_test.go): update test cases to use helper functions fro…
Browse files Browse the repository at this point in the history
…m testutils package

The changes in this commit remove unused imports and functions from the
arangodb_test.go file. It also updates the test cases to use helper
functions from the testutils package, which provides functions for
creating and parsing content data. This improves code readability and
maintainability by reducing duplication and improving test organization.
  • Loading branch information
cybersiddhu committed Nov 6, 2023
1 parent 2c3695f commit 8b8dadd
Showing 1 changed file with 12 additions and 42 deletions.
54 changes: 12 additions & 42 deletions internal/repository/arangodb/arangodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package arangodb

import (
"encoding/json"
"fmt"
"strconv"
"testing"
"time"
Expand All @@ -12,39 +11,10 @@ import (
"github.com/dictyBase/go-genproto/dictybaseapis/content"
"github.com/dictyBase/modware-content/internal/model"
"github.com/dictyBase/modware-content/internal/repository"
"github.com/dictyBase/modware-content/internal/testutils"
"github.com/stretchr/testify/require"
)

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: "[email protected]",
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
}

func setUp(t *testing.T) (*require.Assertions, repository.ContentRepository) {
t.Helper()
tra, err := testarango.NewTestArangoFromEnv(true)
Expand Down Expand Up @@ -79,7 +49,7 @@ func TestAddContent(t *testing.T) {
t.Parallel()
assert, repo := setUp(t)
defer tearDown(repo)
nct, err := repo.AddContent(NewStoreContent("catalog", "dsc"))
nct, err := repo.AddContent(testutils.NewStoreContent("catalog", "dsc"))
assert.NoErrorf(err, "expect no error from creating content %s", err)
assert.Equal(nct.Name, "catalog", "name should match")
assert.Equal(nct.Namespace, "dsc", "namespace should match")
Expand All @@ -97,11 +67,11 @@ func TestAddContent(t *testing.T) {
nct.CreatedOn.Before(time.Now()),
"should have created before the current time",
)
ctnt, err := ContentFromStore(nct.Content)
ctnt, err := testutils.ContentFromStore(nct.Content)
assert.NoError(err, "should not have any error with json unmarshaling")
assert.Equal(
ctnt,
&ContentJSON{Paragraph: "paragraph", Text: "text"},
&testutils.ContentJSON{Paragraph: "paragraph", Text: "text"},
"should match the content",
)
}
Expand All @@ -110,7 +80,7 @@ func TestGetContentBySlug(t *testing.T) {
t.Parallel()
assert, repo := setUp(t)
defer tearDown(repo)
nct, err := repo.AddContent(NewStoreContent("catalog", "dsc"))
nct, err := repo.AddContent(testutils.NewStoreContent("catalog", "dsc"))
assert.NoErrorf(err, "expect no error from creating content %s", err)
sct, err := repo.GetContentBySlug(nct.Slug)
assert.NoErrorf(err, "expect no error from getting content by slug %s", err)
Expand All @@ -121,7 +91,7 @@ func TestGetContent(t *testing.T) {
t.Parallel()
assert, repo := setUp(t)
defer tearDown(repo)
nct, err := repo.AddContent(NewStoreContent("catalog", "dsc"))
nct, err := repo.AddContent(testutils.NewStoreContent("catalog", "dsc"))
assert.NoErrorf(err, "expect no error from creating content %s", err)
key, err := strconv.ParseInt(nct.Key, 10, 64)
assert.NoErrorf(
Expand All @@ -138,7 +108,7 @@ func TestDeleteContent(t *testing.T) {
t.Parallel()
assert, repo := setUp(t)
defer tearDown(repo)
nct, err := repo.AddContent(NewStoreContent("catalog", "dsc"))
nct, err := repo.AddContent(testutils.NewStoreContent("catalog", "dsc"))
assert.NoErrorf(err, "expect no error from creating content %s", err)
key, err := strconv.ParseInt(nct.Key, 10, 64)
assert.NoErrorf(
Expand All @@ -161,15 +131,15 @@ func TestEditContent(t *testing.T) {
t.Parallel()
assert, repo := setUp(t)
defer tearDown(repo)
nct, err := repo.AddContent(NewStoreContent("catalog", "dsc"))
nct, err := repo.AddContent(testutils.NewStoreContent("catalog", "dsc"))
assert.NoErrorf(err, "expect no error from creating content %s", err)
key, err := strconv.ParseInt(nct.Key, 10, 64)
assert.NoErrorf(
err,
"expect no error from string to int64 conversion of key %s",
err,
)
cdata, _ := json.Marshal(&ContentJSON{
cdata, _ := json.Marshal(&testutils.ContentJSON{
Paragraph: "clompous",
Text: "jack",
})
Expand Down Expand Up @@ -201,11 +171,11 @@ func TestSchemaValidation(t *testing.T) {
t.Parallel()
assert, repo := setUp(t)
defer tearDown(repo)
_, err := repo.AddContent(NewStoreContent("catalog", "dsc"))
_, err := repo.AddContent(testutils.NewStoreContent("catalog", "dsc"))
assert.NoErrorf(err, "expect no error from creating content %s", err)
_, err = repo.AddContent(NewStoreContent("catalog", "dsc"))
_, err = repo.AddContent(testutils.NewStoreContent("catalog", "dsc"))
assert.Error(err, "expect schema validation error for duplicate slug")
ncnt := NewStoreContent("price", "dsc")
ncnt := testutils.NewStoreContent("price", "dsc")
ncnt.CreatedBy = "yadayadayada"
_, err = repo.AddContent(ncnt)
assert.Error(
Expand Down

0 comments on commit 8b8dadd

Please sign in to comment.