From 2d657a321aabc0db456f5ebfe12f48111e9deccb Mon Sep 17 00:00:00 2001 From: meriy100 Date: Sat, 16 Mar 2024 14:22:04 +0900 Subject: [PATCH 1/3] =?UTF-8?q?test(entities):=20TestPostToHistories=20?= =?UTF-8?q?=E3=81=AE=E6=96=87=E9=87=8F=E3=82=92=E5=89=8A=E6=B8=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .golangci.yml | 3 +-- entities/post_test.go | 59 ++++++++----------------------------------- go.mod | 1 + 3 files changed, 13 insertions(+), 50 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index c7bf94a..dfddd97 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -24,5 +24,4 @@ run: linters-settings: gocognit: - # TODO : Set the threshold to 10 - min-complexity: 54 + min-complexity: 10 diff --git a/entities/post_test.go b/entities/post_test.go index 1074d20..d29ee7d 100644 --- a/entities/post_test.go +++ b/entities/post_test.go @@ -1,23 +1,22 @@ package entities import ( - "reflect" "testing" + + "github.com/google/go-cmp/cmp" ) func TestPost_ToHistories(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { post Post - got []History + got []*History gotErr bool }{ - { - "success", + "success": { Post{ BodyMd: "# 株式会社 Photosynth.inc\r\n## スマートロック関連のプロダクト開発業務\r\n### startMonth\r\n2020/03\r\n### endMonth\r\n\r\n### description\r\n- サーバーサイド設計, 開発, レビュー\r\n- フロントエンド設計, 開発, レビュー\r\n\r\n### technologyUsed \r\n- Ruby on Rails\r\n- Vue.js\r\n- HIDDEN\r\n\r\n# 合同会社フレイズ (業務委託)\r\n\r\n## スマートフォンブラウザでの映像撮影, 画像認識処理\r\n### startMonth\r\n 2019/11\r\n### endMonth\r\n 2020/02\r\n### description\r\n- 技術調査 (Web カメラ, 顔認識, 文字認識等)\r\n- フロントエンド設計, 開発\r\n### technologyUsed \r\n- TypeScript\r\n- Google Cloud Platform\r\n- Vue.js\r\n- OpenCV\r\n- face api (tensorflow.js)\r\n\r\n## サンプルプロダクト\r\n### startMonth\r\n 2019/11\r\n### endMonth\r\n 2020/02\r\n### description\r\n- 技術調査 (Web カメラ, 顔認識, 文字認識等)\r\n- フロントエンド設計, 開発\r\n### technologyUsed \r\n- TypeScript\r\n- Google Cloud Platform\r\n- Vue.js\r\n- OpenCV\r\n- face api (tensorflow.js)\r\n", }, - []History{ + []*History{ { "株式会社 Photosynth.inc", []Product{ @@ -79,51 +78,15 @@ func TestPost_ToHistories(t *testing.T) { }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { + for name, tt := range tests { + t.Run(name, func(t *testing.T) { histories, err := tt.post.ToHistories() if (err != nil) != tt.gotErr { - t.Errorf("ToHistory() : err = %v want gotErr %v\n", err, tt.gotErr) - } - if len(histories) != len(tt.got) { - t.Errorf("length different : histories = %v want %v\n", histories, tt.got) + t.Fatalf("ToHistory() : err = %v want gotErr %v\n", err, tt.gotErr) } - for i, got := range tt.got { - h := histories[i] - if histories[i].Organization != got.Organization { - t.Errorf("histories.[%d].Organization = %v want %v\n", i, h.Organization, got.Organization) - - } - - if len(h.Products) != len(got.Products) { - t.Errorf("histories.[%d].Products length different : Products = %v want %v\n", i, h.Products, got.Products) - } - - for j, gotP := range got.Products { - p := h.Products[j] - if p.Title != gotP.Title { - t.Errorf("histories.[%d].Products[%d].Title = %v want %v\n", i, j, p.Title, gotP.Title) - } - if p.StartMonth != gotP.StartMonth { - t.Errorf("histories.[%d].Products[%d].StartMonth= %v want %v\n", i, j, p.StartMonth, gotP.StartMonth) - } - if (p.EndMonth == nil) != (gotP.EndMonth == nil) { - t.Errorf("histories.[%d].Products[%d].EndMonth= %v want %v\n", i, j, p.EndMonth, gotP.EndMonth) - } else if p.EndMonth != nil && gotP.EndMonth != nil { - if *p.EndMonth != *gotP.EndMonth { - t.Errorf("histories.[%d].Products[%d].EndMonth= %v want %v\n", i, j, p.EndMonth, gotP.EndMonth) - } - } - - if !reflect.DeepEqual(p.Description, gotP.Description) { - t.Errorf("histories.[%d].Products[%d].Description= %s want %s\n", i, j, p.Description, gotP.Description) - } - - if !reflect.DeepEqual(p.Technologies, gotP.Technologies) { - t.Errorf("histories.[%d].Products[%d].Technologies= %v want %v\n", i, j, p.Technologies, gotP.Technologies) - } - } + if diff := cmp.Diff(tt.got, histories); diff != "" { + t.Errorf("ToHistory() mismatch (-want +got):\n%s", diff) } }) } diff --git a/go.mod b/go.mod index 1b0971d..d051658 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( cloud.google.com/go/firestore v1.14.0 firebase.google.com/go v3.13.0+incompatible github.com/GoogleCloudPlatform/functions-framework-go v1.8.1 + github.com/google/go-cmp v0.6.0 google.golang.org/api v0.170.0 ) From 05e9263cf520db97404bf766da02e503eb945634 Mon Sep 17 00:00:00 2001 From: meriy100 Date: Sat, 16 Mar 2024 14:27:16 +0900 Subject: [PATCH 2/3] =?UTF-8?q?refact(entities):=20toEndMonth=20=E3=81=AB?= =?UTF-8?q?=E5=88=87=E3=82=8A=E5=87=BA=E3=81=97=E3=81=A6=E8=A4=87=E9=9B=91?= =?UTF-8?q?=E5=BA=A6=E3=82=92=E4=B8=8B=E3=81=92=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entities/post.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/entities/post.go b/entities/post.go index 194b9cb..3d1f489 100644 --- a/entities/post.go +++ b/entities/post.go @@ -117,6 +117,17 @@ func toHistory(historyPart string) (History, error) { return history, nil } +func toEndMonth(body string) (*Month, error) { + if len(body) != 0 { + em, err := strToMonth(body) + if err != nil { + return nil, err + } + return &em, nil + } + return nil, nil +} + func toProduct(productPart string) (Product, error) { product := Product{} prdName, prdBody := separateHeadTail(productPart) @@ -133,13 +144,11 @@ func toProduct(productPart string) (Product, error) { } product.StartMonth = sm case "endMonth": - if len(body) != 0 { - em, err := strToMonth(body) - if err != nil { - return Product{}, err - } - product.EndMonth = &em + endMonth, err := toEndMonth(body) + if err != nil { + return Product{}, err } + product.EndMonth = endMonth case "description": product.Description = mdListToSlice(body) case "technologyUsed": From 3da8fdaea0756a2e72a43dcd8183abf33e8ce554 Mon Sep 17 00:00:00 2001 From: meriy100 Date: Sat, 16 Mar 2024 14:32:11 +0900 Subject: [PATCH 3/3] =?UTF-8?q?refact(entities):=20=E7=8F=BE=E5=9C=A8?= =?UTF-8?q?=E6=99=82=E5=88=BB=E3=81=AE=E5=8F=96=E5=BE=97=E3=82=92=20flexti?= =?UTF-8?q?me=20=E3=81=A7=E3=83=A2=E3=83=83=E3=82=AF=E3=81=A7=E3=81=8D?= =?UTF-8?q?=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entities/profile.go | 8 ++++++-- entities/skill.go | 4 +++- go.mod | 1 + go.sum | 2 ++ usecase/skill_interactor_test.go | 35 +++++++++----------------------- 5 files changed, 22 insertions(+), 28 deletions(-) diff --git a/entities/profile.go b/entities/profile.go index d05907a..576d9e2 100644 --- a/entities/profile.go +++ b/entities/profile.go @@ -1,6 +1,10 @@ package entities -import "time" +import ( + "time" + + "github.com/Songmu/flextime" +) type Profile struct { Job string `json:"job"` @@ -12,5 +16,5 @@ type Profile struct { } func NewProfile(job, description string, skillDescription, licenses []string, pr string) *Profile { - return &Profile{job, description, skillDescription, licenses, pr, time.Now()} + return &Profile{job, description, skillDescription, licenses, pr, flextime.Now()} } diff --git a/entities/skill.go b/entities/skill.go index aa00bdf..f9ac1f6 100644 --- a/entities/skill.go +++ b/entities/skill.go @@ -4,6 +4,8 @@ import ( "fmt" "strings" "time" + + "github.com/Songmu/flextime" ) type SkillCategory int @@ -32,7 +34,7 @@ func NewSkill(name string, lv int, description string, category SkillCategory) * lv, description, category, - time.Now(), + flextime.Now(), } } diff --git a/go.mod b/go.mod index d051658..cf2f665 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( cloud.google.com/go/firestore v1.14.0 firebase.google.com/go v3.13.0+incompatible github.com/GoogleCloudPlatform/functions-framework-go v1.8.1 + github.com/Songmu/flextime v0.1.0 github.com/google/go-cmp v0.6.0 google.golang.org/api v0.170.0 ) diff --git a/go.sum b/go.sum index 25eb624..e69ed0e 100644 --- a/go.sum +++ b/go.sum @@ -778,6 +778,8 @@ github.com/GoogleCloudPlatform/functions-framework-go v1.8.1 h1:wMO6lE8uR68ReG+/ github.com/GoogleCloudPlatform/functions-framework-go v1.8.1/go.mod h1:kKqAKLm08tjDVs37IG/Dl4hC1/go4E85Udn1LeSdAEI= github.com/JohnCGriffin/overflow v0.0.0-20211019200055-46fa312c352c/go.mod h1:X0CRv0ky0k6m906ixxpzmDRLvX58TFUKS2eePweuyxk= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= +github.com/Songmu/flextime v0.1.0 h1:sss5IALl84LbvU/cS5D1cKNd5ffT94N2BZwC+esgAJI= +github.com/Songmu/flextime v0.1.0/go.mod h1:ofUSZ/qj7f1BfQQ6rEH4ovewJ0SZmLOjBF1xa8iE87Q= github.com/ajstarks/deck v0.0.0-20200831202436-30c9fc6549a9/go.mod h1:JynElWSGnm/4RlzPXRlREEwqTHAN3T56Bv2ITsFT3gY= github.com/ajstarks/deck/generate v0.0.0-20210309230005-c3f852c02e19/go.mod h1:T13YZdzov6OU0A1+RfKZiZN9ca6VeKdBdyDV+BY97Tk= github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw= diff --git a/usecase/skill_interactor_test.go b/usecase/skill_interactor_test.go index 88884eb..4329116 100644 --- a/usecase/skill_interactor_test.go +++ b/usecase/skill_interactor_test.go @@ -4,7 +4,10 @@ import ( "fmt" "reflect" "testing" + "time" + "github.com/Songmu/flextime" + "github.com/google/go-cmp/cmp" "github.com/meriy100/portfolio-api/entities" "github.com/meriy100/portfolio-api/usecase/ports" ) @@ -96,16 +99,8 @@ func TestSkillInteractor_IndexSkills(t *testing.T) { } } -func findSkill(name string, skills []*entities.Skill) *entities.Skill { - for _, s := range skills { - if s.Name == name { - return s - } - } - - return nil -} func TestSkillInteractor_UpdateSkills(t *testing.T) { + now := time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC) type fields struct { outputPort ports.SkillOutputPort postRepository ports.PostRepository @@ -132,6 +127,7 @@ func TestSkillInteractor_UpdateSkills(t *testing.T) { Lv: 2, Description: "testD", Category: entities.Os, + Timestamp: now, }, }, false, @@ -139,6 +135,9 @@ func TestSkillInteractor_UpdateSkills(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + flextime.Fix(now) + defer flextime.Restore() + s := &SkillInteractor{ outputPort: tt.fields.outputPort, postRepository: tt.fields.postRepository, @@ -151,22 +150,8 @@ func TestSkillInteractor_UpdateSkills(t *testing.T) { if len(tt.want) != len(tt.fields.skillRepository.insert) { t.Errorf("UdateSkills() saved value = %v, want %v", tt.fields.skillRepository.insert, tt.want) } - - for _, wantSkill := range tt.want { - expect := findSkill(wantSkill.Name, tt.fields.skillRepository.insert) - if expect == nil { - t.Fatalf("UpdateSkills() saved value = %v, want %v", tt.fields.skillRepository.insert, tt.want) - } - - if expect.Lv != wantSkill.Lv { - t.Errorf("UpdateSkills() %v's Lv = %v, want %v", expect.Name, expect.Lv, wantSkill.Lv) - } - if expect.Category != wantSkill.Category { - t.Errorf("UpdateSkills() %v's Category = %v, want %v", expect.Name, expect.Category, wantSkill.Category) - } - if expect.Description != wantSkill.Description { - t.Errorf("UpdateSkills() %v's Description = %v, want %v", expect.Name, expect.Description, wantSkill.Description) - } + if diff := cmp.Diff(tt.want, tt.fields.skillRepository.insert); diff != "" { + t.Errorf("UpdateSkills() mismatch (-want +got):\n%s", diff) } }) }