def MakeCompletedResults(userkey): all_answers = [ model.TestAnswer(user=userkey, expected=word, got='grapes') for word in word_categories.keys() ] result = model.TestResult(parent=userkey, answers=all_answers) self.assertTrue(result.AllWordsAnswered()) return result
def PutTestResult(self, user_key, flavor, answers): result = model.TestResult(id=flavor, parent=user_key, flavor=flavor) for answer in answers: result.answers.append( model.TestAnswer(user=user_key, expected=answer[0], got=answer[1], test_flavor=flavor)) return result.put()
def testAnswersAreComplete(self): user1 = model.User(name=model.UserName('user-1')) # Test a result with only one answer. result = model.TestResult(parent=user1.key, answers=[ model.TestAnswer(user=user1.key, expected='pair', got='pair') ]) self.assertFalse(result.AllWordsAnswered()) # Add all the answers. for word in model.WordCategorizer.GetTestWords(): result.answers.append( model.TestAnswer(user=user1.key, expected=word)) # Verify all words accounted for. self.assertTrue(result.AllWordsAnswered())
def testGroup1Answers(self): user1 = model.User(name=model.UserName('user-1')) user1.put() answer = model.TestAnswer(user=user1.key, expected='pair', got='pair') result = model.TestResult(parent=user1.key, answers=[answer]) result.put() # Expect that "pair" for a user in group 1 to be categorized as "dm-imm". # See static/data/word-categories.json. self.assertEqual('dm-imm', result.answers[0].category) self.assertTrue(result.answers[0].correct) answer = model.TestAnswer(user=user1.key, expected='save', got='recover') result.answers.append(answer) result.put() readback = result.key.get() self.assertEqual('dm-imm', readback.answers[0].category) self.assertTrue(readback.answers[0].correct) self.assertEqual('nt-imm', readback.answers[1].category) self.assertFalse(readback.answers[1].correct)
def answer(): user, posted_json = validate_json(['expected', 'answer', 'test_flavor']) flavor = posted_json['test_flavor'] key = ndb.Key(model.TestResult, flavor, parent=user.key) test_result = key.get() if not test_result: raise NoTestInProgress() answer = model.TestAnswer(user=user.key, expected=posted_json['expected'], got=posted_json['answer'], test_flavor=flavor) logging.info('Saving answer %(answer)s (flavor=%(test_flavor)s)', posted_json) test_result.answers.append(answer) test_result.put() return jsonify(message='ok', done=test_result.AllWordsAnswered())