go.mercari.io/datastore@v1.8.2/testsuite/favcliptools/main.go (about) 1 //go:generate jwg -output model_json.go -transcripttag swagger . 2 //go:generate qbg -output model_query.go -usedatastorewrapper . 3 4 package favcliptools 5 6 import ( 7 "context" 8 "encoding/json" 9 "fmt" 10 "testing" 11 12 "go.mercari.io/datastore" 13 "go.mercari.io/datastore/boom" 14 "go.mercari.io/datastore/testsuite" 15 ) 16 17 var _ datastore.PropertyTranslator = userID(0) 18 var _ json.Marshaler = userID(1) 19 var _ json.Unmarshaler = (*userID)(nil) 20 21 type contextClient struct{} 22 23 const kindUser = "User" 24 25 type userID int64 26 27 // User kind 28 // +jwg 29 // +qbg 30 type User struct { 31 ID userID `datastore:"-" boom:"id" json:"id"` 32 Name string `json:"name"` 33 MentorID userID `json:"mentorID"` 34 } 35 36 func (id userID) ToPropertyValue(ctx context.Context) (interface{}, error) { 37 client := ctx.Value(contextClient{}).(datastore.Client) 38 key := client.IDKey(kindUser, int64(id), nil) 39 return key, nil 40 } 41 42 func (id userID) FromPropertyValue(ctx context.Context, p datastore.Property) (dst interface{}, err error) { 43 key, ok := p.Value.(datastore.Key) 44 if !ok { 45 return nil, datastore.ErrInvalidEntityType 46 } 47 return userID(key.ID()), nil 48 } 49 50 func (id userID) MarshalJSON() ([]byte, error) { 51 jsonNumber := json.Number(fmt.Sprintf("%d", int64(id))) 52 return json.Marshal(jsonNumber) 53 } 54 55 func (id *userID) UnmarshalJSON(b []byte) error { 56 var jsonNumber json.Number 57 err := json.Unmarshal(b, &jsonNumber) 58 if err != nil { 59 return err 60 } 61 v, err := jsonNumber.Int64() 62 if err != nil { 63 return err 64 } 65 66 *id = userID(v) 67 return nil 68 } 69 70 // TestSuite contains all the test cases that this package provides. 71 var TestSuite = map[string]testsuite.Test{ 72 "FavclipTools": favclipTools, 73 } 74 75 func init() { 76 testsuite.MergeTestSuite(TestSuite) 77 } 78 79 func favclipTools(ctx context.Context, t *testing.T, client datastore.Client) { 80 defer func() { 81 err := client.Close() 82 if err != nil { 83 t.Fatal(err) 84 } 85 }() 86 87 // qbg using client.Context internally 88 ctx = context.WithValue(ctx, contextClient{}, client) 89 client.SetContext(ctx) 90 91 bm := boom.FromClient(ctx, client) 92 93 user := &User{ 94 ID: userID(100), 95 Name: "foobar", 96 MentorID: userID(200), 97 } 98 99 _, err := bm.Put(user) 100 if err != nil { 101 t.Fatal(err) 102 } 103 104 b, err := json.Marshal(user) 105 if err != nil { 106 t.Fatal(err) 107 } 108 109 if v := string(b); v != `{"id":100,"name":"foobar","mentorID":200}` { 110 t.Errorf("unexpected: %v", v) 111 } 112 113 user = &User{} 114 err = json.Unmarshal(b, user) 115 if err != nil { 116 t.Fatal(err) 117 } 118 119 if v := int64(user.ID); v != 100 { 120 t.Errorf("unexpected: %v", v) 121 } 122 if v := user.Name; v != "foobar" { 123 t.Errorf("unexpected: %v", v) 124 } 125 if v := int64(user.MentorID); v != 200 { 126 t.Errorf("unexpected: %v", v) 127 } 128 129 { // for jwg 130 b := NewUserJSONBuilder() 131 b.Add(b.ID) 132 b.Add(b.MentorID) 133 134 userJSON, err := b.Convert(user) 135 if err != nil { 136 t.Fatal(err) 137 } 138 139 if v := int64(userJSON.ID); v != 100 { 140 t.Errorf("unexpected: %v", v) 141 } 142 // removed 143 if v := userJSON.Name; v != "" { 144 t.Errorf("unexpected: %v", v) 145 } 146 if v := int64(userJSON.MentorID); v != 200 { 147 t.Errorf("unexpected: %v", v) 148 } 149 } 150 { // for qbg 151 b := NewUserQueryBuilder(client) 152 b.MentorID.Equal(userID(200)) 153 var list []*User 154 _, err = bm.GetAll(b.Query(), &list) 155 if err != nil { 156 t.Fatal(err) 157 } 158 159 if v := len(list); v != 1 { 160 t.Errorf("unexpected: %v", v) 161 } 162 } 163 }