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 7b32266 commit 60f9c93
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
7 changes: 3 additions & 4 deletions internal/handlers/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,15 +580,14 @@ func TestReadOnlyHandlers(t *testing.T) {
compareFunc: func(t testing.TB, expected, actual any) {
expectedDoc, actualDoc := expected.(types.Document), actual.(types.Document)

testutil.CompareByPath(t, expectedDoc, actualDoc, 1_000_000, "totalSize")
testutil.CompareByPath(t, expectedDoc, actualDoc, 1, "totalSizeMb")
testutil.CompareAndSetByPath(t, expectedDoc, actualDoc, 1_000_000, "totalSize")
testutil.CompareAndSetByPath(t, expectedDoc, actualDoc, 1, "totalSizeMb")

expectedDBs := testutil.GetByPath(t, expectedDoc, "databases").(types.Array)
actualDBs := testutil.GetByPath(t, actualDoc, "databases").(types.Array)
require.Equal(t, len(expectedDBs), len(actualDBs))
for i, actualDB := range actualDBs {
expectedDB := expectedDBs[i]
testutil.CompareByPath(t, expectedDB, actualDB, 200_000, "sizeOnDisk")
testutil.CompareAndSetByPath(t, expectedDBs[i], actualDB, 200_000, "sizeOnDisk")
}

assert.Equal(t, expectedDoc, actualDoc)
Expand Down
4 changes: 2 additions & 2 deletions internal/types/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import "fmt"
// Array represents BSON array.
type Array []any

// Get returns the value at the given index.
// Get returns a value at the given index.
func (a Array) Get(index int) (any, error) {
if l := len(a); index < 0 || index >= l {
return nil, fmt.Errorf("types.Array.Get: index %d is out of bounds [0-%d)", index, l)
Expand All @@ -28,7 +28,7 @@ func (a Array) Get(index int) (any, error) {
return a[index], nil
}

// GetByPath returns Array or Object value by path - a sequence of indexes and keys.
// GetByPath returns a value by path - a sequence of indexes and keys.
func (a Array) GetByPath(path ...string) (any, error) {
return getByPath(a, path...)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/types/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (d *Document) add(key string, value any) error {
return nil
}

// Get returns the value at the given key.
// Get returns a value at the given key.
func (d Document) Get(key string) (any, error) {
if value, ok := d.m[key]; ok {
return value, nil
Expand All @@ -190,7 +190,7 @@ func (d Document) Get(key string) (any, error) {
return nil, fmt.Errorf("types.Document.Get: key not found: %q", key)
}

// GetByPath returns Array or Object value by path - a sequence of indexes and keys.
// GetByPath returns a value by path - a sequence of indexes and keys.
func (d Document) GetByPath(path ...string) (any, error) {
return getByPath(d, path...)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/types/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"strconv"
)

// getByPath returns Array or Object value by path - a sequence of indexes and keys.
// getByPath returns a value by path - a sequence of indexes and keys.
func getByPath(str any, path ...string) (any, error) {
for _, p := range path {
switch s := str.(type) {
Expand Down
26 changes: 17 additions & 9 deletions internal/util/testutil/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/FerretDB/FerretDB/internal/types"
)

// GetByPath returns a value by path - a sequence of indexes and keys.
func GetByPath(tb testing.TB, str any, path ...string) any {
tb.Helper()

Expand All @@ -43,6 +44,9 @@ func GetByPath(tb testing.TB, str any, path ...string) any {
return res
}

// SetByPath sets the value by path - a sequence of indexes and keys.
//
// The path must exist.
func SetByPath(tb testing.TB, str any, value any, path ...string) {
tb.Helper()

Expand All @@ -56,29 +60,33 @@ func SetByPath(tb testing.TB, str any, value any, path ...string) {
index, err := strconv.Atoi(p)
require.NoError(tb, err)

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

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

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

if last {
err = s.Set(p, value)
require.NoError(tb, err)
}
require.NoError(tb, err)

default:
tb.Fatalf("can't access %T by path %q", str, p)
}
}
}

func CompareByPath(tb testing.TB, expected, actual any, delta float64, path ...string) {
// CompareAndSetByPath asserts that two values with the same path in two objects (documents or arrays)
// are within a given delta, then updates the expected object with the actual value.
func CompareAndSetByPath(tb testing.TB, expected, actual any, delta float64, path ...string) {
tb.Helper()

expectedV := GetByPath(tb, expected, path...)
Expand Down

0 comments on commit 60f9c93

Please sign in to comment.