Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: service/datastore/pls_test.go

Issue 1270113002: Re-add metadata passthrough on Get operations (Closed) Base URL: https://github.com/luci/gae.git@fix_other_interfaces
Patch Set: safer, add tests Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // adapted from github.com/golang/appengine/datastore 5 // adapted from github.com/golang/appengine/datastore
6 6
7 package datastore 7 package datastore
8 8
9 import ( 9 import (
10 "bytes" 10 "bytes"
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 type MismatchTypes struct { 330 type MismatchTypes struct {
331 S string 331 S string
332 B bool 332 B bool
333 F float32 333 F float32
334 K Key 334 K Key
335 T time.Time 335 T time.Time
336 G GeoPoint 336 G GeoPoint
337 IS []int 337 IS []int
338 } 338 }
339 339
340 type BadSpecial struct { 340 type BadMeta struct {
341 ID int64 `gae:"$id"` 341 ID int64 `gae:"$id"`
342 id string `gae:"$id"` 342 id string `gae:"$id"`
343 } 343 }
344 344
345 type Doubler struct { 345 type Doubler struct {
346 S string 346 S string
347 I int64 347 I int64
348 B bool 348 B bool
349 } 349 }
350 350
(...skipping 17 matching lines...) Expand all
368 props[i].SetValue(v+v, props[i].IndexSetting()) 368 props[i].SetValue(v+v, props[i].IndexSetting())
369 case int64: 369 case int64:
370 // + means integer addition. 370 // + means integer addition.
371 props[i].SetValue(v+v, props[i].IndexSetting()) 371 props[i].SetValue(v+v, props[i].IndexSetting())
372 } 372 }
373 } 373 }
374 } 374 }
375 return propMap, nil 375 return propMap, nil
376 } 376 }
377 377
378 func (d *Doubler) GetMeta(string) (interface{}, error) { return nil, ErrMetaFiel dUnset } 378 func (d *Doubler) GetMeta(string) (interface{}, error) { retur n nil, ErrMetaFieldUnset }
379 func (d *Doubler) SetMeta(string, interface{}) error { return ErrMetaFieldUnse t } 379 func (d *Doubler) GetMetaDefault(_ string, dflt interface{}) interface{} { retur n dflt }
380 func (d *Doubler) Problem() error { return nil } 380 func (d *Doubler) SetMeta(string, interface{}) error { retur n ErrMetaFieldUnset }
381 func (d *Doubler) Problem() error { retur n nil }
381 382
382 var _ PropertyLoadSaver = (*Doubler)(nil) 383 var _ PropertyLoadSaver = (*Doubler)(nil)
383 384
384 type Deriver struct { 385 type Deriver struct {
385 S, Derived, Ignored string 386 S, Derived, Ignored string
386 } 387 }
387 388
388 func (d *Deriver) Load(props PropertyMap) error { 389 func (d *Deriver) Load(props PropertyMap) error {
389 for name, p := range props { 390 for name, p := range props {
390 if name != "S" { 391 if name != "S" {
391 continue 392 continue
392 } 393 }
393 d.S = p[0].Value().(string) 394 d.S = p[0].Value().(string)
394 d.Derived = "derived+" + d.S 395 d.Derived = "derived+" + d.S
395 } 396 }
396 return nil 397 return nil
397 } 398 }
398 399
399 func (d *Deriver) Save(withMeta bool) (PropertyMap, error) { 400 func (d *Deriver) Save(withMeta bool) (PropertyMap, error) {
400 return map[string][]Property{ 401 return map[string][]Property{
401 "S": {mp(d.S)}, 402 "S": {mp(d.S)},
402 }, nil 403 }, nil
403 } 404 }
404 405
405 func (d *Deriver) GetMeta(string) (interface{}, error) { return nil, ErrMetaFiel dUnset } 406 func (d *Deriver) GetMeta(string) (interface{}, error) { retur n nil, ErrMetaFieldUnset }
406 func (d *Deriver) SetMeta(string, interface{}) error { return ErrMetaFieldUnse t } 407 func (d *Deriver) GetMetaDefault(_ string, dflt interface{}) interface{} { retur n dflt }
407 func (d *Deriver) Problem() error { return nil } 408 func (d *Deriver) SetMeta(string, interface{}) error { retur n ErrMetaFieldUnset }
409 func (d *Deriver) Problem() error { retur n nil }
408 410
409 var _ PropertyLoadSaver = (*Deriver)(nil) 411 var _ PropertyLoadSaver = (*Deriver)(nil)
410 412
411 type BK struct { 413 type BK struct {
412 Key blobstore.Key 414 Key blobstore.Key
413 } 415 }
414 416
415 type Convertable []int64 417 type Convertable []int64
416 418
417 var _ PropertyConverter = (*Convertable)(nil) 419 var _ PropertyConverter = (*Convertable)(nil)
(...skipping 1139 matching lines...) Expand 10 before | Expand all | Expand 10 after
1557 // We therefore test equality explicitly , instead of relying on reflect.DeepEqual. 1559 // We therefore test equality explicitly , instead of relying on reflect.DeepEqual.
1558 So(gotT.T.Equal(tc.want.(*T).T), ShouldB eTrue) 1560 So(gotT.T.Equal(tc.want.(*T).T), ShouldB eTrue)
1559 } else { 1561 } else {
1560 So(got, ShouldResemble, tc.want) 1562 So(got, ShouldResemble, tc.want)
1561 } 1563 }
1562 }) 1564 })
1563 } 1565 }
1564 }) 1566 })
1565 } 1567 }
1566 1568
1567 func TestSpecial(t *testing.T) { 1569 func TestMeta(t *testing.T) {
1568 t.Parallel() 1570 t.Parallel()
1569 1571
1570 » Convey("Test special fields", t, func() { 1572 » Convey("Test meta fields", t, func() {
1571 Convey("Can retrieve from struct", func() { 1573 Convey("Can retrieve from struct", func() {
1572 o := &N0{ID: 100} 1574 o := &N0{ID: 100}
1573 pls := GetPLS(o) 1575 pls := GetPLS(o)
1574 val, err := pls.GetMeta("id") 1576 val, err := pls.GetMeta("id")
1575 So(err, ShouldBeNil) 1577 So(err, ShouldBeNil)
1576 So(val, ShouldEqual, 100) 1578 So(val, ShouldEqual, 100)
1577 1579
1578 val, err = pls.GetMeta("kind") 1580 val, err = pls.GetMeta("kind")
1579 So(err, ShouldBeNil) 1581 So(err, ShouldBeNil)
1580 So(val, ShouldEqual, "whatnow") 1582 So(val, ShouldEqual, "whatnow")
1583
1584 So(pls.GetMetaDefault("kind", "zappo"), ShouldEqual, "wh atnow")
1585 So(pls.GetMetaDefault("id", "stringID"), ShouldEqual, "s tringID")
1586 So(pls.GetMetaDefault("id", 6), ShouldEqual, 100)
1581 }) 1587 })
1582 1588
1583 Convey("Getting something not there is an error", func() { 1589 Convey("Getting something not there is an error", func() {
1584 o := &N0{ID: 100} 1590 o := &N0{ID: 100}
1585 pls := GetPLS(o) 1591 pls := GetPLS(o)
1586 _, err := pls.GetMeta("wat") 1592 _, err := pls.GetMeta("wat")
1587 So(err, ShouldEqual, ErrMetaFieldUnset) 1593 So(err, ShouldEqual, ErrMetaFieldUnset)
1588 }) 1594 })
1589 1595
1596 Convey("Default works for missing fields", func() {
1597 o := &N0{ID: 100}
1598 pls := GetPLS(o)
1599 So(pls.GetMetaDefault("whozit", 10), ShouldEqual, 10)
1600 })
1601
1590 Convey("getting/setting from a bad struct is an error", func() { 1602 Convey("getting/setting from a bad struct is an error", func() {
1591 o := &Recursive{} 1603 o := &Recursive{}
1592 pls := GetPLS(o) 1604 pls := GetPLS(o)
1593 _, err := pls.GetMeta("wat") 1605 _, err := pls.GetMeta("wat")
1594 So(err, ShouldNotBeNil) 1606 So(err, ShouldNotBeNil)
1595 1607
1596 err = pls.SetMeta("wat", 100) 1608 err = pls.SetMeta("wat", 100)
1597 So(err, ShouldNotBeNil) 1609 So(err, ShouldNotBeNil)
1598 }) 1610 })
1599 1611
1600 » » Convey("can assign values to exported special fields", func() { 1612 » » Convey("Default works for bad structs", func() {
1613 » » » o := &Recursive{}
1614 » » » pls := GetPLS(o)
1615 » » » So(pls.GetMetaDefault("whozit", 10), ShouldEqual, 10)
1616 » » })
1617
1618 » » Convey("can assign values to exported meta fields", func() {
1601 o := &N0{ID: 100} 1619 o := &N0{ID: 100}
1602 pls := GetPLS(o) 1620 pls := GetPLS(o)
1603 err := pls.SetMeta("id", int64(200)) 1621 err := pls.SetMeta("id", int64(200))
1604 So(err, ShouldBeNil) 1622 So(err, ShouldBeNil)
1605 So(o.ID, ShouldEqual, 200) 1623 So(o.ID, ShouldEqual, 200)
1606 1624
1607 }) 1625 })
1608 1626
1609 Convey("assigning to unsassiagnable fields is a simple error", f unc() { 1627 Convey("assigning to unsassiagnable fields is a simple error", f unc() {
1610 o := &N0{ID: 100} 1628 o := &N0{ID: 100}
1611 pls := GetPLS(o) 1629 pls := GetPLS(o)
1612 err := pls.SetMeta("kind", "hi") 1630 err := pls.SetMeta("kind", "hi")
1613 So(err.Error(), ShouldContainSubstring, "unexported fiel d") 1631 So(err.Error(), ShouldContainSubstring, "unexported fiel d")
1614 1632
1615 err = pls.SetMeta("noob", "hi") 1633 err = pls.SetMeta("noob", "hi")
1616 So(err, ShouldEqual, ErrMetaFieldUnset) 1634 So(err, ShouldEqual, ErrMetaFieldUnset)
1617 }) 1635 })
1618 }) 1636 })
1619 1637
1620 Convey("StructPLS Miscellaneous", t, func() { 1638 Convey("StructPLS Miscellaneous", t, func() {
1621 Convey("multiple overlapping fields is an error", func() { 1639 Convey("multiple overlapping fields is an error", func() {
1622 » » » o := &BadSpecial{} 1640 » » » o := &BadMeta{}
1623 pls := GetPLS(o) 1641 pls := GetPLS(o)
1624 err := pls.Load(nil) 1642 err := pls.Load(nil)
1625 So(err, ShouldErrLike, "multiple times") 1643 So(err, ShouldErrLike, "multiple times")
1626 e := pls.Problem() 1644 e := pls.Problem()
1627 _, err = pls.Save(true) 1645 _, err = pls.Save(true)
1628 So(err, ShouldEqual, e) 1646 So(err, ShouldEqual, e)
1629 err = pls.Load(nil) 1647 err = pls.Load(nil)
1630 So(err, ShouldEqual, e) 1648 So(err, ShouldEqual, e)
1631 }) 1649 })
1632 1650
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
1738 1756
1739 Convey("Bad default meta type", func() { 1757 Convey("Bad default meta type", func() {
1740 type BadDefault struct { 1758 type BadDefault struct {
1741 Val time.Time `gae:"$meta,tomorrow"` 1759 Val time.Time `gae:"$meta,tomorrow"`
1742 } 1760 }
1743 pls := GetPLS(&BadDefault{}) 1761 pls := GetPLS(&BadDefault{})
1744 So(pls.Problem().Error(), ShouldContainSubstring, "bad t ype") 1762 So(pls.Problem().Error(), ShouldContainSubstring, "bad t ype")
1745 }) 1763 })
1746 }) 1764 })
1747 } 1765 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698