From d723895f5dd963906de89110da5da9f15237df55 Mon Sep 17 00:00:00 2001 From: Azareal Date: Thu, 20 Feb 2020 21:28:49 +1000 Subject: [PATCH] remove polls from poll cache on deletion add test for poll castvote and delete --- common/poll.go | 1 + common/poll_store.go | 2 +- misc_test.go | 18 +++++++++++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/common/poll.go b/common/poll.go index df45923a..c607f405 100644 --- a/common/poll.go +++ b/common/poll.go @@ -50,6 +50,7 @@ func (p *Poll) Delete() error { return err } _, err = pollStmts.deletePoll.Exec(p.ID) + _ = Polls.GetCache().Remove(p.ID) return err } diff --git a/common/poll_store.go b/common/poll_store.go index cf1253fa..53f4a7d8 100644 --- a/common/poll_store.go +++ b/common/poll_store.go @@ -207,11 +207,11 @@ func (s *DefaultPollStore) unpackOptionsMap(rawOptions map[int]string) []PollOpt // TODO: Use a transaction for this func (s *DefaultPollStore) Create(parent Pollable, pollType int, pollOptions map[int]string) (id int, err error) { + // TODO: Move the option names into the polls_options table and get rid of this json sludge? pollOptionsTxt, err := json.Marshal(pollOptions) if err != nil { return 0, err } - res, err := s.createPoll.Exec(parent.GetID(), parent.GetTable(), pollType, pollOptionsTxt) if err != nil { return 0, err diff --git a/misc_test.go b/misc_test.go index 8c0f25ab..21dee227 100644 --- a/misc_test.go +++ b/misc_test.go @@ -1269,6 +1269,7 @@ func TestPolls(t *testing.T) { pid, err := c.Polls.Create(topic, pollType, map[int]string{0: "item 1", 1: "item 2", 2: "item 3"}) expectNilErr(t, err) expect(t, pid == 1, fmt.Sprintf("poll id should be 1 not %d", pid)) + expect(t, c.Polls.Exists(1), "poll 1 should exist") p, err := c.Polls.Get(1) expectNilErr(t, err) expect(t, p.ID == 1, fmt.Sprintf("p.ID should be 1 not %d", p.ID)) @@ -1279,7 +1280,22 @@ func TestPolls(t *testing.T) { // TODO: More fields expect(t, p.VoteCount == 0, fmt.Sprintf("p.VoteCount should be 0 not %d", p.VoteCount)) - // TODO: More tests + expectNilErr(t, p.CastVote(0, 1, "")) + expectNilErr(t, c.Polls.Reload(p.ID)) + p, err = c.Polls.Get(1) + expectNilErr(t, err) + expect(t, p.ID == 1, fmt.Sprintf("p.ID should be 1 not %d", p.ID)) + expect(t, p.ParentID == tid, fmt.Sprintf("p.ParentID should be %d not %d", tid, p.ParentID)) + expect(t, p.ParentTable == "topics", fmt.Sprintf("p.ParentID should be %s not %s", "topics", p.ParentTable)) + expect(t, p.Type == 0, fmt.Sprintf("p.ParentID should be 0 not %d", p.Type)) + expect(t, !p.AntiCheat, "p.AntiCheat should be false") + // TODO: More fields + expect(t, p.VoteCount == 1, fmt.Sprintf("p.VoteCount should be 1 not %d", p.VoteCount)) + + expectNilErr(t, p.Delete()) + expect(t, !c.Polls.Exists(1), "poll 1 should no longer exist") + _, err = c.Polls.Get(1) + recordMustNotExist(t, err, "poll 1 should no longer exist") } func TestProfileReplyStore(t *testing.T) {