forked from mfcochauxlaberge/jsonapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_test.go
More file actions
56 lines (41 loc) · 1.48 KB
/
resource_test.go
File metadata and controls
56 lines (41 loc) · 1.48 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
package jsonapi
import (
"testing"
"time"
"github.com/kkaribu/tchek"
)
func TestResource(t *testing.T) {
loc, _ := time.LoadLocation("")
p1 := &painting{
ID: "persistence-memory",
Title: "The Persistence of Memory",
PaintedIn: time.Date(1931, 0, 0, 0, 0, 0, 0, loc),
Author: "some-artist",
}
res := Wrap(p1)
// Get
tchek.AreEqual(t, "get attribute", p1.Title, res.Get("title"))
tchek.AreEqual(t, "get to-one relationship", "some-artist", res.GetToOne("author"))
// Set
res.Set("title", "New Title")
tchek.AreEqual(t, "set string attribute", "New Title", p1.Title)
tchek.AreEqual(t, "set string attribute 2", "New Title", res.Get("title"))
p1.PaintedIn = time.Date(1932, 0, 0, 0, 0, 0, 0, loc)
tchek.AreEqual(t, "set time attribute", p1.PaintedIn, res.Get("painted-in"))
res.SetToOne("author", "another-artist")
tchek.AreEqual(t, "set to-one relationship", "another-artist", p1.Author)
tchek.AreEqual(t, "set to-one relationship 2", "another-artist", res.GetToOne("author"))
}
type painting struct {
ID string `json:"id" api:"paintings"`
Title string `json:"title" api:"attr"`
Value uint `json:"value" api:"attr"`
PaintedIn time.Time `json:"painted-in" api:"attr"`
Author string `json:"author" api:"rel,artists,paintings"`
}
type artist struct {
ID string `json:"id" api:"artists"`
Name string `json:"name" api:"attr"`
BornAt time.Time `json:"born-at" api:"attr"`
Paintings string `json:"paintings" api:"rel,paintings,author"`
}