Fix missing error checks and test

This commit is contained in:
Balakrishnan Balasubramanian 2022-12-28 01:36:04 -05:00
parent f80aae9e03
commit a30aae9bf4
3 changed files with 14 additions and 12 deletions

View File

@ -86,15 +86,15 @@ outer:
PersistReqC <- g PersistReqC <- g
} }
func (g *GList) Toggle(text string) error { func (g *GList) Toggle(text string) {
for i, item := range g.Items { for i, item := range g.Items {
if item.Text == text { if item.Text == text {
g.Items[i].Checked = !g.Items[i].Checked g.Items[i].Checked = !g.Items[i].Checked
PersistReqC <- g 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() { func (g *GList) ClearChecked() {

View File

@ -2,34 +2,32 @@ package glist
import ( import (
"fmt" "fmt"
"sync"
"testing" "testing"
) )
func TestGList(t *testing.T) { func TestGList(t *testing.T) {
var ti int g := NewGList(4342, "foo")
var m sync.Mutex
g := GList{ti, m, 4342, nil, []Entry{{"foo", true}}}
data, err := g.GenSendListReq() data, err := g.GenSendListReq()
if err != nil { if err != nil {
t.Fatal(err) 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) { if expected != string(data) {
t.Fatalf("expected: %s\n got:%s\n", expected, string(data)) t.Fatalf("expected: %s\n got:%s\n", expected, string(data))
} }
} }
func TestSplit(t *testing.T) { func TestSplit(t *testing.T) {
g := GList{} g := &GList{}
//This resets the channel, so test does not try to persist //This resets the channel, so test does not try to persist
PersistReqC = make(chan *GList, 50) PersistReqC = make(chan *GList, 50)
g.Add("foo") g.Add("foo")
g.Add("bar\nfoo\nblah") g.Add("bar\nfoo\nblah")
g.Add("foo") g.Add("foo")
g.Add("lskfj") g.Add("lskfj")
expected := `{0 {0 0} 0 <nil> [{foo false} {bar false} {blah false} {lskfj false}]}` g.Toggle("foo")
expected := `&{0 {0 0} 0 <nil> [{foo true} {bar false} {blah false} {lskfj false}]}`
actual := fmt.Sprintf("%v", g) actual := fmt.Sprintf("%v", g)
if expected != actual { if expected != actual {
t.Fatalf("expected: %s\n got:%s\n", expected, actual) t.Fatalf("expected: %s\n got:%s\n", expected, actual)

View File

@ -70,7 +70,11 @@ func main() {
} }
botPath := fmt.Sprintf("/bot%s", apiToken) botPath := fmt.Sprintf("/bot%s", apiToken)
http.HandleFunc(botPath, func(w http.ResponseWriter, r *http.Request) { 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) body, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
@ -118,7 +122,7 @@ func main() {
chatID := update.CallbackQuery.Message.Chat.ID chatID := update.CallbackQuery.Message.Chat.ID
g, ok := chats.Load(chatID) g, ok := chats.Load(chatID)
if !ok { if !ok {
log.Println("Chat not found: %s", chatID) log.Printf("Chat not found: %d\n", chatID)
return return
} }
gl := g.(*glist.GList) gl := g.(*glist.GList)