summaryrefslogtreecommitdiff
path: root/internal/pipeline/put_test.go
blob: 5fb5b434e57b07f587e13616374a1d1f0f416d71 (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2021 Nick White.
// Use of this source code is governed by the GPLv3
// license that can be found in the LICENSE file.

package pipeline

import (
	"errors"
	"log"
	"os"
	"rescribe.xyz/bookpipeline"
	"testing"
)

func Test_CheckImages(t *testing.T) {
	cases := []struct {
		dir string
		err error
	}{
		{"testdata/good", nil},
		{"testdata/bad", errors.New("Decoding image testdata/bad/bad.png failed: png: invalid format: invalid checksum")},
		{"testdata/notreadable", errors.New("Opening image testdata/notreadable/1.png failed: open testdata/notreadable/1.png: permission denied")},
	}

	for _, c := range cases {
		t.Run(c.dir, func(t *testing.T) {
			if c.dir == "testdata/notreadable" {
				err := os.Chmod("testdata/notreadable/1.png", 0000)
				if err != nil {
					t.Fatalf("Error preparing test by setting file to be unreadable: %v", err)
				}
			}

			err := CheckImages(c.dir)
			if err == nil && c.err != nil {
				t.Fatalf("Expected error '%v', got no error", c.err)
			}
			if err != nil && c.err == nil {
				t.Fatalf("Expected no error, got error '%v'", err)
			}
			if err != nil && c.err != nil && err.Error() != c.err.Error() {
				t.Fatalf("Got an unexpected error, expected '%v', got '%v'", c.err, err)
			}

			if c.dir == "testdata/notreadable" {
				err := os.Chmod("testdata/notreadable/1.png", 0644)
				if err != nil {
					t.Fatalf("Error resetting test by setting file to be readable: %v", err)
				}
			}
		})
	}
}

func Test_DetectQueueType(t *testing.T) {
	conn := &bookpipeline.LocalConn{}

	cases := []struct {
		dir string
		qid string
	}{
		{"testdata/mostlypng", conn.WipeQueueId()},
		{"testdata/mostlyjpg", conn.PreQueueId()},
		{"testdata/equalpngandjpg", conn.PreQueueId()},
		{"testdata/nonexistent", conn.PreQueueId()},
	}

	for _, c := range cases {
		t.Run(c.dir, func(t *testing.T) {
			qid := DetectQueueType(c.dir, conn)
			if qid != c.qid {
				t.Fatalf("Error, expected qid %v, got qid %v", qid, c.qid)
			}
		})
	}
}

func Test_UploadImages(t *testing.T) {
	var slog StrLog
        vlog := log.New(&slog, "", 0)
	var conns []connection

	conns = append(conns, connection{name: "local", c: &bookpipeline.LocalConn{Logger: vlog}})

	if !testing.Short() {
		conns = append(conns, connection{name: "aws", c: &bookpipeline.AwsConn{Logger: vlog}})
	}

	for _, conn := range conns {
		t.Run(conn.name, func(t *testing.T) {
			err := conn.c.Init()
			if err != nil {
				t.Fatalf("Could not initialise %s connection: %v\nLog: %s", conn.name, err, slog.log)
			}
			slog.log = ""

			err = UploadImages("testdata/good", "good", conn.c)
			if err != nil {
				t.Fatalf("Error in UploadImages for %s: %v\nLog: %s", conn.name, err, slog.log)
			}

			// TODO: download all files and test that they match
			// TODO: remove test files from conn storage
		})
	}
}