GH-12630 Migrate tests from model/file_info_test.go to use testify (#12647)

* Rewrite tests on testify for model/file_info_test.go
Этот коммит содержится в:
413x
2019-10-28 17:44:20 +02:00
коммит произвёл Guillermo Vayá
родитель d6f8ea293b
Коммит ef5a5d574f

Просмотреть файл

@@ -5,6 +5,8 @@ package model
import ( import (
"encoding/base64" "encoding/base64"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
_ "image/gif" _ "image/gif"
_ "image/png" _ "image/png"
"io/ioutil" "io/ioutil"
@@ -22,179 +24,109 @@ func TestFileInfoIsValid(t *testing.T) {
Path: "fake/path.png", Path: "fake/path.png",
} }
if err := info.IsValid(); err != nil { require.Nil(t, info.IsValid())
t.Fatal(err)
}
info.Id = "" info.Id = ""
if err := info.IsValid(); err == nil { require.NotNil(t, info.IsValid(), "empty Id isn't valid")
t.Fatal("empty Id isn't valid")
}
info.Id = NewId() info.Id = NewId()
info.CreateAt = 0 info.CreateAt = 0
if err := info.IsValid(); err == nil { require.NotNil(t, info.IsValid(), "empty CreateAt isn't valid")
t.Fatal("empty CreateAt isn't valid")
}
info.CreateAt = 1234 info.CreateAt = 1234
info.UpdateAt = 0 info.UpdateAt = 0
if err := info.IsValid(); err == nil { require.NotNil(t, info.IsValid(), "empty UpdateAt isn't valid")
t.Fatal("empty UpdateAt isn't valid")
}
info.UpdateAt = 1234 info.UpdateAt = 1234
info.PostId = NewId() info.PostId = NewId()
if err := info.IsValid(); err != nil { require.Nil(t, info.IsValid())
t.Fatal(err)
}
info.Path = "" info.Path = ""
if err := info.IsValid(); err == nil { require.NotNil(t, info.IsValid(), "empty Path isn't valid")
t.Fatal("empty Path isn't valid")
}
info.Path = "fake/path.png" info.Path = "fake/path.png"
if err := info.IsValid(); err != nil { require.Nil(t, info.IsValid())
t.Fatal(err)
}
} }
func TestFileInfoIsImage(t *testing.T) { func TestFileInfoIsImage(t *testing.T) {
info := &FileInfo{ info := &FileInfo{MimeType: "image/png"}
MimeType: "image/png", assert.True(t, info.IsImage(), "file is an image")
}
if !info.IsImage() {
t.Fatal("file is an image")
}
info.MimeType = "text/plain" info.MimeType = "text/plain"
if info.IsImage() { assert.False(t, info.IsImage(), "file is not an image")
t.Fatal("file is not an image")
}
} }
func TestGetInfoForFile(t *testing.T) { func TestGetInfoForFile(t *testing.T) {
fakeFile := make([]byte, 1000) fakeFile := make([]byte, 1000)
if info, err := GetInfoForBytes("file.txt", fakeFile); err != nil { info, errApp := GetInfoForBytes("file.txt", fakeFile)
t.Fatal(err) require.Nil(t, errApp)
} else if info.Name != "file.txt" { assert.Equalf(t, info.Name, "file.txt", "Got incorrect filename: %v", info.Name)
t.Fatalf("Got incorrect filename: %v", info.Name) assert.Equalf(t, info.Extension, "txt", "Got incorrect extension: %v", info.Extension)
} else if info.Extension != "txt" { assert.EqualValuesf(t, info.Size, 1000, "Got incorrect size: %v", info.Size)
t.Fatalf("Got incorrect extension: %v", info.Extension) assert.Truef(t, strings.HasPrefix(info.MimeType, "text/plain"), "Got incorrect mime type: %v", info.MimeType)
} else if info.Size != 1000 { assert.Equalf(t, info.Width, 0, "Got incorrect width: %v", info.Width)
t.Fatalf("Got incorrect size: %v", info.Size) assert.Equalf(t, info.Height, 0, "Got incorrect height: %v", info.Height)
} else if !strings.HasPrefix(info.MimeType, "text/plain") { assert.Falsef(t, info.HasPreviewImage, "Got incorrect has preview image: %v", info.HasPreviewImage)
t.Fatalf("Got incorrect mime type: %v", info.MimeType)
} else if info.Width != 0 {
t.Fatalf("Got incorrect width: %v", info.Width)
} else if info.Height != 0 {
t.Fatalf("Got incorrect height: %v", info.Height)
} else if info.HasPreviewImage {
t.Fatalf("Got incorrect has preview image: %v", info.HasPreviewImage)
}
pngFile, err := ioutil.ReadFile("../tests/test.png") pngFile, err := ioutil.ReadFile("../tests/test.png")
if err != nil { require.Nilf(t, err, "Failed to load test.png")
t.Fatalf("Failed to load test.png: %v", err.Error())
} info, err = GetInfoForBytes("test.png", pngFile)
if info, err := GetInfoForBytes("test.png", pngFile); err != nil { require.Nil(t, err)
t.Fatal(err) assert.Equalf(t, info.Name, "test.png", "Got incorrect filename: %v", info.Name)
} else if info.Name != "test.png" { assert.Equalf(t, info.Extension, "png", "Got incorrect extension: %v", info.Extension)
t.Fatalf("Got incorrect filename: %v", info.Name) assert.EqualValues(t, info.Size, 279591, "Got incorrect size: %v", info.Size)
} else if info.Extension != "png" { assert.Equalf(t, info.MimeType, "image/png", "Got incorrect mime type: %v", info.MimeType)
t.Fatalf("Got incorrect extension: %v", info.Extension) assert.Equalf(t, info.Width, 408, "Got incorrect width: %v", info.Width)
} else if info.Size != 279591 { assert.Equalf(t, info.Height, 336, "Got incorrect height: %v", info.Height)
t.Fatalf("Got incorrect size: %v", info.Size) assert.Truef(t, info.HasPreviewImage, "Got incorrect has preview image: %v", info.HasPreviewImage)
} else if info.MimeType != "image/png" {
t.Fatalf("Got incorrect mime type: %v", info.MimeType)
} else if info.Width != 408 {
t.Fatalf("Got incorrect width: %v", info.Width)
} else if info.Height != 336 {
t.Fatalf("Got incorrect height: %v", info.Height)
} else if !info.HasPreviewImage {
t.Fatalf("Got incorrect has preview image: %v", info.HasPreviewImage)
}
// base 64 encoded version of handtinywhite.gif from http://probablyprogramming.com/2009/03/15/the-tiniest-gif-ever // base 64 encoded version of handtinywhite.gif from http://probablyprogramming.com/2009/03/15/the-tiniest-gif-ever
gifFile, _ := base64.StdEncoding.DecodeString("R0lGODlhAQABAIABAP///wAAACwAAAAAAQABAAACAkQBADs=") gifFile, _ := base64.StdEncoding.DecodeString("R0lGODlhAQABAIABAP///wAAACwAAAAAAQABAAACAkQBADs=")
if info, err := GetInfoForBytes("handtinywhite.gif", gifFile); err != nil { info, err = GetInfoForBytes("handtinywhite.gif", gifFile)
t.Fatal(err) require.Nil(t, err)
} else if info.Name != "handtinywhite.gif" { assert.Equalf(t, info.Name, "handtinywhite.gif", "Got incorrect filename: %v", info.Name)
t.Fatalf("Got incorrect filename: %v", info.Name) assert.Equalf(t, info.Extension, "gif", "Got incorrect extension: %v", info.Extension)
} else if info.Extension != "gif" { assert.EqualValuesf(t, info.Size, 35, "Got incorrect size: %v", info.Size)
t.Fatalf("Got incorrect extension: %v", info.Extension) assert.Equalf(t, info.MimeType, "image/gif", "Got incorrect mime type: %v", info.MimeType)
} else if info.Size != 35 { assert.Equalf(t, info.Width, 1, "Got incorrect width: %v", info.Width)
t.Fatalf("Got incorrect size: %v", info.Size) assert.Equalf(t, info.Height, 1, "Got incorrect height: %v", info.Height)
} else if info.MimeType != "image/gif" { assert.Truef(t, info.HasPreviewImage, "Got incorrect has preview image: %v", info.HasPreviewImage)
t.Fatalf("Got incorrect mime type: %v", info.MimeType)
} else if info.Width != 1 {
t.Fatalf("Got incorrect width: %v", info.Width)
} else if info.Height != 1 {
t.Fatalf("Got incorrect height: %v", info.Height)
} else if !info.HasPreviewImage {
t.Fatalf("Got incorrect has preview image: %v", info.HasPreviewImage)
}
animatedGifFile, err := ioutil.ReadFile("../tests/testgif.gif") animatedGifFile, err := ioutil.ReadFile("../tests/testgif.gif")
if err != nil { require.Nilf(t, err, "Failed to load testgif.gif")
t.Fatalf("Failed to load testgif.gif: %v", err.Error())
}
if info, err := GetInfoForBytes("testgif.gif", animatedGifFile); err != nil {
t.Fatal(err)
} else if info.Name != "testgif.gif" {
t.Fatalf("Got incorrect filename: %v", info.Name)
} else if info.Extension != "gif" {
t.Fatalf("Got incorrect extension: %v", info.Extension)
} else if info.Size != 38689 {
t.Fatalf("Got incorrect size: %v", info.Size)
} else if info.MimeType != "image/gif" {
t.Fatalf("Got incorrect mime type: %v", info.MimeType)
} else if info.Width != 118 {
t.Fatalf("Got incorrect width: %v", info.Width)
} else if info.Height != 118 {
t.Fatalf("Got incorrect height: %v", info.Height)
} else if info.HasPreviewImage {
t.Fatalf("Got incorrect has preview image: %v", info.HasPreviewImage)
}
if info, err := GetInfoForBytes("filewithoutextension", fakeFile); err != nil { info, err = GetInfoForBytes("testgif.gif", animatedGifFile)
t.Fatal(err) require.Nil(t, err)
} else if info.Name != "filewithoutextension" { assert.Equalf(t, info.Name, "testgif.gif", "Got incorrect filename: %v", info.Name)
t.Fatalf("Got incorrect filename: %v", info.Name) assert.Equalf(t, info.Extension, "gif", "Got incorrect extension: %v", info.Extension)
} else if info.Extension != "" { assert.EqualValuesf(t, info.Size, 38689, "Got incorrect size: %v", info.Size)
t.Fatalf("Got incorrect extension: %v", info.Extension) assert.Equalf(t, info.MimeType, "image/gif", "Got incorrect mime type: %v", info.MimeType)
} else if info.Size != 1000 { assert.Equalf(t, info.Width, 118, "Got incorrect width: %v", info.Width)
t.Fatalf("Got incorrect size: %v", info.Size) assert.Equalf(t, info.Height, 118, "Got incorrect height: %v", info.Height)
} else if info.MimeType != "" { assert.Falsef(t, info.HasPreviewImage, "Got incorrect has preview image: %v", info.HasPreviewImage)
t.Fatalf("Got incorrect mime type: %v", info.MimeType)
} else if info.Width != 0 { info, err = GetInfoForBytes("filewithoutextension", fakeFile)
t.Fatalf("Got incorrect width: %v", info.Width) require.Nil(t, err)
} else if info.Height != 0 { assert.Equalf(t, info.Name, "filewithoutextension", "Got incorrect filename: %v", info.Name)
t.Fatalf("Got incorrect height: %v", info.Height) assert.Equalf(t, info.Extension, "", "Got incorrect extension: %v", info.Extension)
} else if info.HasPreviewImage { assert.EqualValuesf(t, info.Size, 1000, "Got incorrect size: %v", info.Size)
t.Fatalf("Got incorrect has preview image: %v", info.HasPreviewImage) assert.Equalf(t, info.MimeType, "", "Got incorrect mime type: %v", info.MimeType)
} assert.Equalf(t, info.Width, 0, "Got incorrect width: %v", info.Width)
assert.Equalf(t, info.Height, 0, "Got incorrect height: %v", info.Height)
assert.Falsef(t, info.HasPreviewImage, "Got incorrect has preview image: %v", info.HasPreviewImage)
// Always make the extension lower case to make it easier to use in other places // Always make the extension lower case to make it easier to use in other places
if info, err := GetInfoForBytes("file.TXT", fakeFile); err != nil { info, err = GetInfoForBytes("file.TXT", fakeFile)
t.Fatal(err) require.Nil(t, err)
} else if info.Name != "file.TXT" { assert.Equalf(t, info.Name, "file.TXT", "Got incorrect filename: %v", info.Name)
t.Fatalf("Got incorrect filename: %v", info.Name) assert.Equalf(t, info.Extension, "txt", "Got incorrect extension: %v", info.Extension)
} else if info.Extension != "txt" {
t.Fatalf("Got incorrect extension: %v", info.Extension)
}
// Don't error out for image formats we don't support // Don't error out for image formats we don't support
if info, err := GetInfoForBytes("file.tif", fakeFile); err != nil { info, err = GetInfoForBytes("file.tif", fakeFile)
t.Fatal(err) require.Nil(t, err)
} else if info.Name != "file.tif" { assert.Equalf(t, info.Name, "file.tif", "Got incorrect filename: %v", info.Name)
t.Fatalf("Got incorrect filename: %v", info.Name) assert.Equalf(t, info.Extension, "tif", "Got incorrect extension: %v", info.Extension)
} else if info.Extension != "tif" { assert.True(t, info.MimeType == "image/x-tiff" || info.MimeType == "image/tiff", "Got incorrect mime type: %v", info.MimeType)
t.Fatalf("Got incorrect extension: %v", info.Extension)
} else if info.MimeType != "image/tiff" && info.MimeType != "image/x-tiff" {
t.Fatalf("Got incorrect mime type: %v", info.MimeType)
}
} }