<commit_before># Copyright 2015-2016 Akretion (http://www.akretion.com) - <NAME>
# Copyright 2016 Eficent (http://www.eficent.com)
# Copyright 2016 Serpent Consulting Services (<http://www.serpentcs.com>)
# Copyright 2018 Tecnativa - <NAME>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).


from odoo.tests.common import TransactionCase
from odoo.exceptions import ValidationError


class TestStockNoNegative(TransactionCase):

    def setUp(self):
        super(TestStockNoNegative, self).setUp()
        self.product_model = self.env['product.product']
        self.product_ctg_model = self.env['product.category']
        self.picking_type_id = self.env.ref('stock.picking_type_out')
        self.location_id = self.env.ref('stock.stock_location_stock')
        self.location_dest_id = self.env.ref('stock.stock_location_customers')
        # Create product category
        self.product_ctg = self._create_product_category()
        # Create a Product
        self.product = self._create_product('test_product1')
        self._create_picking()

    def _create_product_category(self):
        product_ctg = self.product_ctg_model.create({
            'name': 'test_product_ctg',
            'allow_negative_stock': False,
        })
        return product_ctg

    def _create_product(self, name):
        product = self.product_model.create({
            'name': name,
            'categ_id': self.product_ctg.id,
            'type': 'product',
            'allow_negative_stock': False,
        })
        return product

    def _create_picking(self):
        self.stock_picking = self.env['stock.picking'].with_context(
            test_stock_no_negative=True,
        ).create({
            'picking_type_id': self.picking_type_id.id,
            'move_type': 'direct',
            'location_id': self.location_id.id,
            'location_dest_id': self.location_dest_id.id
        })

        self.stock_move = self.env['stock.move'].create({
            'name': 'Test Move',
            'product_id': self.product.id,
            'product_uom_qty': 100.0,
            'product_uom': self.product.uom_id.id,
            'picking_id': self.stock_picking.id,
            'state': 'draft',
            'location_id': self.location_id.id,
            'location_dest_id': self.location_dest_id.id,
            'quantity_done': 100.0,
        })

    def test_check_constrains(self):
        """Assert that constraint is raised when user
        tries to validate the stock operation which would
        make the stock level of the product negative """
        self.stock_picking.action_confirm()
        with self.assertRaises(ValidationError):
            self.stock_picking.button_validate()

    def test_true_allow_negative_stock(self):
        """Assert that negative stock levels are allowed when
        the allow_negative_stock is set active in the product"""
        self.product.allow_negative_stock = True
        self.stock_picking.action_confirm()
        self.stock_picking.button_validate()
        quant = self.env['stock.quant'].search([
            ('product_id', '=', self.product.id),
            ('location_id', '=', self.location_id.id)])
        self.assertEqual(quant.quantity, -100)
<commit_msg>[IMP] stock_no_negative: Avoid problems on integration tests<commit_after># Copyright 2015-2016 Akretion (http://www.akretion.com) - <NAME>
# Copyright 2016 Eficent (http://www.eficent.com)
# Copyright 2016 Serpent Consulting Services (<http://www.serpentcs.com>)
# Copyright 2018 Tecnativa - <NAME>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).


from odoo.tests.common import TransactionCase
from odoo.exceptions import ValidationError


class TestStockNoNegative(TransactionCase):
    at_install = False
    post_install = True

    def setUp(self):
        super(TestStockNoNegative, self).setUp()
        self.product_model = self.env['product.product']
        self.product_ctg_model = self.env['product.category']
        self.picking_type_id = self.env.ref('stock.picking_type_out')
        self.location_id = self.env.ref('stock.stock_location_stock')
        self.location_dest_id = self.env.ref('stock.stock_location_customers')
        # Create product category
        self.product_ctg = self._create_product_category()
        # Create a Product
        self.product = self._create_product('test_product1')
        self._create_picking()

    def _create_product_category(self):
        product_ctg = self.product_ctg_model.create({
            'name': 'test_product_ctg',
            'allow_negative_stock': False,
        })
        return product_ctg

    def _create_product(self, name):
        product = self.product_model.create({
            'name': name,
            'categ_id': self.product_ctg.id,
            'type': 'product',
            'allow_negative_stock': False,
        })
        return product

    def _create_picking(self):
        self.stock_picking = self.env['stock.picking'].with_context(
            test_stock_no_negative=True,
        ).create({
            'picking_type_id': self.picking_type_id.id,
            'move_type': 'direct',
            'location_id': self.location_id.id,
            'location_dest_id': self.location_dest_id.id
        })

        self.stock_move = self.env['stock.move'].create({
            'name': 'Test Move',
            'product_id': self.product.id,
            'product_uom_qty': 100.0,
            'product_uom': self.product.uom_id.id,
            'picking_id': self.stock_picking.id,
            'state': 'draft',
            'location_id': self.location_id.id,
            'location_dest_id': self.location_dest_id.id,
            'quantity_done': 100.0,
        })

    def test_check_constrains(self):
        """Assert that constraint is raised when user
        tries to validate the stock operation which would
        make the stock level of the product negative """
        self.stock_picking.action_confirm()
        with self.assertRaises(ValidationError):
            self.stock_picking.button_validate()

    def test_true_allow_negative_stock(self):
        """Assert that negative stock levels are allowed when
        the allow_negative_stock is set active in the product"""
        self.product.allow_negative_stock = True
        self.stock_picking.action_confirm()
        self.stock_picking.button_validate()
        quant = self.env['stock.quant'].search([
            ('product_id', '=', self.product.id),
            ('location_id', '=', self.location_id.id)])
        self.assertEqual(quant.quantity, -100)

{'fraction_non_alphanumeric': 0.11264822134387352, 'fraction_numerical': 0.010337488598358164, 'mean_word_length': 2.6287920573634858, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3011985', 'n_tokens_mistral': 2022, 'n_tokens_neox': 1936, 'n_words': 368}