github.com/wendylau87/warungpintar2021/mastersvc@v0.0.0-20210508064910-5fb678f1d33e/domain/item/item__test.go (about) 1 package item_test 2 3 import ( 4 "fmt" 5 "github.com/wendylau87/warungpintar2021/mastersvc/infrastructure" 6 "os" 7 "path" 8 "runtime" 9 "testing" 10 "github.com/wendylau87/warungpintar2021/mastersvc/domain/item" 11 "github.com/wendylau87/warungpintar2021/mastersvc/entities" 12 "github.com/wendylau87/warungpintar2021/mastersvc/infrastructure/logger" 13 "github.com/wendylau87/warungpintar2021/mastersvc/infrastructure/sqlhandler" 14 "github.com/google/uuid" 15 . "github.com/smartystreets/goconvey/convey" 16 ) 17 18 var( 19 20 dom item.DomainItf 21 ) 22 23 func TestMain(t *testing.M){ 24 _, filename, _, _ := runtime.Caller(0) 25 dir := path.Join(path.Dir(filename), "..", "..") 26 err := os.Chdir(dir) 27 if err != nil { 28 panic(err) 29 } 30 31 log := logger.NewLogger() 32 infrastructure.Load(*log) 33 sql, err := sqlhandler.NewSQLHandler(*log) 34 if err != nil{ 35 log.LogError("Failed initiated database") 36 }else{ 37 dom = item.InitItemDomain(*log, sql) 38 39 exitVal := t.Run() 40 os.Exit(exitVal) 41 } 42 43 } 44 45 func TestDomain_CreateItem(t *testing.T) { 46 Convey("create Item", t, func() { 47 testCases := []struct { 48 testID int 49 testType string 50 testDesc string 51 payload entities.Item 52 }{ 53 { 54 testID: 1, 55 testDesc: "success create", 56 testType: "P", 57 payload: entities.Item{ 58 Name: "unit-test-"+uuid.New().String(), 59 }, 60 }, 61 } 62 for _, tc := range testCases { 63 Convey(fmt.Sprintf("%d - [%s] : %s", tc.testID, tc.testType, tc.testDesc), func() { 64 _, err := dom.CreateItem(tc.payload) 65 if tc.testType == "P" { 66 So(err, ShouldBeNil) 67 68 } else { 69 So(err, ShouldNotBeNil) 70 } 71 }) 72 } 73 }) 74 }