92 lines
3.5 KiB
Go
92 lines
3.5 KiB
Go
package register
|
|
|
|
import (
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
|
)
|
|
|
|
func TestRegisterAddsGenericFamily(t *testing.T) {
|
|
registries := completeRegistries()
|
|
if err := Register(registries, nil); err != nil {
|
|
t.Fatalf("Register() error = %v, want nil", err)
|
|
}
|
|
assertKeys(t, "chunkers", registries.Chunkers.RegisteredKeys(), []string{"generic"})
|
|
assertKeys(t, "mergers", registries.Mergers.RegisteredKeys(), []string{"appendorder"})
|
|
assertKeys(t, "normalizers", registries.Normalizers.RegisteredKeys(), []string{"noop"})
|
|
assertKeys(t, "validators", registries.Validators.RegisteredKeys(), []string{
|
|
"generic/always_accept",
|
|
"generic/always_reject",
|
|
"generic/valid_json",
|
|
"generic/valid_json_schema",
|
|
})
|
|
assertKeys(t, "outputs", registries.Outputs.RegisteredKeys(), []string{"json"})
|
|
if got := registries.Inputs.RegisteredKeys(); len(got) != 0 {
|
|
t.Fatalf("input keys = %#v, want generic registrar to leave inputs unchanged", got)
|
|
}
|
|
if got := registries.Extractors.RegisteredKeys(); len(got) != 0 {
|
|
t.Fatalf("extractor keys = %#v, want generic registrar to leave extractors unchanged", got)
|
|
}
|
|
}
|
|
|
|
func TestRegisterRejectsMissingGenericRegistriesBeforeMutation(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
remove func(*pipeline.Registries)
|
|
wantErr string
|
|
}{
|
|
{name: "chunkers", remove: func(r *pipeline.Registries) { r.Chunkers = nil }, wantErr: "chunker registry"},
|
|
{name: "mergers", remove: func(r *pipeline.Registries) { r.Mergers = nil }, wantErr: "merger registry"},
|
|
{name: "normalizers", remove: func(r *pipeline.Registries) { r.Normalizers = nil }, wantErr: "normalizer registry"},
|
|
{name: "validators", remove: func(r *pipeline.Registries) { r.Validators = nil }, wantErr: "validator registry"},
|
|
{name: "outputs", remove: func(r *pipeline.Registries) { r.Outputs = nil }, wantErr: "output registry"},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
registries := completeRegistries()
|
|
test.remove(®istries)
|
|
err := Register(registries, nil)
|
|
if err == nil || !strings.Contains(err.Error(), test.wantErr) {
|
|
t.Fatalf("Register() error = %v, want %q", err, test.wantErr)
|
|
}
|
|
if got := registries.Chunkers; got != nil && len(got.RegisteredKeys()) != 0 {
|
|
t.Fatalf("chunker keys = %#v, want validation before mutation", got.RegisteredKeys())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestRegisterReportsDuplicateGenericRegistration(t *testing.T) {
|
|
registries := completeRegistries()
|
|
if err := Register(registries, nil); err != nil {
|
|
t.Fatalf("first Register() error = %v, want nil", err)
|
|
}
|
|
err := Register(registries, nil)
|
|
if err == nil || !strings.Contains(err.Error(), "register generic chunker") || !strings.Contains(err.Error(), "already registered") {
|
|
t.Fatalf("second Register() error = %v, want contextual duplicate error", err)
|
|
}
|
|
}
|
|
|
|
func completeRegistries() pipeline.Registries {
|
|
return pipeline.Registries{
|
|
Inputs: pipeline.NewInputAdapterRegistry(),
|
|
Chunkers: pipeline.NewChunkerRegistry(),
|
|
ArtifactCodecs: pipeline.NewArtifactCodecRegistry(),
|
|
Extractors: pipeline.NewExtractorRegistry(),
|
|
Mergers: pipeline.NewMergerRegistry(),
|
|
Normalizers: pipeline.NewNormalizerRegistry(),
|
|
Validators: pipeline.NewValidatorRegistry(),
|
|
ValidatorChains: pipeline.NewValidatorChainRegistry(),
|
|
Outputs: pipeline.NewOutputEncoderRegistry(),
|
|
}
|
|
}
|
|
|
|
func assertKeys(t *testing.T, name string, got, want []string) {
|
|
t.Helper()
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("%s keys = %#v, want %#v", name, got, want)
|
|
}
|
|
}
|