Skip to content

Commit

Permalink
test: Add some more tests to the Sync function
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobefu committed Jan 20, 2025
1 parent b6225bc commit 4cb5034
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
6 changes: 3 additions & 3 deletions cmd/cs_sdk/functions/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func addChildRoutes(routes *map[string]structs.Route) error {
err := addRouteChildren(route, routes, 0)

if err != nil {
continue
return err
}
}

Expand All @@ -357,7 +357,7 @@ func addParentRoutes(routes *map[string]structs.Route) error {
err := addRouteParents(route, routes, 0)

if err != nil {
continue
return err
}
}

Expand Down Expand Up @@ -406,7 +406,7 @@ func addRouteParents(route structs.Route, routes *map[string]structs.Route, dept
parentId := utilsGenerateId(route.Parent, route.Locale)

if parentId == "" {
return errors.New("the parent UID is an empty string")
return nil
}

parentRoute := (*routes)[parentId]
Expand Down
64 changes: 63 additions & 1 deletion cmd/cs_sdk/functions/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package functions

import (
"errors"
"fmt"
"testing"

"github.com/Dobefu/csb/cmd/api"
Expand Down Expand Up @@ -48,7 +49,9 @@ func setupTestSync() func() {
return data, nil
}

utilsGenerateId = func(uid string, locale string) string { return "" }
utilsGenerateId = func(uid string, locale string) string {
return fmt.Sprintf("%s%s", uid, locale)
}

apiGetChildEntriesByUid = func(uid string, locale string, includeUnpublished bool) ([]structs.Route, error) {
return []structs.Route{}, nil
Expand Down Expand Up @@ -164,3 +167,62 @@ func TestSyncNoResetErrProcessSyncData(t *testing.T) {
err := Sync(false)
assert.EqualError(t, err, "failed setting route")
}

func TestAddAllAssetsErrNoItems(t *testing.T) {
cleanup := setupTestSync()
defer cleanup()

err := addAllAssets(make(map[string]interface{}))
assert.EqualError(t, err, "sync data has no items")
}

func TestAddAllRoutesErrAddRouteChildren(t *testing.T) {
cleanup := setupTestSync()
defer cleanup()

apiGetChildEntriesByUid = func(uid string, locale string, includeUnpublished bool) ([]structs.Route, error) {
return nil, errors.New("cannot get child entries")
}

err := addAllRoutes(
map[string]interface{}{
"items": []interface{}{
map[string]interface{}{
"content_type_uid": "page",
"data": map[string]interface{}{
"uid": "test-uid-route",
"locale": "en",
},
},
},
},
&(map[string]structs.Route{}),
)

assert.EqualError(t, err, "cannot get child entries")
}

func TestAddAllRoutesErrAddRouteParents(t *testing.T) {
cleanup := setupTestSync()
defer cleanup()

entries := map[string]interface{}{"items": make([]interface{}, 11)}

for i := 0; i < 11; i++ {
entries["items"].([]interface{})[i] = map[string]interface{}{
"content_type_uid": "page",
"data": map[string]interface{}{
"uid": fmt.Sprintf("uid-%d", i),
"locale": "en",
"parent": []interface{}{map[string]interface{}{"uid": fmt.Sprintf("uid-%d", i-1)}},
},
}
}

err := addAllRoutes(
entries,
&(map[string]structs.Route{}),
)

assert.EqualError(t, err, "potential infinite loop detected")
}

0 comments on commit 4cb5034

Please sign in to comment.