-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjob_bind_test.go
More file actions
79 lines (66 loc) · 1.92 KB
/
job_bind_test.go
File metadata and controls
79 lines (66 loc) · 1.92 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package queue
import (
"errors"
"testing"
)
type goodMarshaler struct{}
type badMarshaler struct{}
func (goodMarshaler) MarshalJSON() ([]byte, error) { return []byte(`{"ok":true}`), nil }
func (badMarshaler) MarshalJSON() ([]byte, error) { return nil, errors.New("boom") }
func TestJobBind_Success(t *testing.T) {
type Meta struct {
Active bool `json:"active"`
}
type Payload struct {
ID int `json:"id"`
Name string `json:"name"`
Meta Meta `json:"meta"`
}
job := NewJob("job:bind").Payload(Payload{
ID: 7,
Name: "alice",
Meta: Meta{Active: true},
})
var out Payload
if err := job.Bind(&out); err != nil {
t.Fatalf("bind failed: %v", err)
}
if out.ID != 7 || out.Name != "alice" || !out.Meta.Active {
t.Fatalf("unexpected bind output: %+v", out)
}
}
func TestJobBind_RequiresPointerDestination(t *testing.T) {
job := NewJob("job:bind").Payload(map[string]any{"id": 1})
if err := job.Bind(nil); err == nil {
t.Fatal("expected nil destination error")
}
var out map[string]any
if err := job.Bind(out); err == nil {
t.Fatal("expected non-pointer destination error")
}
var ptr *map[string]any
if err := job.Bind(ptr); err == nil {
t.Fatal("expected nil pointer destination error")
}
}
func TestJobBind_InvalidJSON(t *testing.T) {
job := NewJob("job:bind").Payload("not-json")
var out map[string]any
if err := job.Bind(&out); err == nil {
t.Fatal("expected bind error for invalid json payload")
}
}
func TestJobPayloadJSON_UsesMarshalerAndCapturesError(t *testing.T) {
job := NewJob("job:json").PayloadJSON(goodMarshaler{})
var out map[string]bool
if err := job.Bind(&out); err != nil {
t.Fatalf("bind marshaled payload failed: %v", err)
}
if !out["ok"] {
t.Fatalf("expected marshaled payload to include ok=true, got %#v", out)
}
errJob := NewJob("job:json").PayloadJSON(badMarshaler{})
if err := errJob.validate(); err == nil {
t.Fatal("expected payload marshaling error")
}
}