<commit_before># -*- Mode: Python; coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4

##
## Stoqdrivers
## Copyright (C) 2006 Async Open Source <http://www.async.com.br>
## All rights reserved
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
## USA.
##
## Author(s):   <NAME>        <<EMAIL>>
##

from decimal import Decimal

from stoqdrivers.devices.printers.fiscal import FiscalPrinter
from stoqdrivers.constants import (TAX_NONE, UNIT_LITERS, UNIT_CUSTOM,
                                   MONEY_PM, TAX_ICMS, CUSTOM_PM,)
from stoqdrivers.exceptions import (CouponOpenError, PendingReduceZ,
                                    PendingReadX, PaymentAdditionError,
                                    AlreadyTotalized, CancelItemError,
                                    InvalidValue, CloseCouponError,
                                    CouponNotOpenError)
from tests.base import BaseTest

class TestCoupon(BaseTest):
    """ Test a coupon creation """
    device_class = FiscalPrinter

    #
    # Helper methods
    #

    def _open_coupon(self):
        self._device.identify_customer("<NAME>", "Async", "1234567890")
        while True:
            try:
                self._device.open()
                break
            except CouponOpenError:
                self._device.cancel()
            except PendingReduceZ:
                self._device.close_till()
            except PendingReadX:
                self._device.summarize()

    #
    # Tests
    #

    def test_add_item(self):
        self._open_coupon()
        # 1. Specify discount and surcharge at the same time
        self.failUnlessRaises(TypeError, self._device.add_item, u"123456",
                              u"Monitor LG Flatron T910B", Decimal("500"),
                              TAX_NONE, discount=Decimal("1"),
                              surcharge=Decimal("1"))

        # 2. Specify unit_desc with unit different from UNIT_CUSTOM
        self.failUnlessRaises(ValueError, self._device.add_item, u"123456",
                              u"Monitor LG Flatron T910B", Decimal("500"),
                              TAX_NONE, unit=UNIT_LITERS, unit_desc="XX")

        # 3. Specify unit as UNIT_CUSTOM and not supply a unit_desc
        self.failUnlessRaises(ValueError, self._device.add_item, u"123456",
                              u"Monitor LG Flatron T910B", Decimal("500"),
                              TAX_NONE, unit=UNIT_CUSTOM)

        # 4. Specify unit as UNIT_CUSTOM and unit_desc greater than 2 chars
        self.failUnlessRaises(ValueError, self._device.add_item, u"123456",
                              u"Monitor LG Flatron T910B", Decimal("500"),
                              TAX_NONE, unit=UNIT_CUSTOM, unit_desc="XXXX")

        # 5. Add item without price
        self.failUnlessRaises(InvalidValue, self._device.add_item, u"123456",
                              u"Monitor LG Flatron T910B", Decimal("0"),
                              TAX_NONE)

        #
        # 6. Dataregis specific: the first 6 chars of the product code must
        # be digits.
        self._device.add_item(u"ABCDEF", u"Monitor LG 775N", Decimal("10"),
                              TAX_NONE, items_quantity=Decimal("2"))

        # A "normal" item...
        self._device.add_item(u"987654", u"Monitor LG 775N", Decimal("10"),
                              TAX_NONE, items_quantity=Decimal("1"))

        # A item with customized unit
        self._device.add_item(u"123456", u"Monitor LG 775N", Decimal("10"),
                              TAX_NONE, items_quantity=Decimal("1"),
                              unit=UNIT_CUSTOM, unit_desc="Tx")

        # A item with surcharge
        self._device.add_item(u"123456", u"Monitor LG 775N", Decimal("10"),
                              TAX_NONE, items_quantity=Decimal("1"),
                              surcharge=Decimal("1"))

        # 7. Add item with coupon totalized
        self._device.totalize()
        self.failUnlessRaises(AlreadyTotalized,
            self._device.add_item, u"123456", u"Monitor LG Flatron T910B",
            Decimal("10"), TAX_NONE)<commit_msg>Extend a little bit test_add_item, check the item ids returned.<commit_after># -*- Mode: Python; coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4

##
## Stoqdrivers
## Copyright (C) 2006 Async Open Source <http://www.async.com.br>
## All rights reserved
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
## USA.
##
## Author(s):   <NAME>        <<EMAIL>>
##

from decimal import Decimal

