github.com/ipld/go-ipld-prime@v0.21.0/node/tests/schemaUnionsKinded.go (about)

     1  package tests
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/ipld/go-ipld-prime/datamodel"
     7  	"github.com/ipld/go-ipld-prime/schema"
     8  )
     9  
    10  func SchemaTestUnionKinded(t *testing.T, engine Engine) {
    11  	ts := schema.TypeSystem{}
    12  	ts.Init()
    13  	ts.Accumulate(schema.SpawnString("String"))
    14  	ts.Accumulate(schema.SpawnStruct("SmolStruct",
    15  		[]schema.StructField{
    16  			schema.SpawnStructField("s", "String", false, false),
    17  		},
    18  		schema.SpawnStructRepresentationMap(map[string]string{
    19  			"s": "q",
    20  		}),
    21  	))
    22  	ts.Accumulate(schema.SpawnUnion("WheeUnion",
    23  		[]schema.TypeName{
    24  			"String",
    25  			"SmolStruct",
    26  		},
    27  		schema.SpawnUnionRepresentationKinded(map[datamodel.Kind]schema.TypeName{
    28  			datamodel.Kind_String: "String",
    29  			datamodel.Kind_Map:    "SmolStruct",
    30  		}),
    31  	))
    32  	engine.Init(t, ts)
    33  
    34  	// These are the same *type-level* as in TestUnionKeyedComplexChildren,
    35  	//  but (of course) have very different representations.
    36  	specs := []testcase{
    37  		{
    38  			name:     "InhabitantA",
    39  			typeJson: `{"String":"whee"}`,
    40  			reprJson: `"whee"`,
    41  			typePoints: []testcasePoint{
    42  				{"", datamodel.Kind_Map},
    43  				{"String", "whee"},
    44  				//{"SmolStruct", datamodel.ErrNotExists{}}, // TODO: need better error typing from traversal package.
    45  			},
    46  			reprPoints: []testcasePoint{
    47  				{"", datamodel.Kind_String},
    48  				{"", "whee"},
    49  			},
    50  		},
    51  		{
    52  			name:     "InhabitantB",
    53  			typeJson: `{"SmolStruct":{"s":"whee"}}`,
    54  			reprJson: `{"q":"whee"}`,
    55  			typePoints: []testcasePoint{
    56  				{"", datamodel.Kind_Map},
    57  				//{"String", datamodel.ErrNotExists{}}, // TODO: need better error typing from traversal package.
    58  				{"SmolStruct", datamodel.Kind_Map},
    59  				{"SmolStruct/s", "whee"},
    60  			},
    61  			reprPoints: []testcasePoint{
    62  				{"", datamodel.Kind_Map},
    63  				{"q", "whee"},
    64  			},
    65  		},
    66  	}
    67  
    68  	np := engine.PrototypeByName("WheeUnion")
    69  	nrp := engine.PrototypeByName("WheeUnion.Repr")
    70  	for _, tcase := range specs {
    71  		tcase.Test(t, np, nrp)
    72  	}
    73  }