Share prepared pipeline source validation
This commit is contained in:
48
internal/framework/pipeline/source_preparation_test.go
Normal file
48
internal/framework/pipeline/source_preparation_test.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
)
|
||||
|
||||
type sourcePreparationAdapter struct {
|
||||
doc *source.SourceDocument
|
||||
err error
|
||||
calls int
|
||||
}
|
||||
|
||||
func (*sourcePreparationAdapter) Key() string { return "test/input" }
|
||||
|
||||
func (adapter *sourcePreparationAdapter) Parse(context.Context, contracts.ParseRequest) (*source.SourceDocument, error) {
|
||||
adapter.calls++
|
||||
return adapter.doc, adapter.err
|
||||
}
|
||||
|
||||
func TestPreparedPipelineParsesAndValidatesSource(t *testing.T) {
|
||||
adapter := &sourcePreparationAdapter{doc: typedTestDocumentWithUnits(1)}
|
||||
prepared := &PreparedPipeline{input: adapter}
|
||||
doc, err := prepared.ParseSource(context.Background(), contracts.ParseRequest{})
|
||||
if err != nil || doc != adapter.doc || adapter.calls != 1 {
|
||||
t.Fatalf("ParseSource() = %v, %v; calls = %d", doc, err, adapter.calls)
|
||||
}
|
||||
adapter.doc = &source.SourceDocument{}
|
||||
if _, err := prepared.ParseSource(context.Background(), contracts.ParseRequest{}); err == nil || !strings.Contains(err.Error(), "validate source document") {
|
||||
t.Fatalf("invalid document error = %v", err)
|
||||
}
|
||||
cause := errors.New("adapter failure")
|
||||
adapter.err = cause
|
||||
if _, err := prepared.ParseSource(context.Background(), contracts.ParseRequest{}); !errors.Is(err, cause) {
|
||||
t.Fatalf("adapter error = %v, want original cause", err)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
calls := adapter.calls
|
||||
if _, err := prepared.ParseSource(ctx, contracts.ParseRequest{}); !errors.Is(err, context.Canceled) || adapter.calls != calls {
|
||||
t.Fatalf("canceled parse error = %v; calls = %d", err, adapter.calls)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user