Skip to content

Commit

Permalink
Merge pull request #8 from meriy100/refact-gocognit
Browse files Browse the repository at this point in the history
refact: gocognit の min-complexity を 10 にできるように リファクタ
  • Loading branch information
meriy100 authored Mar 16, 2024
2 parents 2deb3db + 3da8fda commit eaebbe1
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 84 deletions.
3 changes: 1 addition & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ run:

linters-settings:
gocognit:
# TODO : Set the threshold to 10
min-complexity: 54
min-complexity: 10
21 changes: 15 additions & 6 deletions entities/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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":
Expand Down
59 changes: 11 additions & 48 deletions entities/post_test.go
Original file line number Diff line number Diff line change
@@ -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{
Expand Down Expand Up @@ -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)
}
})
}
Expand Down
8 changes: 6 additions & 2 deletions entities/profile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package entities

import "time"
import (
"time"

"github.com/Songmu/flextime"
)

type Profile struct {
Job string `json:"job"`
Expand All @@ -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()}
}
4 changes: 3 additions & 1 deletion entities/skill.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"strings"
"time"

"github.com/Songmu/flextime"
)

type SkillCategory int
Expand Down Expand Up @@ -32,7 +34,7 @@ func NewSkill(name string, lv int, description string, category SkillCategory) *
lv,
description,
category,
time.Now(),
flextime.Now(),
}
}

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ 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
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
35 changes: 10 additions & 25 deletions usecase/skill_interactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand All @@ -132,13 +127,17 @@ func TestSkillInteractor_UpdateSkills(t *testing.T) {
Lv: 2,
Description: "testD",
Category: entities.Os,
Timestamp: now,
},
},
false,
},
}
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,
Expand All @@ -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)
}
})
}
Expand Down

0 comments on commit eaebbe1

Please sign in to comment.