Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions ballot/GetCandidatesVote_152da079ca_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"sync"
"testing"
)

var once sync.Once
var candidateVotesStore map[string]int

func getCandidatesVote() map[string]int {
once.Do(func() {
candidateVotesStore = make(map[string]int)
})
return candidateVotesStore
}

func TestGetCandidatesVote_152da079ca(t *testing.T) {
// Test case 1: Test the function when the candidateVotesStore is not initialized.
candidateVotesStore = nil
votes := getCandidatesVote()
if votes == nil {
t.Error("Expected a non-nil map, but got nil")
}

// Test case 2: Test the function when the candidateVotesStore is already initialized.
candidateVotesStore = map[string]int{"John": 5, "Doe": 3}
votes = getCandidatesVote()
if len(votes) != 2 {
t.Errorf("Expected a map with 2 elements, but got %d elements", len(votes))
}
if votes["John"] != 5 || votes["Doe"] != 3 {
t.Error("Unexpected vote counts for candidates")
}
}
45 changes: 45 additions & 0 deletions ballot/HttpClientRequest_8fc45b1eff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"bytes"
"errors"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)

func TestHttpClientRequest_8fc45b1eff(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
if req.URL.String() != "/test" {
t.Errorf("request url is wrong, got: %s want: %s", req.URL.String(), "/test")
}
rw.Write([]byte(`OK`))
}))
defer server.Close()

statusCode, body, err := httpClientRequest("GET", server.URL, "/test", nil)
if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
}
if statusCode != http.StatusOK {
t.Errorf("Unexpected status code, got: %d, want: %d", statusCode, http.StatusOK)
}
if string(body) != "OK" {
t.Errorf("Unexpected body, got: %s, want: %s", string(body), "OK")
}
}

func TestHttpClientRequest_8fc45b1eff_Failure(t *testing.T) {
statusCode, _, err := httpClientRequest("GET", "wrong address", "/test", nil)
if err == nil {
t.Error("Expected error, got nil")
}
if !errors.Is(err, err.(*url.Error).Err) {
t.Errorf("Unexpected error, got: %s, want: %s", err.Error(), "Failed to create HTTP request.")
}
if statusCode != http.StatusBadRequest {
t.Errorf("Unexpected status code, got: %d, want: %d", statusCode, http.StatusBadRequest)
}
}
69 changes: 69 additions & 0 deletions ballot/RunTest_13039fcb01_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
)

type Status struct {
Code int
Message string
}

func TestBallot(t *testing.T) {
t.Run("TestBallot success", func(t *testing.T) {
req, err := http.NewRequest("GET", "/test", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
handler := http.HandlerFunc(runTest)

handler.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}

expected := `{"Code":200,"Message":"Test Cases passed"}`
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}
})

t.Run("TestBallot failure", func(t *testing.T) {
req, err := http.NewRequest("GET", "/test", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
handler := http.HandlerFunc(runTest)

handler.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusBadRequest {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusBadRequest)
}

expected := `{"Code":400,"Message":"Test Cases Failed with error : error"}`
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}
})
}

func runTest(w http.ResponseWriter, r *http.Request) {
// TODO: Implement the runTest function
}

func writeVoterResponse(w http.ResponseWriter, status Status) {
w.Write([]byte(`{"Code":` + string(status.Code) + `,"Message":"` + status.Message + `"}`))
}
46 changes: 46 additions & 0 deletions ballot/SaveVote_3a682778fa_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"testing"
)

type Vote struct {
CandidateID string
}

var candidateVotesStore map[string]int

func getCandidatesVote() map[string]int {
if candidateVotesStore == nil {
candidateVotesStore = make(map[string]int)
}
return candidateVotesStore
}

func saveVote(vote Vote) error {
candidateVotesStore = getCandidatesVote()
candidateVotesStore[vote.CandidateID]++
return nil
}

func TestSaveVote_3a682778fa(t *testing.T) {
vote := Vote{CandidateID: "candidate1"}
err := saveVote(vote)
if err != nil {
t.Error("Expected no error, got ", err)
}

if candidateVotesStore[vote.CandidateID] != 1 {
t.Error("Expected 1 vote, got ", candidateVotesStore[vote.CandidateID])
}

// Test with multiple votes
err = saveVote(vote)
if err != nil {
t.Error("Expected no error, got ", err)
}

if candidateVotesStore[vote.CandidateID] != 2 {
t.Error("Expected 2 votes, got ", candidateVotesStore[vote.CandidateID])
}
}
90 changes: 90 additions & 0 deletions ballot/ServeRoot_e6109c0b6f_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)

