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)))
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)
def test_sum(self): self.assertEqual(calculate("3,+,4")[0], 7)
def test_div(self): self.assertEqual(calculate("4,/,2")[0], 2)
def test_pow(self): self.assertEqual(calculate("2,**,3")[0], 8)
def test_mul(self): self.assertEqual(calculate("2,*,3")[0], 6)
def test_sub(self): self.assertEqual(calculate("3,-,4")[0], -1)