Writing tests for the Forms package

This commit is contained in:
Muyao CHEN 2024-07-02 21:48:00 +02:00
parent 875be55076
commit d87d8ed594
4 changed files with 156 additions and 24 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
**/c.out
**/c.html

View File

@ -2,7 +2,6 @@ package forms
import ( import (
"fmt" "fmt"
"net/http"
"net/url" "net/url"
"strings" "strings"
@ -25,18 +24,18 @@ func New(data url.Values) *Form {
// Required checks required fields // Required checks required fields
func (f *Form) Required(fields ...string) { func (f *Form) Required(fields ...string) {
for _, field := range fields { for _, field := range fields {
value := f.Get(field) value := f.Get(field)
if strings.TrimSpace(value) == "" { if strings.TrimSpace(value) == "" {
f.Errors.Add(field, "This field cannot be blank") f.Errors.Add(field, "This field cannot be blank")
} }
} }
} }
// Has checks if form field is in post and not emtpy // Has checks if form field is in post and not emtpy
func (f *Form) Has(field string, r *http.Request) bool { func (f *Form) Has(field string) bool {
x := r.Form.Get(field) x := f.Get(field)
return x != "" return x != ""
} }
// Valid returns true if there are no errors, otherwise false // Valid returns true if there are no errors, otherwise false
@ -45,19 +44,19 @@ func (f *Form) Valid() bool {
} }
// MinLength checks for string minimum length // MinLength checks for string minimum length
func (f *Form) MinLength(field string, length int, r *http.Request) bool { func (f *Form) MinLength(field string, length int) bool {
value := r.Form.Get(field) value := f.Get(field)
if len(value) < length { if len(value) < length {
f.Errors.Add(field, fmt.Sprintf("This field must have at least %d letters", length)) f.Errors.Add(field, fmt.Sprintf("This field must have at least %d letters", length))
return false return false
} }
return true return true
} }
// IsEmail checks the email address // IsEmail checks the email address
func (f *Form) IsEmail(field string, r *http.Request) { func (f *Form) IsEmail(field string) {
value := r.Form.Get(field) value := f.Get(field)
if !govalidator.IsEmail(value) { if !govalidator.IsEmail(value) {
f.Errors.Add(field, "Invalid email address") f.Errors.Add(field, "Invalid email address")
} }
} }

View File

@ -0,0 +1,131 @@
package forms
import (
"net/http/httptest"
"net/url"
"testing"
)
func TestForms_Valid(t *testing.T) {
r := httptest.NewRequest("POST", "/test-url", nil)
form := New(r.PostForm)
isValid := form.Valid()
if !isValid {
t.Error("got invalid when should have been valid")
}
}
func TestForms_Required(t *testing.T) {
r := httptest.NewRequest("POST", "/test-url", nil)
form := New(r.PostForm)
form.Required("a", "b", "c")
if form.Valid() {
t.Error("required fields are not given, should be invalid")
}
postData := url.Values{}
postData.Add("a", "a")
postData.Add("b", "a")
postData.Add("c", "a")
r = httptest.NewRequest("POST", "/test-url", nil)
r.PostForm = postData
form = New(r.PostForm)
form.Required("a", "b", "c")
if !form.Valid() {
t.Error("required fields are given, should be valid")
}
}
func TestForms_Has(t *testing.T) {
postData := url.Values{}
form := New(postData)
if form.Has("a") {
t.Error("the field should not exist")
}
postData = url.Values{}
postData.Add("a", "a")
form = New(postData)
if !form.Has("a") {
t.Error("the field should exist")
}
}
func TestForms_MinLength(t *testing.T) {
postData := url.Values{}
form := New(postData)
if form.MinLength("a", 3) {
t.Error("the field should not exist")
}
errMsg := form.Errors.Get("a")
if errMsg != "This field must have at least 3 letters" {
t.Error("should have an errMsg")
}
if form.Valid() {
t.Error("should be invalid")
}
postData = url.Values{}
postData.Add("a", "ab")
postData.Add("b", "abc")
form = New(postData)
if form.MinLength("a", 3) {
t.Error("the field is shorter than 3 chars")
}
if !form.MinLength("b", 3) {
t.Error("the field is equal to 3 chars")
}
if form.Valid() {
t.Error("should be invalid")
}
}
func TestForms_IsEmail(t *testing.T) {
postData := url.Values{}
form := New(postData)
form.IsEmail("a")
if form.Valid() {
t.Error("email should not exit")
}
postData = url.Values{}
postData.Add("a", "a@a.com")
form = New(postData)
form.IsEmail("a")
errMsg := form.Errors.Get("a")
if errMsg != "" {
t.Error("should not have an errMsg")
}
if !form.Valid() {
t.Error("should be a valid email")
}
postData = url.Values{}
postData.Add("a", "a@.com")
form = New(postData)
form.IsEmail("a")
if form.Valid() {
t.Error("Should not be a valid email")
}
}

View File

@ -100,8 +100,8 @@ func (m *Repository) PostMakeReservation(w http.ResponseWriter, r *http.Request)
form := forms.New(r.PostForm) form := forms.New(r.PostForm)
form.Required("first_name", "last_name", "email") form.Required("first_name", "last_name", "email")
form.MinLength("first_name", 2, r) form.MinLength("first_name", 2)
form.IsEmail("email", r) form.IsEmail("email")
if !form.Valid() { if !form.Valid() {
data := make(map[string]interface{}) data := make(map[string]interface{})