57 lines
1.8 KiB
Go
57 lines
1.8 KiB
Go
package artifactmodel
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestArtifactMetadataJSON(t *testing.T) {
|
|
type envelope struct {
|
|
Contract *ContractMetadata `json:"contract,omitempty"`
|
|
ExternalProvenance *ExternalProvenance `json:"external_provenance,omitempty"`
|
|
}
|
|
|
|
complete, err := json.Marshal(envelope{
|
|
Contract: &ContractMetadata{
|
|
MediaType: "application/json",
|
|
SchemaID: "notarius.dnd.npc_registry",
|
|
SchemaVersion: "v1",
|
|
ModuleKey: "dnd/npc-registry",
|
|
},
|
|
ExternalProvenance: &ExternalProvenance{
|
|
System: "notarius",
|
|
RunID: "run-123",
|
|
PipelineID: "dnd-session",
|
|
ArtifactID: "npc-registry",
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("Marshal() complete metadata error = %v", err)
|
|
}
|
|
wantComplete := `{"contract":{"media_type":"application/json","schema_id":"notarius.dnd.npc_registry","schema_version":"v1","module_key":"dnd/npc-registry"},"external_provenance":{"system":"notarius","run_id":"run-123","pipeline_id":"dnd-session","artifact_id":"npc-registry"}}`
|
|
if string(complete) != wantComplete {
|
|
t.Fatalf("complete metadata JSON = %s, want %s", complete, wantComplete)
|
|
}
|
|
|
|
omitted, err := json.Marshal(envelope{})
|
|
if err != nil {
|
|
t.Fatalf("Marshal() omitted metadata error = %v", err)
|
|
}
|
|
if string(omitted) != `{}` {
|
|
t.Fatalf("omitted metadata JSON = %s, want {}", omitted)
|
|
}
|
|
|
|
withoutModule, err := json.Marshal(envelope{Contract: &ContractMetadata{
|
|
MediaType: "application/json",
|
|
SchemaID: "notarius.dnd.npc_registry",
|
|
SchemaVersion: "v1",
|
|
}})
|
|
if err != nil {
|
|
t.Fatalf("Marshal() contract without module key error = %v", err)
|
|
}
|
|
if strings.Contains(string(withoutModule), "module_key") {
|
|
t.Fatalf("contract JSON unexpectedly contains omitted module_key: %s", withoutModule)
|
|
}
|
|
}
|