MCPcopy
hub / github.com/go-gorm/gorm / TestCreateFromMapWithTable

Function TestCreateFromMapWithTable

tests/create_test.go:732–810  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

730}
731
732func TestCreateFromMapWithTable(t *testing.T) {
733 tableDB := DB.Table("users")
734 supportLastInsertID := isMysql() || isSqlite()
735
736 // case 1: create from map[string]interface{}
737 record := map[string]interface{}{"name": "create_from_map_with_table", "age": 18}
738 if err := tableDB.Create(record).Error; err != nil {
739 t.Fatalf("failed to create data from map with table, got error: %v", err)
740 }
741
742 if _, ok := record["@id"]; !ok && supportLastInsertID {
743 t.Fatal("failed to create data from map with table, returning map has no key '@id'")
744 }
745
746 var res map[string]interface{}
747 if err := tableDB.Select([]string{"id", "name", "age"}).Where("name = ?", "create_from_map_with_table").Find(&res).Error; err != nil || res["age"] != int64(18) {
748 t.Fatalf("failed to create from map, got error %v", err)
749 }
750
751 if _, ok := record["@id"]; ok && fmt.Sprint(res["id"]) != fmt.Sprint(record["@id"]) {
752 t.Fatalf("failed to create data from map with table, @id != id, got %v, expect %v", res["id"], record["@id"])
753 }
754
755 // case 2: create from *map[string]interface{}
756 record1 := map[string]interface{}{"name": "create_from_map_with_table_1", "age": 18}
757 tableDB2 := DB.Table("users")
758 if err := tableDB2.Create(&record1).Error; err != nil {
759 t.Fatalf("failed to create data from map, got error: %v", err)
760 }
761 if _, ok := record1["@id"]; !ok && supportLastInsertID {
762 t.Fatal("failed to create data from map with table, returning map has no key '@id'")
763 }
764
765 var res1 map[string]interface{}
766 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) {
767 t.Fatalf("failed to create from map, got error %v", err)
768 }
769
770 if _, ok := record1["@id"]; ok && fmt.Sprint(res1["id"]) != fmt.Sprint(record1["@id"]) {
771 t.Fatal("failed to create data from map with table, @id != id")
772 }
773
774 // case 3: create from []map[string]interface{}
775 records := []map[string]interface{}{
776 {"name": "create_from_map_with_table_2", "age": 19},
777 {"name": "create_from_map_with_table_3", "age": 20},
778 }
779
780 tableDB = DB.Table("users")
781 if err := tableDB.Create(&records).Error; err != nil {
782 t.Fatalf("failed to create data from slice of map, got error: %v", err)
783 }
784
785 if _, ok := records[0]["@id"]; !ok && supportLastInsertID {
786 t.Fatal("failed to create data from map with table, returning map has no key '@id'")
787 }
788
789 if _, ok := records[1]["@id"]; !ok && supportLastInsertID {

Callers

nothing calls this directly

Calls 7

isMysqlFunction · 0.85
isSqliteFunction · 0.85
TableMethod · 0.65
CreateMethod · 0.65
FindMethod · 0.65
WhereMethod · 0.65
SelectMethod · 0.65

Tested by

no test coverage detected