from stoqdrivers.devices.printers.fiscal import FiscalPrinter
from stoqdrivers.constants import (TAX_NONE, UNIT_LITERS, UNIT_CUSTOM,
                                   MONEY_PM, TAX_ICMS, CUSTOM_PM,)
from stoqdrivers.exceptions import (CouponOpenError, PendingReduceZ,
                                    PendingReadX, PaymentAdditionError,
                                    AlreadyTotalized, CancelItemError,
                                    InvalidValue, CloseCouponError,
                                    CouponNotOpenError)
from tests.base import BaseTest

class TestCoupon(BaseTest):
    """ Test a coupon creation """
    device_class = FiscalPrinter

    #
    # Helper methods
    #

    def _open_coupon(self):
        self._device.identify_customer("<NAME>", "Async", "1234567890")
        while True:
            try:
                self._device.open()
                break
            except CouponOpenError:
                self._device.cancel()
            except PendingReduceZ:
                self._device.close_till()
            except PendingReadX:
                self._device.summarize()

    #
    # Tests
    #

    def test_add_item(self):
        self._open_coupon()
        # 1. Specify discount and surcharge at the same time
        self.failUnlessRaises(TypeError, self._device.add_item, u"123456",
                              u"Monitor LG Flatron T910B", Decimal("500"),
                              TAX_NONE, discount=Decimal("1"),
                              surcharge=Decimal("1"))

        # 2. Specify unit_desc with unit different from UNIT_CUSTOM
        self.failUnlessRaises(ValueError, self._device.add_item, u"123456",
                              u"Monitor LG Flatron T910B", Decimal("500"),
                              TAX_NONE, unit=UNIT_LITERS, unit_desc="XX")

        # 3. Specify unit as UNIT_CUSTOM and not supply a unit_desc
        self.failUnlessRaises(ValueError, self._device.add_item, u"123456",
                              u"Monitor LG Flatron T910B", Decimal("500"),
                              TAX_NONE, unit=UNIT_CUSTOM)

        # 4. Specify unit as UNIT_CUSTOM and unit_desc greater than 2 chars
        self.failUnlessRaises(ValueError, self._device.add_item, u"123456",
                              u"Monitor LG Flatron T910B", Decimal("500"),
                              TAX_NONE, unit=UNIT_CUSTOM, unit_desc="XXXX")

        # 5. Add item without price
        self.failUnlessRaises(InvalidValue, self._device.add_item, u"123456",
                              u"Monitor LG Flatron T910B", Decimal("0"),
                              TAX_NONE)

        #
        # 6. Dataregis specific: the first 6 chars of the product code must
        # be digits.
        item_id = self._device.add_item(u"ABCDEF", u"Monitor LG 775N",
                                        Decimal("10"), TAX_NONE,
                                        items_quantity=Decimal("2"))
        self.failUnless(item_id == 1, "Item id should be 1")

        # A "normal" item...
        item_id = self._device.add_item(u"987654", u"Monitor LG 775N",
                                        Decimal("10"), TAX_NONE,
                                        items_quantity=Decimal("1"))
        self.failUnless(item_id == 2, "Item id should be 2")

        # A item with customized unit
        item_id = self._device.add_item(u"123456", u"Monitor LG 775N",
                                        Decimal("10"), TAX_NONE,
                                        items_quantity=Decimal("1"),
                                        unit=UNIT_CUSTOM, unit_desc="Tx")
        self.failUnless(item_id == 3, "Item id should be 3")

        # A item with surcharge
        item_id = self._device.add_item(u"123456", u"Monitor LG 775N",
                                        Decimal("10"), TAX_NONE,
                                        items_quantity=Decimal("1"),
                                        surcharge=Decimal("1"))
        self.failUnless(item_id == 4, "Item id should be 4")

        # 7. Add item with coupon totalized
        self._device.totalize()
        self.failUnlessRaises(AlreadyTotalized,
            self._device.add_item, u"123456", u"Monitor LG Flatron T910B",
            Decimal("10"), TAX_NONE)
{'fraction_non_alphanumeric': 0.09323040380047506, 'fraction_numerical': 0.03167062549485352, 'mean_word_length': 1.8764588670651865, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '471923', 'n_tokens_mistral': 3082, 'n_tokens_neox': 2748, 'n_words': 774}