probo/hasher/hasher_test.go

65 lines
1.9 KiB
Go
Raw Permalink Normal View History

2022-06-24 18:56:06 +02:00
package hasher
import (
"testing"
"git.andreafazzi.eu/andrea/probo/client"
"github.com/remogatto/prettytest"
)
type testSuite struct {
prettytest.Suite
}
func TestRunner(t *testing.T) {
prettytest.Run(
t,
new(testSuite),
)
}
func (t *testSuite) TestQuizHashes() {
h := NewDefaultHash(DefaultSHA256HashingFn)
firstQuizRequest := &client.CreateQuizRequest{
Question: &client.CreateQuestionRequest{
Text: "Question 1"},
Answers: []*client.CreateAnswerRequest{
{Text: "Answer 2", Correct: false},
{Text: "Answer 3", Correct: false},
{Text: "Answer 1", Correct: true},
},
}
secondQuizRequest := &client.CreateQuizRequest{
Question: &client.CreateQuestionRequest{
Text: "Question 1"},
Answers: []*client.CreateAnswerRequest{
{Text: "Answer 1", Correct: false},
{Text: "Answer 2", Correct: false},
{Text: "Answer 3", Correct: true},
},
}
thirdQuizRequest := &client.CreateQuizRequest{
Question: &client.CreateQuestionRequest{
Text: "Question 2"},
Answers: []*client.CreateAnswerRequest{
{Text: "Answer 1", Correct: false},
{Text: "Answer 2", Correct: false},
{Text: "Answer 3", Correct: true},
},
}
hashesFromFirstRequest := h.QuizHashes(firstQuizRequest)
hashesFromSecondRequest := h.QuizHashes(secondQuizRequest)
hashesFromThirdRequest := h.QuizHashes(thirdQuizRequest)
t.Equal(5, len(hashesFromFirstRequest))
t.True(hashesFromFirstRequest[1] == hashesFromSecondRequest[2], "Answers' hashes should maintain original request's order")
t.True(hashesFromFirstRequest[4] == hashesFromSecondRequest[4], "Quiz hash should be the same because quizzes are duplicated")
t.True(hashesFromFirstRequest[1] != hashesFromThirdRequest[1], "Questions' hashes should not be the same because texts are different")
t.True(hashesFromFirstRequest[4] != hashesFromThirdRequest[4], "Quiz hash should not be the same because quizzes are not duplicated")
}