Skip to content
Snippets Groups Projects
Verified Commit 64342c75 authored by Paweł J. Wal's avatar Paweł J. Wal :wave:
Browse files

simple validator test

parent e89c41d3
No related branches found
No related tags found
1 merge request!45Resolve "Commons tests"
Pipeline #502 passed
// SPDX-FileCopyrightText: © 2025 Paweł J. Wal <p@steamshard.net>
//
// SPDX-License-Identifier: AGPL-3.0-only
package commons_test
import (
"testing"
"github.com/stretchr/testify/suite"
"kita.gawa.moe/paweljw/vellum/commons"
)
type testForm struct {
Name string `validate:"required,min=3"`
}
type ValidatorTestSuite struct {
suite.Suite
sut *commons.Validator
}
func (s *ValidatorTestSuite) SetupTest() {
s.sut = commons.NewValidator()
}
func (s *ValidatorTestSuite) TestValidateWithValidData() {
form := testForm{
Name: "Test Name",
}
result := s.sut.Validate(form)
s.True(result)
s.Nil(s.sut.Error)
}
func (s *ValidatorTestSuite) TestValidateWithInvalidData() {
form := testForm{
Name: "Te", // Too short, minimum 3 characters
}
result := s.sut.Validate(form)
s.False(result)
s.NotNil(s.sut.Error)
}
func (s *ValidatorTestSuite) TestMessagesWithValidData() {
form := testForm{
Name: "Test Name",
}
_ = s.sut.Validate(form)
messages := s.sut.Messages("Name")
s.Equal("", messages)
}
func (s *ValidatorTestSuite) TestMessagesWithInvalidData() {
form := testForm{
Name: "Te", // Too short, minimum 3 characters
}
_ = s.sut.Validate(form)
messages := s.sut.Messages("Name")
s.Equal("Name must be at least 3 characters in length", messages)
}
func TestValidatorTestSuite(t *testing.T) {
suite.Run(t, new(ValidatorTestSuite))
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment