68 lines
1.6 KiB
Go
68 lines
1.6 KiB
Go
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
|
|
|
package folio
|
|
|
|
import (
|
|
"github.com/stretchr/testify/assert"
|
|
"testing"
|
|
"fmt"
|
|
_ "decl/internal/data"
|
|
"decl/internal/mapper"
|
|
"decl/internal/codec"
|
|
"log/slog"
|
|
"strings"
|
|
"io"
|
|
)
|
|
|
|
func TestReference(t *testing.T) {
|
|
f := NewFooResource()
|
|
resourceMapper := mapper.New[URI, *Declaration]()
|
|
f.Name = string(TempDir)
|
|
f.Size = 10
|
|
f.MockResource.InjectURI = func() string { return fmt.Sprintf("%s://%s", "foo", f.Name) }
|
|
d := NewDeclaration()
|
|
d.Type = "foo"
|
|
d.Attributes = f
|
|
resourceMapper[URI(d.URI())] = d
|
|
slog.Info("TestReference", "declaration", d, "mapper", resourceMapper)
|
|
|
|
var fooRef *Ref = NewRef()
|
|
fooRef.RefType = ReferenceTypeResource
|
|
fooRef.Uri = URI(fmt.Sprintf("foo://%s", string(TempDir)))
|
|
u := fooRef.Uri.Parse().URL()
|
|
assert.Equal(t, "foo", u.Scheme)
|
|
assert.True(t, fooRef.Uri.Exists())
|
|
|
|
fromRef := fooRef.Lookup(resourceMapper)
|
|
assert.NotNil(t, fromRef)
|
|
}
|
|
|
|
func TestDocumentRef(t *testing.T) {
|
|
docUri := fmt.Sprintf("file://%s/doc.yaml", TempDir)
|
|
DocumentRegistry.ResourceTypes = TestResourceTypes
|
|
document := `
|
|
---
|
|
resources:
|
|
- type: foo
|
|
attributes:
|
|
name: "testfoo"
|
|
size: 10022
|
|
`
|
|
TempDir.CreateFile("doc.yaml", document)
|
|
|
|
d := DocumentRegistry.NewDocument(URI(docUri))
|
|
assert.NotNil(t, d)
|
|
docReader := io.NopCloser(strings.NewReader(document))
|
|
|
|
e := d.LoadReader(docReader, codec.FormatYaml)
|
|
assert.Nil(t, e)
|
|
|
|
var docRef *Ref = NewRef()
|
|
docRef.RefType = ReferenceTypeDocument
|
|
docRef.Uri = URI(docUri)
|
|
u := docRef.Uri.Parse().URL()
|
|
assert.Equal(t, "file", u.Scheme)
|
|
|
|
assert.Equal(t, d, docRef.Dereference(DocumentRegistry.UriMap).(*Document))
|
|
}
|