github.com/yandex/pandora@v0.5.32/core/config/validations_test.go (about)

     1  package config
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/c2h5oh/datasize"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  type WithDuration struct {
    12  	T time.Duration `validate:"min-time=10ms,max-time=100ms"`
    13  }
    14  
    15  func TestValidateTimeDuration(t *testing.T) {
    16  	assert.NoError(t, Validate(&WithDuration{50 * time.Millisecond}))
    17  
    18  	assert.Error(t, Validate(&WithDuration{5 * time.Millisecond}))
    19  	assert.Error(t, Validate(&WithDuration{500 * time.Millisecond}))
    20  }
    21  
    22  type WithSize struct {
    23  	T datasize.ByteSize `validate:"min-size=10kb,max-size=10M"`
    24  }
    25  
    26  func TestValidateByteSize(t *testing.T) {
    27  	assert.NoError(t, Validate(&WithSize{50 * datasize.KB}))
    28  
    29  	assert.Error(t, Validate(&WithSize{5 * datasize.KB}))
    30  	assert.Error(t, Validate(&WithSize{500 * datasize.MB}))
    31  }
    32  
    33  type WithEndpoint struct {
    34  	Endpoint string `validate:"required,endpoint"`
    35  }
    36  
    37  func TestValidateEndpoint(t *testing.T) {
    38  	assert.NoError(t, Validate(&WithEndpoint{"192.168.0.1:9999"}))
    39  	assert.NoError(t, Validate(&WithEndpoint{"localhost:9999"}))
    40  	assert.NoError(t, Validate(&WithEndpoint{":9999"}))
    41  
    42  	assert.Error(t, Validate(&WithEndpoint{"aaaaa"}))
    43  }
    44  
    45  type WithURLPath struct {
    46  	Path string `validate:"url-path"`
    47  }
    48  
    49  func TestValidatePath(t *testing.T) {
    50  	assert.NoError(t, Validate(&WithURLPath{"/some"}))
    51  	assert.NoError(t, Validate(&WithURLPath{"/just/some/path"}))
    52  	assert.NoError(t, Validate(&WithURLPath{"/just@/some()/+@!="}))
    53  	assert.Error(t, Validate(&WithURLPath{""}))
    54  	assert.Error(t, Validate(&WithURLPath{"/"}))
    55  	assert.Error(t, Validate(&WithURLPath{"/just/"}))
    56  	assert.Error(t, Validate(&WithURLPath{"/just//some"}))
    57  	assert.Error(t, Validate(&WithURLPath{"/just/some?"}))
    58  }