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

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

Issue 1516173002: Fix error message from KeyForObj when passing an invalid struct. (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: Fix GetMetaDefault silliness Created 5 years 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 568 matching lines...) Expand 10 before | Expand all | Expand 10 after
579 return pm 579 return pm
580 } 580 }
581 581
582 func (i *IDParser) GetMeta(key string) (interface{}, error) { 582 func (i *IDParser) GetMeta(key string) (interface{}, error) {
583 if key == "id" { 583 if key == "id" {
584 return i.getFullID(), nil 584 return i.getFullID(), nil
585 } 585 }
586 return GetPLS(i).GetMeta(key) 586 return GetPLS(i).GetMeta(key)
587 } 587 }
588 588
589 func (i *IDParser) GetMetaDefault(key string, dflt interface{}) interface{} {
590 return GetMetaDefaultImpl(i.GetMeta, key, dflt)
591 }
592
593 func (i *IDParser) SetMeta(key string, value interface{}) (err error) { 589 func (i *IDParser) SetMeta(key string, value interface{}) (err error) {
594 if key == "id" { 590 if key == "id" {
595 // let the panics flooowwww 591 // let the panics flooowwww
596 vS := strings.SplitN(value.(string), "|", 2) 592 vS := strings.SplitN(value.(string), "|", 2)
597 i.parent = vS[0] 593 i.parent = vS[0]
598 i.id, err = strconv.ParseInt(vS[1], 10, 64) 594 i.id, err = strconv.ParseInt(vS[1], 10, 64)
599 return 595 return
600 } 596 }
601 return GetPLS(i).SetMeta(key, value) 597 return GetPLS(i).SetMeta(key, value)
602 } 598 }
(...skipping 14 matching lines...) Expand all
617 return pm 613 return pm
618 } 614 }
619 615
620 func (i *KindOverride) GetMeta(key string) (interface{}, error) { 616 func (i *KindOverride) GetMeta(key string) (interface{}, error) {
621 if key == "kind" && i.customKind != "" { 617 if key == "kind" && i.customKind != "" {
622 return i.customKind, nil 618 return i.customKind, nil
623 } 619 }
624 return GetPLS(i).GetMeta(key) 620 return GetPLS(i).GetMeta(key)
625 } 621 }
626 622
627 func (i *KindOverride) GetMetaDefault(key string, dflt interface{}) interface{} {
628 return GetMetaDefaultImpl(i.GetMeta, key, dflt)
629 }
630
631 func (i *KindOverride) SetMeta(key string, value interface{}) error { 623 func (i *KindOverride) SetMeta(key string, value interface{}) error {
632 if key == "kind" { 624 if key == "kind" {
633 kind := value.(string) 625 kind := value.(string)
634 if kind != "KindOverride" { 626 if kind != "KindOverride" {
635 i.customKind = kind 627 i.customKind = kind
636 } else { 628 } else {
637 i.customKind = "" 629 i.customKind = ""
638 } 630 }
639 return nil 631 return nil
640 } 632 }
(...skipping 978 matching lines...) Expand 10 before | Expand all | Expand 10 after
1619 } else { 1611 } else {
1620 So(actual, ShouldErrLike, expected) 1612 So(actual, ShouldErrLike, expected)
1621 } 1613 }
1622 return expected != "" 1614 return expected != ""
1623 } 1615 }
1624 1616
1625 Convey("Test round-trip", t, func() { 1617 Convey("Test round-trip", t, func() {
1626 for _, tc := range testCases { 1618 for _, tc := range testCases {
1627 tc := tc 1619 tc := tc
1628 Convey(tc.desc, func() { 1620 Convey(tc.desc, func() {
1629 » » » » pls, ok := tc.src.(PropertyLoadSaver) 1621 » » » » pls, ok := tc.src.(interface {
1622 » » » » » PropertyLoadSaver
1623 » » » » » Problem() error
1624 » » » » })
iannucci 2015/12/12 03:52:22 This silliness is because of the way the tests are
1630 if !ok { 1625 if !ok {
1631 pls = GetPLS(tc.src) 1626 pls = GetPLS(tc.src)
1632 } 1627 }
1633 if checkErr(pls.Problem(), tc.plsErr) { 1628 if checkErr(pls.Problem(), tc.plsErr) {
1634 return 1629 return
1635 } 1630 }
1636 So(pls, ShouldNotBeNil) 1631 So(pls, ShouldNotBeNil)
1637 1632
1638 savedProps, err := pls.Save(false) 1633 savedProps, err := pls.Save(false)
1639 if checkErr(err, tc.saveErr) { 1634 if checkErr(err, tc.saveErr) {
1640 return 1635 return
1641 } 1636 }
1642 So(savedProps, ShouldNotBeNil) 1637 So(savedProps, ShouldNotBeNil)
1643 1638
1644 var got interface{} 1639 var got interface{}
1645 if _, ok := tc.want.(PropertyMap); ok { 1640 if _, ok := tc.want.(PropertyMap); ok {
1646 pls = PropertyMap{} 1641 pls = PropertyMap{}
1647 got = pls 1642 got = pls
1648 } else { 1643 } else {
1649 got = reflect.New(reflect.TypeOf(tc.want ).Elem()).Interface() 1644 got = reflect.New(reflect.TypeOf(tc.want ).Elem()).Interface()
1650 » » » » » if pls, ok = got.(PropertyLoadSaver); !o k { 1645 » » » » » if pls, ok = got.(interface {
1646 » » » » » » PropertyLoadSaver
1647 » » » » » » Problem() error
1648 » » » » » }); !ok {
1651 pls = GetPLS(got) 1649 pls = GetPLS(got)
1652 } 1650 }
1653 } 1651 }
1654 1652
1655 if checkErr(pls.Problem(), tc.plsLoadErr) { 1653 if checkErr(pls.Problem(), tc.plsLoadErr) {
1656 return 1654 return
1657 } 1655 }
1658 So(pls, ShouldNotBeNil) 1656 So(pls, ShouldNotBeNil)
1659 1657
1660 err = pls.Load(savedProps) 1658 err = pls.Load(savedProps)
(...skipping 24 matching lines...) Expand all
1685 o := &N0{ID: 100} 1683 o := &N0{ID: 100}
1686 mgs := getMGS(o) 1684 mgs := getMGS(o)
1687 val, err := mgs.GetMeta("id") 1685 val, err := mgs.GetMeta("id")
1688 So(err, ShouldBeNil) 1686 So(err, ShouldBeNil)
1689 So(val, ShouldEqual, 100) 1687 So(val, ShouldEqual, 100)
1690 1688
1691 val, err = mgs.GetMeta("kind") 1689 val, err = mgs.GetMeta("kind")
1692 So(err, ShouldBeNil) 1690 So(err, ShouldBeNil)
1693 So(val, ShouldEqual, "whatnow") 1691 So(val, ShouldEqual, "whatnow")
1694 1692
1695 » » » So(mgs.GetMetaDefault("kind", "zappo"), ShouldEqual, "wh atnow") 1693 » » » v, err := GetMetaDefault(mgs, "kind", "zappo")
1696 » » » So(mgs.GetMetaDefault("id", "stringID"), ShouldEqual, "s tringID") 1694 » » » So(err, ShouldBeNil)
1697 » » » So(mgs.GetMetaDefault("id", 6), ShouldEqual, 100) 1695 » » » So(v, ShouldEqual, "whatnow")
1696
1697 » » » v, err = GetMetaDefault(mgs, "id", "stringID")
1698 » » » So(err, ShouldBeNil)
1699 » » » So(v, ShouldEqual, "stringID")
1700
1701 » » » v, err = GetMetaDefault(mgs, "id", 6)
1702 » » » So(err, ShouldBeNil)
1703 » » » So(v, ShouldEqual, 100)
1698 }) 1704 })
1699 1705
1700 Convey("Getting something not there is an error", func() { 1706 Convey("Getting something not there is an error", func() {
1701 o := &N0{ID: 100} 1707 o := &N0{ID: 100}
1702 mgs := getMGS(o) 1708 mgs := getMGS(o)
1703 _, err := mgs.GetMeta("wat") 1709 _, err := mgs.GetMeta("wat")
1704 So(err, ShouldEqual, ErrMetaFieldUnset) 1710 So(err, ShouldEqual, ErrMetaFieldUnset)
1705 }) 1711 })
1706 1712
1707 Convey("Default works for missing fields", func() { 1713 Convey("Default works for missing fields", func() {
1708 o := &N0{ID: 100} 1714 o := &N0{ID: 100}
1709 mgs := getMGS(o) 1715 mgs := getMGS(o)
1710 » » » So(mgs.GetMetaDefault("whozit", 10), ShouldEqual, 10) 1716 » » » v, err := GetMetaDefault(mgs, "whozit", 10)
1717 » » » So(err, ShouldBeNil)
1718 » » » So(v, ShouldEqual, 10)
1711 }) 1719 })
1712 1720
1713 Convey("getting/setting from a bad struct is an error", func() { 1721 Convey("getting/setting from a bad struct is an error", func() {
1714 o := &Recursive{} 1722 o := &Recursive{}
1715 mgs := getMGS(o) 1723 mgs := getMGS(o)
1716 _, err := mgs.GetMeta("wat") 1724 _, err := mgs.GetMeta("wat")
1717 So(err, ShouldNotBeNil) 1725 So(err, ShouldNotBeNil)
1718 1726
1719 err = mgs.SetMeta("wat", 100) 1727 err = mgs.SetMeta("wat", 100)
1720 So(err, ShouldNotBeNil) 1728 So(err, ShouldNotBeNil)
1721 }) 1729 })
1722 1730
1723 » » Convey("Default works for bad structs", func() { 1731 » » Convey("Default fails for bad structs", func() {
1724 o := &Recursive{} 1732 o := &Recursive{}
1725 mgs := getMGS(o) 1733 mgs := getMGS(o)
1726 » » » So(mgs.GetMetaDefault("whozit", 10), ShouldEqual, 10) 1734 » » » _, err := GetMetaDefault(mgs, "whozit", 10)
1735 » » » So(err, ShouldErrLike, `field "R" is recursively defined `)
1727 }) 1736 })
1728 1737
1729 Convey("can assign values to exported meta fields", func() { 1738 Convey("can assign values to exported meta fields", func() {
1730 o := &N0{ID: 100} 1739 o := &N0{ID: 100}
1731 mgs := getMGS(o) 1740 mgs := getMGS(o)
1732 err := mgs.SetMeta("id", int64(200)) 1741 err := mgs.SetMeta("id", int64(200))
1733 So(err, ShouldBeNil) 1742 So(err, ShouldBeNil)
1734 So(o.ID, ShouldEqual, 200) 1743 So(o.ID, ShouldEqual, 200)
1735 1744
1736 }) 1745 })
1737 1746
1738 Convey("assigning to unsassiagnable fields is a simple error", f unc() { 1747 Convey("assigning to unsassiagnable fields is a simple error", f unc() {
1739 o := &N0{ID: 100} 1748 o := &N0{ID: 100}
1740 mgs := getMGS(o) 1749 mgs := getMGS(o)
1741 err := mgs.SetMeta("kind", "hi") 1750 err := mgs.SetMeta("kind", "hi")
1742 » » » So(err.Error(), ShouldContainSubstring, "unexported fiel d") 1751 » » » So(err, ShouldErrLike, "unexported field")
1743 1752
1744 err = mgs.SetMeta("noob", "hi") 1753 err = mgs.SetMeta("noob", "hi")
1745 So(err, ShouldEqual, ErrMetaFieldUnset) 1754 So(err, ShouldEqual, ErrMetaFieldUnset)
1746 }) 1755 })
1747 }) 1756 })
1748 1757
1749 Convey("StructPLS Miscellaneous", t, func() { 1758 Convey("StructPLS Miscellaneous", t, func() {
1750 Convey("a simple struct has a default $kind", func() { 1759 Convey("a simple struct has a default $kind", func() {
1751 So(GetPLS(&Simple{}).GetAllMeta(), ShouldResemble, Prope rtyMap{ 1760 So(GetPLS(&Simple{}).GetAllMeta(), ShouldResemble, Prope rtyMap{
1752 "$kind": []Property{mpNI("Simple")}, 1761 "$kind": []Property{mpNI("Simple")},
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
1823 v, err = mgs.GetMeta("doit") 1832 v, err = mgs.GetMeta("doit")
1824 So(err, ShouldBeNil) 1833 So(err, ShouldBeNil)
1825 So(v, ShouldBeTrue) 1834 So(v, ShouldBeTrue)
1826 So(okd.DoIt, ShouldEqual, On) 1835 So(okd.DoIt, ShouldEqual, On)
1827 1836
1828 Convey("Toggle fields REQUIRE a default", func() { 1837 Convey("Toggle fields REQUIRE a default", func() {
1829 type BadToggle struct { 1838 type BadToggle struct {
1830 Bad Toggle `gae:"$wut"` 1839 Bad Toggle `gae:"$wut"`
1831 } 1840 }
1832 pls := GetPLS(&BadToggle{}) 1841 pls := GetPLS(&BadToggle{})
1833 » » » » So(pls.Problem().Error(), ShouldContainSubstring , "bad/missing default") 1842 » » » » So(pls.Problem(), ShouldErrLike, "bad/missing de fault")
1834 }) 1843 })
1835 }) 1844 })
1836 1845
1837 Convey("meta fields can be saved", func() { 1846 Convey("meta fields can be saved", func() {
1838 type OKDefaults struct { 1847 type OKDefaults struct {
1839 When string `gae:"$when,tomorrow"` 1848 When string `gae:"$when,tomorrow"`
1840 Amount int64 `gae:"$amt,100"` 1849 Amount int64 `gae:"$amt,100"`
1841 } 1850 }
1842 pls := GetPLS(&OKDefaults{}) 1851 pls := GetPLS(&OKDefaults{})
1843 pm, err := pls.Save(true) 1852 pm, err := pls.Save(true)
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1907 So(mgs.SetMeta("foo", 20), ShouldBeNil) 1916 So(mgs.SetMeta("foo", 20), ShouldBeNil)
1908 So(o.ID, ShouldEqual, DerivedString("nerds")) 1917 So(o.ID, ShouldEqual, DerivedString("nerds"))
1909 So(o.Foo, ShouldEqual, DerivedInt(20)) 1918 So(o.Foo, ShouldEqual, DerivedInt(20))
1910 }) 1919 })
1911 1920
1912 Convey("Bad default meta type", func() { 1921 Convey("Bad default meta type", func() {
1913 type BadDefault struct { 1922 type BadDefault struct {
1914 Val time.Time `gae:"$meta,tomorrow"` 1923 Val time.Time `gae:"$meta,tomorrow"`
1915 } 1924 }
1916 pls := GetPLS(&BadDefault{}) 1925 pls := GetPLS(&BadDefault{})
1917 » » » So(pls.Problem().Error(), ShouldContainSubstring, "bad t ype") 1926 » » » So(pls.Problem(), ShouldErrLike, "bad type")
1918 }) 1927 })
1919 1928
1920 Convey("MetaGetterSetter implementation (IDParser)", func() { 1929 Convey("MetaGetterSetter implementation (IDParser)", func() {
1921 idp := &IDParser{parent: "moo", id: 100} 1930 idp := &IDParser{parent: "moo", id: 100}
1922 mgs := getMGS(idp) 1931 mgs := getMGS(idp)
1923 » » » So(mgs.GetMetaDefault("id", ""), ShouldEqual, "moo|100") 1932 » » » v, err := GetMetaDefault(mgs, "id", "")
1924 » » » So(mgs.GetMetaDefault("kind", ""), ShouldEqual, "CoolKin d") 1933 » » » So(err, ShouldBeNil)
1934 » » » So(v, ShouldEqual, "moo|100")
1935
1936 » » » v, err = GetMetaDefault(mgs, "kind", "")
1937 » » » So(err, ShouldBeNil)
1938 » » » So(v, ShouldEqual, "CoolKind")
1925 1939
1926 So(mgs.SetMeta("kind", "Something"), ShouldErrLike, "une xported field") 1940 So(mgs.SetMeta("kind", "Something"), ShouldErrLike, "une xported field")
1927 So(mgs.SetMeta("id", "happy|27"), ShouldBeNil) 1941 So(mgs.SetMeta("id", "happy|27"), ShouldBeNil)
1928 1942
1929 So(idp.parent, ShouldEqual, "happy") 1943 So(idp.parent, ShouldEqual, "happy")
1930 So(idp.id, ShouldEqual, 27) 1944 So(idp.id, ShouldEqual, 27)
1931 1945
1932 So(mgs.GetAllMeta(), ShouldResemble, PropertyMap{ 1946 So(mgs.GetAllMeta(), ShouldResemble, PropertyMap{
1933 "$id": {mpNI("happy|27")}, 1947 "$id": {mpNI("happy|27")},
1934 "$kind": {mpNI("CoolKind")}, 1948 "$kind": {mpNI("CoolKind")},
1935 }) 1949 })
1936 }) 1950 })
1937 1951
1938 Convey("MetaGetterSetter implementation (KindOverride)", func() { 1952 Convey("MetaGetterSetter implementation (KindOverride)", func() {
1939 ko := &KindOverride{ID: 20} 1953 ko := &KindOverride{ID: 20}
1940 mgs := getMGS(ko) 1954 mgs := getMGS(ko)
1941 » » » So(mgs.GetMetaDefault("kind", ""), ShouldEqual, "KindOve rride") 1955 » » » v, err := GetMetaDefault(mgs, "kind", "")
1956 » » » So(err, ShouldBeNil)
1957 » » » So(v, ShouldEqual, "KindOverride")
1942 1958
1943 ko.customKind = "something" 1959 ko.customKind = "something"
1944 » » » So(mgs.GetMetaDefault("kind", ""), ShouldEqual, "somethi ng") 1960 » » » v, err = GetMetaDefault(mgs, "kind", "")
1961 » » » So(err, ShouldBeNil)
1962 » » » So(v, ShouldEqual, "something")
1945 1963
1946 So(mgs.SetMeta("kind", "Nerp"), ShouldBeNil) 1964 So(mgs.SetMeta("kind", "Nerp"), ShouldBeNil)
1947 So(ko.customKind, ShouldEqual, "Nerp") 1965 So(ko.customKind, ShouldEqual, "Nerp")
1948 1966
1949 So(mgs.SetMeta("kind", "KindOverride"), ShouldBeNil) 1967 So(mgs.SetMeta("kind", "KindOverride"), ShouldBeNil)
1950 So(ko.customKind, ShouldEqual, "") 1968 So(ko.customKind, ShouldEqual, "")
1951 1969
1952 So(mgs.GetAllMeta(), ShouldResemble, PropertyMap{ 1970 So(mgs.GetAllMeta(), ShouldResemble, PropertyMap{
1953 "$id": {mpNI(20)}, 1971 "$id": {mpNI(20)},
1954 "$kind": {mpNI("KindOverride")}, 1972 "$kind": {mpNI("KindOverride")},
(...skipping 22 matching lines...) Expand all
1977 So(pls.SetMeta("id", "sup|1337"), ShouldBeNil) 1995 So(pls.SetMeta("id", "sup|1337"), ShouldBeNil)
1978 So(ide.EmbeddedID, ShouldResemble, EmbeddedID{"sup", 133 7}) 1996 So(ide.EmbeddedID, ShouldResemble, EmbeddedID{"sup", 133 7})
1979 1997
1980 So(pls.GetAllMeta(), ShouldResembleV, PropertyMap{ 1998 So(pls.GetAllMeta(), ShouldResembleV, PropertyMap{
1981 "$id": {mpNI("sup|1337")}, 1999 "$id": {mpNI("sup|1337")},
1982 "$kind": {mpNI("IDEmbedder")}, 2000 "$kind": {mpNI("IDEmbedder")},
1983 }) 2001 })
1984 }) 2002 })
1985 }) 2003 })
1986 } 2004 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698