Fixing up the go code, adding initial work for the stats pages, adding a function to pre-populate the database with some example polls for testing. Will be removed later

This commit is contained in:
2026-01-25 19:45:56 -05:00
parent 8b5940741c
commit b39a094d92
11 changed files with 821 additions and 337 deletions

View File

@@ -13,8 +13,8 @@ import (
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-=`~!@#$%^&*()_+[]\\;',./{}|:\"<>?"
var new_members = []struct{
email string
var new_members = []struct {
email string
member_name string
}{
{"test1@mail.me", "test1"},
@@ -32,20 +32,20 @@ var new_members = []struct{
{"test105@mail.me", "test105"},
}
var new_polls = []struct{
question string
member_yes_votes int64
member_no_votes int64
var new_polls = []struct {
question string
member_yes_votes int64
member_no_votes int64
non_member_yes_votes int64
non_member_no_votes int64
non_member_no_votes int64
}{
{"ques1", 1, 2, 3, 4},
{"ques2", 3, 2, 4, 5},
{"ques3", 4, 3, 6, 5},
}
var new_voters = []struct{
poll_id int64
var new_voters = []struct {
poll_id int64
voter_email string
}{
{1, "test1@mail.me"},
@@ -117,7 +117,7 @@ func PreLoadDB() error {
// Insert polls
for i := range new_polls {
result, err := db_conn.Exec(`INSERT INTO polls (question, member_yes_votes, member_no_votes, non_member_yes_votes, non_member_no_votes, expires_at) VALUES (?, ?, ?, ?, ?, ?)`, new_polls[i].question, new_polls[i].member_yes_votes, new_polls[i].member_no_votes, new_polls[i].non_member_yes_votes, new_polls[i].non_member_no_votes, time.Now().Add(time.Hour * 10).Format("2006-01-02 15:04:05"))
result, err := db_conn.Exec(`INSERT INTO polls (question, member_yes_votes, member_no_votes, non_member_yes_votes, non_member_no_votes, expires_at) VALUES (?, ?, ?, ?, ?, ?)`, new_polls[i].question, new_polls[i].member_yes_votes, new_polls[i].member_no_votes, new_polls[i].non_member_yes_votes, new_polls[i].non_member_no_votes, time.Now().Add(time.Hour*10).Format("2006-01-02 15:04:05"))
if err != nil {
return err
}
@@ -139,8 +139,8 @@ func PreLoadDB() error {
}
func TestCreatePoll(t *testing.T) {
parameters := []struct{
question string
parameters := []struct {
question string
table_index int64
}{
{RandString(10) + "1", 1},
@@ -165,29 +165,29 @@ func TestCreatePoll(t *testing.T) {
defer os.Remove(tmp_db.Name())
tmp_db.Close()
if _, err := db.Connect(); err != nil {
t.Errorf(`Failed to create the database: %v`, err)
}
for i := range parameters {
create_poll := &models.Poll{
Question: parameters[i].question,
Question: parameters[i].question,
ExpiresAt: time.Now().Add(time.Hour * 10).Format("2006-01-02 15:04:05"),
}
new_poll, err := CreatePoll(create_poll)
new_poll_id, err := CreatePoll(create_poll)
if err != nil {
t.Fatalf(`Failed to create new poll %s: %v`, parameters[i].question, err)
t.Errorf(`Failed to create new poll %s: %v`, parameters[i].question, err)
}
if new_poll == nil {
t.Fatalf(`Failed to insert %s into table`, parameters[i].question)
if new_poll_id == -1 {
t.Errorf(`Failed to insert %s into table`, parameters[i].question)
}
if new_poll.ID != parameters[i].table_index {
t.Fatalf(`Incorrect increment in index for %s: expected %d != %d`, parameters[i].question, parameters[i].table_index, new_poll.ID)
if new_poll_id != parameters[i].table_index {
t.Errorf(`Incorrect increment in index for %s: expected %d != %d`, parameters[i].question, parameters[i].table_index, new_poll_id)
}
}
}
@@ -207,30 +207,30 @@ func TestAlreadyExists(t *testing.T) {
defer os.Remove(tmp_db.Name())
tmp_db.Close()
if _, err := db.Connect(); err != nil {
t.Errorf(`Failed to create the database: %v`, err)
}
create_poll := &models.Poll{
Question: question,
Question: question,
ExpiresAt: time.Now().Add(time.Hour * 10).Format("2006-01-02 15:04:05"),
}
new_poll, err := CreatePoll(create_poll)
if err != nil {
t.Fatalf(`Failed to create new poll %s: %v`, question, err)
t.Errorf(`Failed to create new poll %s: %v`, question, err)
}
if new_poll == nil {
t.Fatalf(`Failed to insert %s into table`, question)
if new_poll == -1 {
t.Errorf(`Failed to insert %s into table`, question)
}
new_poll, err = CreatePoll(create_poll)
if err != ErrQuestionAlreadyExists {
t.Fatalf(`Should have failed adding %s as it already exists`, question)
t.Errorf(`Should have failed adding %s as it already exists`, question)
}
}
@@ -249,40 +249,40 @@ func TestGetPollByQuestion(t *testing.T) {
defer os.Remove(tmp_db.Name())
tmp_db.Close()
if _, err := db.Connect(); err != nil {
t.Errorf(`Failed to create the database: %v`, err)
}
create_poll := &models.Poll{
Question: question,
Question: question,
ExpiresAt: time.Now().Add(time.Hour * 10).Format("2006-01-02 15:04:05"),
}
new_poll, err := CreatePoll(create_poll)
if err != nil {
t.Fatalf(`Failed to create new poll %s: %v`, question, err)
t.Errorf(`Failed to create new poll %s: %v`, question, err)
}
if new_poll == nil {
t.Fatalf(`Failed to insert %s into table`, question)
if new_poll == -1 {
t.Errorf(`Failed to insert %s into table`, question)
}
get_poll, err := GetPollByQuestion(question)
if err != nil {
t.Fatalf(`Failed to get the poll %s: %v`, question, err)
t.Errorf(`Failed to get the poll %s: %v`, question, err)
}
if get_poll.Question != question {
t.Fatalf(`Questions don't match: expected %s: recieved %s`, question, get_poll.Question)
t.Errorf(`Questions don't match: expected %s: recieved %s`, question, get_poll.Question)
}
}
func TestGetCreatePollByQuestion(t *testing.T) {
parameters := []struct{
question string
parameters := []struct {
question string
table_index int64
}{
{RandString(10) + "1", 1},
@@ -305,7 +305,7 @@ func TestGetCreatePollByQuestion(t *testing.T) {
defer os.Remove(tmp_db.Name())
tmp_db.Close()
if _, err := db.Connect(); err != nil {
t.Errorf(`Failed to create the database: %v`, err)
}
@@ -314,184 +314,184 @@ func TestGetCreatePollByQuestion(t *testing.T) {
new_poll, err := GetAndCreatePollByQuestion(parameters[i].question)
if err != nil {
t.Fatalf(`Failed to create new poll %s: %v`, parameters[i].question, err)
t.Errorf(`Failed to create new poll %s: %v`, parameters[i].question, err)
}
if new_poll == nil {
t.Fatalf(`Failed to insert %s into table`, parameters[i].question)
t.Errorf(`Failed to insert %s into table`, parameters[i].question)
}
if new_poll.ID != parameters[i].table_index {
t.Fatalf(`Incorrect increment in index for %s: expected %d != %d`, parameters[i].question, parameters[i].table_index, new_poll.ID)
t.Errorf(`Incorrect increment in index for %s: expected %d != %d`, parameters[i].question, parameters[i].table_index, new_poll.ID)
}
if new_poll.Question != parameters[i].question {
t.Fatalf(`Incorrect question returned: Expected %s != %s`, parameters[i].question, new_poll.Question)
t.Errorf(`Incorrect question returned: Expected %s != %s`, parameters[i].question, new_poll.Question)
}
}
}
func TestSetVote(t *testing.T) {
// Preload the database with members, polls, and voters
tmp_db, err := os.CreateTemp("", "vote_test.*.db")
if err != nil {
t.Fatalf("Failed to create temporary database: %v", err)
}
defer os.Remove(tmp_db.Name())
// Preload the database with members, polls, and voters
tmp_db, err := os.CreateTemp("", "vote_test.*.db")
if err != nil {
t.Errorf("Failed to create temporary database: %v", err)
}
defer os.Remove(tmp_db.Name())
init_conf := &config.Config{
DBPath: string(tmp_db.Name()),
}
config.SetConfig(init_conf)
init_conf := &config.Config{
DBPath: string(tmp_db.Name()),
}
config.SetConfig(init_conf)
err = PreLoadDB()
if err != nil {
t.Fatalf("Failed to preload database: %v", err)
}
err = PreLoadDB()
if err != nil {
t.Errorf("Failed to preload database: %v", err)
}
// Add a non-member vote
random_email := RandString(10) + "@mail.me"
vote := &models.Vote{
PollId: 1,
Email: random_email,
Vote: true,
}
err = SetVote(vote)
if err != nil {
t.Fatalf("Failed to set non-member vote: %v", err)
}
// Add a non-member vote
random_email := RandString(10) + "@mail.me"
vote := &models.Vote{
PollId: 1,
Email: random_email,
Vote: true,
}
err = SetVote(vote)
if err != nil {
t.Errorf("Failed to set non-member vote: %v", err)
}
// Add a member vote
member_email := "test100@mail.me"
vote = &models.Vote{
PollId: 1,
Email: member_email,
Vote: true,
}
err = SetVote(vote)
if err != nil {
t.Fatalf("Failed to set member vote: %v", err)
}
// Add a member vote
member_email := "test100@mail.me"
vote = &models.Vote{
PollId: 1,
Email: member_email,
Vote: true,
}
err = SetVote(vote)
if err != nil {
t.Errorf("Failed to set member vote: %v", err)
}
// Verify the votes were added correctly
voters, err := models.GetVoters(1) // Use GetVoters from models
if err != nil {
t.Fatalf("Failed to get voters: %v", err)
}
// Verify the votes were added correctly
voters, err := models.GetVoters(1) // Use GetVoters from models
if err != nil {
t.Errorf("Failed to get voters: %v", err)
}
expected_non_member_votes := 4 + 1 // Original non-member votes + new non-member vote
expected_member_votes := 3 + 1 // Original member votes + new member vote
expected_non_member_votes := 4 + 1 // Original non-member votes + new non-member vote
expected_member_votes := 3 + 1 // Original member votes + new member vote
for _, voter := range voters {
if voter.Email == random_email && voter.YesVote {
expected_non_member_votes--
} else if voter.Email == member_email && voter.YesVote {
expected_member_votes--
}
}
for _, voter := range voters {
if voter.Email == random_email && voter.YesVote {
expected_non_member_votes--
} else if voter.Email == member_email && voter.YesVote {
expected_member_votes--
}
}
if expected_non_member_votes != 5 || expected_member_votes != 4 {
t.Errorf("Expected %d non-member votes and %d member votes, but got %d non-member votes and %d member votes", 4+1, 3+1, expected_non_member_votes, expected_member_votes)
}
if expected_non_member_votes != 5 || expected_member_votes != 4 {
t.Errorf("Expected %d non-member votes and %d member votes, but got %d non-member votes and %d member votes", 4+1, 3+1, expected_non_member_votes, expected_member_votes)
}
}
func TestVoterAlreadyVoted(t *testing.T) {
// Preload the database with members, polls, and voters
tmp_db, err := os.CreateTemp("", "vote_test.*.db")
if err != nil {
t.Fatalf("Failed to create temporary database: %v", err)
}
defer os.Remove(tmp_db.Name())
// Preload the database with members, polls, and voters
tmp_db, err := os.CreateTemp("", "vote_test.*.db")
if err != nil {
t.Errorf("Failed to create temporary database: %v", err)
}
defer os.Remove(tmp_db.Name())
init_conf := &config.Config{
DBPath: string(tmp_db.Name()),
}
config.SetConfig(init_conf)
init_conf := &config.Config{
DBPath: string(tmp_db.Name()),
}
config.SetConfig(init_conf)
err = PreLoadDB()
if err != nil {
t.Fatalf("Failed to preload database: %v", err)
}
err = PreLoadDB()
if err != nil {
t.Errorf("Failed to preload database: %v", err)
}
// Add a non-member vote
random_email := RandString(10) + "@mail.me"
vote := &models.Vote{
PollId: 1,
Email: random_email,
Vote: true,
}
err = SetVote(vote)
if err != nil {
t.Fatalf("Failed to set non-member vote: %v", err)
}
// Add a non-member vote
random_email := RandString(10) + "@mail.me"
vote := &models.Vote{
PollId: 1,
Email: random_email,
Vote: true,
}
err = SetVote(vote)
if err != nil {
t.Errorf("Failed to set non-member vote: %v", err)
}
// Add a member vote
member_email := "test100@mail.me"
vote = &models.Vote{
PollId: 1,
Email: member_email,
Vote: true,
}
err = SetVote(vote)
if err != nil {
t.Fatalf("Failed to set member vote: %v", err)
}
// Add a member vote
member_email := "test100@mail.me"
vote = &models.Vote{
PollId: 1,
Email: member_email,
Vote: true,
}
err = SetVote(vote)
if err != nil {
t.Errorf("Failed to set member vote: %v", err)
}
// Attempt to add another non-member vote
vote = &models.Vote{
PollId: 1,
Email: random_email,
Vote: true,
}
err = SetVote(vote)
if err != ErrVoterAlreadyVoted {
t.Fatalf("Expected ErrVoterAlreadyVoted, but got %v", err)
}
// Attempt to add another non-member vote
vote = &models.Vote{
PollId: 1,
Email: random_email,
Vote: true,
}
err = SetVote(vote)
if err != ErrVoterAlreadyVoted {
t.Errorf("Expected ErrVoterAlreadyVoted, but got %v", err)
}
// Attempt to add another member vote
vote = &models.Vote{
PollId: 1,
Email: member_email,
Vote: true,
}
err = SetVote(vote)
if err != ErrVoterAlreadyVoted {
t.Fatalf("Expected ErrVoterAlreadyVoted, but got %v", err)
}
// Attempt to add another member vote
vote = &models.Vote{
PollId: 1,
Email: member_email,
Vote: true,
}
err = SetVote(vote)
if err != ErrVoterAlreadyVoted {
t.Errorf("Expected ErrVoterAlreadyVoted, but got %v", err)
}
}
func TestDeletePollByQuestion(t *testing.T) {
// Preload the database with members, polls, and voters
tmp_db, err := os.CreateTemp("", "vote_test.*.db")
if err != nil {
t.Fatalf("Failed to create temporary database: %v", err)
}
defer os.Remove(tmp_db.Name())
// Preload the database with members, polls, and voters
tmp_db, err := os.CreateTemp("", "vote_test.*.db")
if err != nil {
t.Errorf("Failed to create temporary database: %v", err)
}
defer os.Remove(tmp_db.Name())
init_conf := &config.Config{
DBPath: string(tmp_db.Name()),
}
config.SetConfig(init_conf)
init_conf := &config.Config{
DBPath: string(tmp_db.Name()),
}
config.SetConfig(init_conf)
err = PreLoadDB()
if err != nil {
t.Fatalf("Failed to preload database: %v", err)
}
err = PreLoadDB()
if err != nil {
t.Errorf("Failed to preload database: %v", err)
}
// Get a question from the new_polls array
testQuestion := new_polls[0].question
// Get a question from the new_polls array
testQuestion := new_polls[0].question
// Delete the poll by question
err = DeletePollByQuestion(testQuestion)
if err != nil {
t.Fatalf("Failed to delete poll by question: %v", err)
}
// Delete the poll by question
err = DeletePollByQuestion(testQuestion)
if err != nil {
t.Errorf("Failed to delete poll by question: %v", err)
}
// Verify that the poll was deleted
_, err = GetPollByQuestion(testQuestion)
if err == nil {
t.Fatalf("Expected error when getting deleted poll, but got none")
} else if err != ErrPollNotFound {
t.Fatalf("Expected ErrPollNotFound, but got %v", err)
}
}
// Verify that the poll was deleted
_, err = GetPollByQuestion(testQuestion)
if err == nil {
t.Errorf("Expected error when getting deleted poll, but got none")
} else if err != ErrPollNotFound {
t.Errorf("Expected ErrPollNotFound, but got %v", err)
}
}