From 8780dda26a819b4b93caba75a893fb082849406e Mon Sep 17 00:00:00 2001 From: Nick White Date: Tue, 27 Jul 2021 14:22:03 +0100 Subject: internal/pipeline: add tests for DetectQueueType --- internal/pipeline/put.go | 7 ++++++- internal/pipeline/put_test.go | 24 ++++++++++++++++++++++++ internal/pipeline/testdata/equalpngandjpg/1.jpg | 0 internal/pipeline/testdata/equalpngandjpg/1.png | 0 internal/pipeline/testdata/mostlyjpg/1.jpg | 0 internal/pipeline/testdata/mostlyjpg/1.png | 0 internal/pipeline/testdata/mostlyjpg/2.jpg | 0 internal/pipeline/testdata/mostlypng/1.jpg | 0 internal/pipeline/testdata/mostlypng/1.png | 0 internal/pipeline/testdata/mostlypng/2.png | 0 internal/pipeline/testdata/notreadable/1.png | Bin 0 -> 9123 bytes 11 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 internal/pipeline/testdata/equalpngandjpg/1.jpg create mode 100644 internal/pipeline/testdata/equalpngandjpg/1.png create mode 100644 internal/pipeline/testdata/mostlyjpg/1.jpg create mode 100644 internal/pipeline/testdata/mostlyjpg/1.png create mode 100644 internal/pipeline/testdata/mostlyjpg/2.jpg create mode 100644 internal/pipeline/testdata/mostlypng/1.jpg create mode 100644 internal/pipeline/testdata/mostlypng/1.png create mode 100644 internal/pipeline/testdata/mostlypng/2.png create mode 100644 internal/pipeline/testdata/notreadable/1.png (limited to 'internal/pipeline') diff --git a/internal/pipeline/put.go b/internal/pipeline/put.go index 647b7a9..7b292b9 100644 --- a/internal/pipeline/put.go +++ b/internal/pipeline/put.go @@ -23,6 +23,8 @@ func (w NullWriter) Write(p []byte) (n int, err error) { type fileWalk chan string +// Walk sends the path of all files to the channel, with the exception of +// any file or directory which starts with "." func (f fileWalk) Walk(path string, info os.FileInfo, err error) error { if err != nil { return err @@ -38,6 +40,8 @@ func (f fileWalk) Walk(path string, info os.FileInfo, err error) error { return nil } +// CheckImages checks that all files in a directory are images +// that can be decoded (skipping dotfiles) func CheckImages(dir string) error { checker := make(fileWalk) go func() { @@ -59,8 +63,9 @@ func CheckImages(dir string) error { return nil } +// DetectQueueType detects which queue to use based on the preponderance +// of files of a particular extension in a directory func DetectQueueType(dir string, conn Queuer) string { - // Auto detect type of queue to send to based on file extension pngdirs, _ := filepath.Glob(dir + "/*.png") jpgdirs, _ := filepath.Glob(dir + "/*.jpg") pngcount := len(pngdirs) diff --git a/internal/pipeline/put_test.go b/internal/pipeline/put_test.go index 9cc8b02..af18ab4 100644 --- a/internal/pipeline/put_test.go +++ b/internal/pipeline/put_test.go @@ -7,6 +7,7 @@ package pipeline import ( "errors" "os" + "rescribe.xyz/bookpipeline" "testing" ) @@ -49,3 +50,26 @@ func Test_CheckImages(t *testing.T) { }) } } + +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) + } + }) + } +} diff --git a/internal/pipeline/testdata/equalpngandjpg/1.jpg b/internal/pipeline/testdata/equalpngandjpg/1.jpg new file mode 100644 index 0000000..e69de29 diff --git a/internal/pipeline/testdata/equalpngandjpg/1.png b/internal/pipeline/testdata/equalpngandjpg/1.png new file mode 100644 index 0000000..e69de29 diff --git a/internal/pipeline/testdata/mostlyjpg/1.jpg b/internal/pipeline/testdata/mostlyjpg/1.jpg new file mode 100644 index 0000000..e69de29 diff --git a/internal/pipeline/testdata/mostlyjpg/1.png b/internal/pipeline/testdata/mostlyjpg/1.png new file mode 100644 index 0000000..e69de29 diff --git a/internal/pipeline/testdata/mostlyjpg/2.jpg b/internal/pipeline/testdata/mostlyjpg/2.jpg new file mode 100644 index 0000000..e69de29 diff --git a/internal/pipeline/testdata/mostlypng/1.jpg b/internal/pipeline/testdata/mostlypng/1.jpg new file mode 100644 index 0000000..e69de29 diff --git a/internal/pipeline/testdata/mostlypng/1.png b/internal/pipeline/testdata/mostlypng/1.png new file mode 100644 index 0000000..e69de29 diff --git a/internal/pipeline/testdata/mostlypng/2.png b/internal/pipeline/testdata/mostlypng/2.png new file mode 100644 index 0000000..e69de29 diff --git a/internal/pipeline/testdata/notreadable/1.png b/internal/pipeline/testdata/notreadable/1.png new file mode 100644 index 0000000..8f90f19 Binary files /dev/null and b/internal/pipeline/testdata/notreadable/1.png differ -- cgit v1.2.1-24-ge1ad