Skip to content

DateTime cannot be parsed #814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
strategy:
matrix:
dbversion: ['mysql:latest'] # 'mysql:5.7', 'mysql:5.6'
go: ['1.24']
go: ['1.23','1.24']
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}

Expand Down Expand Up @@ -70,20 +70,20 @@ jobs:
uses: actions/checkout@v2

- name: go mod pakcage cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('go.mod') }}

- name: Tests
run: GORM_ENABLE_CACHE=true GORM_DIALECT=mysql GORM_DSN="gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True" ./test.sh
run: GORM_ENABLE_CACHE=true GORM_DIALECT=mysql GORM_DSN="gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8mb4" DEBUG=true ./test.sh

postgres:
needs: sqlite
strategy:
matrix:
dbversion: ['postgres:latest'] # 'postgres:11', 'postgres:10'
go: ['1.24']
go: ['1.23','1.24']
platform: [ubuntu-latest] # can not run in macOS and widnowsOS
runs-on: ${{ matrix.platform }}

Expand Down Expand Up @@ -114,7 +114,7 @@ jobs:
uses: actions/checkout@v2

- name: go mod pakcage cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('go.mod') }}
Expand Down Expand Up @@ -158,7 +158,7 @@ jobs:
uses: actions/checkout@v2

- name: go mod pakcage cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('go.mod') }}
Expand Down
10 changes: 6 additions & 4 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"time"
"fmt"

"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
Expand Down Expand Up @@ -47,9 +48,10 @@ func OpenTestConnection() (db *gorm.DB, err error) {
case "mysql":
log.Println("testing mysql...")
if dbDSN == "" {
dbDSN = "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True&loc=Local"
dbDSN = "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8mb4"
}
db, err = gorm.Open(mysql.Open(dbDSN), &gorm.Config{})
log.Printf("db dmp: %#v\n", db)
case "postgres":
log.Println("testing postgres...")
if dbDSN == "" {
Expand Down Expand Up @@ -87,10 +89,10 @@ func RunMigrations() {
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(allModels), func(i, j int) { allModels[i], allModels[j] = allModels[j], allModels[i] })

DB.Migrator().DropTable("user_friends", "user_speaks")
fmt.Println("Model Migrations: Migrations starting ...")

if err = DB.Migrator().DropTable(allModels...); err != nil {
log.Printf("Failed to drop table, got error %v\n", err)
if err = DB.AutoMigrate(&Company{}, &Language{}, &User{}); err != nil {
log.Printf("Failed to auto migrate, but got error %v\n", err)
os.Exit(1)
}

Expand Down
17 changes: 15 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package main

import (
import (
"testing"
"fmt"
)

// GORM_REPO: https://github.com/go-gorm/gorm.git
Expand All @@ -13,8 +14,20 @@ func TestGORM(t *testing.T) {

DB.Create(&user)

if user.ID != 0 {
fmt.Printf("User '%s': User (%d) was created\n", user.Name, user.ID)
} else {
t.Errorf("User '%s': User creation failed\n", user.Name)
}

var result User
if err := DB.First(&result, user.ID).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
t.Errorf("Failed, got error: %v\n", err)
}

if result.ID != 0 {
fmt.Printf("User (%d): User (%d) was fetched\n", user.ID, result.ID)
} else {
t.Errorf("User (%d): User could not be fetched\n", user.ID)
}
}
89 changes: 89 additions & 0 deletions models_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package main

import (
"testing"
"database/sql"
"fmt"
)

// GORM_REPO: https://github.com/go-gorm/gorm.git
// GORM_BRANCH: master
// TEST_DRIVERS: sqlite, mysql, postgres, sqlserver

func TestUserCreate(t *testing.T) {
user := User{Name: "Test User No. 2"}

DB.Create(&user)

if user.ID != 0 {
fmt.Printf("User '%s': User (%d) was created\n", user.Name, user.ID)
} else {
t.Errorf("User '%s': User creation failed\n", user.Name)
}

var result User
if err := DB.First(&result, user.ID).Error; err != nil {
t.Errorf("Failed, got error: %v\n", err)
}

if result.ID != 0 {
fmt.Printf("User (%d): User (%d) was fetched\n", user.ID, result.ID)
} else {
t.Errorf("User (%d): User could not be fetched\n", user.ID)
}
}

func TestUserAccountWithCompanyCreate(t *testing.T) {
company := Company{Name: "Test Company No. 3"}

DB.Create(&company)

if company.ID != 0 {
fmt.Printf("Company '%s': Company (%d) was created\n", company.Name, company.ID)
} else {
t.Errorf("Company '%s': Company creation failed\n", company.Name)
}

user := User{Name: "Test User No. 3", CompanyID: &company.ID, Company: company}

DB.Create(&user)

if user.ID != 0 {
fmt.Printf("User '%s': User (%d) was created\n", user.Name, user.ID)
} else {
t.Errorf("User '%s': User creation failed\n", user.Name)
}

account := Account{Number: "UserAccount-3", UserID: sql.NullInt64{Int64: int64(user.ID), Valid: true}}

DB.Create(&account)

if account.ID != 0 {
fmt.Printf("Account '%s': Account (%d) was created\n", account.Number, account.ID)
} else {
t.Errorf("Account '%s': Account creation failed\n", account.Number)
}

var fetchedUser User
var fetchedAccount Account

if err := DB.First(&fetchedUser, user.ID).Error; err != nil {
t.Errorf("Failed, got error: %v\n", err)
}

if fetchedUser.ID != 0 {
fmt.Printf("User (%d): User (%d) was fetched\n", fetchedUser.ID, fetchedUser.ID)
} else {
t.Errorf("User (%d): User could not be fetched\n", user.ID)
}

if err := DB.Where("user_id", user.ID).First(&fetchedAccount).Error; err != nil {
t.Errorf("Failed, got error: %v\n", err)
}

if fetchedAccount.ID != 0 {
fmt.Printf("Account for User (%d): Account Number '%s' was fetched\n", user.ID, fetchedAccount.Number)
} else {
t.Errorf("Account for User (%d): Account could not be fetched\n", user.ID)
}
}
Loading