gof3/tree/tests/f3/forge_compliance.go
Twenty Panda 05f8b56e88
All checks were successful
/ backend-checks (pull_request) Successful in 6m49s
generic: refactor NodeID to encapsulate string/int conversions
2024-06-27 11:00:20 +02:00

168 lines
5.7 KiB
Go

// Copyright Earl Warren <contact@earl-warren.org>
// Copyright Loïc Dachary <loic@dachary.org>
// SPDX-License-Identifier: MIT
package f3
import (
"context"
"slices"
"testing"
"time"
"code.forgejo.org/f3/gof3/v3/f3"
filesystem_options "code.forgejo.org/f3/gof3/v3/forges/filesystem/options"
f3_tree "code.forgejo.org/f3/gof3/v3/tree/f3"
"code.forgejo.org/f3/gof3/v3/tree/generic"
tests_forge "code.forgejo.org/f3/gof3/v3/tree/tests/f3/forge"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func ForgeCompliance(t *testing.T, name string) {
testForge := tests_forge.GetFactory(name)()
ctx := context.Background()
forgeOptions := testForge.NewOptions(t)
forgeTree := generic.GetFactory("f3")(ctx, forgeOptions)
forgeTree.Trace("======= build fixture")
fixtureTree := generic.GetFactory("f3")(ctx, tests_forge.GetFactory(filesystem_options.Name)().NewOptions(t))
TreeBuildPartial(t, name, testForge.GetExceptions(), forgeOptions, fixtureTree)
forgeTree.Trace("======= mirror fixture to forge")
generic.TreeMirror(ctx, fixtureTree, forgeTree, generic.NewPathFromString(""), generic.NewMirrorOptions())
forgeTree.Trace("======= run compliance tests")
kindToFixturePath := make(map[generic.Kind]string, len(KindToFixturePath))
for kind, path := range KindToFixturePath {
remappedPath := generic.TreePathRemap(ctx, fixtureTree, generic.NewPathFromString(path), forgeTree)
if remappedPath.Empty() {
forgeTree.Trace("%s was not mirrored, ignored", path)
continue
}
kindToFixturePath[kind] = remappedPath.String()
}
ComplianceTests(t, name, forgeTree.(f3_tree.TreeInterface), kindToFixturePath, testForge.GetExceptions())
TreeDelete(t, testForge.GetNonTestUsers(), forgeOptions, forgeTree)
}
func ComplianceTests(t *testing.T, name string, tree f3_tree.TreeInterface, kindToFixturePath map[generic.Kind]string, exceptions []generic.Kind) {
ctx := context.Background()
exceptions = append(exceptions, f3_tree.KindRepositories)
for _, kind := range KindWithFixturePath {
path := kindToFixturePath[kind]
if !tree.IsContainer(kind) {
continue
}
if slices.Contains(exceptions, kind) {
continue
}
t.Run(string(kind), func(t *testing.T) {
tree.Trace("%s", path)
parent := tree.Find(generic.NewPathFromString(path))
require.NotEqualValues(t, generic.NilNode, parent, path)
tree.Trace("create a new child in memory")
child := tree.Factory(ctx, tree.GetChildrenKind(kind))
childFormat := GeneratorSetRandom(t, name, child.NewFormat(), parent.GetCurrentPath())
child.FromFormat(childFormat)
if id := child.GetID(); id != generic.NilID {
tree.Trace("about to insert child %s", id)
assert.EqualValues(t, generic.NilNode, parent.GetChild(child.GetID()))
} else {
tree.Trace("about to insert child with nil ID")
}
if child.GetDriver().IsNull() {
t.Skip("no driver, skipping")
}
tree.Trace("'Upsert' the new child in the parent and store it in the forge")
child.SetParent(parent)
child.Upsert(ctx)
tree.Trace("done inserting child '%s'", child.GetID())
before := child.ToFormat()
require.EqualValues(t, before.GetID(), child.GetID().String())
tree.Trace("'Get' the child '%s' from the forge", child.GetID())
child.Get(ctx)
after := child.ToFormat()
cmpOptions := f3.NewCmpOptions(before)
tree.Trace("check the F3 representations Upsert & Get to/from the forge are equivalent")
require.True(t, cmp.Equal(before, after, cmpOptions), cmp.Diff(before, after, cmpOptions))
tree.Trace("check the F3 representation FromFormat/ToFormat are identical")
{
saved := after
a := childFormat.Clone()
if tree.AllocateID() {
a.SetID("123456")
}
child.FromFormat(a)
b := child.ToFormat()
cmpOptions := f3.NewCmpOptions(a)
require.True(t, cmp.Equal(a, b, cmpOptions), cmp.Diff(a, b, cmpOptions))
child.FromFormat(saved)
}
if childFormat.GetName() != childFormat.GetID() {
tree.Trace("'GetIDFromName' %s %s", kind, childFormat.GetName())
id := parent.GetIDFromName(ctx, childFormat.GetName())
assert.EqualValues(t, child.GetID(), id)
}
modified := GeneratorModify(t, after, parent.GetCurrentPath())
if !modified.IsNil() {
tree.Trace("%s 'Upsert' a modified child %v", kind, modified)
child.FromFormat(modified)
child.Upsert(ctx)
tree.Trace("'Get' the modified child '%s' from the forge", child.GetID())
child.Get(ctx)
after = child.ToFormat()
tree.Trace("check the F3 representations Upsert & Get to/from the forge of the modified child are equivalent")
require.True(t, cmp.Equal(modified, after, cmpOptions), cmp.Diff(modified, after, cmpOptions))
}
nodeChildren := parent.GetNodeChildren()
tree.Trace("'ListPage' and only 'Get' known %d children of %s", len(nodeChildren), parent.GetKind())
if len(nodeChildren) > 0 {
parent.List(ctx)
for _, child := range parent.GetChildren() {
if _, ok := nodeChildren[child.GetID()]; ok {
tree.Trace("'WalkAndGet' %s child %s %s", parent.GetCurrentPath(), child.GetKind(), child.GetID())
child.WalkAndGet(ctx, parent.GetCurrentPath(), generic.NewWalkOptions(nil))
}
}
}
tree.Trace("'Delete' child '%s' from the forge", child.GetID())
child.Delete(ctx)
assert.EqualValues(t, generic.NilNode, parent.GetChild(child.GetID()))
assert.True(t, child.GetIsSync())
loop := 100
for i := 0; i < loop; i++ {
child.SetIsSync(false)
child.Get(ctx)
if !child.GetIsSync() {
break
}
tree.Trace("waiting for asynchronous child deletion (%d/%d)", i, loop)
time.Sleep(5 * time.Second)
}
assert.False(t, child.GetIsSync())
tree.Trace("%s did something %s", kind, child)
})
}
}