Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi committed Dec 11, 2021
1 parent 30f6a5d commit 3b579d4
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
59 changes: 59 additions & 0 deletions internal/util/testutil/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2021 FerretDB Inc.
//
// 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 testutil

import (
"strconv"
"testing"

"github.com/stretchr/testify/require"

"github.com/FerretDB/FerretDB/internal/types"
)

func SetByPath(tb testing.TB, str any, value any, path ...string) {
tb.Helper()

l := len(path)
require.NotZero(tb, l, "path is empty")

for i, p := range path {
last := i == l-1
switch s := str.(type) {
case types.Array:
index, err := strconv.Atoi(p)
require.NoError(tb, err)

if !last {
str, err = s.Get(index)
} else {
err = s.Set(index, value)
}
require.NoError(tb, err)

case types.Document:
var err error
if !last {
str, err = s.Get(p)
} else {
err = s.Set(p, value)
}
require.NoError(tb, err)

default:
tb.Fatalf("can't access %T by path %q", str, p)
}
}
}
74 changes: 74 additions & 0 deletions internal/util/testutil/path_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2021 FerretDB Inc.
//
// 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 testutil

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"

"github.com/FerretDB/FerretDB/internal/types"
)

func TestSetByPath(t *testing.T) {
t.Parallel()

newDoc := func() types.Document {
return types.MustMakeDocument(
"client", types.MustMakeDocument(
"driver", types.MustMakeDocument(
"name", "nodejs",
),
),
"compression", types.Array{"none"},
)
}

type testCase struct {
path []string
value any
res any
}

for _, tc := range []testCase{{ //nolint:paralleltest // false positive
path: []string{"compression", "0"},
value: "zstd",
res: types.MustMakeDocument(
"client", types.MustMakeDocument(
"driver", types.MustMakeDocument(
"name", "nodejs",
),
),
"compression", types.Array{"zstd"},
),
}, {
path: []string{"client"},
value: "foo",
res: types.MustMakeDocument(
"client", "foo",
"compression", types.Array{"none"},
),
}} {
tc := tc
t.Run(fmt.Sprint(tc.path), func(t *testing.T) {
t.Parallel()

doc := newDoc()
SetByPath(t, doc, tc.value, tc.path...)
assert.Equal(t, tc.res, doc)
})
}
}

0 comments on commit 3b579d4

Please sign in to comment.