diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a1a33bf9..b00fa877 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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 }} @@ -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 }} @@ -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') }} @@ -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') }} diff --git a/db.go b/db.go index ccab03ed..a3cf09ca 100644 --- a/db.go +++ b/db.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "time" + "fmt" "gorm.io/driver/mysql" "gorm.io/driver/postgres" @@ -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 == "" { @@ -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) } diff --git a/main_test.go b/main_test.go index 60a388f7..8ccc7543 100644 --- a/main_test.go +++ b/main_test.go @@ -1,7 +1,8 @@ package main -import ( +import ( "testing" + "fmt" ) // GORM_REPO: https://github.com/go-gorm/gorm.git @@ -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) } } diff --git a/models_test.go b/models_test.go new file mode 100644 index 00000000..7109c489 --- /dev/null +++ b/models_test.go @@ -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) + } +}