Skip to content

Commit

Permalink
add create test
Browse files Browse the repository at this point in the history
  • Loading branch information
方圣卿 committed Nov 7, 2023
1 parent c47f0e1 commit 526bf5a
Showing 1 changed file with 150 additions and 1 deletion.
151 changes: 150 additions & 1 deletion tests/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tests_test

import (
"errors"
"fmt"
"regexp"
"testing"
"time"
Expand Down Expand Up @@ -580,7 +581,7 @@ func TestCreateWithAutoIncrementCompositeKey(t *testing.T) {
}
}

func TestCreateOnConfilctWithDefalutNull(t *testing.T) {
func TestCreateOnConflictWithDefaultNull(t *testing.T) {
type OnConfilctUser struct {
ID string
Name string `gorm:"default:null"`
Expand Down Expand Up @@ -615,3 +616,151 @@ func TestCreateOnConfilctWithDefalutNull(t *testing.T) {
AssertEqual(t, u2.Email, "on-confilct-user-email-2")
AssertEqual(t, u2.Mobile, "133xxxx")
}

func TestCreateFromMapWithoutPK(t *testing.T) {
cases := []string{"create_from_map_with_schema1", "create_from_map_with_schema2"}
mapValue1 := map[string]interface{}{"name": cases[0], "age": 1}
mapValue2 := map[string]interface{}{"name": cases[1], "age": 1}

// case 1: one record
for i, c := range cases {
if i == 0 {
if err := DB.Model(&User{}).Create(mapValue1).Error; err != nil {
t.Fatalf("failed to create data from map, got error: %v", err)
}

if _, ok := mapValue1["id"]; !ok {
t.Fatal("failed to create data from map with table, returning map has no primary key")
}

} else {
if err := DB.Model(&User{}).Create(&mapValue2).Error; err != nil {
t.Fatalf("failed to create data from map, got error: %v", err)
}

if _, ok := mapValue2["id"]; !ok {
t.Fatal("failed to create data from map with table, returning map has no primary key")
}
}

var result User
if err := DB.Where("name = ?", c).First(&result).Error; err != nil || result.Age != 1 {
t.Fatalf("failed to create from map, got error %v", err)
}

var idVal int64
if i == 0 {
idVal = mapValue1["id"].(int64)
} else {
idVal = mapValue2["id"].(int64)
}

if int64(result.ID) != idVal {
t.Fatal("failed to create data from map with table, @id != id")
}
}

// case 2: records
values := []map[string]interface{}{
{"name": "create_from_map_with_schema11", "age": 1}, {"name": "create_from_map_with_schema12", "age": 1},
}

if err := DB.Model(&User{}).Create(&values).Error; err != nil {
t.Fatalf("failed to create data from map, got error: %v", err)
}

for i, _ := range values {

Check failure on line 672 in tests/create_test.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[gofumpt] reported by reviewdog 🐶 Raw Output: tests/create_test.go:672:- for i, _ := range values { tests/create_test.go:672:+ for i := range values {
v, ok := values[i]["id"]
if !ok {
t.Fatal("failed to create data from map with table, returning map has no primary key")
}

var result User
if err := DB.Where("name = ?", fmt.Sprintf("create_from_map_with_schema1%d", i+1)).First(&result).Error; err != nil || result.Age != 1 {
t.Fatalf("failed to create from map, got error %v", err)
}
if int64(result.ID) != v.(int64) {
t.Fatal("failed to create data from map with table, @id != id")
}
}
}

func TestCreateFromMapWithTable(t *testing.T) {
tableDB := DB.Table("`users`")

// case 1: create from map[string]interface{}
record := map[string]interface{}{"`name`": "create_from_map_with_table", "`age`": 18}
if err := tableDB.Create(record).Error; err != nil {
t.Fatalf("failed to create data from map with table, got error: %v", err)
}

if _, ok := record["@id"]; !ok {
t.Fatal("failed to create data from map with table, returning map has no key '@id'")
}

var res map[string]interface{}
if err := tableDB.Select([]string{"id", "name", "age"}).Where("name = ?", "create_from_map_with_table").Find(&res).Error; err != nil || res["age"] != int64(18) {
t.Fatalf("failed to create from map, got error %v", err)
}

if int64(res["id"].(uint64)) != record["@id"] {
t.Fatal("failed to create data from map with table, @id != id")
}

// case 2: create from *map[string]interface{}
record1 := map[string]interface{}{"name": "create_from_map_with_table_1", "age": 18}
tableDB2 := DB.Table("users")
if err := tableDB2.Create(&record1).Error; err != nil {
t.Fatalf("failed to create data from map, got error: %v", err)
}
if _, ok := record1["@id"]; !ok {
t.Fatal("failed to create data from map with table, returning map has no key '@id'")
}

var res1 map[string]interface{}
if err := tableDB2.Select([]string{"id", "name", "age"}).Where("name = ?", "create_from_map_with_table_1").Find(&res1).Error; err != nil || res1["age"] != int64(18) {
t.Fatalf("failed to create from map, got error %v", err)
}

if int64(res1["id"].(uint64)) != record1["@id"] {
t.Fatal("failed to create data from map with table, @id != id")
}

// case 3: create from []map[string]interface{}
records := []map[string]interface{}{
{"name": "create_from_map_with_table_2", "age": 19},
{"name": "create_from_map_with_table_3", "age": 20},
}

tableDB = DB.Table("users")
if err := tableDB.Create(&records).Error; err != nil {
t.Fatalf("failed to create data from slice of map, got error: %v", err)
}

if _, ok := records[0]["@id"]; !ok {
t.Fatal("failed to create data from map with table, returning map has no key '@id'")
}

if _, ok := records[1]["@id"]; !ok {
t.Fatal("failed to create data from map with table, returning map has no key '@id'")
}

var res2 map[string]interface{}
if err := tableDB.Select([]string{"id", "name", "age"}).Where("name = ?", "create_from_map_with_table_2").Find(&res2).Error; err != nil || res2["age"] != int64(19) {
t.Fatalf("failed to query data after create from slice of map, got error %v", err)
}

var res3 map[string]interface{}
if err := DB.Table("users").Select([]string{"id", "name", "age"}).Where("name = ?", "create_from_map_with_table_3").Find(&res3).Error; err != nil || res3["age"] != int64(20) {
t.Fatalf("failed to query data after create from slice of map, got error %v", err)
}

if int64(res2["id"].(uint64)) != records[0]["@id"] {
t.Fatal("failed to create data from map with table, @id != id")
}

if int64(res3["id"].(uint64)) != records[1]["@id"] {
t.Fatal("failed to create data from map with table, @id != id")
}

Check failure on line 765 in tests/create_test.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[gofumpt] reported by reviewdog 🐶 Raw Output: tests/create_test.go:765:-
}

0 comments on commit 526bf5a

Please sign in to comment.