Skip to content

Commit

Permalink
[fix] db test exclusion time.time's assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
get-me-power committed May 5, 2021
1 parent 273b209 commit a44b3ac
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
2 changes: 1 addition & 1 deletion test/db_test/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}

Expand Down
7 changes: 0 additions & 7 deletions test/db_test/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
34 changes: 28 additions & 6 deletions test/db_test/tag.go
Original file line number Diff line number Diff line change
@@ -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,
},
},
Expand All @@ -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)
}
}
Expand Down
12 changes: 12 additions & 0 deletions test/db_test/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}{
Expand Down

0 comments on commit a44b3ac

Please sign in to comment.