| 104 | } |
| 105 | |
| 106 | func TestAttributeExprValidate(t *testing.T) { |
| 107 | var ( |
| 108 | ctx = "ctx" |
| 109 | normalizedCtx = ctx + " - " |
| 110 | |
| 111 | validation = &ValidationExpr{ |
| 112 | Required: []string{"foo"}, |
| 113 | } |
| 114 | |
| 115 | metadata = MetaExpr{ |
| 116 | "view": []string{"foo"}, |
| 117 | } |
| 118 | |
| 119 | fieldNotExistType = &Object{} |
| 120 | notAResultType = Boolean |
| 121 | viewNotDefinedTypeName = "ViewNotDefinedType" |
| 122 | |
| 123 | errAttributeTypeNil = fmt.Errorf("attribute type is nil") |
| 124 | errRequiredFieldNotExist = fmt.Errorf(`%srequired field %q does not exist in type %s`, normalizedCtx, "foo", fieldNotExistType.Name()) |
| 125 | errViewButNotAResultType = fmt.Errorf("%s uses view %q but %q is not a result type", normalizedCtx, metadata["view"][0], notAResultType.Name()) |
| 126 | errTypeNotDefineView = fmt.Errorf("%s: type %q does not define view %q", normalizedCtx, viewNotDefinedTypeName, "foo") |
| 127 | errConflictingTypes = fmt.Errorf("type \"%s\" has conflicting packages %s and %s", "SecondType", "types2", "types") |
| 128 | ) |
| 129 | cases := map[string]struct { |
| 130 | typ DataType |
| 131 | validation *ValidationExpr |
| 132 | metadata MetaExpr |
| 133 | expected *eval.ValidationErrors |
| 134 | }{ |
| 135 | "no error": { |
| 136 | typ: Boolean, |
| 137 | expected: &eval.ValidationErrors{}, |
| 138 | }, |
| 139 | "attribute type is nil": { |
| 140 | typ: nil, |
| 141 | expected: &eval.ValidationErrors{Errors: []error{errAttributeTypeNil}}, |
| 142 | }, |
| 143 | "attribute type is nil in the object": { |
| 144 | typ: &Object{ |
| 145 | &NamedAttributeExpr{ |
| 146 | Name: "foo", |
| 147 | Attribute: &AttributeExpr{ |
| 148 | Type: nil, |
| 149 | }, |
| 150 | }, |
| 151 | }, |
| 152 | expected: &eval.ValidationErrors{Errors: []error{errAttributeTypeNil}}, |
| 153 | }, |
| 154 | "attribute type is nil in the array": { |
| 155 | typ: &Array{ |
| 156 | ElemType: &AttributeExpr{ |
| 157 | Type: nil, |
| 158 | }, |
| 159 | }, |
| 160 | expected: &eval.ValidationErrors{Errors: []error{errAttributeTypeNil}}, |
| 161 | }, |
| 162 | "required field does not exist": { |
| 163 | typ: &Object{ |