Add domain states and event runtime (T-002)
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAppStatusValid(t *testing.T) {
|
||||
statuses := []AppStatus{
|
||||
StatusNotInstalled,
|
||||
StatusQueued,
|
||||
StatusDownloading,
|
||||
StatusVerifying,
|
||||
StatusExtracting,
|
||||
StatusInstalling,
|
||||
StatusInstalled,
|
||||
StatusUpdateAvailable,
|
||||
StatusRunning,
|
||||
StatusFailed,
|
||||
StatusRollbackPending,
|
||||
StatusIncompatible,
|
||||
}
|
||||
|
||||
for _, status := range statuses {
|
||||
if !status.Valid() {
|
||||
t.Errorf("status %q should be valid", status)
|
||||
}
|
||||
}
|
||||
|
||||
if AppStatus("unknown").Valid() {
|
||||
t.Fatal("unknown status should be invalid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateTransition(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
from AppStatus
|
||||
to AppStatus
|
||||
want error
|
||||
}{
|
||||
{name: "queue install", from: StatusNotInstalled, to: StatusQueued},
|
||||
{name: "start download", from: StatusQueued, to: StatusDownloading},
|
||||
{name: "pause download", from: StatusDownloading, to: StatusQueued},
|
||||
{name: "finish install", from: StatusInstalling, to: StatusInstalled},
|
||||
{name: "start app", from: StatusInstalled, to: StatusRunning},
|
||||
{name: "recover rollback", from: StatusRollbackPending, to: StatusInstalled},
|
||||
{name: "skip install pipeline", from: StatusNotInstalled, to: StatusInstalled, want: ErrInvalidTransition},
|
||||
{name: "download while running", from: StatusRunning, to: StatusDownloading, want: ErrInvalidTransition},
|
||||
{name: "extract incompatible app", from: StatusIncompatible, to: StatusExtracting, want: ErrInvalidTransition},
|
||||
{name: "same status", from: StatusInstalled, to: StatusInstalled, want: ErrInvalidTransition},
|
||||
{name: "unknown source", from: AppStatus("unknown"), to: StatusQueued, want: ErrInvalidStatus},
|
||||
{name: "unknown target", from: StatusQueued, to: AppStatus("unknown"), want: ErrInvalidStatus},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
err := ValidateTransition(test.from, test.to)
|
||||
if test.want == nil {
|
||||
if err != nil {
|
||||
t.Fatalf("ValidateTransition(%q, %q) error = %v", test.from, test.to, err)
|
||||
}
|
||||
if !CanTransition(test.from, test.to) {
|
||||
t.Fatalf("CanTransition(%q, %q) = false, want true", test.from, test.to)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if !errors.Is(err, test.want) {
|
||||
t.Fatalf("ValidateTransition(%q, %q) error = %v, want %v", test.from, test.to, err, test.want)
|
||||
}
|
||||
if CanTransition(test.from, test.to) {
|
||||
t.Fatalf("CanTransition(%q, %q) = true, want false", test.from, test.to)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user