Skip to content

Commit

Permalink
Add basic unit tests (#5)
Browse files Browse the repository at this point in the history
* Add basic unit tests
  • Loading branch information
kkoutsilis authored Dec 27, 2023
1 parent 1be6d73 commit 91c7744
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
10 changes: 7 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ func generateSecretSantaMatches(data []Data) []MatchPair {
shuffledData[i], shuffledData[j] = shuffledData[j], shuffledData[i]
})

return circlularMatchingAlgorithm(shuffledData)
}

func circlularMatchingAlgorithm(data []Data) []MatchPair {
var matches []MatchPair
for i := 0; i < len(shuffledData); i++ {
from := shuffledData[i]
to := shuffledData[(i+1)%len(shuffledData)]
for i := 0; i < len(data); i++ {
from := data[i]
to := data[(i+1)%len(data)]
matches = append(matches, MatchPair{From: from, To: to})
}
return matches
Expand Down
64 changes: 64 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cmd

import (
"reflect"
"testing"
)

func TestCheckFileExistsReturnsTrueWhenFileExists(t *testing.T) {
if !checkFileExists("../test_data.json") {
t.Error("Expected checkFileExists to return true when file exists")
}

}

func TestCheckFileExistsReturnsFalseWhenFileDoesNotExist(t *testing.T) {
if checkFileExists("file_does_not_exist.json") {
t.Error("Expected checkFileExists to return false when file does not exist")
}
}

func TestCheckIsJsonReturnsTrueWhenFileIsJson(t *testing.T) {
if !checkIsJson("../test_data.json") {
t.Error("Expected checkIsJson to return true when file is json")
}
}

func TestCheckIsJsonReturnsFalseWhenFileIsNotJson(t *testing.T) {
if checkIsJson("../test_data.txt") {
t.Error("Expected checkIsJson to return false when file is not json")
}
}

func TestGenerateSecretSantaMachesReturnsAppropriateLenghtOfMatchPairs(t *testing.T) {
var payload []Data
payload = append(payload, Data{Name: "A", Email: "testa@test.org"})
payload = append(payload, Data{Name: "B", Email: "testb@test.org"})
payload = append(payload, Data{Name: "C", Email: "testb@test.org"})

matches := generateSecretSantaMatches(payload)

if len(matches) != len(payload) {
t.Error("Expected generateSecretSantaMatches to return the same number of matches as participants")
}
}

func TestCircularMatchingAlgorithmReturnsExpectedMatchPairs(t *testing.T) {
var payload []Data
payload = append(payload, Data{Name: "A", Email: "testa@test.org"})
payload = append(payload, Data{Name: "B", Email: "testb@test.org"})
payload = append(payload, Data{Name: "C", Email: "testb@test.org"})

var expected []MatchPair

expected = append(expected, MatchPair{From: payload[0], To: payload[1]})
expected = append(expected, MatchPair{From: payload[1], To: payload[2]})
expected = append(expected, MatchPair{From: payload[2], To: payload[0]})

result := circlularMatchingAlgorithm(payload)

if !reflect.DeepEqual(result, expected) {
t.Error("Expected circlularMatchingAlgorithm to return expected list of match pairs")
}

}

0 comments on commit 91c7744

Please sign in to comment.