def test_sum_of_squares_huge(self): # Model supports numbers > 100. Frontend does not record = NumberRecord(number=101) self.assertEqual( record.sum_of_squares(), 348551 )
def display_difference(): ''' Given a number through GET, returns json with following attrs: - number: the number given through GET - value: calculated value from number (see models.NumberRecord) - last_requested: datetime in isoformat of when record is updated - occurences: number of times this number has been queried If any errors occur, a JSON with the following content will return: - message: explanation of error - code: associated error code ''' try: number = int(request.args.get('number')) except (TypeError, ValueError): return jsonify(**{ 'message': 'Please provide a number', 'code': ErrorCode.NO_NUMBER } ) if not (1 <= number <= 100): return jsonify(**{ 'message': 'Number must be in the range of 1 to 100', 'code': ErrorCode.OUT_OF_RANGE } ) record = db.session.query(NumberRecord).filter( NumberRecord.number==number).first() if not record: record = NumberRecord(number=number, occurences=0) record.set_value() db.session.add(record) record.last_requested = datetime.datetime.now() record.occurences += 1 # This feels dirty # Would be better to incorporate something like ZopeTransactionManager... db.session.commit() return jsonify(**record.to_json())
def test_set_value_out_of_range(self): record = NumberRecord(number=0) record.set_value() self.assertIsNone(record.value)
def test_set_value_huge(self): record = NumberRecord(number=101) record.set_value() self.assertEqual(record.value, 26184250)
def test_set_value_bigger(self): record = NumberRecord(number=10) record.set_value() self.assertEqual(record.value, 2640)
def test_set_value_small(self): record = NumberRecord(number=3) record.set_value() self.assertEqual(record.value, 22)
def test_square_of_sum_out_of_range(self): record = NumberRecord(number=0) self.assertIsNone(record.square_of_sum())
def test_square_of_sum_huge(self): record = NumberRecord(number=101) self.assertEqual( record.square_of_sum(), 26532801 )
def test_square_of_sum_bigger(self): record = NumberRecord(number=10) self.assertEqual( record.square_of_sum(), 3025 )
def test_square_of_sum_small(self): record = NumberRecord(number=3) self.assertEqual( record.square_of_sum(), 36 )
def test_sum_of_squares_bigger(self): record = NumberRecord(number=10) self.assertEqual( record.sum_of_squares(), 385 )
def test_sum_of_squares_small(self): record = NumberRecord(number=3) self.assertEqual( record.sum_of_squares(), 14 )