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
46 changes: 46 additions & 0 deletions ballot/GetCandidatesVote_5ba0d06cb0_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"sync"
"testing"
)

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

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

func TestGetCandidatesVote_5ba0d06cb0(t *testing.T) {
// Test case 1: Check if the function returns an empty map on first call
votes := getCandidatesVote()
if len(votes) != 0 {
t.Error("Expected empty map on first call, got", votes)
}

// Test case 2: Check if the function returns the same map on subsequent calls
candidateVotesStore["John"] = 5
votes = getCandidatesVote()
if len(votes) != 1 {
t.Error("Expected map with 1 entry, got", votes)
}
if votes["John"] != 5 {
t.Error("Expected 5 votes for John, got", votes["John"])
}

// Test case 3: Check if the function returns the updated map after changes
candidateVotesStore["Doe"] = 10
votes = getCandidatesVote()
if len(votes) != 2 {
t.Error("Expected map with 2 entries, got", votes)
}
if votes["Doe"] != 10 {
t.Error("Expected 10 votes for Doe, got", votes["Doe"])
}
}
118 changes: 118 additions & 0 deletions ballot/HttpClientRequest_a374070552_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package main

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

func httpClientRequest(method, url, path string, body io.Reader) (int, []byte, error) {
client := &http.Client{}
req, err := http.NewRequest(method, url+path, body)
if err != nil {
return 0, nil, err
}
resp, err := client.Do(req)
if err != nil {
return 0, nil, err
}
defer resp.Body.Close()

respBody, err := io.ReadAll(resp.Body)
if err != nil {
return 0, nil, err
}
return resp.StatusCode, respBody, nil
}

func TestHttpClientRequest_a374070552(t *testing.T) {
t.Run("Successful request", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
fmt.Fprintln(rw, "Hello, client")
}))
defer server.Close()

statusCode, body, err := httpClientRequest("GET", server.URL, "/", nil)
if err != nil {
t.Error(err)
}
if statusCode != 200 {
t.Errorf("Expected status code 200, got %d", statusCode)
}
if !strings.Contains(string(body), "Hello, client") {
t.Errorf("Unexpected body: got %v", string(body))
}
})

t.Run("Unsuccessful request - invalid operation", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
fmt.Fprintln(rw, "Hello, client")
}))
defer server.Close()

_, _, err := httpClientRequest("INVALID", server.URL, "/", nil)
if err == nil {
t.Error("Expected error, got nil")
}
})

t.Run("Unsuccessful request - invalid url", func(t *testing.T) {
_, _, err := httpClientRequest("GET", "http://invalid-url", "/", nil)
if err == nil {
t.Error("Expected error, got nil")
}
})

t.Run("Unsuccessful request - non 2xx status code", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
http.Error(rw, "Bad request", http.StatusBadRequest)
}))
defer server.Close()

statusCode, _, err := httpClientRequest("GET", server.URL, "/", nil)
if err != nil {
t.Error(err)
}
if statusCode != http.StatusBadRequest {
t.Errorf("Expected status code %d, got %d", http.StatusBadRequest, statusCode)
}
})

t.Run("Successful POST request with params", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
t.Errorf("Expected method 'POST', got '%s'", req.Method)
}

body := new(bytes.Buffer)
_, err := io.Copy(body, req.Body)
if err != nil {
t.Error(err)
}
req.Body.Close()

if body.String() != "param1=value1&param2=value2" {
t.Errorf("Expected body to be 'param1=value1&param2=value2', got '%s'", body.String())
}

fmt.Fprintln(rw, "Hello, client")
}))
defer server.Close()

params := strings.NewReader("param1=value1&param2=value2")
statusCode, body, err := httpClientRequest("POST", server.URL, "/", params)
if err != nil {
t.Error(err)
}
if statusCode != 200 {
t.Errorf("Expected status code 200, got %d", statusCode)
}
if !strings.Contains(string(body), "Hello, client") {
t.Errorf("Unexpected body: got %v", string(body))
}
})
}
55 changes: 55 additions & 0 deletions ballot/RunTest_a2bb790205_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

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

func TestRunTest_a2bb790205(t *testing.T) {
t.Run("Test case passed", func(t *testing.T) {
req, err := http.NewRequest("GET", "/test", bytes.NewBuffer([]byte{}))
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 := `{"Message":"Test Cases passed","Code":200}`
if rr.Body.String() != expected {
t.Errorf("Handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}
})

t.Run("Test case failed", func(t *testing.T) {
req, err := http.NewRequest("GET", "/test", bytes.NewBuffer([]byte{}))
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(runTest)

// TODO: Make TestBallot return an error to simulate a failure
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 := `{"Message":"Test Cases Failed with error : test error","Code":400}`
if rr.Body.String() != expected {
t.Errorf("Handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}
})
}
48 changes: 48 additions & 0 deletions ballot/SaveVote_3f5729642d_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"testing"
)

// Mock Vote struct
type Vote struct {
CandidateID string
}

// Mock candidateVotesStore
var candidateVotesStore map[string]int

// Mock getCandidatesVote function
func getCandidatesVote() map[string]int {
return candidateVotesStore
}

// Method to be tested
func saveVote(vote Vote) error {
candidateVotesStore = getCandidatesVote()
candidateVotesStore[vote.CandidateID]++
return nil
}

// Test method
func TestSaveVote_3f5729642d(t *testing.T) {
// Test case 1: When vote is saved successfully
candidateVotesStore = make(map[string]int)
vote := Vote{CandidateID: "123"}
err := saveVote(vote)
if err != nil {
t.Error("Expected nil, got ", err)
}
if candidateVotesStore[vote.CandidateID] != 1 {
t.Error("Expected 1, got ", candidateVotesStore[vote.CandidateID])
}

// Test case 2: When vote is saved for the second time
err = saveVote(vote)
if err != nil {
t.Error("Expected nil, got ", err)
}
if candidateVotesStore[vote.CandidateID] != 2 {
t.Error("Expected 2, got ", candidateVotesStore[vote.CandidateID])
}
}
101 changes: 101 additions & 0 deletions ballot/ServeRoot_da512bfd0e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package main

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

type Vote struct {
VoterID string
CandidateID string
}

type MockSaveVote struct{}

func (m *MockSaveVote) saveVote(vote Vote) error {
if vote.VoterID == "" || vote.CandidateID == "" {
return errors.New("Vote is not valid. Vote can not be saved")
}
return nil
}

func serveRoot(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
w.WriteHeader(http.StatusOK)
case "POST":
var vote Vote
err := json.NewDecoder(r.Body).Decode(&vote)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
m := MockSaveVote{}
err = m.saveVote(vote)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusCreated)
default:
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
}
}

func TestServeRoot_da512bfd0e(t *testing.T) {
// Test case 1: Successful GET request
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)
}

// Test case 2: Successful POST request
vote := Vote{VoterID: "voter1", CandidateID: "candidate1"}
jsonVote, _ := json.Marshal(vote)
req, err = http.NewRequest("POST", "/", bytes.NewBuffer(jsonVote))
if err != nil {
t.Fatal(err)
}
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)
}

// Test case 3: Unsuccessful POST request with invalid vote
vote = Vote{VoterID: "", CandidateID: ""}
jsonVote, _ = json.Marshal(vote)
req, err = http.NewRequest("POST", "/", bytes.NewBuffer(jsonVote))
if err != nil {
t.Fatal(err)
}
rr = httptest.NewRecorder()
handler = http.HandlerFunc(serveRoot)
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)
}

// Test case 4: Unsupported method
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)
}
}
Loading