forked from Macilias/go-images-orientation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
111 lines (91 loc) · 3.15 KB
/
utils_test.go
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
107
108
109
110
111
package orientation
import (
"fmt"
"image"
"os"
"testing"
"github.com/sirupsen/logrus"
)
//This test should output the same image stripped out of exif data, can be checked on https://www.thexifer.net/ if needed
func TestF3(t *testing.T) {
F3Img, err := getImageFromFilePath("./TestImages/F-3.jpg")
if err != nil {
fmt.Printf("error: %s", err)
}
newimg := ReadImage(F3Img, logrus.NewEntry(logrus.StandardLogger()), "whateverID1")
orientation, err := GetExifOrientation(GetExif(F3Img, logrus.NewEntry(logrus.StandardLogger()), "whateverID1"))
if err != nil {
t.Errorf("error: %s", err)
}
neworientation, err := GetExifOrientation(GetExif(newimg, logrus.NewEntry(logrus.StandardLogger()), "whateverID1"))
if err != nil {
t.Errorf("error: %s", err)
}
os.WriteFile("./TestImages/f3_after_tests.jpg", newimg, os.ModeDevice.Perm())
if neworientation == "none" {
t.Logf("test F3 passed with orientation from %s to %s", orientation, neworientation)
return
}
t.Errorf("Expected no orientation, found: %s", neworientation)
}
func TestF1(t *testing.T) {
F1Img, err := getImageFromFilePath("./TestImages/F-1.jpg")
if err != nil {
fmt.Printf("error: %s", err)
}
newimg := ReadImage(F1Img, logrus.NewEntry(logrus.StandardLogger()), "whateverID2")
orientation, err := GetExifOrientation(GetExif(F1Img, logrus.NewEntry(logrus.StandardLogger()), "whateverID2"))
if err != nil {
t.Errorf("error: %s", err)
}
neworientation, err := GetExifOrientation(GetExif(newimg, logrus.NewEntry(logrus.StandardLogger()), "whateverID2"))
if err != nil {
t.Errorf("error: %s", err)
}
if neworientation != "1" {
t.Errorf("Expected to read orientation 1, found %s", neworientation)
}
if neworientation == "1" && orientation == "1" {
t.Log("test F1 passed with unchanged orientation 1")
return
}
t.Errorf("Expected orientation 1, found: %s", orientation)
}
func TestFnone(t *testing.T) {
FnoneImg, err := getImageFromFilePath("./TestImages/F-none.jpg")
if err != nil {
fmt.Printf("error: %s", err)
}
newimg := ReadImage(FnoneImg, logrus.NewEntry(logrus.StandardLogger()), "whateverID3")
orientation, err := GetExifOrientation(GetExif(FnoneImg, logrus.NewEntry(logrus.StandardLogger()), "whateverID3"))
if err != nil {
t.Errorf("error: %s", err)
}
neworientation, err := GetExifOrientation(GetExif(newimg, logrus.NewEntry(logrus.StandardLogger()), "whateverID3"))
if err != nil {
t.Errorf("error: %s", err)
}
if orientation != "none" {
t.Errorf("Expected no orientation, found: %s", orientation)
}
if neworientation != "none" {
t.Errorf("unexpected orientation found: %v", neworientation)
}
t.Logf("test Fnone passed with orientation %s", neworientation)
}
func TestOrientError(t *testing.T) {
var messedUpImage image.Image = nil
t.Logf("should return error")
orientation := "4"
img, err := reverseOrientation(messedUpImage, orientation, logrus.NewEntry(logrus.StandardLogger()), "I D-test go :)")
if img != nil {
t.Errorf("unexpected result for reversing nil img")
}
if err == nil {
t.Errorf("expected error was not found")
}
}
func getImageFromFilePath(filePath string) ([]byte, error) {
file, err := os.ReadFile(filePath)
return file, err
}