possibly first working draft
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"donniemarko/internal/note"
|
||||
@ -14,56 +13,36 @@ func TestMain(m *testing.M) {
|
||||
storage := storage.NewNoteStorage()
|
||||
|
||||
notes := []*note.Note{
|
||||
{Title: "Golang Tutorial", Content: "Learn Go"},
|
||||
{Title: "Rust Guide", Content: "Learn Rust"},
|
||||
{ID: "test1", Title: "Golang Tutorial", Content: "Learn Go"},
|
||||
{ID: "test2", Title: "Rust Guide", Content: "Learn Rust"},
|
||||
}
|
||||
for _, note := range notes {
|
||||
storage.Create(note)
|
||||
}
|
||||
|
||||
service = NewNoteService()
|
||||
service.storage = storage
|
||||
service.SetStorage(storage)
|
||||
m.Run()
|
||||
}
|
||||
|
||||
func TestQueryNotes_WithSearch(t *testing.T) {
|
||||
|
||||
opts := QueryOptions{
|
||||
SearchTerm: "golang",
|
||||
SortBy: "alpha",
|
||||
}
|
||||
|
||||
results, err := service.QueryNotes(opts)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(results) != 1 {
|
||||
t.Errorf("expected 1 result, got %d", len(results))
|
||||
}
|
||||
|
||||
if results[0].Title != "Golang Tutorial" {
|
||||
t.Error("wrong note returned")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandler_BuildViewState(t *testing.T) {
|
||||
handler := NewHandler(service, nil)
|
||||
|
||||
req := httptest.NewRequest("GET", "/?search=test&sort=alpha", nil)
|
||||
|
||||
state, err := handler.buildViewState(req)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if state.SearchTerm != "test" {
|
||||
t.Error("search term not extracted")
|
||||
}
|
||||
|
||||
if state.SortBy != "alpha" {
|
||||
t.Error("sort option not extracted")
|
||||
}
|
||||
opts := QueryOptions{
|
||||
SearchTerm: "Go",
|
||||
SortBy: "alpha",
|
||||
}
|
||||
|
||||
results, err := service.QueryNotes(opts)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(results) != 1 {
|
||||
t.Errorf("expected 1 result, got %d", len(results))
|
||||
}
|
||||
|
||||
if results[0].Title != "Golang Tutorial" {
|
||||
t.Error("wrong note returned")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user