Skip to content

Commit

Permalink
refact(entities): 現在時刻の取得を flextime でモックできるように
Browse files Browse the repository at this point in the history
  • Loading branch information
meriy100 committed Mar 16, 2024
1 parent 05e9263 commit 3da8fda
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 28 deletions.
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
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 3da8fda

Please sign in to comment.