From 107b82f6e13840c218706d55e434071fb2f2ed28 Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Fri, 9 Feb 2018 15:09:41 +0100 Subject: [PATCH] [IMP] clean up use of cursors in tests --- addons/stock/tests/test_quant.py | 22 ++++++++++------------ odoo/addons/base/tests/test_ir_sequence.py | 1 - odoo/addons/base/tests/test_uninstall.py | 1 - 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/addons/stock/tests/test_quant.py b/addons/stock/tests/test_quant.py index bff1f902ba5..9a8dc37ee51 100644 --- a/addons/stock/tests/test_quant.py +++ b/addons/stock/tests/test_quant.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. +from contextlib import closing from datetime import datetime, timedelta from odoo.exceptions import ValidationError @@ -227,14 +228,12 @@ class StockQuant(TransactionCase): # opens a new cursor and SELECT FOR UPDATE the quant, to simulate another concurrent reserved # quantity increase - cr2 = self.registry.cursor() - cr2.execute("SELECT id FROM stock_quant WHERE product_id=%s AND location_id=%s", (product.id, stock_location.id)) - quant_id = cr2.fetchone() - cr2.execute("SELECT 1 FROM stock_quant WHERE id=%s FOR UPDATE", quant_id) + with closing(self.registry.cursor()) as cr: + cr.execute("SELECT id FROM stock_quant WHERE product_id=%s AND location_id=%s", (product.id, stock_location.id)) + quant_id = cr.fetchone() + cr.execute("SELECT 1 FROM stock_quant WHERE id=%s FOR UPDATE", quant_id) + self.env['stock.quant']._update_available_quantity(product, stock_location, 1.0) - self.env['stock.quant']._update_available_quantity(product, stock_location, 1.0) - cr2.rollback() - cr2.close() self.assertEqual(self.env['stock.quant']._get_available_quantity(product, stock_location), 11.0) self.assertEqual(len(self.env['stock.quant']._gather(product, stock_location)), 2) @@ -344,11 +343,10 @@ class StockQuant(TransactionCase): # opens a new cursor and SELECT FOR UPDATE the quant, to simulate another concurrent reserved # quantity increase - cr2 = self.registry.cursor() - cr2.execute("SELECT 1 FROM stock_quant WHERE id = %s FOR UPDATE", quants.ids) - self.env['stock.quant']._update_available_quantity(product, stock_location, -1.0) - cr2.rollback() - cr2.close() + with closing(self.registry.cursor()) as cr: + cr.execute("SELECT 1 FROM stock_quant WHERE id = %s FOR UPDATE", quants.ids) + self.env['stock.quant']._update_available_quantity(product, stock_location, -1.0) + self.assertEqual(self.env['stock.quant']._get_available_quantity(product, stock_location), 9.0) self.assertEqual(len(self.env['stock.quant']._gather(product, stock_location)), 2) diff --git a/odoo/addons/base/tests/test_ir_sequence.py b/odoo/addons/base/tests/test_ir_sequence.py index 589a237f699..6c31728de35 100644 --- a/odoo/addons/base/tests/test_ir_sequence.py +++ b/odoo/addons/base/tests/test_ir_sequence.py @@ -20,7 +20,6 @@ def environment(): registry = odoo.registry(common.get_db_name()) with registry.cursor() as cr: yield odoo.api.Environment(cr, ADMIN_USER_ID, {}) - cr.commit() def drop_sequence(code): diff --git a/odoo/addons/base/tests/test_uninstall.py b/odoo/addons/base/tests/test_uninstall.py index 5f7afc7bd46..041825d463d 100644 --- a/odoo/addons/base/tests/test_uninstall.py +++ b/odoo/addons/base/tests/test_uninstall.py @@ -19,7 +19,6 @@ def environment(): reg = registry(common.get_db_name()) with reg.cursor() as cr: yield api.Environment(cr, SUPERUSER_ID, {}) - cr.commit() MODULE = 'test_uninstall'