 baa93ec66e
			
		
	
	
	baa93ec66e
	
	
	
		
			
			This PR is part of https://codeberg.org/forgejo/forgejo/pulls/4767 filed by @algernon Validateable is enhanced for less duplication. ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. ### Release notes - [x] I do not want this change to show in the release notes. - [ ] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title. Co-authored-by: zam <mirco.zachmann@meissa.de> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7714 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: Michael Jerger <michael.jerger@meissa-gmbh.de> Co-committed-by: Michael Jerger <michael.jerger@meissa-gmbh.de>
		
			
				
	
	
		
			89 lines
		
	
	
	
		
			1.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			89 lines
		
	
	
	
		
			1.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Copyright 2024, 2025 The Forgejo Authors. All rights reserved.
 | |
| // SPDX-License-Identifier: MIT
 | |
| 
 | |
| package validation
 | |
| 
 | |
| import (
 | |
| 	"testing"
 | |
| 
 | |
| 	"forgejo.org/modules/timeutil"
 | |
| 
 | |
| 	ap "github.com/go-ap/activitypub"
 | |
| 	"github.com/stretchr/testify/assert"
 | |
| )
 | |
| 
 | |
| type Sut struct {
 | |
| 	valid bool
 | |
| }
 | |
| 
 | |
| func (sut Sut) Validate() []string {
 | |
| 	if sut.valid {
 | |
| 		return []string{}
 | |
| 	}
 | |
| 	return []string{"invalid"}
 | |
| }
 | |
| 
 | |
| func Test_IsValid(t *testing.T) {
 | |
| 	sut := Sut{valid: true}
 | |
| 	if res, _ := IsValid(sut); !res {
 | |
| 		t.Errorf("sut expected to be valid: %v\n", sut.Validate())
 | |
| 	}
 | |
| 	sut = Sut{valid: false}
 | |
| 	res, err := IsValid(sut)
 | |
| 	if res {
 | |
| 		t.Errorf("sut expected to be invalid: %v\n", sut.Validate())
 | |
| 	}
 | |
| 	if err == nil || !IsErrNotValid(err) || err.Error() != "Validation Error: validation.Sut: invalid" {
 | |
| 		t.Errorf("validation error expected, but was %v", err)
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func Test_ValidateNotEmpty_ForString(t *testing.T) {
 | |
| 	sut := ""
 | |
| 	res := ValidateNotEmpty(sut, "dummyField")
 | |
| 	assert.Len(t, res, 1)
 | |
| 
 | |
| 	sut = "not empty"
 | |
| 	res = ValidateNotEmpty(sut, "dummyField")
 | |
| 	assert.Empty(t, res, 0)
 | |
| }
 | |
| 
 | |
| func Test_ValidateNotEmpty_ForTimestamp(t *testing.T) {
 | |
| 	sut := timeutil.TimeStamp(0)
 | |
| 	res := ValidateNotEmpty(sut, "dummyField")
 | |
| 	assert.Len(t, res, 1)
 | |
| 
 | |
| 	sut = timeutil.TimeStampNow()
 | |
| 	res = ValidateNotEmpty(sut, "dummyField")
 | |
| 	assert.Empty(t, res, 0)
 | |
| }
 | |
| 
 | |
| func Test_ValidateIDExists_ForItem(t *testing.T) {
 | |
| 	sut := ap.Activity{
 | |
| 		Object: nil,
 | |
| 	}
 | |
| 	res := ValidateIDExists(sut.Object, "dummyField")
 | |
| 	assert.Len(t, res, 1)
 | |
| 
 | |
| 	sut = ap.Activity{
 | |
| 		Object: ap.IRI(""),
 | |
| 	}
 | |
| 	res = ValidateIDExists(sut.Object, "dummyField")
 | |
| 	assert.Len(t, res, 1)
 | |
| 
 | |
| 	sut = ap.Activity{
 | |
| 		Object: ap.IRI("https://dummy.link/id"),
 | |
| 	}
 | |
| 	res = ValidateIDExists(sut.Object, "dummyField")
 | |
| 	assert.Empty(t, res, 0)
 | |
| }
 | |
| 
 | |
| func Test_ValidateMaxLen(t *testing.T) {
 | |
| 	sut := "0123456789"
 | |
| 	res := ValidateMaxLen(sut, 9, "dummyField")
 | |
| 	assert.Len(t, res, 1)
 | |
| 
 | |
| 	sut = "0123456789"
 | |
| 	res = ValidateMaxLen(sut, 11, "dummyField")
 | |
| 	assert.Empty(t, res, 0)
 | |
| }
 |