Skip to content

Commit

Permalink
tests: more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aldor007 committed Apr 21, 2019
1 parent d818e17 commit 1877f4a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 37 deletions.
9 changes: 0 additions & 9 deletions pkg/engine/image_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,15 @@ import (

"gopkg.in/h2non/bimg.v1"

"bytes"
"crypto/md5"
"encoding/hex"
"github.com/aldor007/mort/pkg/monitoring"
"github.com/aldor007/mort/pkg/object"
"github.com/aldor007/mort/pkg/response"
"github.com/aldor007/mort/pkg/transforms"
"go.uber.org/zap"
"sync"
)

// bufPool for string concatenations
var bufPool = sync.Pool{
New: func() interface{} {
return &bytes.Buffer{}
},
}

// ImageEngine is main struct that is responding for image processing
type ImageEngine struct {
parent *response.Response // source file
Expand Down
30 changes: 27 additions & 3 deletions pkg/object/file_object_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package object

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -427,7 +428,7 @@ func TestNewFileUnknownPreset(t *testing.T) {
func TestObjectType(t *testing.T) {
mortConfig := &config.Config{}
mortConfig.Load("testdata/bucket-transform-query-parent-storage.yml")
obj, err := NewFileObject(pathToURL("/bucket/parent.jpg?width=100&operation=watermark&opacity=0.5&minAmpl=0.5&image=http://www&position=top-left"), mortConfig)
obj, err := NewFileObject(pathToURL("/bucket/parent.jpg?width=100&operation=extract&top=100&left=1&width=12&height=11"), mortConfig)
assert.Nil(t, err)

assert.Equal(t, obj.Type(), "transform")
Expand All @@ -437,12 +438,35 @@ func TestObjectType(t *testing.T) {
func TestObjectCacheKeyQuery(t *testing.T) {
mortConfig := &config.Config{}
mortConfig.Load("testdata/bucket-transform-query-parent-storage.yml")
obj, err := NewFileObject(pathToURL("/bucket/parent.jpg?width=100&operation=watermark&opacity=0.5&minAmpl=0.5&image=http://www&position=top-left"), mortConfig)
obj, err := NewFileObject(pathToURL("/bucket/parent.jpg?width=100&operation=resizeCropAuto&width=100&height=100"), mortConfig)
assert.Nil(t, err)

assert.Equal(t, obj.GetResponseCacheKey(), "/parent.jpg/90e8c676de60865efa843590826a08e1")
assert.Equal(t, obj.GetResponseCacheKey(), "/parent.jpg/179e44cbdd0d290829b89286562fa8b6")
}

func TestFileObject_FillWithRequest(t *testing.T) {
mortConfig := &config.Config{}
mortConfig.Load("testdata/bucket-transform-query-parent-storage.yml")
obj, err := NewFileObjectFromPath("/bucket/parent.jpg?width=100&operation=resizeCropAuto&width=100&height=100", mortConfig)
assert.Nil(t, err)

req, _ := http.NewRequest("GET", "http://mort", nil)
req.Header.Set("Range", "bytes=1-199")
obj.FillWithRequest(req, req.Context())

assert.Equal(t, obj.Range, "bytes=1-199")
}

func TestFileObject_ErrorObject(t *testing.T) {
mortConfig := &config.Config{}
mortConfig.Load("testdata/bucket-transform-query-parent-storage.yml")
obj, err := NewFileObjectFromPath("/bucket/parent.jpg?width=100&operation=resizeCropAuto&width=100&height=100", mortConfig)
assert.Nil(t, err)
erroredObject, err := NewFileErrorObject("/parent", obj)
assert.Nil(t, err)

assert.Equal(t, erroredObject.GetResponseCacheKey(), "/parenta692a0f768855173")
}
func TestObjectCacheKeyPreset(t *testing.T) {
mortConfig := config.GetInstance()
mortConfig.Load("testdata/bucket-transform-hash.yml")
Expand Down
21 changes: 1 addition & 20 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
httpStorage "github.com/aldor007/stow/http"
fileStorage "github.com/aldor007/stow/local"
metaStorage "github.com/aldor007/stow/local-meta"
// import blank to register noop adapter in stow.Register
"bytes"

"encoding/xml"
"fmt"
"github.com/aldor007/mort/pkg/monitoring"
Expand All @@ -21,20 +20,13 @@ import (
"mime"
"net/http"
"path"
"strconv"
"strings"
"sync"
"time"
)

const notFound = "{\"error\":\"item not found\"}"

var bufPool = sync.Pool{
New: func() interface{} {
return &bytes.Buffer{}
},
}

// storageClient struct that contain location and container
type storageClient struct {
container stow.Container
Expand Down Expand Up @@ -552,17 +544,6 @@ func inc(obj *object.FileObject, method string) {
method, obj.Storage.Kind, obj.Storage.Bucket, obj.Type()))
}

func createBytesHeader(bytesRage string, size int64) string {
buf := bufPool.Get().(*bytes.Buffer)
defer bufPool.Put(buf)
buf.Reset()
buf.WriteString(strings.Replace(bytesRage, "=", "", 1))
buf.WriteByte('/')
buf.WriteString(strconv.FormatInt(size, 10))
return buf.String()

}

func isDir(item stow.Item) bool {
metaData, err := item.Metadata()
if err != nil {
Expand Down
9 changes: 4 additions & 5 deletions pkg/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package storage
import (
"bytes"
"fmt"
"github.com/aldor007/mort/pkg/config"
"github.com/aldor007/mort/pkg/object"
"github.com/aldor007/mort/pkg/response"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"testing"

"github.com/aldor007/mort/pkg/config"
"github.com/aldor007/mort/pkg/object"
"github.com/aldor007/mort/pkg/response"
"github.com/stretchr/testify/assert"
)

func TestMain(m *testing.M) {
Expand Down
13 changes: 13 additions & 0 deletions pkg/transforms/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,16 @@ func TestTransforms_Extract(t *testing.T) {
hashStr := strconv.FormatUint(uint64(trans.Hash().Sum64()), 16)
assert.Equal(t, "76e63c06a9aacad3", hashStr)
}

func TestTransforms_ResizeCropAuto(t *testing.T) {
trans := New()
trans.ResizeCropAuto(210, 200)

optsArr, err := trans.BimgOptions(ImageInfo{})
assert.Nil(t, err)
assert.Equal(t, len(optsArr), 3)
assert.True(t, trans.NotEmpty)

hashStr := strconv.FormatUint(uint64(trans.Hash().Sum64()), 16)
assert.Equal(t, "a9476be4baa3fb94", hashStr)
}

0 comments on commit 1877f4a

Please sign in to comment.