aboutsummaryrefslogtreecommitdiffstats
path: root/lulu_test.go
blob: 063d5aac3629c59af13d65cea3bad2c8b61083f8 (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
90
91
92
93
package lulu

import (
	"context"
	_ "embed"
	"encoding/json"
	"flag"
	"strings"
	"testing"
	"time"

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

const (
	interiorUrl = "https://www.dropbox.com/sh/p3zh22vzsaegiri/AACOUn3LFKsITDzylh13bQpsa/161025/thesis2.pdf?dl=1"
	coverUrl    = "https://www.dropbox.com/sh/p3zh22vzsaegiri/AADP367j0bTWlt8fCu-_tm2ia/161025/139056_cover.pdf?dl=1"

	timeout = 15 * time.Second
)

var (
	//go:embed testdata/clientkey
	clientKey string
	//go:embed testdata/clientsecret
	clientSecret string
)

func TestMain(m *testing.M) {
	flag.BoolVar(&Debug, "d", false, "Print debug info to stderr.")
	flag.Parse()
	Sandbox()
	m.Run()
}

func newClient(t *testing.T) *Client {
	t.Helper()
	creds := Credentials{
		Key:    strings.TrimSpace(clientKey),
		Secret: strings.TrimSpace(clientSecret),
	}
	c, err := NewClient(t.Context(), creds)
	require.NoError(t, err)
	return c
}

func requireMarshalJsonEq(t *testing.T, expected string, marshaler any) {
	t.Helper()
	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)
}

// assert that t2 is after t1
func requireAfter(t *testing.T, t2, t1 time.Time) {
	t.Helper()
	require.Truef(t, t2.After(t1), "%s not after %s", t2, t1)
}

// assert that dt2 is not before tt1
func requireNotBeforeDate(t *testing.T, dt2, dt1 Date) {
	t.Helper()
	y2, m2, d2 := time.Time(dt2).UTC().Date()
	y1, m1, d1 := time.Time(dt1).UTC().Date()
	require.Truef(t, y2 >= y1 && m2 >= m1 && d2 >= d1, "%s is before %s",
		time.Date(y2, m2, d2, 0, 0, 0, 0, time.UTC).Format(time.DateOnly),
		time.Date(y1, m1, d1, 0, 0, 0, 0, time.UTC).Format(time.DateOnly))
}

func tpoll(t *testing.T, f func() bool) {
	t.Helper()
	ctx, cancel := context.WithTimeout(t.Context(), timeout)
	defer cancel()
	_, err := poll(ctx, func() (struct{}, bool, error) {
		return struct{}{}, f(), nil
	})
	require.NoError(t, err)
}

func mustParseTime(layout, value string) time.Time {
	t, err := time.Parse(layout, value)
	if err != nil {
		panic(err)
	}
	return t
}