Files
db/exp/update_test.go
whatandClaude 304d553b3c docs: Add CLAUDE.md with codebase guidance
Create comprehensive documentation for future Claude Code instances working in this repository, including:
- Development commands for testing, building, and code quality
- Core architecture overview of the SQL query builder system
- Directory structure and component explanations
- Testing patterns and conventions
- Key dependencies and their purposes

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-27 15:47:28 +08:00

183 lines
5.4 KiB
Go

package exp_test
import (
"testing"
"git.fsdpf.net/go/db/exp"
"github.com/stretchr/testify/suite"
)
type updateExpressionTestSuite struct {
suite.Suite
}
func (uets *updateExpressionTestSuite) TestNewUpdateExpressions_withInvalidValue() {
_, err := exp.NewUpdateExpressions(true)
uets.EqualError(err, "db: unsupported update interface type bool")
}
func (uets *updateExpressionTestSuite) TestNewUpdateExpressions_withRecords() {
ie, err := exp.NewUpdateExpressions(exp.Record{"c": "a", "b": "d"})
uets.NoError(err)
eie := []exp.UpdateExpression{
exp.NewIdentifierExpression("", "", "b").Set("d"),
exp.NewIdentifierExpression("", "", "c").Set("a"),
}
uets.Equal(eie, ie)
}
func (uets *updateExpressionTestSuite) TestNewUpdateExpressions_withMap() {
ie, err := exp.NewUpdateExpressions(map[string]interface{}{"c": "a", "b": "d"})
uets.NoError(err)
eie := []exp.UpdateExpression{
exp.NewIdentifierExpression("", "", "b").Set("d"),
exp.NewIdentifierExpression("", "", "c").Set("a"),
}
uets.Equal(eie, ie)
}
func (uets *updateExpressionTestSuite) TestNewUpdateExpressions_withStructs() {
type testRecord struct {
C string `db:"c"`
B string `db:"b"`
}
ie, err := exp.NewUpdateExpressions(testRecord{C: "a", B: "d"})
uets.NoError(err)
eie := []exp.UpdateExpression{
exp.NewIdentifierExpression("", "", "b").Set("d"),
exp.NewIdentifierExpression("", "", "c").Set("a"),
}
uets.Equal(eie, ie)
}
func (uets *updateExpressionTestSuite) TestNewUpdateExpressions_withStructsWithoutTags() {
type testRecord struct {
FieldA int64
FieldB bool
FieldC string
}
ie, err := exp.NewUpdateExpressions(testRecord{FieldA: 1, FieldB: true, FieldC: "a"})
uets.NoError(err)
eie := []exp.UpdateExpression{
exp.NewIdentifierExpression("", "", "fielda").Set(int64(1)),
exp.NewIdentifierExpression("", "", "fieldb").Set(true),
exp.NewIdentifierExpression("", "", "fieldc").Set("a"),
}
uets.Equal(eie, ie)
}
func (uets *updateExpressionTestSuite) TestNewUpdateExpressions_withStructsIgnoredDbTag() {
type testRecord struct {
FieldA int64 `db:"-"`
FieldB bool
FieldC string
}
ie, err := exp.NewUpdateExpressions(testRecord{FieldA: 1, FieldB: true, FieldC: "a"})
uets.NoError(err)
eie := []exp.UpdateExpression{
exp.NewIdentifierExpression("", "", "fieldb").Set(true),
exp.NewIdentifierExpression("", "", "fieldc").Set("a"),
}
uets.Equal(eie, ie)
}
func (uets *updateExpressionTestSuite) TestNewUpdateExpressions_withStructsWithDbv2SkipUpdate() {
type testRecord struct {
FieldA int64
FieldB bool `ff:"skipupdate"`
FieldC string `ff:"skipinsert"`
}
ie, err := exp.NewUpdateExpressions(testRecord{FieldA: 1, FieldB: true, FieldC: "a"})
uets.NoError(err)
eie := []exp.UpdateExpression{
exp.NewIdentifierExpression("", "", "fielda").Set(int64(1)),
exp.NewIdentifierExpression("", "", "fieldc").Set("a"),
}
uets.Equal(eie, ie)
}
func (uets *updateExpressionTestSuite) TestNewUpdateExpressions_withStructPointers() {
type testRecord struct {
C string `db:"c"`
B string `db:"b"`
}
ie, err := exp.NewUpdateExpressions(&testRecord{C: "a", B: "d"})
uets.NoError(err)
eie := []exp.UpdateExpression{
exp.NewIdentifierExpression("", "", "b").Set("d"),
exp.NewIdentifierExpression("", "", "c").Set("a"),
}
uets.Equal(eie, ie)
}
func (uets *updateExpressionTestSuite) TestNewUpdateExpressions_withStructsWithEmbeddedStructs() {
type Phone struct {
Primary string `db:"primary_phone"`
Home string `db:"home_phone"`
}
type item struct {
Phone
Address string `db:"address"`
Name string `db:"name"`
}
ie, err := exp.NewUpdateExpressions(
item{Address: "111 Test Addr", Name: "Test1", Phone: Phone{Home: "123123", Primary: "456456"}},
)
uets.NoError(err)
eie := []exp.UpdateExpression{
exp.NewIdentifierExpression("", "", "address").Set("111 Test Addr"),
exp.NewIdentifierExpression("", "", "home_phone").Set("123123"),
exp.NewIdentifierExpression("", "", "name").Set("Test1"),
exp.NewIdentifierExpression("", "", "primary_phone").Set("456456"),
}
uets.Equal(eie, ie)
}
func (uets *updateExpressionTestSuite) TestNewUpdateExpressions_withStructsWithEmbeddedStructPointers() {
type Phone struct {
Primary string `db:"primary_phone"`
Home string `db:"home_phone"`
}
type item struct {
*Phone
Address string `db:"address"`
Name string `db:"name"`
}
ie, err := exp.NewUpdateExpressions(
item{Address: "111 Test Addr", Name: "Test1", Phone: &Phone{Home: "123123", Primary: "456456"}},
)
uets.NoError(err)
eie := []exp.UpdateExpression{
exp.NewIdentifierExpression("", "", "address").Set("111 Test Addr"),
exp.NewIdentifierExpression("", "", "home_phone").Set("123123"),
exp.NewIdentifierExpression("", "", "name").Set("Test1"),
exp.NewIdentifierExpression("", "", "primary_phone").Set("456456"),
}
uets.Equal(eie, ie)
}
func (uets *updateExpressionTestSuite) TestNewUpdateExpressions_withNilEmbeddedStructPointers() {
type Phone struct {
Primary string `db:"primary_phone"`
Home string `db:"home_phone"`
}
type item struct {
*Phone
Address string `db:"address"`
Name string `db:"name"`
}
ie, err := exp.NewUpdateExpressions(
item{Address: "111 Test Addr", Name: "Test1"},
)
uets.NoError(err)
eie := []exp.UpdateExpression{
exp.NewIdentifierExpression("", "", "address").Set("111 Test Addr"),
exp.NewIdentifierExpression("", "", "name").Set("Test1"),
}
uets.Equal(eie, ie)
}
func TestUpdateExpressionSuite(t *testing.T) {
suite.Run(t, new(updateExpressionTestSuite))
}