Exemplo n.º 1
0
 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
         )
Exemplo n.º 2
0
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())
Exemplo n.º 3
0
 def test_set_value_out_of_range(self):
     record = NumberRecord(number=0)
     record.set_value()
     self.assertIsNone(record.value)
Exemplo n.º 4
0
 def test_set_value_huge(self):
     record = NumberRecord(number=101)
     record.set_value()
     self.assertEqual(record.value, 26184250)
Exemplo n.º 5
0
 def test_set_value_bigger(self):
     record = NumberRecord(number=10)
     record.set_value()
     self.assertEqual(record.value, 2640)
Exemplo n.º 6
0
 def test_set_value_small(self):
     record = NumberRecord(number=3)
     record.set_value()
     self.assertEqual(record.value, 22)
Exemplo n.º 7
0
 def test_square_of_sum_out_of_range(self):
     record = NumberRecord(number=0)
     self.assertIsNone(record.square_of_sum())
Exemplo n.º 8
0
 def test_square_of_sum_huge(self):
     record = NumberRecord(number=101)
     self.assertEqual(
         record.square_of_sum(),
         26532801
         )
Exemplo n.º 9
0
 def test_square_of_sum_bigger(self):
     record = NumberRecord(number=10)
     self.assertEqual(
         record.square_of_sum(),
         3025
         )
Exemplo n.º 10
0
 def test_square_of_sum_small(self):
     record = NumberRecord(number=3)
     self.assertEqual(
         record.square_of_sum(),
         36
         )
Exemplo n.º 11
0
 def test_sum_of_squares_bigger(self):
     record = NumberRecord(number=10)
     self.assertEqual(
         record.sum_of_squares(),
         385
         )
Exemplo n.º 12
0
 def test_sum_of_squares_small(self):
     record = NumberRecord(number=3)
     self.assertEqual(
         record.sum_of_squares(),
         14
         )