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

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

Issue 1427933002: Decouple PLS from MGS. (Closed) Base URL: https://github.com/luci/gae@master
Patch Set: Derp Created 5 years, 1 month 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
« no previous file with comments | « service/datastore/pls_impl.go ('k') | service/datastore/properties.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 357 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 So(props[i].SetValue(v+v, props[i].IndexSetting( )), ShouldBeNil) 368 So(props[i].SetValue(v+v, props[i].IndexSetting( )), ShouldBeNil)
369 case int64: 369 case int64:
370 // + means integer addition. 370 // + means integer addition.
371 So(props[i].SetValue(v+v, props[i].IndexSetting( )), ShouldBeNil) 371 So(props[i].SetValue(v+v, props[i].IndexSetting( )), ShouldBeNil)
372 } 372 }
373 } 373 }
374 } 374 }
375 return propMap, nil 375 return propMap, nil
376 } 376 }
377 377
378 func (d *Doubler) GetAllMeta() PropertyMap { retur n nil } 378 func (d *Doubler) Problem() error { return nil }
379 func (d *Doubler) GetMeta(string) (interface{}, error) { retur n nil, ErrMetaFieldUnset }
380 func (d *Doubler) GetMetaDefault(_ string, dflt interface{}) interface{} { retur n dflt }
381 func (d *Doubler) SetMeta(string, interface{}) error { retur n ErrMetaFieldUnset }
382 func (d *Doubler) Problem() error { retur n nil }
383 379
384 var _ PropertyLoadSaver = (*Doubler)(nil) 380 var _ PropertyLoadSaver = (*Doubler)(nil)
385 381
386 type Deriver struct { 382 type Deriver struct {
387 S, Derived, Ignored string 383 S, Derived, Ignored string
388 } 384 }
389 385
390 func (d *Deriver) Load(props PropertyMap) error { 386 func (d *Deriver) Load(props PropertyMap) error {
391 for name, p := range props { 387 for name, p := range props {
392 if name != "S" { 388 if name != "S" {
393 continue 389 continue
394 } 390 }
395 d.S = p[0].Value().(string) 391 d.S = p[0].Value().(string)
396 d.Derived = "derived+" + d.S 392 d.Derived = "derived+" + d.S
397 } 393 }
398 return nil 394 return nil
399 } 395 }
400 396
401 func (d *Deriver) Save(withMeta bool) (PropertyMap, error) { 397 func (d *Deriver) Save(withMeta bool) (PropertyMap, error) {
402 return map[string][]Property{ 398 return map[string][]Property{
403 "S": {mp(d.S)}, 399 "S": {mp(d.S)},
404 }, nil 400 }, nil
405 } 401 }
406 402
407 func (d *Deriver) GetAllMeta() PropertyMap { retur n nil } 403 func (d *Deriver) Problem() error { return nil }
408 func (d *Deriver) GetMeta(string) (interface{}, error) { retur n nil, ErrMetaFieldUnset }
409 func (d *Deriver) GetMetaDefault(_ string, dflt interface{}) interface{} { retur n dflt }
410 func (d *Deriver) SetMeta(string, interface{}) error { retur n ErrMetaFieldUnset }
411 func (d *Deriver) Problem() error { retur n nil }
412 404
413 var _ PropertyLoadSaver = (*Deriver)(nil) 405 var _ PropertyLoadSaver = (*Deriver)(nil)
414 406
407 type Augmenter struct {
408 S string
409
410 g string `gae:"-"`
411 }
412
413 func (a *Augmenter) Load(props PropertyMap) error {
414 if e := props["Extra"]; len(e) > 0 {
415 a.g = e[0].Value().(string)
416 delete(props, "Extra")
417 }
418 if err := GetPLS(a).Load(props); err != nil {
419 return err
420 }
421 return nil
422 }
423
424 func (a *Augmenter) Save(withMeta bool) (PropertyMap, error) {
425 props, err := GetPLS(a).Save(withMeta)
426 if err != nil {
427 return nil, err
428 }
429 props["Extra"] = []Property{MkProperty("ohai!")}
430 return props, nil
431 }
432
433 func (a *Augmenter) Problem() error { return nil }
434
435 var _ PropertyLoadSaver = (*Augmenter)(nil)
436
415 type BK struct { 437 type BK struct {
416 Key blobstore.Key 438 Key blobstore.Key
417 } 439 }
418 440
419 type Convertable []int64 441 type Convertable []int64
420 442
421 var _ PropertyConverter = (*Convertable)(nil) 443 var _ PropertyConverter = (*Convertable)(nil)
422 444
423 func (c *Convertable) ToProperty() (ret Property, err error) { 445 func (c *Convertable) ToProperty() (ret Property, err error) {
424 buf := make([]string, len(*c)) 446 buf := make([]string, len(*c))
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
545 id int64 `gae:"-"` 567 id int64 `gae:"-"`
546 } 568 }
547 569
548 var _ MetaGetterSetter = (*IDParser)(nil) 570 var _ MetaGetterSetter = (*IDParser)(nil)
549 571
550 func (i *IDParser) getFullID() string { 572 func (i *IDParser) getFullID() string {
551 return fmt.Sprintf("%s|%d", i.parent, i.id) 573 return fmt.Sprintf("%s|%d", i.parent, i.id)
552 } 574 }
553 575
554 func (i *IDParser) GetAllMeta() PropertyMap { 576 func (i *IDParser) GetAllMeta() PropertyMap {
555 » pm := PropertyMap{} 577 » pm := GetPLS(i).GetAllMeta()
556 pm.SetMeta("id", i.getFullID()) 578 pm.SetMeta("id", i.getFullID())
557 return pm 579 return pm
558 } 580 }
559 581
560 func (i *IDParser) GetMeta(key string) (interface{}, error) { 582 func (i *IDParser) GetMeta(key string) (interface{}, error) {
561 if key == "id" { 583 if key == "id" {
562 return i.getFullID(), nil 584 return i.getFullID(), nil
563 } 585 }
564 » return nil, ErrMetaFieldUnset 586 » return GetPLS(i).GetMeta(key)
565 } 587 }
566 588
567 func (i *IDParser) GetMetaDefault(key string, dflt interface{}) interface{} { 589 func (i *IDParser) GetMetaDefault(key string, dflt interface{}) interface{} {
568 return GetMetaDefaultImpl(i.GetMeta, key, dflt) 590 return GetMetaDefaultImpl(i.GetMeta, key, dflt)
569 } 591 }
570 592
571 func (i *IDParser) SetMeta(key string, value interface{}) (err error) { 593 func (i *IDParser) SetMeta(key string, value interface{}) (err error) {
572 if key == "id" { 594 if key == "id" {
573 // let the panics flooowwww 595 // let the panics flooowwww
574 vS := strings.SplitN(value.(string), "|", 2) 596 vS := strings.SplitN(value.(string), "|", 2)
575 i.parent = vS[0] 597 i.parent = vS[0]
576 i.id, err = strconv.ParseInt(vS[1], 10, 64) 598 i.id, err = strconv.ParseInt(vS[1], 10, 64)
577 return 599 return
578 } 600 }
579 » return ErrMetaFieldUnset 601 » return GetPLS(i).SetMeta(key, value)
580 } 602 }
581 603
582 type KindOverride struct { 604 type KindOverride struct {
583 ID int64 `gae:"$id"` 605 ID int64 `gae:"$id"`
584 606
585 customKind string `gae:"-"` 607 customKind string `gae:"-"`
586 } 608 }
587 609
588 var _ MetaGetterSetter = (*KindOverride)(nil) 610 var _ MetaGetterSetter = (*KindOverride)(nil)
589 611
590 func (i *KindOverride) GetAllMeta() PropertyMap { 612 func (i *KindOverride) GetAllMeta() PropertyMap {
591 » pm := PropertyMap{} 613 » pm := GetPLS(i).GetAllMeta()
592 if i.customKind != "" { 614 if i.customKind != "" {
593 pm.SetMeta("kind", i.customKind) 615 pm.SetMeta("kind", i.customKind)
594 } 616 }
595 return pm 617 return pm
596 } 618 }
597 619
598 func (i *KindOverride) GetMeta(key string) (interface{}, error) { 620 func (i *KindOverride) GetMeta(key string) (interface{}, error) {
599 if key == "kind" && i.customKind != "" { 621 if key == "kind" && i.customKind != "" {
600 return i.customKind, nil 622 return i.customKind, nil
601 } 623 }
602 » return nil, ErrMetaFieldUnset 624 » return GetPLS(i).GetMeta(key)
603 } 625 }
604 626
605 func (i *KindOverride) GetMetaDefault(key string, dflt interface{}) interface{} { 627 func (i *KindOverride) GetMetaDefault(key string, dflt interface{}) interface{} {
606 return GetMetaDefaultImpl(i.GetMeta, key, dflt) 628 return GetMetaDefaultImpl(i.GetMeta, key, dflt)
607 } 629 }
608 630
609 func (i *KindOverride) SetMeta(key string, value interface{}) error { 631 func (i *KindOverride) SetMeta(key string, value interface{}) error {
610 if key == "kind" { 632 if key == "kind" {
611 kind := value.(string) 633 kind := value.(string)
612 if kind != "KindOverride" { 634 if kind != "KindOverride" {
613 i.customKind = kind 635 i.customKind = kind
614 } else { 636 } else {
615 i.customKind = "" 637 i.customKind = ""
616 } 638 }
617 return nil 639 return nil
618 } 640 }
619 return ErrMetaFieldUnset 641 return ErrMetaFieldUnset
620 } 642 }
621 643
644 type Simple struct{}
645
622 type testCase struct { 646 type testCase struct {
623 desc string 647 desc string
624 src interface{} 648 src interface{}
625 want interface{} 649 want interface{}
626 plsErr string 650 plsErr string
627 saveErr string 651 saveErr string
628 plsLoadErr string 652 plsLoadErr string
629 loadErr string 653 loadErr string
630 } 654 }
631 655
(...skipping 548 matching lines...) Expand 10 before | Expand all | Expand 10 after
1180 { 1204 {
1181 desc: "save struct load deriver", 1205 desc: "save struct load deriver",
1182 src: &X0{S: "s", I: 1}, 1206 src: &X0{S: "s", I: 1},
1183 want: &Deriver{S: "s", Derived: "derived+s"}, 1207 want: &Deriver{S: "s", Derived: "derived+s"},
1184 }, 1208 },
1185 { 1209 {
1186 desc: "save deriver load struct", 1210 desc: "save deriver load struct",
1187 src: &Deriver{S: "s", Derived: "derived+s", Ignored: "ignored"} , 1211 src: &Deriver{S: "s", Derived: "derived+s", Ignored: "ignored"} ,
1188 want: &X0{S: "s"}, 1212 want: &X0{S: "s"},
1189 }, 1213 },
1214 {
1215 desc: "augmenter save",
1216 src: &Augmenter{S: "s"},
1217 want: PropertyMap{
1218 "S": {mp("s")},
1219 "Extra": {mp("ohai!")},
1220 },
1221 },
1222 {
1223 desc: "augmenter load",
1224 src: PropertyMap{
1225 "S": {mp("s")},
1226 "Extra": {mp("kthxbye!")},
1227 },
1228 want: &Augmenter{S: "s", g: "kthxbye!"},
1229 },
1190 // Regression: CL 25062824 broke handling of appengine.BlobKey fields. 1230 // Regression: CL 25062824 broke handling of appengine.BlobKey fields.
1191 { 1231 {
1192 desc: "appengine.BlobKey", 1232 desc: "appengine.BlobKey",
1193 src: &BK{Key: "blah"}, 1233 src: &BK{Key: "blah"},
1194 want: &BK{Key: "blah"}, 1234 want: &BK{Key: "blah"},
1195 }, 1235 },
1196 { 1236 {
1197 desc: "zero time.Time", 1237 desc: "zero time.Time",
1198 src: &T{T: time.Time{}}, 1238 src: &T{T: time.Time{}},
1199 want: &T{T: time.Time{}}, 1239 want: &T{T: time.Time{}},
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after
1603 } 1643 }
1604 }) 1644 })
1605 } 1645 }
1606 1646
1607 func TestMeta(t *testing.T) { 1647 func TestMeta(t *testing.T) {
1608 t.Parallel() 1648 t.Parallel()
1609 1649
1610 Convey("Test meta fields", t, func() { 1650 Convey("Test meta fields", t, func() {
1611 Convey("Can retrieve from struct", func() { 1651 Convey("Can retrieve from struct", func() {
1612 o := &N0{ID: 100} 1652 o := &N0{ID: 100}
1613 » » » pls := GetPLS(o) 1653 » » » mgs := getMGS(o)
1614 » » » val, err := pls.GetMeta("id") 1654 » » » val, err := mgs.GetMeta("id")
1615 So(err, ShouldBeNil) 1655 So(err, ShouldBeNil)
1616 So(val, ShouldEqual, 100) 1656 So(val, ShouldEqual, 100)
1617 1657
1618 » » » val, err = pls.GetMeta("kind") 1658 » » » val, err = mgs.GetMeta("kind")
1619 So(err, ShouldBeNil) 1659 So(err, ShouldBeNil)
1620 So(val, ShouldEqual, "whatnow") 1660 So(val, ShouldEqual, "whatnow")
1621 1661
1622 » » » So(pls.GetMetaDefault("kind", "zappo"), ShouldEqual, "wh atnow") 1662 » » » So(mgs.GetMetaDefault("kind", "zappo"), ShouldEqual, "wh atnow")
1623 » » » So(pls.GetMetaDefault("id", "stringID"), ShouldEqual, "s tringID") 1663 » » » So(mgs.GetMetaDefault("id", "stringID"), ShouldEqual, "s tringID")
1624 » » » So(pls.GetMetaDefault("id", 6), ShouldEqual, 100) 1664 » » » So(mgs.GetMetaDefault("id", 6), ShouldEqual, 100)
1625 }) 1665 })
1626 1666
1627 Convey("Getting something not there is an error", func() { 1667 Convey("Getting something not there is an error", func() {
1628 o := &N0{ID: 100} 1668 o := &N0{ID: 100}
1629 » » » pls := GetPLS(o) 1669 » » » mgs := getMGS(o)
1630 » » » _, err := pls.GetMeta("wat") 1670 » » » _, err := mgs.GetMeta("wat")
1631 So(err, ShouldEqual, ErrMetaFieldUnset) 1671 So(err, ShouldEqual, ErrMetaFieldUnset)
1632 }) 1672 })
1633 1673
1634 Convey("Default works for missing fields", func() { 1674 Convey("Default works for missing fields", func() {
1635 o := &N0{ID: 100} 1675 o := &N0{ID: 100}
1636 » » » pls := GetPLS(o) 1676 » » » mgs := getMGS(o)
1637 » » » So(pls.GetMetaDefault("whozit", 10), ShouldEqual, 10) 1677 » » » So(mgs.GetMetaDefault("whozit", 10), ShouldEqual, 10)
1638 }) 1678 })
1639 1679
1640 Convey("getting/setting from a bad struct is an error", func() { 1680 Convey("getting/setting from a bad struct is an error", func() {
1641 o := &Recursive{} 1681 o := &Recursive{}
1642 » » » pls := GetPLS(o) 1682 » » » mgs := getMGS(o)
1643 » » » _, err := pls.GetMeta("wat") 1683 » » » _, err := mgs.GetMeta("wat")
1644 So(err, ShouldNotBeNil) 1684 So(err, ShouldNotBeNil)
1645 1685
1646 » » » err = pls.SetMeta("wat", 100) 1686 » » » err = mgs.SetMeta("wat", 100)
1647 So(err, ShouldNotBeNil) 1687 So(err, ShouldNotBeNil)
1648 }) 1688 })
1649 1689
1650 Convey("Default works for bad structs", func() { 1690 Convey("Default works for bad structs", func() {
1651 o := &Recursive{} 1691 o := &Recursive{}
1652 » » » pls := GetPLS(o) 1692 » » » mgs := getMGS(o)
1653 » » » So(pls.GetMetaDefault("whozit", 10), ShouldEqual, 10) 1693 » » » So(mgs.GetMetaDefault("whozit", 10), ShouldEqual, 10)
1654 }) 1694 })
1655 1695
1656 Convey("can assign values to exported meta fields", func() { 1696 Convey("can assign values to exported meta fields", func() {
1657 o := &N0{ID: 100} 1697 o := &N0{ID: 100}
1658 » » » pls := GetPLS(o) 1698 » » » mgs := getMGS(o)
1659 » » » err := pls.SetMeta("id", int64(200)) 1699 » » » err := mgs.SetMeta("id", int64(200))
1660 So(err, ShouldBeNil) 1700 So(err, ShouldBeNil)
1661 So(o.ID, ShouldEqual, 200) 1701 So(o.ID, ShouldEqual, 200)
1662 1702
1663 }) 1703 })
1664 1704
1665 Convey("assigning to unsassiagnable fields is a simple error", f unc() { 1705 Convey("assigning to unsassiagnable fields is a simple error", f unc() {
1666 o := &N0{ID: 100} 1706 o := &N0{ID: 100}
1667 » » » pls := GetPLS(o) 1707 » » » mgs := getMGS(o)
1668 » » » err := pls.SetMeta("kind", "hi") 1708 » » » err := mgs.SetMeta("kind", "hi")
1669 So(err.Error(), ShouldContainSubstring, "unexported fiel d") 1709 So(err.Error(), ShouldContainSubstring, "unexported fiel d")
1670 1710
1671 » » » err = pls.SetMeta("noob", "hi") 1711 » » » err = mgs.SetMeta("noob", "hi")
1672 So(err, ShouldEqual, ErrMetaFieldUnset) 1712 So(err, ShouldEqual, ErrMetaFieldUnset)
1673 }) 1713 })
1674 }) 1714 })
1675 1715
1676 Convey("StructPLS Miscellaneous", t, func() { 1716 Convey("StructPLS Miscellaneous", t, func() {
1717 Convey("a simple struct has a default $kind", func() {
1718 So(GetPLS(&Simple{}).GetAllMeta(), ShouldResemble, Prope rtyMap{
1719 "$kind": []Property{mpNI("Simple")},
1720 })
1721 })
1722
1677 Convey("multiple overlapping fields is an error", func() { 1723 Convey("multiple overlapping fields is an error", func() {
1678 o := &BadMeta{} 1724 o := &BadMeta{}
1679 pls := GetPLS(o) 1725 pls := GetPLS(o)
1680 err := pls.Load(nil) 1726 err := pls.Load(nil)
1681 So(err, ShouldErrLike, "multiple times") 1727 So(err, ShouldErrLike, "multiple times")
1682 e := pls.Problem() 1728 e := pls.Problem()
1683 _, err = pls.Save(true) 1729 _, err = pls.Save(true)
1684 So(err, ShouldEqual, e) 1730 So(err, ShouldEqual, e)
1685 err = pls.Load(nil) 1731 err = pls.Load(nil)
1686 So(err, ShouldEqual, e) 1732 So(err, ShouldEqual, e)
1687 }) 1733 })
1688 1734
1689 Convey("empty property names are invalid", func() { 1735 Convey("empty property names are invalid", func() {
1690 So(validPropertyName(""), ShouldBeFalse) 1736 So(validPropertyName(""), ShouldBeFalse)
1691 }) 1737 })
1692 1738
1693 Convey("attempting to get a PLS for a non *struct is an error", func() { 1739 Convey("attempting to get a PLS for a non *struct is an error", func() {
1694 pls := GetPLS((*[]string)(nil)) 1740 pls := GetPLS((*[]string)(nil))
1695 So(pls.Problem(), ShouldEqual, ErrInvalidEntityType) 1741 So(pls.Problem(), ShouldEqual, ErrInvalidEntityType)
1742
1743 Convey("the error PLS can still be used", func() {
1744 k, _ := pls.GetMeta("kind")
1745 So(k, ShouldBeNil)
1746
1747 props := pls.GetAllMeta()
1748 So(props, ShouldResemble, PropertyMap{
1749 "$kind": []Property{mpNI("")},
1750 })
1751 })
1696 }) 1752 })
1697 1753
1698 Convey("convertible meta default types", func() { 1754 Convey("convertible meta default types", func() {
1699 type OKDefaults struct { 1755 type OKDefaults struct {
1700 When string `gae:"$when,tomorrow"` 1756 When string `gae:"$when,tomorrow"`
1701 Amount int64 `gae:"$amt,100"` 1757 Amount int64 `gae:"$amt,100"`
1702 DoIt Toggle `gae:"$doit,on"` 1758 DoIt Toggle `gae:"$doit,on"`
1703 } 1759 }
1704 okd := &OKDefaults{} 1760 okd := &OKDefaults{}
1705 pls := GetPLS(okd) 1761 pls := GetPLS(okd)
1762 mgs := getMGS(okd)
1706 So(pls.Problem(), ShouldBeNil) 1763 So(pls.Problem(), ShouldBeNil)
1707 1764
1708 » » » v, err := pls.GetMeta("when") 1765 » » » v, err := mgs.GetMeta("when")
1709 So(err, ShouldBeNil) 1766 So(err, ShouldBeNil)
1710 So(v, ShouldEqual, "tomorrow") 1767 So(v, ShouldEqual, "tomorrow")
1711 1768
1712 » » » v, err = pls.GetMeta("amt") 1769 » » » v, err = mgs.GetMeta("amt")
1713 So(err, ShouldBeNil) 1770 So(err, ShouldBeNil)
1714 So(v, ShouldEqual, int64(100)) 1771 So(v, ShouldEqual, int64(100))
1715 1772
1716 So(okd.DoIt, ShouldEqual, Auto) 1773 So(okd.DoIt, ShouldEqual, Auto)
1717 » » » v, err = pls.GetMeta("doit") 1774 » » » v, err = mgs.GetMeta("doit")
1718 So(err, ShouldBeNil) 1775 So(err, ShouldBeNil)
1719 So(v, ShouldBeTrue) 1776 So(v, ShouldBeTrue)
1720 1777
1721 » » » err = pls.SetMeta("doit", false) 1778 » » » err = mgs.SetMeta("doit", false)
1722 So(err, ShouldBeNil) 1779 So(err, ShouldBeNil)
1723 » » » v, err = pls.GetMeta("doit") 1780 » » » v, err = mgs.GetMeta("doit")
1724 So(err, ShouldBeNil) 1781 So(err, ShouldBeNil)
1725 So(v, ShouldBeFalse) 1782 So(v, ShouldBeFalse)
1726 So(okd.DoIt, ShouldEqual, Off) 1783 So(okd.DoIt, ShouldEqual, Off)
1727 1784
1728 » » » err = pls.SetMeta("doit", true) 1785 » » » err = mgs.SetMeta("doit", true)
1729 So(err, ShouldBeNil) 1786 So(err, ShouldBeNil)
1730 » » » v, err = pls.GetMeta("doit") 1787 » » » v, err = mgs.GetMeta("doit")
1731 So(err, ShouldBeNil) 1788 So(err, ShouldBeNil)
1732 So(v, ShouldBeTrue) 1789 So(v, ShouldBeTrue)
1733 So(okd.DoIt, ShouldEqual, On) 1790 So(okd.DoIt, ShouldEqual, On)
1734 1791
1735 Convey("Toggle fields REQUIRE a default", func() { 1792 Convey("Toggle fields REQUIRE a default", func() {
1736 type BadToggle struct { 1793 type BadToggle struct {
1737 Bad Toggle `gae:"$wut"` 1794 Bad Toggle `gae:"$wut"`
1738 } 1795 }
1739 pls := GetPLS(&BadToggle{}) 1796 pls := GetPLS(&BadToggle{})
1740 So(pls.Problem().Error(), ShouldContainSubstring , "bad/missing default") 1797 So(pls.Problem().Error(), ShouldContainSubstring , "bad/missing default")
(...skipping 21 matching lines...) Expand all
1762 v, err = pm.GetMeta("amt") 1819 v, err = pm.GetMeta("amt")
1763 So(err, ShouldBeNil) 1820 So(err, ShouldBeNil)
1764 So(v, ShouldEqual, int64(100)) 1821 So(v, ShouldEqual, int64(100))
1765 }) 1822 })
1766 1823
1767 Convey("default are optional", func() { 1824 Convey("default are optional", func() {
1768 type OverrideDefault struct { 1825 type OverrideDefault struct {
1769 Val int64 `gae:"$val"` 1826 Val int64 `gae:"$val"`
1770 } 1827 }
1771 o := &OverrideDefault{} 1828 o := &OverrideDefault{}
1772 » » » pls := GetPLS(o) 1829 » » » mgs := getMGS(o)
1773 1830
1774 » » » v, err := pls.GetMeta("val") 1831 » » » v, err := mgs.GetMeta("val")
1775 So(err, ShouldBeNil) 1832 So(err, ShouldBeNil)
1776 So(v, ShouldEqual, int64(0)) 1833 So(v, ShouldEqual, int64(0))
1777 }) 1834 })
1778 1835
1779 Convey("overridable defaults", func() { 1836 Convey("overridable defaults", func() {
1780 type OverrideDefault struct { 1837 type OverrideDefault struct {
1781 Val int64 `gae:"$val,100"` 1838 Val int64 `gae:"$val,100"`
1782 } 1839 }
1783 o := &OverrideDefault{} 1840 o := &OverrideDefault{}
1784 » » » pls := GetPLS(o) 1841 » » » mgs := getMGS(o)
1785 1842
1786 » » » v, err := pls.GetMeta("val") 1843 » » » v, err := mgs.GetMeta("val")
1787 So(err, ShouldBeNil) 1844 So(err, ShouldBeNil)
1788 So(v, ShouldEqual, int64(100)) 1845 So(v, ShouldEqual, int64(100))
1789 1846
1790 o.Val = 10 1847 o.Val = 10
1791 » » » v, err = pls.GetMeta("val") 1848 » » » v, err = mgs.GetMeta("val")
1792 So(err, ShouldBeNil) 1849 So(err, ShouldBeNil)
1793 So(v, ShouldEqual, int64(10)) 1850 So(v, ShouldEqual, int64(10))
1794 }) 1851 })
1795 1852
1796 Convey("Bad default meta type", func() { 1853 Convey("Bad default meta type", func() {
1797 type BadDefault struct { 1854 type BadDefault struct {
1798 Val time.Time `gae:"$meta,tomorrow"` 1855 Val time.Time `gae:"$meta,tomorrow"`
1799 } 1856 }
1800 pls := GetPLS(&BadDefault{}) 1857 pls := GetPLS(&BadDefault{})
1801 So(pls.Problem().Error(), ShouldContainSubstring, "bad t ype") 1858 So(pls.Problem().Error(), ShouldContainSubstring, "bad t ype")
1802 }) 1859 })
1803 1860
1804 Convey("MetaGetterSetter implementation (IDParser)", func() { 1861 Convey("MetaGetterSetter implementation (IDParser)", func() {
1805 idp := &IDParser{parent: "moo", id: 100} 1862 idp := &IDParser{parent: "moo", id: 100}
1806 » » » pls := GetPLS(idp) 1863 » » » mgs := getMGS(idp)
1807 » » » So(pls.GetMetaDefault("id", ""), ShouldEqual, "moo|100") 1864 » » » So(mgs.GetMetaDefault("id", ""), ShouldEqual, "moo|100")
1808 » » » So(pls.GetMetaDefault("kind", ""), ShouldEqual, "CoolKin d") 1865 » » » So(mgs.GetMetaDefault("kind", ""), ShouldEqual, "CoolKin d")
1809 1866
1810 » » » So(pls.SetMeta("kind", "Something"), ShouldErrLike, "une xported field") 1867 » » » So(mgs.SetMeta("kind", "Something"), ShouldErrLike, "une xported field")
1811 » » » So(pls.SetMeta("id", "happy|27"), ShouldBeNil) 1868 » » » So(mgs.SetMeta("id", "happy|27"), ShouldBeNil)
1812 1869
1813 So(idp.parent, ShouldEqual, "happy") 1870 So(idp.parent, ShouldEqual, "happy")
1814 So(idp.id, ShouldEqual, 27) 1871 So(idp.id, ShouldEqual, 27)
1815 1872
1816 » » » So(pls.GetAllMeta(), ShouldResemble, PropertyMap{ 1873 » » » So(mgs.GetAllMeta(), ShouldResemble, PropertyMap{
1817 "$id": {MkPropertyNI("happy|27")}, 1874 "$id": {MkPropertyNI("happy|27")},
1818 "$kind": {MkPropertyNI("CoolKind")}, 1875 "$kind": {MkPropertyNI("CoolKind")},
1819 }) 1876 })
1820 }) 1877 })
1821 1878
1822 Convey("MetaGetterSetter implementation (KindOverride)", func() { 1879 Convey("MetaGetterSetter implementation (KindOverride)", func() {
1823 ko := &KindOverride{ID: 20} 1880 ko := &KindOverride{ID: 20}
1824 » » » pls := GetPLS(ko) 1881 » » » mgs := getMGS(ko)
1825 » » » So(pls.GetMetaDefault("kind", ""), ShouldEqual, "KindOve rride") 1882 » » » So(mgs.GetMetaDefault("kind", ""), ShouldEqual, "KindOve rride")
1826 1883
1827 ko.customKind = "something" 1884 ko.customKind = "something"
1828 » » » So(pls.GetMetaDefault("kind", ""), ShouldEqual, "somethi ng") 1885 » » » So(mgs.GetMetaDefault("kind", ""), ShouldEqual, "somethi ng")
1829 1886
1830 » » » So(pls.SetMeta("kind", "Nerp"), ShouldBeNil) 1887 » » » So(mgs.SetMeta("kind", "Nerp"), ShouldBeNil)
1831 So(ko.customKind, ShouldEqual, "Nerp") 1888 So(ko.customKind, ShouldEqual, "Nerp")
1832 1889
1833 » » » So(pls.SetMeta("kind", "KindOverride"), ShouldBeNil) 1890 » » » So(mgs.SetMeta("kind", "KindOverride"), ShouldBeNil)
1834 So(ko.customKind, ShouldEqual, "") 1891 So(ko.customKind, ShouldEqual, "")
1835 1892
1836 » » » So(pls.GetAllMeta(), ShouldResemble, PropertyMap{ 1893 » » » So(mgs.GetAllMeta(), ShouldResemble, PropertyMap{
1837 "$id": {MkPropertyNI(20)}, 1894 "$id": {MkPropertyNI(20)},
1838 "$kind": {MkPropertyNI("KindOverride")}, 1895 "$kind": {MkPropertyNI("KindOverride")},
1839 }) 1896 })
1840 ko.customKind = "wut" 1897 ko.customKind = "wut"
1841 » » » So(pls.GetAllMeta(), ShouldResemble, PropertyMap{ 1898 » » » So(mgs.GetAllMeta(), ShouldResemble, PropertyMap{
1842 "$id": {MkPropertyNI(20)}, 1899 "$id": {MkPropertyNI(20)},
1843 "$kind": {MkPropertyNI("wut")}, 1900 "$kind": {MkPropertyNI("wut")},
1844 }) 1901 })
1845 }) 1902 })
1846 }) 1903 })
1847 } 1904 }
OLDNEW
« no previous file with comments | « service/datastore/pls_impl.go ('k') | service/datastore/properties.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698