-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update form functions and add form tests
- Loading branch information
Showing
3 changed files
with
133 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package forms | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
) | ||
|
||
func TestForm_Valid(t *testing.T) { | ||
r := httptest.NewRequest("POST", "/whatever", nil) | ||
form := New(r.PostForm) | ||
|
||
isValid := form.Valid() | ||
if !isValid { | ||
t.Error("got invalid when should have been valid") | ||
} | ||
} | ||
|
||
func TestForm_Required(t *testing.T) { | ||
r := httptest.NewRequest("POST", "/whatever", nil) | ||
form := New(r.PostForm) | ||
|
||
form.Required("a", "b", "c") | ||
if form.Valid() { | ||
t.Error("form shows valid when required fields missing") | ||
} | ||
|
||
postedData := url.Values{} | ||
postedData.Add("a", "a") | ||
postedData.Add("b", "a") | ||
postedData.Add("c", "a") | ||
|
||
r, _ = http.NewRequest("POST", "/whatever", nil) | ||
|
||
r.PostForm = postedData | ||
form = New(r.PostForm) | ||
form.Required("a", "b", "c") | ||
if !form.Valid() { | ||
t.Error("shows does not have required fields when it does") | ||
} | ||
} | ||
|
||
func TestForm_Has(t *testing.T) { | ||
r := httptest.NewRequest("POST", "/whatever", nil) | ||
form := New(r.PostForm) | ||
|
||
has := form.Has("whatever") | ||
if has { | ||
t.Error("form shows has field when it does not") | ||
} | ||
|
||
postedData := url.Values{} | ||
postedData.Add("a", "a") | ||
form = New(postedData) | ||
|
||
has = form.Has("a") | ||
if !has { | ||
t.Error("shows form does not have field when it should") | ||
} | ||
} | ||
|
||
func TestForm_MinLength(t *testing.T) { | ||
r := httptest.NewRequest("POST", "/whatever", nil) | ||
form := New(r.PostForm) | ||
|
||
form.MinLength("x", 10) | ||
if form.Valid() { | ||
t.Error("form shows min length for non-existent field") | ||
} | ||
|
||
isError := form.Errors.Get("x") | ||
if isError == "" { | ||
t.Error("should have an error, but did not get one") | ||
} | ||
|
||
postedValues := url.Values{} | ||
postedValues.Add("some_field", "some value") | ||
form = New(postedValues) | ||
|
||
form.MinLength("some_field", 100) | ||
if form.Valid() { | ||
t.Error("form shows min length of 100 met when data is shorter") | ||
} | ||
|
||
postedValues = url.Values{} | ||
postedValues.Add("another_field", "abc123") | ||
form = New(postedValues) | ||
|
||
form.MinLength("another_field", 1) | ||
if !form.Valid() { | ||
t.Error("form shows min length of 1 is not met when it is") | ||
} | ||
|
||
isError = form.Errors.Get("another_field") | ||
if isError != "" { | ||
t.Error("should not have an error, but got one") | ||
} | ||
} | ||
|
||
func TestForm_IsEmail(t *testing.T) { | ||
postedValues := url.Values{} | ||
form := New(postedValues) | ||
|
||
form.IsEmail("not_email_field") | ||
if form.Valid() { | ||
t.Error("form shows valid email for non-existent field") | ||
} | ||
|
||
postedValues = url.Values{} | ||
postedValues.Add("invalid_email", "abc") | ||
form = New(postedValues) | ||
|
||
form.IsEmail("invalid_email") | ||
if form.Valid() { | ||
t.Error("form shows valid email when it does not") | ||
} | ||
|
||
postedValues = url.Values{} | ||
postedValues.Add("valid_email", "[email protected]") | ||
form = New(postedValues) | ||
|
||
form.IsEmail("valid_email") | ||
if !form.Valid() { | ||
t.Error("form shows invalid email when it does") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package forms |