Skip to content

Commit

Permalink
Refactor SpecialistFaker
Browse files Browse the repository at this point in the history
  • Loading branch information
emacsway committed Feb 18, 2024
1 parent d580d8e commit 27ce560
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 40 deletions.
57 changes: 30 additions & 27 deletions grade/internal/domain/specialist/specialist_faker.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ func NewSpecialistFaker(opts ...SpecialistFakerOption) *SpecialistFaker {
}

type SpecialistFaker struct {
Id memberVal.MemberIdFaker
Grade uint8
ReceivedEndorsements []ReceivedEndorsementFakeItem
CreatedAt time.Time
Repository SpecialistRepository
ArtifactFaker *artifact.ArtifactFaker
EndorserFaker *endorser.EndorserFaker
MemberFaker *member.MemberFaker
agg *Specialist
Id memberVal.MemberIdFaker
Grade uint8
Commands []interface{}
CreatedAt time.Time
Repository SpecialistRepository
ArtifactFaker *artifact.ArtifactFaker
EndorserFaker *endorser.EndorserFaker
MemberFaker *member.MemberFaker
agg *Specialist
}

func (f *SpecialistFaker) fake() {
Expand Down Expand Up @@ -145,7 +145,7 @@ func (f *SpecialistFaker) receiveEndorsement(ef *endorser.EndorserFaker) error {
return err
}

f.ReceivedEndorsements = append(f.ReceivedEndorsements, ReceivedEndorsementFakeItem{
f.Commands = append(f.Commands, ReceivedEndorsementFakeCommand{
Endorser: *ef,
Artifact: *af,
CreatedAt: time.Now().Truncate(time.Microsecond),
Expand All @@ -169,22 +169,25 @@ func (f SpecialistFaker) Create() (*Specialist, error) {
if err != nil {
return nil, err
}
for i := range f.ReceivedEndorsements {
e, err := f.ReceivedEndorsements[i].Endorser.Create()
if err != nil {
return nil, err
}
art, err := f.ReceivedEndorsements[i].Artifact.Create()
if err != nil {
return nil, err
}
err = e.ReserveEndorsement()
if err != nil {
return nil, err
}
err = agg.ReceiveEndorsement(*e, *art, f.ReceivedEndorsements[i].CreatedAt)
if err != nil {
return nil, err
for i := range f.Commands {
switch cmd := f.Commands[i].(type) {
case ReceivedEndorsementFakeCommand:
e, err := cmd.Endorser.Create()
if err != nil {
return nil, err
}
art, err := cmd.Artifact.Create()
if err != nil {
return nil, err
}
err = e.ReserveEndorsement()
if err != nil {
return nil, err
}
err = agg.ReceiveEndorsement(*e, *art, cmd.CreatedAt)
if err != nil {
return nil, err
}
}
agg.SetVersion(agg.Version() + 1)
}
Expand Down Expand Up @@ -225,7 +228,7 @@ func (f *SpecialistFaker) BuildDependencies() (err error) {
return err
}

type ReceivedEndorsementFakeItem struct {
type ReceivedEndorsementFakeCommand struct {
Endorser endorser.EndorserFaker
Artifact artifact.ArtifactFaker
CreatedAt time.Time
Expand Down
26 changes: 13 additions & 13 deletions grade/internal/domain/specialist/specialist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,52 +42,52 @@ func TestSpecialistExport(t *testing.T) {
SpecialistGrade: exporters.Uint8Exporter(sf.Grade),
SpecialistVersion: 0,
ArtifactId: artifact.NewArtifactIdExporter(
sf.ReceivedEndorsements[0].Artifact.Id.TenantId,
sf.ReceivedEndorsements[0].Artifact.Id.ArtifactId,
sf.Commands[0].(ReceivedEndorsementFakeCommand).Artifact.Id.TenantId,
sf.Commands[0].(ReceivedEndorsementFakeCommand).Artifact.Id.ArtifactId,
),
EndorserId: member.NewMemberIdExporter(ef.Id.TenantId, ef.Id.MemberId),
EndorserGrade: exporters.Uint8Exporter(ef.Grade),
EndorserVersion: 0,
CreatedAt: sf.ReceivedEndorsements[0].CreatedAt,
CreatedAt: sf.Commands[0].(ReceivedEndorsementFakeCommand).CreatedAt,
},
{
SpecialistId: member.NewMemberIdExporter(sf.Id.TenantId, sf.Id.MemberId),
SpecialistGrade: exporters.Uint8Exporter(sf.Grade),
SpecialistVersion: 1,
ArtifactId: artifact.NewArtifactIdExporter(
sf.ReceivedEndorsements[1].Artifact.Id.TenantId,
sf.ReceivedEndorsements[1].Artifact.Id.ArtifactId,
sf.Commands[1].(ReceivedEndorsementFakeCommand).Artifact.Id.TenantId,
sf.Commands[1].(ReceivedEndorsementFakeCommand).Artifact.Id.ArtifactId,
),
EndorserId: member.NewMemberIdExporter(ef.Id.TenantId, ef.Id.MemberId),
EndorserGrade: exporters.Uint8Exporter(ef.Grade),
EndorserVersion: 0,
CreatedAt: sf.ReceivedEndorsements[1].CreatedAt,
CreatedAt: sf.Commands[1].(ReceivedEndorsementFakeCommand).CreatedAt,
},
{
SpecialistId: member.NewMemberIdExporter(sf.Id.TenantId, sf.Id.MemberId),
SpecialistGrade: exporters.Uint8Exporter(sf.Grade),
SpecialistVersion: 2,
ArtifactId: artifact.NewArtifactIdExporter(
sf.ReceivedEndorsements[2].Artifact.Id.TenantId,
sf.ReceivedEndorsements[2].Artifact.Id.ArtifactId,
sf.Commands[2].(ReceivedEndorsementFakeCommand).Artifact.Id.TenantId,
sf.Commands[2].(ReceivedEndorsementFakeCommand).Artifact.Id.ArtifactId,
),
EndorserId: member.NewMemberIdExporter(ef.Id.TenantId, ef.Id.MemberId),
EndorserGrade: exporters.Uint8Exporter(ef.Grade),
EndorserVersion: 0,
CreatedAt: sf.ReceivedEndorsements[2].CreatedAt,
CreatedAt: sf.Commands[2].(ReceivedEndorsementFakeCommand).CreatedAt,
},
{
SpecialistId: member.NewMemberIdExporter(sf.Id.TenantId, sf.Id.MemberId),
SpecialistGrade: exporters.Uint8Exporter(sf.Grade + 1),
SpecialistVersion: 3,
ArtifactId: artifact.NewArtifactIdExporter(
sf.ReceivedEndorsements[3].Artifact.Id.TenantId,
sf.ReceivedEndorsements[3].Artifact.Id.ArtifactId,
sf.Commands[3].(ReceivedEndorsementFakeCommand).Artifact.Id.TenantId,
sf.Commands[3].(ReceivedEndorsementFakeCommand).Artifact.Id.ArtifactId,
),
EndorserId: member.NewMemberIdExporter(ef.Id.TenantId, ef.Id.MemberId),
EndorserGrade: exporters.Uint8Exporter(ef.Grade),
EndorserVersion: 0,
CreatedAt: sf.ReceivedEndorsements[3].CreatedAt,
CreatedAt: sf.Commands[3].(ReceivedEndorsementFakeCommand).CreatedAt,
},
},
Assignments: []assignment.AssignmentExporter{
Expand All @@ -96,7 +96,7 @@ func TestSpecialistExport(t *testing.T) {
SpecialistVersion: 2,
AssignedGrade: exporters.Uint8Exporter(sf.Grade + 1),
Reason: exporters.StringExporter("Achieved"),
CreatedAt: sf.ReceivedEndorsements[2].CreatedAt,
CreatedAt: sf.Commands[2].(ReceivedEndorsementFakeCommand).CreatedAt,
},
},
Version: 4,
Expand Down

0 comments on commit 27ce560

Please sign in to comment.