Skip to content

Commit

Permalink
Correct question set results scoring per #8
Browse files Browse the repository at this point in the history
Previously the response was not sending the question_id and answer_id to the backend API, resulting in no valid response.
  • Loading branch information
tjchambers committed Mar 24, 2016
1 parent 0ee55d7 commit 86ce6a9
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 14 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.0.1
4.0.2
8 changes: 4 additions & 4 deletions lib/ApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,13 @@ protected function _score($answers)
$url = static::instanceUrl() . '/score';

// Weird request requires us to build the cURL request manually
$params = '';
$params = array();
foreach ($answers as $key => $value) {
$params = $params . 'answers[][{$key}]={$value}&';
$params[] = "answers[][question_id]={$value['question_id']}";
$params[] = "answers[][answer_id]={$value['answer_id']}";
}
rtrim($params, '&');

$response = static::_makeRequest('post', $url, $params);
$response = static::_makeRequest('post', $url, implode('&', $params));
return Util\Util::convertToBlockScoreObject($response);
}
}
105 changes: 96 additions & 9 deletions tests/QuestionSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@

class QuestionSetTest extends TestCase
{
private static $qs_answers = array(
'Which one of the following addresses is associated with you?' => '309 Colver Rd',
'Which one of the following area codes is associated with you?' => '812',
'Which one of the following counties is associated with you?' => 'Jasper',
'Which one of the following zip codes is associated with you?' => '49230',
'What state was your SSN issued in?' => 'None Of The Above',
'Which one of the following adult individuals is most closely associated with you?' => 'None Of The Above',
);

public function testUrl()
{
$this->assertSame(QuestionSet::classUrl(), '/question_sets');
Expand All @@ -16,7 +25,7 @@ public function testClassType()
$this->assertTrue($qs instanceof QuestionSet);
$this->assertTrue($qs instanceof Object);
}

public function testCreateQuestionSet()
{
$person = self::createTestPerson();
Expand All @@ -25,7 +34,7 @@ public function testCreateQuestionSet()
$this->assertSame('None Of The Above',$i->answers[4]->answer);
}
}

public function testAllQuestionSet()
{
$person = self::createTestPerson();
Expand All @@ -35,7 +44,7 @@ public function testAllQuestionSet()
$all_qs = $person->question_sets->all();
$this->assertSame($qs->id, $all_qs[0]->id);
}

public function testRetrieveQuestionSet()
{
$person = self::createTestPerson();
Expand All @@ -45,21 +54,99 @@ public function testRetrieveQuestionSet()
$this->assertSame($retrieved_qs->$key, $value);
}
}

public function testScoringQuestionSet()
{
$person = self::createTestPerson();
$qs = $person->question_sets->create();

// Compute the correct answer to the first question
$first_question = $qs->questions[0];
$first_question_answer = self::$qs_answers[$first_question->question];
$first_question_answer_id = 0;

foreach ($first_question->answers as $answer) {
if ($answer->answer == $first_question_answer) {
$first_question_answer_id = $answer->id;
}
}

$answers = array(
array('question_id' => 1, 'answer_id' => 2),
array('question_id' => 2, 'answer_id' => 2),
array('question_id' => 3, 'answer_id' => 2),
array('question_id' => 4, 'answer_id' => 2),
array('question_id' => 5, 'answer_id' => 2),
array('question_id' => 1, 'answer_id' => $first_question_answer_id),
array('question_id' => 2, 'answer_id' => 6),
array('question_id' => 3, 'answer_id' => 6),
array('question_id' => 4, 'answer_id' => 6),
array('question_id' => 5, 'answer_id' => 6),
);

$score = $qs->score($answers);

// Test whether questions returned are the same
foreach ($qs as $key => $value) {
$this->assertSame($score->questions->$key, $value);
}

// Test whether we scored a 20%!
$this->assertSame($score->score, 20.0);
}

public function testScoringQuestionSet2()
{
$person = self::createTestPerson();
$qs = $person->question_sets->create();

// Compute the correct answer to all the questions!
$answer_ids = array();

foreach ($qs->questions as $question) {
$question_answer = self::$qs_answers[$question->question];
foreach ($question->answers as $answer) {
if ($answer->answer == $question_answer) {
array_push($answer_ids, $answer->id);
}
}
}

$answers = array(
array('question_id' => 1, 'answer_id' => $answer_ids[0]),
array('question_id' => 2, 'answer_id' => $answer_ids[1]),
array('question_id' => 3, 'answer_id' => $answer_ids[2]),
array('question_id' => 4, 'answer_id' => $answer_ids[3]),
array('question_id' => 5, 'answer_id' => $answer_ids[4]),
);

$score = $qs->score($answers);

// Test whether questions returned are the same
foreach ($qs as $key => $value) {
$this->assertSame($score->questions->$key, $value);
}

// Test whether we scored a 100%!
$this->assertSame($score->score, 100.0);
}

public function testScoringQuestionSet3()
{
$person = self::createTestPerson();
$qs = $person->question_sets->create();

$answers = array(
array('question_id' => 1, 'answer_id' => 6),
array('question_id' => 2, 'answer_id' => 6),
array('question_id' => 3, 'answer_id' => 6),
array('question_id' => 4, 'answer_id' => 6),
array('question_id' => 5, 'answer_id' => 6),
);

$score = $qs->score($answers);

// Test whether questions returned are the same
foreach ($qs as $key => $value) {
$this->assertSame($score->questions->$key, $value);
}

// Test whether we scored a 0%!
$this->assertSame($score->score, 0.0);
}
}

0 comments on commit 86ce6a9

Please sign in to comment.