type Vote struct {
VoterID string
CandidateID string
}

func serveRoot(w http.ResponseWriter, r *http.Request) {
// Mock function to be implemented
}

func TestServeRoot_e6109c0b6f(t *testing.T) {
t.Run("Test GET Method", func(t *testing.T) {
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
handler := http.HandlerFunc(serveRoot)

handler.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}

expected := `{"Results":[{"CandidateID":"1","Votes":2},{"CandidateID":"2","Votes":1}],"TotalVotes":3}`
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
}
})

t.Run("Test POST Method", func(t *testing.T) {
vote := &Vote{
VoterID: "1",
CandidateID: "1",
}
jsonVote, _ := json.Marshal(vote)
req, err := http.NewRequest("POST", "/", bytes.NewBuffer(jsonVote))
if err != nil {
t.Fatal(err)
}
req.Header.Set("Content-Type", "application/json")

rr := httptest.NewRecorder()
handler := http.HandlerFunc(serveRoot)

handler.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusCreated {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusCreated)
}

expected := `{"Code":201,"Message":"Vote saved sucessfully"}`
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
}
})

t.Run("Test Invalid Method", func(t *testing.T) {
req, err := http.NewRequest("PUT", "/", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
handler := http.HandlerFunc(serveRoot)

handler.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusMethodNotAllowed {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusMethodNotAllowed)
}

expected := `{"Code":405,"Message":"Bad Request. Vote can not be saved"}`
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
}
})
}
107 changes: 107 additions & 0 deletions ballot/TestBallot_90aa96f4bb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package main

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net"
"net/http"
"testing"
)

type Response struct {
TotalVotes int
}

type Vote struct {
CandidateID string
VoterID string
}

type Status struct {
Code int
}

func httpClientRequest(method, url, path string, body io.Reader) (int, []byte, error) {
// TODO: Implement this function
return 0, nil, nil
}

func ballotTest() error {
port := "8080" // TODO: Replace with your port
_, result, err := httpClientRequest(http.MethodGet, net.JoinHostPort("", port), "/", nil)
if err != nil {
log.Printf("Failed to get ballot count resp:%s error:%+v", string(result), err)
return err
}
log.Println("get ballot resp:", string(result))
var initalRespData Response
if err = json.Unmarshal(result, &initalRespData); err != nil {
log.Printf("Failed to unmarshal get ballot response. %+v", err)
return err
}

var ballotvotereq Vote
ballotvotereq.CandidateID = fmt.Sprint(rand.Intn(10))
ballotvotereq.VoterID = fmt.Sprint(rand.Intn(10))
reqBuff, err := json.Marshal(ballotvotereq)
if err != nil {
log.Printf("Failed to marshall post ballot request %+v", err)
return err
}

_, result, err = httpClientRequest(http.MethodPost, net.JoinHostPort("", port), "/", bytes.NewReader(reqBuff))
if err != nil {
log.Printf("Failed to get ballot count resp:%s error:%+v", string(result), err)
return err
}
log.Println("post ballot resp:", string(result))
var postballotResp Status
if err = json.Unmarshal(result, &postballotResp); err != nil {
log.Printf("Failed to unmarshal post ballot response. %+v", err)
return err
}
if postballotResp.Code != 201 {
return errors.New("post ballot resp status code")
}

_, result, err = httpClientRequest(http.MethodGet, net.JoinHostPort("", port), "/", nil)
if err != nil {
log.Printf("Failed to get final ballot count resp:%s error:%+v", string(result), err)
return err
}
log.Println("get final ballot resp:", string(result))
var finalRespData Response
if err = json.Unmarshal(result, &finalRespData); err != nil {
log.Printf("Failed to unmarshal get final ballot response. %+v", err)
return err
}
if finalRespData.TotalVotes-initalRespData.TotalVotes != 1 {
return errors.New("ballot vote count error")
}
return nil
}

func TestBallot(t *testing.T) {
err := ballotTest()
if err != nil {
t.Error(err)
} else {
t.Log("TestBallot passed")
}
}

func TestBallotFailure(t *testing.T) {
// TODO: Setup conditions for failure
err := ballotTest()
if err == nil {
t.Error("Expected an error but didn't get one")
} else {
t.Log("TestBallot failed as expected")
}
}
Loading