From a30aae9bf450c2b01817990c17aca191a23a5d42 Mon Sep 17 00:00:00 2001 From: Balakrishnan Balasubramanian Date: Wed, 28 Dec 2022 01:36:04 -0500 Subject: [PATCH] Fix missing error checks and test --- glist/glist.go | 6 +++--- glist/glist_test.go | 12 +++++------- main.go | 8 ++++++-- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/glist/glist.go b/glist/glist.go index e912e17..4919362 100644 --- a/glist/glist.go +++ b/glist/glist.go @@ -86,15 +86,15 @@ outer: PersistReqC <- g } -func (g *GList) Toggle(text string) error { +func (g *GList) Toggle(text string) { for i, item := range g.Items { if item.Text == text { g.Items[i].Checked = !g.Items[i].Checked PersistReqC <- g - return nil + return } } - return fmt.Errorf("not found:%s", text) + log.Printf("item not found in toggle, chat: %d, item: %s\n", g.ChatID, text) } func (g *GList) ClearChecked() { diff --git a/glist/glist_test.go b/glist/glist_test.go index f96d300..22ec34c 100644 --- a/glist/glist_test.go +++ b/glist/glist_test.go @@ -2,34 +2,32 @@ package glist import ( "fmt" - "sync" "testing" ) func TestGList(t *testing.T) { - var ti int - var m sync.Mutex - g := GList{ti, m, 4342, nil, []Entry{{"foo", true}}} + g := NewGList(4342, "foo") data, err := g.GenSendListReq() if err != nil { t.Fatal(err) } - expected := `{"chat_id":4342,"text":"List:","reply_markup":{"inline_keyboard":[[{"text":"✓ foo","callback_data":"foo"}],[{"text":"clear checked","callback_data":"clear"}]]}}` + expected := `{"chat_id":4342,"text":"List:","reply_markup":{"inline_keyboard":[[{"text":"foo","callback_data":"foo"}],[{"text":"clear checked","callback_data":"clear"}]]}}` if expected != string(data) { t.Fatalf("expected: %s\n got:%s\n", expected, string(data)) } } func TestSplit(t *testing.T) { - g := GList{} + g := &GList{} //This resets the channel, so test does not try to persist PersistReqC = make(chan *GList, 50) g.Add("foo") g.Add("bar\nfoo\nblah") g.Add("foo") g.Add("lskfj") - expected := `{0 {0 0} 0 [{foo false} {bar false} {blah false} {lskfj false}]}` + g.Toggle("foo") + expected := `&{0 {0 0} 0 [{foo true} {bar false} {blah false} {lskfj false}]}` actual := fmt.Sprintf("%v", g) if expected != actual { t.Fatalf("expected: %s\n got:%s\n", expected, actual) diff --git a/main.go b/main.go index 1b4a74f..474211e 100644 --- a/main.go +++ b/main.go @@ -70,7 +70,11 @@ func main() { } botPath := fmt.Sprintf("/bot%s", apiToken) http.HandleFunc(botPath, func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("ok")) + _, err := w.Write([]byte("ok")) + if err != nil { + log.Println(err) + return + } body, err := io.ReadAll(r.Body) if err != nil { log.Println(err) @@ -118,7 +122,7 @@ func main() { chatID := update.CallbackQuery.Message.Chat.ID g, ok := chats.Load(chatID) if !ok { - log.Println("Chat not found: %s", chatID) + log.Printf("Chat not found: %d\n", chatID) return } gl := g.(*glist.GList)