github.com/josephbuchma/goa@v1.2.0/design/apidsl/type_test.go (about)

     1  package apidsl_test
     2  
     3  import (
     4  	. "github.com/goadesign/goa/design"
     5  	. "github.com/goadesign/goa/design/apidsl"
     6  	"github.com/goadesign/goa/dslengine"
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  )
    10  
    11  var _ = Describe("Type", func() {
    12  	var name string
    13  	var dsl func()
    14  
    15  	var ut *UserTypeDefinition
    16  
    17  	BeforeEach(func() {
    18  		dslengine.Reset()
    19  		name = ""
    20  		dsl = nil
    21  	})
    22  
    23  	JustBeforeEach(func() {
    24  		Type(name, dsl)
    25  		dslengine.Run()
    26  		ut, _ = Design.Types[name]
    27  	})
    28  
    29  	Context("with no dsl and no name", func() {
    30  		It("produces an invalid type definition", func() {
    31  			Ω(ut).ShouldNot(BeNil())
    32  			Ω(ut.Validate("test", Design)).Should(HaveOccurred())
    33  		})
    34  	})
    35  
    36  	Context("with no dsl", func() {
    37  		BeforeEach(func() {
    38  			name = "foo"
    39  		})
    40  
    41  		It("produces a valid type definition", func() {
    42  			Ω(ut).ShouldNot(BeNil())
    43  			Ω(ut.Validate("test", Design)).ShouldNot(HaveOccurred())
    44  		})
    45  	})
    46  
    47  	Context("with attributes", func() {
    48  		const attName = "att"
    49  
    50  		BeforeEach(func() {
    51  			name = "foo"
    52  			dsl = func() {
    53  				Attribute(attName)
    54  			}
    55  		})
    56  
    57  		It("sets the attributes", func() {
    58  			Ω(ut).ShouldNot(BeNil())
    59  			Ω(ut.Validate("test", Design)).ShouldNot(HaveOccurred())
    60  			Ω(ut.AttributeDefinition).ShouldNot(BeNil())
    61  			Ω(ut.Type).Should(BeAssignableToTypeOf(Object{}))
    62  			o := ut.Type.(Object)
    63  			Ω(o).Should(HaveLen(1))
    64  			Ω(o).Should(HaveKey(attName))
    65  		})
    66  	})
    67  
    68  	Context("with a name and uuid datatype", func() {
    69  		const attName = "att"
    70  		BeforeEach(func() {
    71  			name = "foo"
    72  			dsl = func() {
    73  				Attribute(attName, UUID)
    74  			}
    75  		})
    76  
    77  		It("produces an attribute of date type", func() {
    78  			Ω(ut).ShouldNot(BeNil())
    79  			Ω(ut.Validate("test", Design)).ShouldNot(HaveOccurred())
    80  			Ω(ut.AttributeDefinition).ShouldNot(BeNil())
    81  			Ω(ut.Type).Should(BeAssignableToTypeOf(Object{}))
    82  			o := ut.Type.(Object)
    83  			Ω(o).Should(HaveLen(1))
    84  			Ω(o).Should(HaveKey(attName))
    85  			Ω(o[attName].Type).Should(Equal(UUID))
    86  		})
    87  	})
    88  
    89  	Context("with a name and date datatype", func() {
    90  		const attName = "att"
    91  		BeforeEach(func() {
    92  			name = "foo"
    93  			dsl = func() {
    94  				Attribute(attName, DateTime)
    95  			}
    96  		})
    97  
    98  		It("produces an attribute of date type", func() {
    99  			Ω(ut).ShouldNot(BeNil())
   100  			Ω(ut.Validate("test", Design)).ShouldNot(HaveOccurred())
   101  			Ω(ut.AttributeDefinition).ShouldNot(BeNil())
   102  			Ω(ut.Type).Should(BeAssignableToTypeOf(Object{}))
   103  			o := ut.Type.(Object)
   104  			Ω(o).Should(HaveLen(1))
   105  			Ω(o).Should(HaveKey(attName))
   106  			Ω(o[attName].Type).Should(Equal(DateTime))
   107  		})
   108  	})
   109  })
   110  
   111  var _ = Describe("ArrayOf", func() {
   112  	Context("used on a global variable", func() {
   113  		var (
   114  			ut *UserTypeDefinition
   115  			ar *Array
   116  		)
   117  		BeforeEach(func() {
   118  			dslengine.Reset()
   119  			ut = Type("example", func() {
   120  				Attribute("id")
   121  			})
   122  			ar = ArrayOf(ut)
   123  			Ω(dslengine.Errors).ShouldNot(HaveOccurred())
   124  		})
   125  
   126  		JustBeforeEach(func() {
   127  			dslengine.Run()
   128  			Ω(dslengine.Errors).ShouldNot(HaveOccurred())
   129  		})
   130  
   131  		It("produces a array type", func() {
   132  			Ω(ar).ShouldNot(BeNil())
   133  			Ω(ar.Kind()).Should(Equal(ArrayKind))
   134  			Ω(ar.ElemType.Type).Should(Equal(ut))
   135  		})
   136  	})
   137  
   138  	Context("with a DSL", func() {
   139  		var (
   140  			pattern = "foo"
   141  			ar      *Array
   142  		)
   143  
   144  		BeforeEach(func() {
   145  			dslengine.Reset()
   146  			ar = ArrayOf(String, func() {
   147  				Pattern(pattern)
   148  			})
   149  			Ω(dslengine.Errors).ShouldNot(HaveOccurred())
   150  		})
   151  
   152  		It("records the validations", func() {
   153  			Ω(ar).ShouldNot(BeNil())
   154  			Ω(ar.Kind()).Should(Equal(ArrayKind))
   155  			Ω(ar.ElemType.Type).Should(Equal(String))
   156  			Ω(ar.ElemType.Validation).ShouldNot(BeNil())
   157  			Ω(ar.ElemType.Validation.Pattern).Should(Equal(pattern))
   158  		})
   159  	})
   160  
   161  	Context("defined with the type name", func() {
   162  		var ar *UserTypeDefinition
   163  		BeforeEach(func() {
   164  			dslengine.Reset()
   165  			Type("name", func() {
   166  				Attribute("id")
   167  			})
   168  			ar = Type("names", func() {
   169  				Attribute("ut", ArrayOf("name"))
   170  			})
   171  		})
   172  
   173  		JustBeforeEach(func() {
   174  			dslengine.Run()
   175  			Ω(dslengine.Errors).ShouldNot(HaveOccurred())
   176  		})
   177  
   178  		It("produces a user type", func() {
   179  			Ω(ar).ShouldNot(BeNil())
   180  			Ω(ar.TypeName).Should(Equal("names"))
   181  			Ω(ar.Type).ShouldNot(BeNil())
   182  			Ω(ar.Type.ToObject()).ShouldNot(BeNil())
   183  			Ω(ar.Type.ToObject()).Should(HaveKey("ut"))
   184  			ut := ar.Type.ToObject()["ut"]
   185  			Ω(ut.Type).ShouldNot(BeNil())
   186  			Ω(ut.Type).Should(BeAssignableToTypeOf(&Array{}))
   187  			et := ut.Type.ToArray().ElemType
   188  			Ω(et).ShouldNot(BeNil())
   189  			Ω(et.Type).Should(BeAssignableToTypeOf(&UserTypeDefinition{}))
   190  			Ω(et.Type.(*UserTypeDefinition).TypeName).Should(Equal("name"))
   191  		})
   192  	})
   193  
   194  	Context("defined with a media type name", func() {
   195  		var mt *MediaTypeDefinition
   196  		BeforeEach(func() {
   197  			dslengine.Reset()
   198  			mt = MediaType("application/vnd.test", func() {
   199  				Attributes(func() {
   200  					Attribute("ut", ArrayOf("application/vnd.test"))
   201  				})
   202  				View("default", func() {
   203  					Attribute("ut")
   204  				})
   205  			})
   206  		})
   207  
   208  		JustBeforeEach(func() {
   209  			dslengine.Run()
   210  			Ω(dslengine.Errors).ShouldNot(HaveOccurred())
   211  		})
   212  
   213  		It("produces a user type", func() {
   214  			Ω(mt).ShouldNot(BeNil())
   215  			Ω(mt.TypeName).Should(Equal("Test"))
   216  			Ω(mt.Type).ShouldNot(BeNil())
   217  			Ω(mt.Type.ToObject()).ShouldNot(BeNil())
   218  			Ω(mt.Type.ToObject()).Should(HaveKey("ut"))
   219  			ut := mt.Type.ToObject()["ut"]
   220  			Ω(ut.Type).ShouldNot(BeNil())
   221  			Ω(ut.Type).Should(BeAssignableToTypeOf(&Array{}))
   222  			et := ut.Type.ToArray().ElemType
   223  			Ω(et).ShouldNot(BeNil())
   224  			Ω(et.Type).Should(BeAssignableToTypeOf(&MediaTypeDefinition{}))
   225  			Ω(et.Type.(*MediaTypeDefinition).TypeName).Should(Equal("Test"))
   226  		})
   227  	})
   228  })
   229  
   230  var _ = Describe("HashOf", func() {
   231  	Context("used on a global variable", func() {
   232  		var (
   233  			kt *UserTypeDefinition
   234  			vt *UserTypeDefinition
   235  			ha *Hash
   236  		)
   237  		BeforeEach(func() {
   238  			dslengine.Reset()
   239  			kt = Type("key", func() {
   240  				Attribute("id")
   241  			})
   242  			vt = Type("val", func() {
   243  				Attribute("id")
   244  			})
   245  			ha = HashOf(kt, vt)
   246  			Ω(dslengine.Errors).ShouldNot(HaveOccurred())
   247  		})
   248  
   249  		JustBeforeEach(func() {
   250  			dslengine.Run()
   251  			Ω(dslengine.Errors).ShouldNot(HaveOccurred())
   252  		})
   253  
   254  		It("produces a hash type", func() {
   255  			Ω(ha).ShouldNot(BeNil())
   256  			Ω(ha.Kind()).Should(Equal(HashKind))
   257  			Ω(ha.KeyType.Type).Should(Equal(kt))
   258  			Ω(ha.ElemType.Type).Should(Equal(vt))
   259  		})
   260  	})
   261  
   262  	Context("with DSLs", func() {
   263  		var (
   264  			kp = "foo"
   265  			vp = "bar"
   266  			ha *Hash
   267  		)
   268  
   269  		BeforeEach(func() {
   270  			dslengine.Reset()
   271  			ha = HashOf(String, String, func() { Pattern(kp) }, func() { Pattern(vp) })
   272  			Ω(dslengine.Errors).ShouldNot(HaveOccurred())
   273  		})
   274  
   275  		JustBeforeEach(func() {
   276  			dslengine.Run()
   277  			Ω(dslengine.Errors).ShouldNot(HaveOccurred())
   278  		})
   279  
   280  		It("records the validations", func() {
   281  			Ω(ha).ShouldNot(BeNil())
   282  			Ω(ha.Kind()).Should(Equal(HashKind))
   283  			Ω(ha.KeyType.Type).Should(Equal(String))
   284  			Ω(ha.KeyType.Validation).ShouldNot(BeNil())
   285  			Ω(ha.KeyType.Validation.Pattern).Should(Equal(kp))
   286  			Ω(ha.ElemType.Type).Should(Equal(String))
   287  			Ω(ha.ElemType.Validation).ShouldNot(BeNil())
   288  			Ω(ha.ElemType.Validation.Pattern).Should(Equal(vp))
   289  		})
   290  	})
   291  })