typesystem_test.go 4.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
package schema

import (
	"fmt"
	"strings"
	"testing"

	. "github.com/warpfork/go-wish"

	"github.com/ipld/go-ipld-prime/codec/dagjson"
	schemadmt "github.com/ipld/go-ipld-prime/schema/dmt"
)

func TestBuildTypeSystem(t *testing.T) {
15
	// NOTE: several of these fixtures will need updating when support for implicits is completed.
16 17 18 19 20 21 22 23 24 25 26 27 28
	t.Run("SimpleHappyPath", func(t *testing.T) {
		ts := testParse(t,
			`{
				"types": {
					"Woop": {
						"string": {}
					}
				}
			}`,
			nil,
			nil,
		)
		Wish(t, ts.types["Woop"], ShouldBeSameTypeAs, &TypeString{})
29
		Wish(t, ts.types["Woop"].TypeKind(), ShouldEqual, TypeKind_String)
30 31 32 33 34 35 36
	})
	t.Run("MissingTypeInList", func(t *testing.T) {
		testParse(t,
			`{
				"types": {
					"SomeList": {
						"list": {
37 38 39 40 41
							"valueType": "Bork",
							"valueNullable": false,
							"representation": {
								"list": {}
							}
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
						}
					}
				}
			}`,
			nil,
			[]error{
				fmt.Errorf("type SomeList refers to missing type Bork as value type"),
			},
		)
	})
	t.Run("MissingTypeInMap", func(t *testing.T) {
		testParse(t,
			`{
				"types": {
					"SomeMap": {
						"map": {
							"keyType": "Bork"
59 60 61 62 63
							"valueType": "Spork",
							"valueNullable": false,
							"representation": {
								"map": {}
							}
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
						}
					}
				}
			}`,
			nil,
			[]error{
				fmt.Errorf("type SomeMap refers to missing type Bork as key type"),
				fmt.Errorf("type SomeMap refers to missing type Spork as value type"),
			},
		)
	})
	t.Run("SimpleValidMapKeyType", func(t *testing.T) {
		ts := testParse(t,
			`{
				"types": {
					"SomeMap": {
						"map": {
							"keyType": "String"
82 83 84 85 86
							"valueType": "String",
							"valueNullable": false,
							"representation": {
								"map": {}
							}
87 88 89 90 91 92 93 94 95 96 97
						}
					},
					"String": {
						"string": {}
					}
				}
			}`,
			nil,
			nil,
		)
		Wish(t, ts.types["SomeMap"], ShouldBeSameTypeAs, &TypeMap{})
98
		Wish(t, ts.types["SomeMap"].TypeKind(), ShouldEqual, TypeKind_Map)
99 100 101 102 103 104 105 106 107
		Wish(t, ts.types["SomeMap"].(*TypeMap).KeyType().Name().String(), ShouldEqual, "String")
	})
	t.Run("ComplexValidMapKeyType", func(t *testing.T) {
		ts := testParse(t,
			`{
				"types": {
					"SomeMap": {
						"map": {
							"keyType": "StringyStruct",
108 109 110 111 112
							"valueType": "String",
							"valueNullable": false,
							"representation": {
								"map": {}
							}
113 114 115 116 117 118 119 120 121
						}
					},
					"String": {
						"string": {}
					},
					"StringyStruct": {
						"struct": {
							"fields": {
								"f1": {
122 123 124
									"type": "String",
									"optional": false,
									"nullable": false
125 126
								},
								"f2": {
127 128 129
									"type": "String",
									"optional": false,
									"nullable": false
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
								}
							},
							"representation": {
								"stringjoin": {
									"join": ":"
								}
							}
						}
					}
				}
			}`,
			nil,
			nil,
		)
		Wish(t, ts.types["SomeMap"], ShouldBeSameTypeAs, &TypeMap{})
145
		Wish(t, ts.types["SomeMap"].TypeKind(), ShouldEqual, TypeKind_Map)
146 147 148 149 150 151 152 153 154
		Wish(t, ts.types["SomeMap"].(*TypeMap).KeyType().Name().String(), ShouldEqual, "StringyStruct")
	})
	t.Run("InvalidMapKeyType", func(t *testing.T) {
		testParse(t,
			`{
				"types": {
					"SomeMap": {
						"map": {
							"keyType": "StringyStruct",
155 156 157 158 159
							"valueType": "String",
							"valueNullable": false,
							"representation": {
								"map": {}
							}
160 161 162 163 164 165 166 167 168
						}
					},
					"String": {
						"string": {}
					},
					"StringyStruct": {
						"struct": {
							"fields": {
								"f1": {
169 170 171
									"type": "String",
									"optional": false,
									"nullable": false
172 173
								},
								"f2": {
174 175 176
									"type": "String",
									"optional": false,
									"nullable": false
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
								}
							},
							"representation": {
								"map": {}
							}
						}
					}
				}
			}`,
			nil,
			[]error{
				fmt.Errorf("type SomeMap refers to type StringyStruct as key type, but it is not a valid key type because it is not stringable"),
			},
		)
	})
}

func testParse(t *testing.T, schemajson string, expectParseErr error, expectTypesystemError []error) *TypeSystem {
	t.Helper()
	dmt, parseErr := parseSchema(schemajson)
	Wish(t, parseErr, ShouldEqual, expectParseErr)
	if parseErr != nil {
		return nil
	}
	ts, typesystemErr := BuildTypeSystem(dmt)
	Wish(t, typesystemErr, ShouldEqual, expectTypesystemError)
	return ts
}

func parseSchema(schemajson string) (schemadmt.Schema, error) {
	nb := schemadmt.Type.Schema__Repr.NewBuilder()
Rod Vagg's avatar
Rod Vagg committed
208
	if err := dagjson.Decode(nb, strings.NewReader(schemajson)); err != nil {
209 210 211 212
		return nil, err
	}
	return nb.Build().(schemadmt.Schema), nil
}