chore: add tests

This commit is contained in:
Martin Munilla 2024-04-17 11:55:40 +02:00
parent af8ad0403d
commit a0c1da1160
4 changed files with 106 additions and 7 deletions

View File

@ -300,13 +300,11 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam
field.AutoIncrement = true field.AutoIncrement = true
} }
case String: case String:
if _, ok := field.TagSettings["PRIMARYKEY"]; !ok { if !field.HasDefaultValue || field.DefaultValueInterface != nil {
if !field.HasDefaultValue || field.DefaultValueInterface != nil { schema.FieldsWithDefaultDBValue = append(schema.FieldsWithDefaultDBValue, field)
schema.FieldsWithDefaultDBValue = append(schema.FieldsWithDefaultDBValue, field)
}
field.HasDefaultValue = true
} }
field.HasDefaultValue = true
} }
} }

View File

@ -334,3 +334,48 @@ func TestCompositePrimaryKeyWithAutoIncrement(t *testing.T) {
t.Fatalf("PrioritizedPrimaryField of non autoincrement composite key should be nil") t.Fatalf("PrioritizedPrimaryField of non autoincrement composite key should be nil")
} }
} }
func TestStringPrimaryKeyDefault(t *testing.T) {
type Product struct {
ID string
Code string
Name string
}
type ProductWithNamedPrimaryKey struct {
ProductID string `gorm:"primaryKey"`
Code string
Name string
}
product, err := schema.Parse(&Product{}, &sync.Map{}, schema.NamingStrategy{})
if err != nil {
t.Fatalf("failed to parse product struct with composite primary key, got error %v", err)
}
isInDefault := false
for _, field := range product.FieldsWithDefaultDBValue {
if field.Name == "ID" {
isInDefault = true
break
}
}
if !isInDefault {
t.Errorf("ID should be fields with default")
}
productWithNamedPrimaryKey, err := schema.Parse(&ProductWithNamedPrimaryKey{}, &sync.Map{}, schema.NamingStrategy{})
if err != nil {
t.Fatalf("failed to parse product struct with composite primary key, got error %v", err)
}
isInDefault = false
for _, field := range productWithNamedPrimaryKey.FieldsWithDefaultDBValue {
if field.Name == "ProductID" {
isInDefault = true
break
}
}
if !isInDefault {
t.Errorf("ProductID should be fields with default")
}
}

View File

@ -0,0 +1,54 @@
package tests_test
import (
"sync"
"testing"
"github.com/google/uuid"
"gorm.io/gorm/schema"
)
func TestStringPrimaryKeyDefault(t *testing.T) {
type Product struct {
ID uuid.UUID
Code string
Name string
}
type ProductWithNamedPrimaryKey struct {
ProductID uuid.UUID `gorm:"primaryKey"`
Code string
Name string
}
product, err := schema.Parse(&Product{}, &sync.Map{}, schema.NamingStrategy{})
if err != nil {
t.Fatalf("failed to parse product struct with composite primary key, got error %v", err)
}
isInDefault := false
for _, field := range product.FieldsWithDefaultDBValue {
if field.Name == "ID" {
isInDefault = true
break
}
}
if !isInDefault {
t.Errorf("ID should be fields with default")
}
productWithNamedPrimaryKey, err := schema.Parse(&ProductWithNamedPrimaryKey{}, &sync.Map{}, schema.NamingStrategy{})
if err != nil {
t.Fatalf("failed to parse product struct with composite primary key, got error %v", err)
}
isInDefault = false
for _, field := range productWithNamedPrimaryKey.FieldsWithDefaultDBValue {
if field.Name == "ProductID" {
isInDefault = true
break
}
}
if !isInDefault {
t.Errorf("ProductID should be fields with default")
}
}

View File

@ -1,6 +1,7 @@
package tests_test package tests_test
import ( import (
"fmt"
"regexp" "regexp"
"testing" "testing"
"time" "time"
@ -62,7 +63,8 @@ func TestUpsert(t *testing.T) {
} }
r := DB.Session(&gorm.Session{DryRun: true}).Clauses(clause.OnConflict{UpdateAll: true}).Create(&RestrictedLanguage{Code: "upsert_code", Name: "upsert_name", Lang: "upsert_lang"}) r := DB.Session(&gorm.Session{DryRun: true}).Clauses(clause.OnConflict{UpdateAll: true}).Create(&RestrictedLanguage{Code: "upsert_code", Name: "upsert_name", Lang: "upsert_lang"})
if !regexp.MustCompile(`INTO .restricted_languages. .*\(.code.,.name.,.lang.\) .* (SET|UPDATE) .name.=.*.name.\W*$`).MatchString(r.Statement.SQL.String()) { fmt.Println(r.Statement.SQL.String())
if !regexp.MustCompile(`INTO .restricted_languages. .*\(.name.,.lang.,.code.\) .* (SET|UPDATE) .name.=.*.name. RETURNING .code.\W*$`).MatchString(r.Statement.SQL.String()) {
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String()) t.Errorf("Table with escape character, got %v", r.Statement.SQL.String())
} }
} }