refactor and unit test filters
parent
b47327dfe6
commit
cb73169eeb
|
|
@ -13,21 +13,25 @@ import (
|
||||||
|
|
||||||
func (a *Ajax) loadTasks(w http.ResponseWriter, r *http.Request) error {
|
func (a *Ajax) loadTasks(w http.ResponseWriter, r *http.Request) error {
|
||||||
listID, _, _ := a.Cur(r)
|
listID, _, _ := a.Cur(r)
|
||||||
filterComplete := func(t *task.Task) bool {
|
filterComplete := filterComplete(form.Get(r, "compl"))
|
||||||
if form.Get(r, "compl") == "" {
|
filterTags := filterTags(form.ToStrArr(form.Get(r, "t")))
|
||||||
return true
|
filterSubstr := filterSubstr(form.Get(r, "s"))
|
||||||
|
tasks, err := a.storageListTasks(listID, filterComplete, filterTags, filterSubstr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
return form.Get(r, "compl") == "1" || !t.Complete
|
return json.NewEncoder(w).Encode(map[string]interface{}{"list": tasks})
|
||||||
|
}
|
||||||
|
|
||||||
|
func filterComplete(compl string) func(t *task.Task) bool {
|
||||||
|
return func(t *task.Task) bool {
|
||||||
|
return compl == "" || (compl == "0" && !t.Complete) || (compl == "1" && t.Complete)
|
||||||
}
|
}
|
||||||
filterTags := func(t *task.Task) bool {
|
}
|
||||||
if form.Get(r, "t") == "" {
|
|
||||||
return true
|
func filterTags(tags []string) func(t *task.Task) bool {
|
||||||
}
|
return func(t *task.Task) bool {
|
||||||
whitelistTags := form.ToStrArr(form.Get(r, "t"))
|
for _, whitelisted := range tags {
|
||||||
if len(whitelistTags) == 0 {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
for _, whitelisted := range whitelistTags {
|
|
||||||
found := false
|
found := false
|
||||||
for _, tag := range t.Tags {
|
for _, tag := range t.Tags {
|
||||||
if whitelisted == tag {
|
if whitelisted == tag {
|
||||||
|
|
@ -40,15 +44,12 @@ func (a *Ajax) loadTasks(w http.ResponseWriter, r *http.Request) error {
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
filterSubstr := func(t *task.Task) bool {
|
}
|
||||||
substr := form.Get(r, "s")
|
|
||||||
|
func filterSubstr(substr string) func(t *task.Task) bool {
|
||||||
|
return func(t *task.Task) bool {
|
||||||
return substr == "" || strings.Contains(fmt.Sprintf("%+v", t), substr)
|
return substr == "" || strings.Contains(fmt.Sprintf("%+v", t), substr)
|
||||||
}
|
}
|
||||||
tasks, err := a.storageListTasks(listID, filterComplete, filterTags, filterSubstr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return json.NewEncoder(w).Encode(map[string]interface{}{"list": tasks})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Ajax) newTask(w http.ResponseWriter, r *http.Request) error {
|
func (a *Ajax) newTask(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package ajax
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"local/todo-server/server/ajax/form"
|
||||||
"local/todo-server/server/ajax/task"
|
"local/todo-server/server/ajax/task"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
|
@ -207,3 +208,48 @@ func TestAjaxChangeOrder(t *testing.T) {
|
||||||
t.Error(tasks[2])
|
t.Error(tasks[2])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFilterComplete(t *testing.T) {
|
||||||
|
task := &task.Task{Complete: false}
|
||||||
|
cases := []struct {
|
||||||
|
query string
|
||||||
|
out bool
|
||||||
|
}{
|
||||||
|
{query: "", out: true},
|
||||||
|
{query: "0", out: true},
|
||||||
|
{query: "1", out: false},
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, c := range cases {
|
||||||
|
out := filterComplete(c.query)
|
||||||
|
if out(task) != c.out {
|
||||||
|
t.Errorf("[%d] want %v, got %v", name, c.out, out(task))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFilterTags(t *testing.T) {
|
||||||
|
cases := map[string]struct {
|
||||||
|
query string
|
||||||
|
tags []string
|
||||||
|
out bool
|
||||||
|
}{
|
||||||
|
"no filter": {query: "", out: true},
|
||||||
|
"single matching filter": {query: "a", out: true, tags: []string{"a"}},
|
||||||
|
"single non-matching filter": {query: "a", out: false, tags: []string{"b"}},
|
||||||
|
"duo matching filter": {query: "a, b", out: true, tags: []string{"b", "a"}},
|
||||||
|
"duo partial-matching filter": {query: "a, c", out: false, tags: []string{"b", "a"}},
|
||||||
|
"duo non-matching filter": {query: "a, c", out: false, tags: []string{"d", "e"}},
|
||||||
|
"trio matching filter": {query: "a, b, c", out: true, tags: []string{"b", "a", "c", "d", "e"}},
|
||||||
|
"trio partial-matching filter": {query: "a, c, d", out: false, tags: []string{"b", "a", "d"}},
|
||||||
|
"trio non-matching filter": {query: "a, b, c", out: false, tags: []string{"x", "y", "z"}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, c := range cases {
|
||||||
|
task := &task.Task{Tags: c.tags}
|
||||||
|
out := filterTags(form.ToStrArr(c.query))
|
||||||
|
if v := out(task); v != c.out {
|
||||||
|
t.Errorf("[%s] want %v, got %v", name, c.out, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue