Ejemplo n.º 1
0
    def db_interaction_test(self, check):
        "Database interacation test"
        operation = "45,-,27"
        result_code = 0
        DATABASE = connect_db()

        cleanup_handler()

        result_raw, result_code, result_error = calculate(operation)
        result = str(result_raw)
        result_id = uuid.uuid4()
        cursor = DATABASE.cursor()
        cursor.execute('INSERT INTO results VALUES (?,?,?)',
                       (result_id.bytes, operation, result_raw))
        DATABASE.commit()

        cursor.execute("SELECT id, operation, result FROM results where id=?;",
                       (result_id.bytes, ))
        raw_fetch = cursor.fetchall()[0]
        check.equal(tuple(raw_fetch),
                    (result_id.bytes, operation, int(result)))
Ejemplo n.º 2
0
def calc_handler():
    # Predefined vars
    result = "deadbeef"
    result_code = 0
    result_id = "deadbeef"

    # Get POST params
    operation = request.form['operation']

    if not re.match(r'^-?[0-9]\d*(\.\d+)?,.+,-?[0-9]\d*(\.\d+)?$', operation):
        result_code = 1
        result = "Некорректный ввод"
    else:
        result_raw, result_code, result_error = calculate(operation)
        if result_code == 0:
            result = str(result_raw)
            # Check if database has less than 100 records
            cursor = DATABASE.cursor()
            cursor.execute('SELECT COUNT(*) FROM results')
            current_results = cursor.fetchone()
            if current_results[0] < 100:
                # Add database result addition
                # Most compact way to store https://wtanaka.com/node/8106
                result_id = uuid.uuid4()
                cursor.execute('INSERT INTO results VALUES (?,?,?)', (result_id.bytes, operation, result_raw))
                DATABASE.commit()
            else:
                result_code = 2
                result = "Превышено количество хранимых результатов расчетов"
        else:
            result = result_error

    return render_template(
        "result.html",
        operation = operation,
        result = result,
        result_code = result_code,
        result_id = result_id)
Ejemplo n.º 3
0
 def test_sum(self):
     self.assertEqual(calculate("3,+,4")[0], 7)
Ejemplo n.º 4
0
 def test_div(self):
     self.assertEqual(calculate("4,/,2")[0], 2)
Ejemplo n.º 5
0
 def test_pow(self):
     self.assertEqual(calculate("2,**,3")[0], 8)
Ejemplo n.º 6
0
 def test_mul(self):
     self.assertEqual(calculate("2,*,3")[0], 6)
Ejemplo n.º 7
0
 def test_sub(self):
     self.assertEqual(calculate("3,-,4")[0], -1)