mirror of
https://github.com/christianselig/apollo-backend
synced 2024-11-10 22:17:44 +00:00
write tests
This commit is contained in:
parent
8cdb8bb621
commit
69ea7494f3
3 changed files with 49 additions and 2 deletions
|
@ -195,7 +195,7 @@ func (a *api) upsertAccountsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := a.httpClient.Do(req)
|
resp, _ := a.httpClient.Do(req)
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
}(apns)
|
}(apns)
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ package domain
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -21,9 +22,21 @@ func (sr *Subreddit) NormalizedName() string {
|
||||||
return strings.ToLower(sr.Name)
|
return strings.ToLower(sr.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func validPrefix(value interface{}) error {
|
||||||
|
s, _ := value.(string)
|
||||||
|
if len(s) < 2 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if s[1] != '_' || s[0] != 'u' {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return errors.New("invalid subreddit format")
|
||||||
|
}
|
||||||
|
|
||||||
func (sr *Subreddit) Validate() error {
|
func (sr *Subreddit) Validate() error {
|
||||||
return validation.ValidateStruct(sr,
|
return validation.ValidateStruct(sr,
|
||||||
validation.Field(&sr.Name, validation.Required, validation.Length(3, 32), validation.Match(regexp.MustCompile(`^(?!u_)[a-zA-Z0-9]\w{1,19}$`))),
|
validation.Field(&sr.Name, validation.Required, validation.Length(3, 32), validation.By(validPrefix), validation.Match(regexp.MustCompile(`^[a-zA-Z0-9]\w{1,19}$`))),
|
||||||
validation.Field(&sr.SubredditID, validation.Required, validation.Length(4, 9)),
|
validation.Field(&sr.SubredditID, validation.Required, validation.Length(4, 9)),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
34
internal/domain/subreddit_test.go
Normal file
34
internal/domain/subreddit_test.go
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
package domain
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestValidate(t *testing.T) {
|
||||||
|
tests := map[string]struct {
|
||||||
|
subreddit Subreddit
|
||||||
|
err error
|
||||||
|
}{
|
||||||
|
"invalid subreddit prefix": {Subreddit{Name: "u_iamthatis"}, errors.New("invalid subreddit format")},
|
||||||
|
"valid subreddit": {Subreddit{Name: "pics", SubredditID: "abcd"}, nil},
|
||||||
|
"valid subreddit starting with u": {Subreddit{Name: "urcool", SubredditID: "abcd"}, nil},
|
||||||
|
"valid subreddit with _": {Subreddit{Name: "p_i_x_a_r", SubredditID: "abcd"}, nil},
|
||||||
|
}
|
||||||
|
|
||||||
|
for scenario, tc := range tests {
|
||||||
|
t.Run(scenario, func(t *testing.T) {
|
||||||
|
err := tc.subreddit.Validate()
|
||||||
|
|
||||||
|
if tc.err == nil {
|
||||||
|
require.NoError(t, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Error(t, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue