Select Git revision
file_test.go
file_test.go 2.24 KiB
package files
import (
"context"
"testing"
"text/template"
"git.perx.ru/perxis/perxis-go/pkg/expr"
"github.com/stretchr/testify/require"
)
func TestFile_SetURLWithTemplate(t *testing.T) {
tests := []struct {
name string
file *File
template *template.Template
wantErr bool
wantURL string
}{
{
name: "template is nil",
file: &File{
URL: "https://cloud.com/",
},
template: nil,
wantErr: false,
wantURL: "https://cloud.com/",
},
{
name: "template with non-existent field",
file: &File{},
template: template.Must(template.New("url").Parse("{{.NonExistentField}}")),
wantErr: true,
},
{
name: "success",
file: &File{
Size: 1024,
MimeType: "image/png",
Key: "file-key",
},
template: template.Must(template.New("url").Parse("https://cloud-proxy.com/{{.Key}}?size={{.Size}}#{{.MimeType}}")),
wantErr: false,
wantURL: "https://cloud-proxy.com/file-key?size=1024#image/png",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.file.SetURLWithTemplate(tt.template)
if tt.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
require.Equal(t, tt.wantURL, tt.file.URL)
}
})
}
}
func TestFile_InExpr(t *testing.T) {
ctx := context.Background()
tests := []struct {
exp string
env map[string]interface{}
wantResult interface{}
wantErr bool
}{
{"f.id", map[string]interface{}{"f": &File{ID: "some_id"}}, "some_id", false},