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 := tNewClient(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 := tNewClient(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 { // ignore timestamp jobsIn[i].Modified = j.Modified // ignore normalizations require.Len(t, jobsIn[i].Items, len(j.Items)) for iti := range jobsIn[i].Items { jobsIn[i].Items[iti].PrintableNormalization = j.Items[iti].PrintableNormalization } require.Equal(t, jobsIn[i], j) jobsIn = append(jobsIn[:i], jobsIn[i+1:]...) // found this one break } } } if len(jobsIn) > 0 { t.Errorf("some jobs not found: %v", jobsIn) } }