github.com/brycereitano/goa@v0.0.0-20170315073847-8ffa6c85e265/design/definitions_test.go (about)

     1  package design_test
     2  
     3  import (
     4  	"path"
     5  
     6  	"github.com/goadesign/goa/design"
     7  	"github.com/goadesign/goa/dslengine"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  )
    11  
    12  var _ = Describe("IsRequired", func() {
    13  	var required string
    14  	var attName string
    15  
    16  	var attribute *design.AttributeDefinition
    17  	var res bool
    18  
    19  	JustBeforeEach(func() {
    20  		integer := &design.AttributeDefinition{Type: design.Integer}
    21  		attribute = &design.AttributeDefinition{
    22  			Type:       design.Object{required: integer},
    23  			Validation: &dslengine.ValidationDefinition{Required: []string{required}},
    24  		}
    25  		res = attribute.IsRequired(attName)
    26  	})
    27  
    28  	Context("called on a required field", func() {
    29  		BeforeEach(func() {
    30  			attName = "required"
    31  			required = "required"
    32  		})
    33  
    34  		It("returns true", func() {
    35  			Ω(res).Should(BeTrue())
    36  		})
    37  	})
    38  
    39  	Context("called on a non-required field", func() {
    40  		BeforeEach(func() {
    41  			attName = "non-required"
    42  			required = "required"
    43  		})
    44  
    45  		It("returns false", func() {
    46  			Ω(res).Should(BeFalse())
    47  		})
    48  	})
    49  })
    50  
    51  var _ = Describe("IterateHeaders", func() {
    52  	It("works when Parent.Headers is nil", func() {
    53  		// create a Resource with no headers, Action with one header
    54  		resource := &design.ResourceDefinition{}
    55  		action := &design.ActionDefinition{
    56  			Parent: resource,
    57  			Headers: &design.AttributeDefinition{
    58  				Type: design.Object{
    59  					"a": &design.AttributeDefinition{Type: design.String},
    60  				},
    61  			},
    62  		}
    63  		names := []string{}
    64  		// iterator that collects header names
    65  		it := func(name string, _ bool, _ *design.AttributeDefinition) error {
    66  			names = append(names, name)
    67  			return nil
    68  		}
    69  		Ω(action.IterateHeaders(it)).Should(Succeed(), "despite action.Parent.Headers being nil")
    70  		Ω(names).Should(ConsistOf("a"))
    71  	})
    72  })
    73  
    74  var _ = Describe("Finalize ActionDefinition", func() {
    75  	Context("with an action with no response", func() {
    76  		var action *design.ActionDefinition
    77  
    78  		BeforeEach(func() {
    79  			// create a Resource with responses, Action with no response
    80  			resource := &design.ResourceDefinition{
    81  				Responses: map[string]*design.ResponseDefinition{
    82  					"NotFound": &design.ResponseDefinition{Name: "NotFound", Status: 404},
    83  				},
    84  			}
    85  			action = &design.ActionDefinition{Parent: resource}
    86  		})
    87  
    88  		It("does not panic and merges the resource responses", func() {
    89  			Ω(action.Finalize).ShouldNot(Panic())
    90  			Ω(action.Responses).Should(HaveKey("NotFound"))
    91  		})
    92  	})
    93  })
    94  
    95  var _ = Describe("FullPath", func() {
    96  
    97  	Context("Given a base resource and a resource with an action with a route", func() {
    98  		var resource, parentResource *design.ResourceDefinition
    99  		var action *design.ActionDefinition
   100  		var route *design.RouteDefinition
   101  
   102  		var actionPath string
   103  		var resourcePath string
   104  		var parentResourcePath string
   105  
   106  		JustBeforeEach(func() {
   107  			showAct := &design.ActionDefinition{}
   108  			showRoute := &design.RouteDefinition{
   109  				Path:   parentResourcePath,
   110  				Parent: showAct,
   111  			}
   112  			showAct.Routes = []*design.RouteDefinition{showRoute}
   113  			parentResource = &design.ResourceDefinition{}
   114  			parentResource.Actions = map[string]*design.ActionDefinition{"show": showAct}
   115  			parentResource.Name = "foo"
   116  			design.Design.Resources = map[string]*design.ResourceDefinition{"foo": parentResource}
   117  			showAct.Parent = parentResource
   118  
   119  			action = &design.ActionDefinition{}
   120  			route = &design.RouteDefinition{
   121  				Path:   actionPath,
   122  				Parent: action,
   123  			}
   124  			action.Routes = []*design.RouteDefinition{route}
   125  			resource = &design.ResourceDefinition{}
   126  			resource.Actions = map[string]*design.ActionDefinition{"action": action}
   127  			resource.BasePath = resourcePath
   128  			resource.ParentName = parentResource.Name
   129  			action.Parent = resource
   130  		})
   131  
   132  		Context("with relative routes", func() {
   133  			BeforeEach(func() {
   134  				actionPath = "/action"
   135  				resourcePath = "/resource"
   136  				parentResourcePath = "/parent"
   137  			})
   138  
   139  			It("FullPath concatenates them", func() {
   140  				Ω(route.FullPath()).Should(Equal(path.Join(parentResourcePath, resourcePath, actionPath)))
   141  			})
   142  
   143  			Context("with an action with absolute route", func() {
   144  				BeforeEach(func() {
   145  					actionPath = "//action"
   146  				})
   147  
   148  				It("FullPath uses it", func() {
   149  					Ω(route.FullPath()).Should(Equal(actionPath[1:]))
   150  				})
   151  			})
   152  
   153  			Context("with n resource with absolute route", func() {
   154  				BeforeEach(func() {
   155  					resourcePath = "//resource"
   156  				})
   157  
   158  				It("FullPath uses it", func() {
   159  					Ω(route.FullPath()).Should(Equal(resourcePath[1:] + "/" + actionPath[1:]))
   160  				})
   161  			})
   162  		})
   163  	})
   164  })