Skip to content

Commit

Permalink
update inte_test/一つのファイルに複数のモデル
Browse files Browse the repository at this point in the history
  • Loading branch information
nakatsu-kouki committed Jun 12, 2024
1 parent cf66499 commit da3cc8d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
12 changes: 12 additions & 0 deletions integration_test/expected_output.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ Stores basic information about orders
| user_id | int | YES | FOREIGN KEY | [users](#users) | | | |
| quantity | int | YES | | | 1 | | Quantity of the product being ordered, defaults to 1 |

## user_details

Stores basic information about users details

| Name | Type | Nullable | Constraints | Referenced | Default | Extra | Comment |
| ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- |
| user_detail_id | varchar(30) | NO | PRIMARY KEY | | | | |
| user_id | int | YES | FOREIGN KEY | [users](#users) | | | |
| name | varchar(255) | NO | | | | | |
| created_at | datetime(3) | YES | | | | | |
| updated_at | datetime(3) | YES | | | | | |

## users

Stores basic information about users
Expand Down
5 changes: 4 additions & 1 deletion integration_test/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ func init() {
log.Fatal(err)
}

// テーブルを自動的に作成または更新する
err = db.Set("gorm:table_options", "COMMENT='Stores basic information about users'").AutoMigrate(&testdata.Users{})
if err != nil {
log.Fatal(err)
}
err = db.Set("gorm:table_options", "COMMENT='Stores basic information about users details'").AutoMigrate(&testdata.UserDetail{})
if err != nil {
log.Fatal(err)
}
err = db.Set("gorm:table_options", "COMMENT='Stores basic information about orders'").AutoMigrate(&another_testdata.Orders{})
if err != nil {
log.Fatal(err)
Expand Down
44 changes: 44 additions & 0 deletions integration_test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,28 @@ func TestIntegration(t *testing.T) {
},
},
},
{
TableName: "user_details",
Comment: "Stores basic information about users details",
Columns: []pipe.Column{
{ColumnName: "created_at", IsNullable: "YES", ColumnType: "datetime(3)"},
{ColumnName: "name", IsNullable: "NO", ColumnType: "varchar(255)"},
{ColumnName: "updated_at", IsNullable: "YES", ColumnType: "datetime(3)"},
{
ColumnName: "user_detail_id",
IsNullable: "NO",
ColumnType: "varchar(30)",
ConstraintTypes: "PRIMARY KEY",
},
{
ColumnName: "user_id",
IsNullable: "YES",
ColumnType: "int",
ReferencedTableName: "users",
ConstraintTypes: "FOREIGN KEY",
},
},
},
{
TableName: "users",
Comment: "Stores basic information about users",
Expand Down Expand Up @@ -158,6 +180,28 @@ func TestIntegration(t *testing.T) {
},
},
},
{
TableName: "user_details",
Comment: "Stores basic information about users details",
Columns: []pipe.Column{
{
ColumnName: "user_detail_id",
IsNullable: "NO",
ColumnType: "varchar(30)",
ConstraintTypes: "PRIMARY KEY",
},
{
ColumnName: "user_id",
IsNullable: "YES",
ColumnType: "int",
ReferencedTableName: "users",
ConstraintTypes: "FOREIGN KEY",
},
{ColumnName: "name", IsNullable: "NO", ColumnType: "varchar(255)"},
{ColumnName: "created_at", IsNullable: "YES", ColumnType: "datetime(3)"},
{ColumnName: "updated_at", IsNullable: "YES", ColumnType: "datetime(3)"},
},
},
{
TableName: "users",
Comment: "Stores basic information about users",
Expand Down
11 changes: 11 additions & 0 deletions integration_test/test_data/users.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package testdata

import "time"

type Users struct {
ID int32 `gorm:"primaryKey;autoIncrement;"`
Name string `gorm:"size:255;not null;"`
Email string `gorm:"size:255;not null;unique;"`
}

type UserDetail struct {
UserDetailID string `gorm:"primaryKey;size:30;not null"`
UserID int32 // 外部キー
User Users `gorm:"foreignKey:UserID"` // Usersとの関連
Name string `gorm:"size:255;not null"`
CreatedAt time.Time
UpdatedAt time.Time
}

0 comments on commit da3cc8d

Please # to comment.