aboutsummaryrefslogtreecommitdiffstats
path: root/job_test.go
blob: fe6ea48f56b29f698642ca6f142d76ea65f1f666 (plain) (blame)
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
80
81
82
83
84
85
86
87
88
89
package lulu

import (
	_ "embed"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
)

//go:embed testdata/jobsresp.json
var jobsRespJson string

func TestUnmarshaljobsResp(t *testing.T) {
	want := jobsResp{
		Count:   1,
		Next:    "https://api.lulu.com/resources/?page=1&page_size=1",
		Prev:    "https://api.lulu.com/resources/?page=1&page_size=1",
		Results: []PrintJob{printJobSample},
	}
	requireUnmarshalJsonEq(t, want, jobsRespJson)
}

func TestJob(t *testing.T) {
	contact := MustParseEmailAddress("test@test.com")
	jobEid := "demo-time"
	productionDelay := 120 * time.Minute
	addr := shipAddrSample
	shipOpt := Mail
	items := []Printable{printableSample}

	c := newClient(t)
	job1, err := c.Print(contact, jobEid, productionDelay, addr, shipOpt, items)
	require.NoError(t, err)
	require.NotZero(t, job1.Id)

	job2, err := c.Job(job1.Id)
	require.NoError(t, err)

	// Ignore timestamp because job gets marked as 'modified' between
	// creation and retrieval time even if it hasn't actually
	// changed.
	job2.Modified = job1.Modified

	require.Equal(t, job1, job2)
}

func TestJobs(t *testing.T) {
	// Create some jobs
	c := newClient(t)
	jobParams := []struct {
		contact string
		extid   string
		delay   time.Duration
		addr    ShippingAddress
		opt     ShippingLevel
		items   []Printable
	}{
		{"test@example.com", "testjobs1", 2 * time.Hour, shipAddrSample, Mail, []Printable{printableSample}},
		{"timmy@timmers.com", "testjobs2", 3 * time.Hour, shipAddrSample, Mail, []Printable{printableSample}},
	}
	jobsIn := make([]PrintJob, len(jobParams))
	for i, j := range jobParams {
		var err error
		contact := MustParseEmailAddress(j.contact)
		jobsIn[i], err = c.Print(contact, j.extid, j.delay, j.addr, j.opt, j.items)
		require.NoError(t, err)
	}

	// Retrieve them
	jobsOut, err := c.Jobs()
	require.NoError(t, err)

	// Are they all present?
	require.Truef(t, len(jobsOut) >= len(jobsIn), "expected >=%d jobs, got %d", len(jobsIn), len(jobsOut))
	for _, j := range jobsOut {
		for i := range jobsIn {
			if j.Id == jobsIn[i].Id {
				jobsIn[i].Modified = j.Modified // ignore timestamp
				require.Equal(t, jobsIn[i], j)
				jobsIn = append(jobsIn[:i], jobsIn[i+1:]...)
				break
			}
		}
	}
	if len(jobsIn) > 0 {
		t.Errorf("some jobs not found: %v", jobsIn)
	}
}