MCPcopy Create free account
hub / github.com/upper/db / TestUpdate

Method TestUpdate

internal/testsuite/sql_suite.go:698–828  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

696}
697
698func (s *SQLTestSuite) TestUpdate() {
699 sess := s.Session()
700
701 artist := sess.Collection("artist")
702
703 _, err := artist.Insert(map[string]string{
704 "name": "Ozzie",
705 })
706 s.NoError(err)
707
708 // Defining destination struct
709 value := struct {
710 ID int64 `db:"id,omitempty"`
711 Name string `db:"name"`
712 }{}
713
714 // Getting the first artist.
715 cond := db.Cond{"id !=": db.NotEq(0)}
716 if s.Adapter() == "ql" {
717 cond = db.Cond{"id() !=": 0}
718 }
719 res := artist.Find(cond).Limit(1)
720
721 err = res.One(&value)
722 s.NoError(err)
723
724 res = artist.Find(value.ID)
725
726 // Updating set with a map
727 rowMap := map[string]interface{}{
728 "name": strings.ToUpper(value.Name),
729 }
730
731 err = res.Update(rowMap)
732 s.NoError(err)
733
734 // Pulling it again.
735 err = res.One(&value)
736 s.NoError(err)
737
738 // Verifying.
739 s.Equal(value.Name, rowMap["name"])
740
741 if s.Adapter() != "ql" {
742
743 // Updating using raw
744 if err = res.Update(map[string]interface{}{"name": db.Raw("LOWER(name)")}); err != nil {
745 s.T().Errorf("%v", err)
746 }
747
748 // Pulling it again.
749 err = res.One(&value)
750 s.NoError(err)
751
752 // Verifying.
753 s.Equal(value.Name, strings.ToLower(rowMap["name"].(string)))
754
755 // Updating using raw

Callers

nothing calls this directly

Calls 10

RawMethod · 0.80
SessionMethod · 0.65
CollectionMethod · 0.65
InsertMethod · 0.65
AdapterMethod · 0.65
LimitMethod · 0.65
FindMethod · 0.65
OneMethod · 0.65
UpdateMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected