File: filesystem_test.go

package info (click to toggle)
golang-code.forgejo-f3-gof3 3.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,952 kB
  • sloc: sh: 100; makefile: 65
file content (105 lines) | stat: -rw-r--r-- 3,437 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright Earl Warren <contact@earl-warren.org>
// Copyright Loïc Dachary <loic@dachary.org>
// SPDX-License-Identifier: MIT

package f3

import (
	"context"
	"path/filepath"
	"testing"

	filesystem_options "code.forgejo.org/f3/gof3/v3/forges/filesystem/options"
	"code.forgejo.org/f3/gof3/v3/id"
	"code.forgejo.org/f3/gof3/v3/options"
	"code.forgejo.org/f3/gof3/v3/path"
	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/stretchr/testify/assert"
)

func TestF3FilesystemMappedID(t *testing.T) {
	ctx := context.Background()

	//
	// aTree only has /forge/user/10111
	//
	aTree := generic.GetFactory("f3")(ctx, tests_forge.GetFactory(filesystem_options.Name)().NewOptions(t))
	aDir := aTree.GetOptions().(options.URLInterface).GetURL()

	creator := NewCreator(t, "F3", aTree.GetLogger())

	aF3Tree := aTree.(f3_tree.TreeInterface)

	userID := "10111"

	aF3Tree.CreateChild(ctx, "/", func(parent path.Path, forge generic.NodeInterface) {
		forge.FromFormat(creator.GenerateForge())
	})

	aF3Tree.CreateChild(ctx, "/forge/users", func(parent path.Path, user generic.NodeInterface) {
		user.FromFormat(GeneratorSetID(creator.GenerateUser(), userID))
	})

	//
	// bTree mirrors aTree exactly
	//
	rootPath := generic.NewPathFromString("")

	bTree := generic.GetFactory("f3")(ctx, tests_forge.GetFactory(filesystem_options.Name)().NewOptions(t))

	generic.TreeMirror(ctx, aTree, bTree, rootPath, generic.NewMirrorOptions())

	assert.True(t, generic.TreeCompare(ctx, aTree, rootPath, bTree, rootPath))

	//
	// aTree maps user id 10111 to 10111.mapped
	//
	userPath := generic.NewPathFromString(filepath.Join("/forge/users", userID))

	userMappedID := id.NewNodeID("10111.mapped")

	assert.True(t, aTree.Apply(ctx, userPath, generic.NewApplyOptions(func(ctx context.Context, parent, path path.Path, node generic.NodeInterface) {
		node.SetMappedID(userMappedID)
		node.Upsert(ctx)
	})))

	aTree = generic.GetFactory("f3")(ctx, tests_forge.GetFactory(filesystem_options.Name)().NewOptions(t))
	aTree.GetOptions().(options.URLInterface).SetURL(aDir)

	aTree.WalkAndGet(ctx, generic.NewWalkOptions(nil))
	assert.NotEqualValues(t, generic.NilNode, aTree.Find(userPath))

	//
	// cTree mirrors aTree with user id 10111 remapped to 10111.mapped
	//
	cTree := generic.GetFactory("f3")(ctx, tests_forge.GetFactory(filesystem_options.Name)().NewOptions(t))
	cDir := cTree.GetOptions().(options.URLInterface).GetURL()

	generic.TreeMirror(ctx, aTree, cTree, rootPath, generic.NewMirrorOptions())

	userMappedPath := generic.NewPathFromString(filepath.Join("/forge/users", userMappedID.String()))

	assert.NotEqualValues(t, generic.NilNode, cTree.Find(userMappedPath))
	assert.EqualValues(t, generic.NilNode, cTree.Find(userPath))

	//
	// reset cTree and read from the filesystem
	//
	cTree = generic.GetFactory("f3")(ctx, tests_forge.GetFactory(filesystem_options.Name)().NewOptions(t))
	cTree.GetOptions().(options.URLInterface).SetURL(cDir)

	cTree.WalkAndGet(ctx, generic.NewWalkOptions(nil))

	assert.NotEqualValues(t, generic.NilNode, cTree.Find(userMappedPath))
	assert.EqualValues(t, generic.NilNode, cTree.Find(userPath))

	//
	// delete aTree user
	//
	deleted := aTree.Find(userPath).Delete(ctx)
	assert.EqualValues(t, userMappedID, deleted.GetMappedID())
	assert.EqualValues(t, generic.NilNode, cTree.Find(userPath))
}