Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
132 lines (117 loc) · 3.1 KB
/
Copy pathexample_test.go
File metadata and controls
132 lines (117 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package goa_test
import (
. "../goa"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"time"
)
/*
* This file tests the various examples in the source comment to make sure they actually work...
*/
var _ = Describe("Model", func() {
Context("example in attribute.go", func() {
var article = Attribute{
Description: "An article",
Type: Composite{
"title": Attribute{
Type: String,
Description: "Article title",
MaxLength: 200,
},
"author": Attribute{
Type: Composite{
"firstName": Attribute{
Type: String,
Description: "Author first name",
},
"lastName": Attribute{
Type: String,
Description: "Author last name",
},
},
Required: true,
},
"published": Attribute{
Type: Time,
Description: "Article publication date",
Required: true,
},
},
}
var document = map[string]interface{}{
"article": map[string]interface{}{
"title": "goa, a novel go web application framework",
"author": map[string]interface{}{
"firstName": "Leeroy",
"lastName": "Jenkins",
},
"published": time.Now(),
},
}
var documentType = Composite{
"article": article,
}
It("should coerce valid values", func() {
val, err := documentType.Load(document)
Ω(err).Should(BeNil())
Ω(val).Should(Equal(document))
})
})
Context("example in model.go", func() {
// Data types corresponding to how we want to manipulate the data internally in our application
type Address struct {
Street string `attribute:"street"` // attribute tag is used to map struct field to attribute definition below
City string `attribute:"city"`
}
type Employee struct {
Name string `attribute:"name"`
Title string `attribute:"title"`
Address Address `attribute:"address"`
}
// External representation of the data (e.g. loaded from JSON)
var data = map[string]interface{}{
"name": "John",
"title": "Accountant",
"address": map[string]interface{}{
"street": "5779 Maley Drive",
"city": "Santa Barbara",
},
}
// Model attribute definitions
// Used to both validate and coerce external representation of the data into internal representation
var attributes = Attributes{
"name": Attribute{
Type: String,
MinLength: 1,
Required: true,
},
"title": Attribute{
Type: String,
MinLength: 1,
Required: true,
},
"address": Attribute{
Type: Composite{
"street": Attribute{
Type: String,
},
"city": Attribute{
Type: String,
},
},
},
}
It("creates models", func() {
model, err := NewModel(attributes, Employee{})
Ω(err).ShouldNot(HaveOccurred())
Ω(model.Attributes).Should(Equal(attributes))
})
It("loads data into models", func() {
model, err := NewModel(attributes, Employee{})
Ω(err).ShouldNot(HaveOccurred())
employee, err := model.Load(data)
Ω(err).ShouldNot(HaveOccurred())
Ω(*(employee.(*Employee))).Should(Equal(Employee{"John", "Accountant", Address{"5779 Maley Drive", "Santa Barbara"}}))
})
})
})