aboutsummaryrefslogtreecommitdiffstats
path: root/lulu_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'lulu_test.go')
-rw-r--r--lulu_test.go58
1 files changed, 51 insertions, 7 deletions
diff --git a/lulu_test.go b/lulu_test.go
index b44427a..5f11e99 100644
--- a/lulu_test.go
+++ b/lulu_test.go
@@ -1,11 +1,13 @@
package lulu
import (
+ "context"
"encoding/json"
"fmt"
"os"
"strings"
"testing"
+ "time"
"github.com/stretchr/testify/require"
)
@@ -63,12 +65,12 @@ func TestMarshalValidateInteriorReq(t *testing.T) {
GoldFoil},
}
- requireJsonEq(t, want, req)
+ requireMarshalJsonEq(t, want, req)
}
func TestMarshalValidateInteriorBasicReq(t *testing.T) {
t.Parallel()
- requireJsonEq(t,
+ requireMarshalJsonEq(t,
`{"source_url": "https://example.com/interior.pdf"}`,
validateInteriorBasicReq{"https://example.com/interior.pdf"})
}
@@ -99,7 +101,7 @@ func TestUnmarshalInteriorValidationRecord(t *testing.T) {
func TestValidateInterior(t *testing.T) {
c := newClient(t)
- pkg := PkgId{
+ mfg := PkgId{
UsTrade,
Mono,
Standard,
@@ -109,9 +111,11 @@ func TestValidateInterior(t *testing.T) {
NoLinen,
NoFoil,
}
- id, err := c.ValidateInterior(interiorUrl, pkg)
+ id, err := c.ValidateInterior(interiorUrl, mfg)
require.NoError(t, err)
require.NotZero(t, id)
+ // It seems the server doesn't populate most of the response
+ // fields, but we just need the ID anyway.
}
func TestValidateInteriorBasic(t *testing.T) {
@@ -119,10 +123,43 @@ func TestValidateInteriorBasic(t *testing.T) {
id, err := c.ValidateInteriorBasic(interiorUrl)
require.NoError(t, err)
require.NotZero(t, id)
+ // It seems the server doesn't populate most of the response
+ // fields, but we just need the ID anyway.
}
func TestGetInteriorValidation(t *testing.T) {
- t.Fail() // TODO
+ c := newClient(t)
+
+ // Start validation job
+ id, err := c.ValidateInteriorBasic(interiorUrl)
+ require.NoError(t, err)
+
+ // Poll until status is non-null
+ timeout := 15 * time.Second
+ period := time.Second
+ ctx, cancel := context.WithTimeout(context.Background(), timeout)
+ defer cancel()
+ timer := time.NewTimer(period)
+ for {
+ select {
+ case <-timer.C:
+ rec, err := c.GetInteriorValidation(id)
+ require.NoError(t, err)
+ if rec.Status.IsFinal() {
+ require.Equal(t, id, rec.Id)
+ require.Equal(t, interiorUrl, rec.SrcUrl)
+ require.Equal(t, uint(210), rec.NPages)
+ require.Empty(t, rec.Errors)
+ require.Equal(t, StatusValidated, rec.Status)
+ require.NotEmpty(t, rec.ValidPkgIds)
+ return
+ }
+ timer.Reset(period)
+ case <-ctx.Done():
+ t.Errorf("status still not finalized after %v", timeout)
+ return
+ }
+ }
}
func newClient(t *testing.T) *Client {
@@ -132,9 +169,16 @@ func newClient(t *testing.T) *Client {
return c
}
-func requireJsonEq(t *testing.T, expected string, actual any) {
+func requireMarshalJsonEq(t *testing.T, expected string, marshaler any) {
t.Helper()
- jactual, err := json.Marshal(actual)
+ jactual, err := json.Marshal(marshaler)
require.NoError(t, err)
require.JSONEq(t, expected, string(jactual))
}
+
+func requireUnmarshalJsonEq[T any](t *testing.T, expected T, j string) {
+ t.Helper()
+ var actual T
+ require.NoError(t, json.Unmarshal([]byte(j), &actual))
+ require.Equal(t, expected, actual)
+}