From cc889eab4daac56e5bee018fe12294d0fa34830b Mon Sep 17 00:00:00 2001 From: kazukazuinaina Date: Wed, 5 May 2021 15:02:26 +0900 Subject: [PATCH 01/11] [add] user_table's struct --- db/model/user.go | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 db/model/user.go diff --git a/db/model/user.go b/db/model/user.go new file mode 100644 index 0000000..8e3ca08 --- /dev/null +++ b/db/model/user.go @@ -0,0 +1,7 @@ +package model + +type User struct { + Id int + Nickname string + Role int +} From 9651252a598a93386531d20fea0c63a9501fc2fd Mon Sep 17 00:00:00 2001 From: kazukazuinaina Date: Wed, 5 May 2021 15:46:08 +0900 Subject: [PATCH 02/11] [add] db's struct --- db/model/item.go | 17 +++++++++++++++++ db/model/item_tagging.go | 6 ++++++ db/model/ldap_user.go | 14 ++++++++++++++ db/model/lending.go | 15 +++++++++++++++ db/model/passsword_reset_user.go | 11 +++++++++++ db/model/password_user.go | 14 ++++++++++++++ db/model/reservation.go | 14 ++++++++++++++ db/model/tag.go | 14 ++++++++++++++ 8 files changed, 105 insertions(+) create mode 100644 db/model/item.go create mode 100644 db/model/item_tagging.go create mode 100644 db/model/ldap_user.go create mode 100644 db/model/lending.go create mode 100644 db/model/passsword_reset_user.go create mode 100644 db/model/password_user.go create mode 100644 db/model/reservation.go create mode 100644 db/model/tag.go diff --git a/db/model/item.go b/db/model/item.go new file mode 100644 index 0000000..e6c68d1 --- /dev/null +++ b/db/model/item.go @@ -0,0 +1,17 @@ +package model + +import ( + "time" +) + +type Item struct { + Id int + Name string + Overview string + Icon byte + Type int + Created_at *time.Time + Created_by int + Updated_at *time.Time + Updated_by int +} diff --git a/db/model/item_tagging.go b/db/model/item_tagging.go new file mode 100644 index 0000000..5b9cc2c --- /dev/null +++ b/db/model/item_tagging.go @@ -0,0 +1,6 @@ +package model + +type Item_tagging struct { + Item_id int + Tag_id int +} diff --git a/db/model/ldap_user.go b/db/model/ldap_user.go new file mode 100644 index 0000000..23ffc39 --- /dev/null +++ b/db/model/ldap_user.go @@ -0,0 +1,14 @@ +package model + +import ( + "time" +) + +type Ldap_user struct { + User_id int + Ldap_id int + Created_at *time.Time + Created_by int + Updated_at *time.Time + Updated_by int +} diff --git a/db/model/lending.go b/db/model/lending.go new file mode 100644 index 0000000..fdf23d5 --- /dev/null +++ b/db/model/lending.go @@ -0,0 +1,15 @@ +package model + +import ( + "time" +) + +type Lending struct { + User_id int + Item_id int + Returned_at *time.Time + Created_at *time.Time + Created_by int + Updated_at *time.Time + Updated_by int +} diff --git a/db/model/passsword_reset_user.go b/db/model/passsword_reset_user.go new file mode 100644 index 0000000..62a316a --- /dev/null +++ b/db/model/passsword_reset_user.go @@ -0,0 +1,11 @@ +package model + +import ( + "time" +) + +type Password_reset_user struct { + User_id int + Reset_hash string + Expired_at *time.Time +} diff --git a/db/model/password_user.go b/db/model/password_user.go new file mode 100644 index 0000000..5613b40 --- /dev/null +++ b/db/model/password_user.go @@ -0,0 +1,14 @@ +package model + +import ( + "time" +) + +type Password_user struct { + User_id int + Mail_address string + Password string + Is_look bool + Created_at *time.Time + Updated_by int +} diff --git a/db/model/reservation.go b/db/model/reservation.go new file mode 100644 index 0000000..299eb39 --- /dev/null +++ b/db/model/reservation.go @@ -0,0 +1,14 @@ +package model + +import ( + "time" +) + +type Reservation struct { + User_id int + Item_id int + Created_at *time.Time + Created_by int + Updated_at *time.Time + Updated_by int +} diff --git a/db/model/tag.go b/db/model/tag.go new file mode 100644 index 0000000..a3b141c --- /dev/null +++ b/db/model/tag.go @@ -0,0 +1,14 @@ +package model + +import ( + "time" +) + +type Tag struct { + Id int + Name string + Created_at *time.Time + Created_by int + Updated_at *time.Time + Updated_by int +} From a780ed70312a60618317c7bd6c6458814bea5867 Mon Sep 17 00:00:00 2001 From: kazukazuinaina Date: Wed, 5 May 2021 15:55:47 +0900 Subject: [PATCH 03/11] [rename] struct's column --- db/model/item.go | 10 +++++----- db/model/item_tagging.go | 6 +++--- db/model/ldap_user.go | 14 +++++++------- db/model/lending.go | 14 +++++++------- db/model/passsword_reset_user.go | 8 ++++---- db/model/password_user.go | 12 ++++++------ db/model/reservation.go | 12 ++++++------ db/model/tag.go | 8 ++++---- 8 files changed, 42 insertions(+), 42 deletions(-) diff --git a/db/model/item.go b/db/model/item.go index e6c68d1..d3273cd 100644 --- a/db/model/item.go +++ b/db/model/item.go @@ -5,13 +5,13 @@ import ( ) type Item struct { - Id int + ID int Name string Overview string Icon byte Type int - Created_at *time.Time - Created_by int - Updated_at *time.Time - Updated_by int + CreatedAt time.Time + CreatedBy int + UpdatedAt time.Time + UpdatedBy int } diff --git a/db/model/item_tagging.go b/db/model/item_tagging.go index 5b9cc2c..6b78077 100644 --- a/db/model/item_tagging.go +++ b/db/model/item_tagging.go @@ -1,6 +1,6 @@ package model -type Item_tagging struct { - Item_id int - Tag_id int +type ItemTagging struct { + ItemID int + TagID int } diff --git a/db/model/ldap_user.go b/db/model/ldap_user.go index 23ffc39..0c29371 100644 --- a/db/model/ldap_user.go +++ b/db/model/ldap_user.go @@ -4,11 +4,11 @@ import ( "time" ) -type Ldap_user struct { - User_id int - Ldap_id int - Created_at *time.Time - Created_by int - Updated_at *time.Time - Updated_by int +type LdapUser struct { + UserID int + LdapID int + CreatedAt time.Time + CreatedBy int + UpdatedAt time.Time + UpdatedBy int } diff --git a/db/model/lending.go b/db/model/lending.go index fdf23d5..c901506 100644 --- a/db/model/lending.go +++ b/db/model/lending.go @@ -5,11 +5,11 @@ import ( ) type Lending struct { - User_id int - Item_id int - Returned_at *time.Time - Created_at *time.Time - Created_by int - Updated_at *time.Time - Updated_by int + UserID int + ItemID int + ReturnedAt time.Time + CreatedAt time.Time + CreatedBy int + UpdatedAt time.Time + UpdatedBy int } diff --git a/db/model/passsword_reset_user.go b/db/model/passsword_reset_user.go index 62a316a..f6f8083 100644 --- a/db/model/passsword_reset_user.go +++ b/db/model/passsword_reset_user.go @@ -4,8 +4,8 @@ import ( "time" ) -type Password_reset_user struct { - User_id int - Reset_hash string - Expired_at *time.Time +type PasswordResetUser struct { + UserID int + ResetHash string + ExpiredAt time.Time } diff --git a/db/model/password_user.go b/db/model/password_user.go index 5613b40..7dc5590 100644 --- a/db/model/password_user.go +++ b/db/model/password_user.go @@ -4,11 +4,11 @@ import ( "time" ) -type Password_user struct { - User_id int - Mail_address string +type PasswordUser struct { + UserID int + MailAddress string Password string - Is_look bool - Created_at *time.Time - Updated_by int + IsLook bool + CreatedAt time.Time + UpdatedBy int } diff --git a/db/model/reservation.go b/db/model/reservation.go index 299eb39..dd24bc0 100644 --- a/db/model/reservation.go +++ b/db/model/reservation.go @@ -5,10 +5,10 @@ import ( ) type Reservation struct { - User_id int - Item_id int - Created_at *time.Time - Created_by int - Updated_at *time.Time - Updated_by int + UserID int + ItemID int + CreatedAt time.Time + CreatedBy int + UpdatedAt time.Time + UpdatedBy int } diff --git a/db/model/tag.go b/db/model/tag.go index a3b141c..f8868dd 100644 --- a/db/model/tag.go +++ b/db/model/tag.go @@ -7,8 +7,8 @@ import ( type Tag struct { Id int Name string - Created_at *time.Time - Created_by int - Updated_at *time.Time - Updated_by int + CreatedAt time.Time + CreatedBy int + UpdatedAt time.Time + UpdatedBy int } From 8710c52ac9b06da08f320cf344eba87fc765bbda Mon Sep 17 00:00:00 2001 From: kazukazuinaina Date: Wed, 5 May 2021 16:01:41 +0900 Subject: [PATCH 04/11] [add] gorm and gorm/driver/mysql --- go.mod | 6 +++++- go.sum | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 775aa71..b8d76d5 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,8 @@ module ritscc/kiri-tansu go 1.16 -require github.com/golang-migrate/migrate/v4 v4.14.1 // indirect +require ( + github.com/golang-migrate/migrate/v4 v4.14.1 // indirect + gorm.io/driver/mysql v1.0.6 // indirect + gorm.io/gorm v1.21.9 // indirect +) diff --git a/go.sum b/go.sum index 6439826..fd073bb 100644 --- a/go.sum +++ b/go.sum @@ -81,6 +81,8 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2 github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= +github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM= github.com/gocql/gocql v0.0.0-20190301043612-f6df8288f9b4/go.mod h1:4Fw1eo5iaEhDUs8XyuhSVCVy52Jq3L+/3GJgYkwc+/0= @@ -180,6 +182,10 @@ github.com/jackc/pgx/v4 v4.0.0-20190421002000-1b8f0016e912/go.mod h1:no/Y67Jkk/9 github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQnOEnf1dqHGpw7JmHqHc1NxDoalibchSk9/RWuDc= github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.2 h1:eVKgfIdy9b6zbWBMgFpfDPoAMifwSZagU9HmEU6zgiI= +github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= @@ -545,6 +551,10 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gorm.io/driver/mysql v1.0.6 h1:mA0XRPjIKi4bkE9nv+NKs6qj6QWOchqUSdWOcpd3x1E= +gorm.io/driver/mysql v1.0.6/go.mod h1:KdrTanmfLPPyAOeYGyG+UpDys7/7eeWT1zCq+oekYnU= +gorm.io/gorm v1.21.9 h1:INieZtn4P2Pw6xPJ8MzT0G4WUOsHq3RhfuDF1M6GW0E= +gorm.io/gorm v1.21.9/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0= gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= From 97ece190689092d10b13e0be3e623e681c2d4a2e Mon Sep 17 00:00:00 2001 From: kazukazuinaina Date: Wed, 5 May 2021 16:17:26 +0900 Subject: [PATCH 05/11] [add] primary key --- db/model/item.go | 2 +- db/model/ldap_user.go | 2 +- db/model/lending.go | 4 ++-- db/model/passsword_reset_user.go | 2 +- db/model/password_user.go | 2 +- db/model/reservation.go | 4 ++-- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/db/model/item.go b/db/model/item.go index d3273cd..3cfc3a5 100644 --- a/db/model/item.go +++ b/db/model/item.go @@ -5,7 +5,7 @@ import ( ) type Item struct { - ID int + ID int `gorm:"primaryKey"` Name string Overview string Icon byte diff --git a/db/model/ldap_user.go b/db/model/ldap_user.go index 0c29371..f3b4ec7 100644 --- a/db/model/ldap_user.go +++ b/db/model/ldap_user.go @@ -5,7 +5,7 @@ import ( ) type LdapUser struct { - UserID int + UserID int `gorm:"primaryKey"` LdapID int CreatedAt time.Time CreatedBy int diff --git a/db/model/lending.go b/db/model/lending.go index c901506..48782e0 100644 --- a/db/model/lending.go +++ b/db/model/lending.go @@ -5,8 +5,8 @@ import ( ) type Lending struct { - UserID int - ItemID int + UserID int `gorm:"primaryKey"` + ItemID int `gorm:"primaryKey"` ReturnedAt time.Time CreatedAt time.Time CreatedBy int diff --git a/db/model/passsword_reset_user.go b/db/model/passsword_reset_user.go index f6f8083..a8fb7f8 100644 --- a/db/model/passsword_reset_user.go +++ b/db/model/passsword_reset_user.go @@ -5,7 +5,7 @@ import ( ) type PasswordResetUser struct { - UserID int + UserID int `gorm:"primaryKey"` ResetHash string ExpiredAt time.Time } diff --git a/db/model/password_user.go b/db/model/password_user.go index 7dc5590..ce05ec7 100644 --- a/db/model/password_user.go +++ b/db/model/password_user.go @@ -5,7 +5,7 @@ import ( ) type PasswordUser struct { - UserID int + UserID int `gorm:"primaryKey"` MailAddress string Password string IsLook bool diff --git a/db/model/reservation.go b/db/model/reservation.go index dd24bc0..e5411fd 100644 --- a/db/model/reservation.go +++ b/db/model/reservation.go @@ -5,8 +5,8 @@ import ( ) type Reservation struct { - UserID int - ItemID int + UserID int `gorm:"primaryKey"` + ItemID int `gorm:"primaryKey"` CreatedAt time.Time CreatedBy int UpdatedAt time.Time From 096b1885349a00da445598271da18e427cd1afce Mon Sep 17 00:00:00 2001 From: kazukazuinaina Date: Wed, 5 May 2021 20:51:54 +0900 Subject: [PATCH 06/11] [add] dbtest --- db/model/user.go | 2 +- docker-compose.yml | 12 +++++ main_test.go | 114 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 main_test.go diff --git a/db/model/user.go b/db/model/user.go index 8e3ca08..4baa783 100644 --- a/db/model/user.go +++ b/db/model/user.go @@ -1,7 +1,7 @@ package model type User struct { - Id int + ID int `gorm:"primaryKey;autoIncrement:true"` Nickname string Role int } diff --git a/docker-compose.yml b/docker-compose.yml index a20043c..eb5168e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,3 +12,15 @@ services: volumes: - ./docker/mysql/data:/var/lib/mysql - ./docker/mysql/my.conf:/etc/mysql/conf.d/my.conf + + mysql_test: + image: "mysql:8" + ports: + - "3307:3306" + environment: + MYSQL_ROOT_PASSWORD: "kiritan" + MYSQL_DATABASE: "kiritan" + MYSQL_USER: "kiritan" + MYSQL_PASSWORD: "kiritan" + volumes: + - ./docker/mysql/my.conf:/etc/mysql/conf.d/my.conf diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..d3da4fe --- /dev/null +++ b/main_test.go @@ -0,0 +1,114 @@ +package main + +import ( + "database/sql" + "fmt" + "log" + "reflect" + "testing" + + model "./db/model" + "github.com/golang-migrate/migrate/v4" + "github.com/golang-migrate/migrate/v4/database/mysql" + _ "github.com/golang-migrate/migrate/v4/source/file" + gomysql "gorm.io/driver/mysql" + "gorm.io/gorm" + "gorm.io/gorm/schema" +) + +const ( + DBNAME = "kiritan" + USERNAME = "kiritan" + PASSWORD = "kiritan" + IPADDRESS = "127.0.0.1" + PORTNUMBER = "3307" +) + +func Test_User_DB(t *testing.T) { + + MigrateBD() + + db, err := DatabaseConnection() + if err != nil { + log.Fatal(err) + return + } + + result := db.Create(&model.User{ + ID: 1, + Nickname: "Hoge", + Role: 1, + }).Error + + if result != nil { + t.Error(result) + return + } + + testCase := []struct { + expected model.User + }{ + { + expected: model.User{ + ID: 1, + Nickname: "Hoge", + Role: 1, + }, + }, + } + + for i, testCase := range testCase { + var actual model.User + db.First(&actual) + expected := testCase.expected + if !reflect.DeepEqual(actual, expected) { + t.Errorf("errorNumber:%d actual: %v, expect: %v", i, actual, expected) + db.Delete(&actual) + return + } + db.Delete(&actual) + } + +} + +func DatabaseConnection() (*gorm.DB, error) { + dsn := USERNAME + ":" + PASSWORD + "@tcp(" + IPADDRESS + ":" + PORTNUMBER + ")/" + DBNAME + "?charset=utf8mb4&parseTime=True&loc=Local" + db, err := gorm.Open(gomysql.Open(dsn), &gorm.Config{ + NamingStrategy: schema.NamingStrategy{ + SingularTable: true, + }, + }) + return db, err +} + +func MigrateBD() { + db, err := sql.Open("mysql", "kiritan:kiritan@tcp(localhost:3307)/kiritan") + + if err != nil { + fmt.Println("DBの接続に失敗しました") + fmt.Printf("%s\n", err) + return + } + + driver, err := mysql.WithInstance(db, &mysql.Config{}) + + if err != nil { + fmt.Println("Hoge") + fmt.Printf("%s\n", err) + return + } + + m, err := migrate.NewWithDatabaseInstance("file://db/migration", "mysql", driver) + if err != nil { + fmt.Printf("%s\n", err) + return + } + + err = m.Up() + if err != nil && err != migrate.ErrNoChange { + fmt.Printf("%s\n", err) + return + } + + db.Close() +} From d9f9bdbcccb60c2c60b6ebbcbcab0cd4adc2a8b4 Mon Sep 17 00:00:00 2001 From: kazukazuinaina Date: Wed, 5 May 2021 21:22:31 +0900 Subject: [PATCH 07/11] [add] reset db function --- main_test.go | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/main_test.go b/main_test.go index d3da4fe..99837e3 100644 --- a/main_test.go +++ b/main_test.go @@ -6,8 +6,7 @@ import ( "log" "reflect" "testing" - - model "./db/model" + model "ritscc/kiri-tansu/db/model" "github.com/golang-migrate/migrate/v4" "github.com/golang-migrate/migrate/v4/database/mysql" _ "github.com/golang-migrate/migrate/v4/source/file" @@ -45,7 +44,7 @@ func Test_User_DB(t *testing.T) { return } - testCase := []struct { + testCases := []struct { expected model.User }{ { @@ -57,16 +56,16 @@ func Test_User_DB(t *testing.T) { }, } - for i, testCase := range testCase { + for i, testCase := range testCases { var actual model.User db.First(&actual) expected := testCase.expected if !reflect.DeepEqual(actual, expected) { t.Errorf("errorNumber:%d actual: %v, expect: %v", i, actual, expected) - db.Delete(&actual) + ResetBD() return } - db.Delete(&actual) + ResetBD() } } @@ -112,3 +111,35 @@ func MigrateBD() { db.Close() } + +func ResetBD() { + db, err := sql.Open("mysql", "kiritan:kiritan@tcp(localhost:3307)/kiritan") + + if err != nil { + fmt.Println("DBの接続に失敗しました") + fmt.Printf("%s\n", err) + return + } + + driver, err := mysql.WithInstance(db, &mysql.Config{}) + + if err != nil { + fmt.Println("Hoge") + fmt.Printf("%s\n", err) + return + } + + m, err := migrate.NewWithDatabaseInstance("file://db/migration", "mysql", driver) + if err != nil { + fmt.Printf("%s\n", err) + return + } + + err = m.Down() + if err != nil && err != migrate.ErrNoChange { + fmt.Printf("%s\n", err) + return + } + + db.Close() +} From 273b2090e02636bc11e6752e36f2f484435430da Mon Sep 17 00:00:00 2001 From: kazukazuinaina Date: Wed, 5 May 2021 22:41:49 +0900 Subject: [PATCH 08/11] [update] database test's configuration --- db/model/tag.go | 2 +- main_test.go | 145 ---------------------------------------- test/db_test/db_test.go | 43 ++++++++++++ test/db_test/setup.go | 80 ++++++++++++++++++++++ test/db_test/tag.go | 34 ++++++++++ test/db_test/user.go | 32 +++++++++ 6 files changed, 190 insertions(+), 146 deletions(-) delete mode 100644 main_test.go create mode 100644 test/db_test/db_test.go create mode 100644 test/db_test/setup.go create mode 100644 test/db_test/tag.go create mode 100644 test/db_test/user.go diff --git a/db/model/tag.go b/db/model/tag.go index f8868dd..174cf58 100644 --- a/db/model/tag.go +++ b/db/model/tag.go @@ -5,7 +5,7 @@ import ( ) type Tag struct { - Id int + ID int Name string CreatedAt time.Time CreatedBy int diff --git a/main_test.go b/main_test.go deleted file mode 100644 index 99837e3..0000000 --- a/main_test.go +++ /dev/null @@ -1,145 +0,0 @@ -package main - -import ( - "database/sql" - "fmt" - "log" - "reflect" - "testing" - model "ritscc/kiri-tansu/db/model" - "github.com/golang-migrate/migrate/v4" - "github.com/golang-migrate/migrate/v4/database/mysql" - _ "github.com/golang-migrate/migrate/v4/source/file" - gomysql "gorm.io/driver/mysql" - "gorm.io/gorm" - "gorm.io/gorm/schema" -) - -const ( - DBNAME = "kiritan" - USERNAME = "kiritan" - PASSWORD = "kiritan" - IPADDRESS = "127.0.0.1" - PORTNUMBER = "3307" -) - -func Test_User_DB(t *testing.T) { - - MigrateBD() - - db, err := DatabaseConnection() - if err != nil { - log.Fatal(err) - return - } - - result := db.Create(&model.User{ - ID: 1, - Nickname: "Hoge", - Role: 1, - }).Error - - if result != nil { - t.Error(result) - return - } - - testCases := []struct { - expected model.User - }{ - { - expected: model.User{ - ID: 1, - Nickname: "Hoge", - Role: 1, - }, - }, - } - - for i, testCase := range testCases { - var actual model.User - db.First(&actual) - expected := testCase.expected - if !reflect.DeepEqual(actual, expected) { - t.Errorf("errorNumber:%d actual: %v, expect: %v", i, actual, expected) - ResetBD() - return - } - ResetBD() - } - -} - -func DatabaseConnection() (*gorm.DB, error) { - dsn := USERNAME + ":" + PASSWORD + "@tcp(" + IPADDRESS + ":" + PORTNUMBER + ")/" + DBNAME + "?charset=utf8mb4&parseTime=True&loc=Local" - db, err := gorm.Open(gomysql.Open(dsn), &gorm.Config{ - NamingStrategy: schema.NamingStrategy{ - SingularTable: true, - }, - }) - return db, err -} - -func MigrateBD() { - db, err := sql.Open("mysql", "kiritan:kiritan@tcp(localhost:3307)/kiritan") - - if err != nil { - fmt.Println("DBの接続に失敗しました") - fmt.Printf("%s\n", err) - return - } - - driver, err := mysql.WithInstance(db, &mysql.Config{}) - - if err != nil { - fmt.Println("Hoge") - fmt.Printf("%s\n", err) - return - } - - m, err := migrate.NewWithDatabaseInstance("file://db/migration", "mysql", driver) - if err != nil { - fmt.Printf("%s\n", err) - return - } - - err = m.Up() - if err != nil && err != migrate.ErrNoChange { - fmt.Printf("%s\n", err) - return - } - - db.Close() -} - -func ResetBD() { - db, err := sql.Open("mysql", "kiritan:kiritan@tcp(localhost:3307)/kiritan") - - if err != nil { - fmt.Println("DBの接続に失敗しました") - fmt.Printf("%s\n", err) - return - } - - driver, err := mysql.WithInstance(db, &mysql.Config{}) - - if err != nil { - fmt.Println("Hoge") - fmt.Printf("%s\n", err) - return - } - - m, err := migrate.NewWithDatabaseInstance("file://db/migration", "mysql", driver) - if err != nil { - fmt.Printf("%s\n", err) - return - } - - err = m.Down() - if err != nil && err != migrate.ErrNoChange { - fmt.Printf("%s\n", err) - return - } - - db.Close() -} diff --git a/test/db_test/db_test.go b/test/db_test/db_test.go new file mode 100644 index 0000000..b18e590 --- /dev/null +++ b/test/db_test/db_test.go @@ -0,0 +1,43 @@ +package db + +import ( + "log" + "testing" + _ "github.com/golang-migrate/migrate/v4/source/file" + gomysql "gorm.io/driver/mysql" + "gorm.io/gorm" + "gorm.io/gorm/schema" +) + +const ( + DBNAME = "kiritan" + USERNAME = "kiritan" + PASSWORD = "kiritan" + IPADDRESS = "127.0.0.1" + PORTNUMBER = "3307" +) + +func Test_DB(t *testing.T) { + + MigrateBD() + defer ResetBD() + + db, err := DatabaseConnection() + if err != nil { + log.Fatal(err) + return + } + test_db_user(db, t) +// test_db_tag(db, t) + +} + +func DatabaseConnection() (*gorm.DB, error) { + dsn := USERNAME + ":" + PASSWORD + "@tcp(" + IPADDRESS + ":" + PORTNUMBER + ")/" + DBNAME + "?charset=utf8mb4&parseTime=True&loc=Local" + db, err := gorm.Open(gomysql.Open(dsn), &gorm.Config{ + NamingStrategy: schema.NamingStrategy{ + SingularTable: true, + }, + }) + return db, err +} diff --git a/test/db_test/setup.go b/test/db_test/setup.go new file mode 100644 index 0000000..dafca12 --- /dev/null +++ b/test/db_test/setup.go @@ -0,0 +1,80 @@ +package db + +import ( + "database/sql" + "fmt" + "github.com/golang-migrate/migrate/v4" + "github.com/golang-migrate/migrate/v4/database/mysql" + "time" + _ "github.com/golang-migrate/migrate/v4/source/file" +) + +func MigrateBD() { + db, err := sql.Open("mysql", "kiritan:kiritan@tcp(localhost:3307)/kiritan") + + if err != nil { + fmt.Println("DBの接続に失敗しました") + fmt.Printf("%s\n", err) + return + } + + driver, err := mysql.WithInstance(db, &mysql.Config{}) + + if err != nil { + fmt.Println("Hoge") + fmt.Printf("%s\n", err) + return + } + + m, err := migrate.NewWithDatabaseInstance("file://../../db/migration", "mysql", driver) + if err != nil { + fmt.Printf("%s\n", err) + return + } + + err = m.Up() + if err != nil && err != migrate.ErrNoChange { + fmt.Printf("%s\n", err) + return + } + + db.Close() +} + +func ResetBD() { + db, err := sql.Open("mysql", "kiritan:kiritan@tcp(localhost:3307)/kiritan") + + if err != nil { + fmt.Println("DBの接続に失敗しました") + fmt.Printf("%s\n", err) + return + } + + driver, err := mysql.WithInstance(db, &mysql.Config{}) + + if err != nil { + fmt.Println("Hoge") + fmt.Printf("%s\n", err) + return + } + + m, err := migrate.NewWithDatabaseInstance("file://../../db/migration", "mysql", driver) + if err != nil { + fmt.Printf("%s\n", err) + return + } + + err = m.Down() + if err != nil && err != migrate.ErrNoChange { + fmt.Printf("%s\n", err) + return + } + + db.Close() +} + +func stringToTime(str string) time.Time { + var layout = "2006-01-02 15:04:05" + t, _ := time.Parse(layout, str) + return t +} diff --git a/test/db_test/tag.go b/test/db_test/tag.go new file mode 100644 index 0000000..ec7c4d5 --- /dev/null +++ b/test/db_test/tag.go @@ -0,0 +1,34 @@ +package db + +import ( + "reflect" + model "ritscc/kiri-tansu/db/model" + "testing" + "gorm.io/gorm" +) + +func test_db_tag(db *gorm.DB, t *testing.T) { + testCases := []struct { + expected model.Tag + }{ + { + expected: model.Tag{ + ID: 1, + Name: "book", + CreatedAt: stringToTime("Jan 2, 2006 at 3:04pm (JST)"), + CreatedBy: 1, + UpdatedAt: stringToTime("Feb 3, 2013 at 7:54pm (JST)"), + UpdatedBy: 1, + }, + }, + } + + for i, testCase := range testCases { + var actual model.Tag + db.First(&actual) + expected := testCase.expected + if !reflect.DeepEqual(actual, expected) { + t.Errorf("errorNumber:%d actual: %v, expect: %v", i, actual, expected) + } + } +} diff --git a/test/db_test/user.go b/test/db_test/user.go new file mode 100644 index 0000000..0693b35 --- /dev/null +++ b/test/db_test/user.go @@ -0,0 +1,32 @@ + +package db + +import ( + "reflect" + model "ritscc/kiri-tansu/db/model" + "testing" + "gorm.io/gorm" +) + +func test_db_user(db *gorm.DB, t *testing.T) { + testCases := []struct { + expected model.User + }{ + { + expected: model.User{ + ID: 1, + Nickname: "Hoge", + Role: 1, + }, + }, + } + + for i, testCase := range testCases { + var actual model.User + db.First(&actual) + expected := testCase.expected + if !reflect.DeepEqual(actual, expected) { + t.Errorf("errorNumber:%d actual: %v, expect: %v", i, actual, expected) + } + } +} From a44b3acd8e3e664c77288219686fb60848cfe170 Mon Sep 17 00:00:00 2001 From: kazukazuinaina Date: Wed, 5 May 2021 23:26:04 +0900 Subject: [PATCH 09/11] [fix] db test exclusion time.time's assertion --- test/db_test/db_test.go | 2 +- test/db_test/setup.go | 7 ------- test/db_test/tag.go | 34 ++++++++++++++++++++++++++++------ test/db_test/user.go | 12 ++++++++++++ 4 files changed, 41 insertions(+), 14 deletions(-) diff --git a/test/db_test/db_test.go b/test/db_test/db_test.go index b18e590..41a8774 100644 --- a/test/db_test/db_test.go +++ b/test/db_test/db_test.go @@ -28,7 +28,7 @@ func Test_DB(t *testing.T) { return } test_db_user(db, t) -// test_db_tag(db, t) + test_db_tag(db, t) } diff --git a/test/db_test/setup.go b/test/db_test/setup.go index dafca12..b8aa583 100644 --- a/test/db_test/setup.go +++ b/test/db_test/setup.go @@ -5,7 +5,6 @@ import ( "fmt" "github.com/golang-migrate/migrate/v4" "github.com/golang-migrate/migrate/v4/database/mysql" - "time" _ "github.com/golang-migrate/migrate/v4/source/file" ) @@ -72,9 +71,3 @@ func ResetBD() { db.Close() } - -func stringToTime(str string) time.Time { - var layout = "2006-01-02 15:04:05" - t, _ := time.Parse(layout, str) - return t -} diff --git a/test/db_test/tag.go b/test/db_test/tag.go index ec7c4d5..e5dbd65 100644 --- a/test/db_test/tag.go +++ b/test/db_test/tag.go @@ -1,23 +1,33 @@ package db import ( + "gorm.io/gorm" "reflect" model "ritscc/kiri-tansu/db/model" "testing" - "gorm.io/gorm" ) func test_db_tag(db *gorm.DB, t *testing.T) { + result := db.Create(&model.Tag{ + ID: 1, + Name: "book", + CreatedBy: 1, + UpdatedBy: 1, + }).Error + + if result != nil { + t.Error(result) + return + } + testCases := []struct { expected model.Tag }{ { expected: model.Tag{ - ID: 1, - Name: "book", - CreatedAt: stringToTime("Jan 2, 2006 at 3:04pm (JST)"), + ID: 1, + Name: "book", CreatedBy: 1, - UpdatedAt: stringToTime("Feb 3, 2013 at 7:54pm (JST)"), UpdatedBy: 1, }, }, @@ -27,7 +37,19 @@ func test_db_tag(db *gorm.DB, t *testing.T) { var actual model.Tag db.First(&actual) expected := testCase.expected - if !reflect.DeepEqual(actual, expected) { + if !reflect.DeepEqual(actual.ID, expected.ID) { + t.Errorf("errorNumber:%d actual: %v, expect: %v", i, actual, expected) + } + + if !reflect.DeepEqual(actual.Name, expected.Name) { + t.Errorf("errorNumber:%d actual: %v, expect: %v", i, actual, expected) + } + + if !reflect.DeepEqual(actual.CreatedBy, expected.CreatedBy) { + t.Errorf("errorNumber:%d actual: %v, expect: %v", i, actual, expected) + } + + if !reflect.DeepEqual(actual.UpdatedBy, expected.UpdatedBy) { t.Errorf("errorNumber:%d actual: %v, expect: %v", i, actual, expected) } } diff --git a/test/db_test/user.go b/test/db_test/user.go index 0693b35..c2a1ff5 100644 --- a/test/db_test/user.go +++ b/test/db_test/user.go @@ -9,6 +9,18 @@ import ( ) func test_db_user(db *gorm.DB, t *testing.T) { + + result := db.Create(&model.User{ + ID: 1, + Nickname: "Hoge", + Role: 1, + }).Error + + if result != nil { + t.Error(result) + return + } + testCases := []struct { expected model.User }{ From 6a4336603afd219fcc6b81e7a07070bad39b9dc9 Mon Sep 17 00:00:00 2001 From: kazukazuinaina Date: Wed, 5 May 2021 23:39:40 +0900 Subject: [PATCH 10/11] [add] item's test --- db/model/item.go | 2 +- db/model/tag.go | 2 +- test/db_test/db_test.go | 1 + test/db_test/item.go | 68 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 test/db_test/item.go diff --git a/db/model/item.go b/db/model/item.go index 3cfc3a5..6cc8136 100644 --- a/db/model/item.go +++ b/db/model/item.go @@ -5,7 +5,7 @@ import ( ) type Item struct { - ID int `gorm:"primaryKey"` + ID int `gorm:"primaryKey;autoIncrement:true"` Name string Overview string Icon byte diff --git a/db/model/tag.go b/db/model/tag.go index 174cf58..ccb46d1 100644 --- a/db/model/tag.go +++ b/db/model/tag.go @@ -5,7 +5,7 @@ import ( ) type Tag struct { - ID int + ID int `gorm:"primaryKey;autoIncrement:true"` Name string CreatedAt time.Time CreatedBy int diff --git a/test/db_test/db_test.go b/test/db_test/db_test.go index 41a8774..35f8180 100644 --- a/test/db_test/db_test.go +++ b/test/db_test/db_test.go @@ -29,6 +29,7 @@ func Test_DB(t *testing.T) { } test_db_user(db, t) test_db_tag(db, t) + test_db_item(db, t) } diff --git a/test/db_test/item.go b/test/db_test/item.go new file mode 100644 index 0000000..74fe285 --- /dev/null +++ b/test/db_test/item.go @@ -0,0 +1,68 @@ +package db + +import ( + "gorm.io/gorm" + "reflect" + model "ritscc/kiri-tansu/db/model" + "testing" +) + +func test_db_item(db *gorm.DB, t *testing.T) { + result := db.Create(&model.Item{ + ID: 1, + Name: "Vim", + Overview: "Vimの本です", + Type: 1, + CreatedBy: 1, + UpdatedBy: 1, + }).Error + + if result != nil { + t.Error(result) + return + } + + testCases := []struct { + expected model.Item + }{ + { + expected: model.Item{ + ID: 1, + Name: "Vim", + Overview: "Vimの本です", + Type: 1, + CreatedBy: 1, + UpdatedBy: 1, + }, + }, + } + + for i, testCase := range testCases { + var actual model.Item + db.First(&actual) + expected := testCase.expected + if !reflect.DeepEqual(actual.ID, expected.ID) { + t.Errorf("errorNumber:%d actual: %v, expect: %v", i, actual, expected) + } + + if !reflect.DeepEqual(actual.Name, expected.Name) { + t.Errorf("errorNumber:%d actual: %v, expect: %v", i, actual, expected) + } + + if !reflect.DeepEqual(actual.Overview, expected.Overview) { + t.Errorf("errorNumber:%d actual: %v, expect: %v", i, actual, expected) + } + + if !reflect.DeepEqual(actual.Type, expected.Type) { + t.Errorf("errorNumber:%d actual: %v, expect: %v", i, actual, expected) + } + + if !reflect.DeepEqual(actual.CreatedBy, expected.CreatedBy) { + t.Errorf("errorNumber:%d actual: %v, expect: %v", i, actual, expected) + } + + if !reflect.DeepEqual(actual.UpdatedBy, expected.UpdatedBy) { + t.Errorf("errorNumber:%d actual: %v, expect: %v", i, actual, expected) + } + } +} From e7d03d51b994b8765c7540778026e1dc3a362ce3 Mon Sep 17 00:00:00 2001 From: kazukazuinaina Date: Mon, 14 Jun 2021 20:57:58 +0900 Subject: [PATCH 11/11] =?UTF-8?q?[patch]=20Timezone=E3=81=ABTokyo=E3=82=92?= =?UTF-8?q?=E6=8C=87=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker-compose.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/docker-compose.yml b/docker-compose.yml index eb5168e..4ee2c11 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,6 +9,7 @@ services: MYSQL_DATABASE: "kiritan" MYSQL_USER: "kiritan" MYSQL_PASSWORD: "kiritan" + TZ: "Asia/Tokyo" volumes: - ./docker/mysql/data:/var/lib/mysql - ./docker/mysql/my.conf:/etc/mysql/conf.d/my.conf