-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_test.go
More file actions
45 lines (40 loc) · 1.26 KB
/
service_test.go
File metadata and controls
45 lines (40 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package multielo
import (
"testing"
"time"
)
type stubCalculator struct{ calls int }
func (s *stubCalculator) Calculate(results []*MatchResult, cfg LeagueConfig) ([]MatchDiff, error) {
s.calls++
diffs := make([]MatchDiff, len(results))
for i, r := range results {
diffs[i] = MatchDiff{Player: r.Player, Diff: 0}
}
return diffs, nil
}
func TestNewLeagueWithDependenciesUsesInjectedCalculator(t *testing.T) {
calc := &stubCalculator{}
l := NewLeagueWithDependencies(DefaultConfig(), LeagueDependencies{Calculator: calc})
_ = l.AddPlayer("alpha")
_ = l.AddPlayer("beta")
alpha, _ := l.GetPlayer("alpha")
beta, _ := l.GetPlayer("beta")
_ = l.AddMatch([]*MatchResult{{Position: 1, Player: alpha}, {Position: 2, Player: beta}}, time.Now())
if calc.calls != 1 {
t.Fatalf("expected 1 calculator call, got %d", calc.calls)
}
}
func TestLeagueServiceProvidesCommandAndQuerySeparation(t *testing.T) {
svc := NewLeagueService(nil)
_ = svc.AddPlayer("solo")
_ = svc.AddPlayer("duo")
p1, _ := svc.Player("solo")
p2, _ := svc.Player("duo")
_ = svc.RecordMatch([]*MatchResult{{Position: 1, Player: p1}, {Position: 2, Player: p2}}, time.Now())
if len(svc.Players()) != 2 {
t.Fatal("expected 2 players")
}
if svc.League() == nil {
t.Fatal("expected league exposed")
}
}