gorm/create_test.go

144 lines
4.1 KiB
Go
Raw Normal View History

2014-07-29 06:59:13 +04:00
package gorm_test
import (
2015-03-12 12:47:31 +03:00
"fmt"
2014-07-29 06:59:13 +04:00
"reflect"
"testing"
"time"
)
func TestCreate(t *testing.T) {
2014-07-29 08:32:58 +04:00
float := 35.03554004971999
user := User{Name: "CreateUser", Age: 18, Birthday: time.Now(), UserNum: Num(111), PasswordHash: []byte{'f', 'a', 'k', '4'}, Latitude: float}
2014-07-29 06:59:13 +04:00
2014-08-28 11:33:43 +04:00
if !DB.NewRecord(user) || !DB.NewRecord(&user) {
2014-07-29 06:59:13 +04:00
t.Error("User should be new record before create")
}
2014-08-28 11:33:43 +04:00
if count := DB.Save(&user).RowsAffected; count != 1 {
2014-07-29 06:59:13 +04:00
t.Error("There should be one record be affected when create record")
}
2014-08-28 11:33:43 +04:00
if DB.NewRecord(user) || DB.NewRecord(&user) {
2014-07-29 06:59:13 +04:00
t.Error("User should not new record after save")
}
var newUser User
2014-08-28 11:33:43 +04:00
DB.First(&newUser, user.Id)
2014-07-29 06:59:13 +04:00
if !reflect.DeepEqual(newUser.PasswordHash, []byte{'f', 'a', 'k', '4'}) {
t.Errorf("User's PasswordHash should be saved ([]byte)")
}
if newUser.Age != 18 {
t.Errorf("User's Age should be saved (int)")
}
if newUser.UserNum != Num(111) {
t.Errorf("User's UserNum should be saved (custom type)")
}
2014-07-29 08:32:58 +04:00
if newUser.Latitude != float {
t.Errorf("Float64 should not be changed after save")
}
if user.CreatedAt.IsZero() {
t.Errorf("Should have created_at after create")
}
if newUser.CreatedAt.IsZero() {
t.Errorf("Should have created_at after create")
}
2014-08-28 11:33:43 +04:00
DB.Model(user).Update("name", "create_user_new_name")
DB.First(&user, user.Id)
2014-07-29 08:32:58 +04:00
if user.CreatedAt != newUser.CreatedAt {
t.Errorf("CreatedAt should not be changed after update")
}
}
func TestCreateWithNoGORMPrimayKey(t *testing.T) {
jt := JoinTable{From: 1, To: 2}
err := DB.Create(&jt).Error
if err != nil {
t.Errorf("No error should happen when create a record without a GORM primary key. But in the database this primary key exists and is the union of 2 or more fields\n But got: %s", err)
}
}
func TestCreateWithNoStdPrimaryKeyAndDefaultValues(t *testing.T) {
2014-07-29 14:21:36 +04:00
animal := Animal{Name: "Ferdinand"}
2014-08-28 11:33:43 +04:00
if DB.Save(&animal).Error != nil {
t.Errorf("No error should happen when create a record without std primary key")
2014-07-29 14:21:36 +04:00
}
if animal.Counter == 0 {
t.Errorf("No std primary key should be filled value after create")
}
if animal.Name != "Ferdinand" {
t.Errorf("Default value should be overrided")
}
// Test create with default value not overrided
an := Animal{From: "nerdz"}
if DB.Save(&an).Error != nil {
t.Errorf("No error should happen when create an record without std primary key")
}
// We must fetch the value again, to have the default fields updated
// (We can't do this in the update statements, since sql default can be expressions
// And be different from the fields' type (eg. a time.Time fiels has a default value of "now()"
DB.Model(Animal{}).Where(&Animal{Counter: an.Counter}).First(&an)
if an.Name != "galeone" {
t.Errorf("Default value should fill the field. But got %v", an.Name)
}
2014-07-29 14:21:36 +04:00
}
2014-07-29 08:32:58 +04:00
func TestAnonymousScanner(t *testing.T) {
user := User{Name: "anonymous_scanner", Role: Role{Name: "admin"}}
2014-08-28 11:33:43 +04:00
DB.Save(&user)
2014-07-29 08:32:58 +04:00
var user2 User
2014-08-28 11:33:43 +04:00
DB.First(&user2, "name = ?", "anonymous_scanner")
2014-07-29 08:32:58 +04:00
if user2.Role.Name != "admin" {
t.Errorf("Should be able to get anonymous scanner")
}
if !user2.IsAdmin() {
t.Errorf("Should be able to get anonymous scanner")
}
}
func TestAnonymousField(t *testing.T) {
user := User{Name: "anonymous_field", Company: Company{Name: "company"}}
2014-08-28 11:33:43 +04:00
DB.Save(&user)
2014-07-29 08:32:58 +04:00
var user2 User
2014-08-28 11:33:43 +04:00
DB.First(&user2, "name = ?", "anonymous_field")
DB.Model(&user2).Related(&user2.Company)
2014-07-29 08:32:58 +04:00
if user2.Company.Name != "company" {
t.Errorf("Should be able to get anonymous field")
}
2014-07-29 06:59:13 +04:00
}
2015-03-12 12:47:31 +03:00
func TestSelectCreate(t *testing.T) {
user := getPreparedUser("user1", "select_create")
DB.Select("Name", "BillingAddress", "CreditCard", "Company", "Emails").Create(&user)
var user2 User
DB.Preload("BillingAddress").Preload("ShippingAddress").
Preload("CreditCard").Preload("Emails").Preload("Company").First(&user2, user.Id)
if user2.Name != user.Name || user2.Age == user.Age {
t.Errorf("Should only create users with name column")
}
fmt.Println(user2.CreditCard.ID)
if user2.BillingAddressID.Int64 == 0 || user2.ShippingAddressId != 0 ||
user2.CreditCard.ID == 0 || len(user2.Emails) == 0 {
t.Errorf("Should only create users with name column")
}
}