def test_batch_number_suggestion(self):
        branch = api.get_current_branch(self.store)
        branch.acronym = u'AB'

        storable = self.create_storable(is_batch=True)
        storable2 = self.create_storable(is_batch=True)

        dialog = BatchIncreaseSelectionDialog(self.store, storable, 10)
        self.assertEqual(dialog._last_entry.get_text(), '')

        api.sysparam(self.store).update_parameter(u'SUGGEST_BATCH_NUMBER', u'1')
        try:
            storable.register_initial_stock(1, self.create_branch(), 0,
                                            batch_number=u'123')
            dialog = BatchIncreaseSelectionDialog(self.store, storable, 10)
            # Make sure it suggested right
            self.assertEqual(dialog._last_entry.get_text(), '124')

            spinbutton = dialog.get_spin_by_entry(dialog._last_entry)
            spinbutton.update(5)
            # Updating the spinbutton should append a new entry with the suggestion
            self.assertEqual(dialog._last_entry.get_text(), '125')
            self.click(dialog.main_dialog.ok_button)

            dialog = BatchIncreaseSelectionDialog(self.store, storable2, 10)
            # Since the dialog above was confirmed on the same store this one is,
            # it should consider it's batch numbers for the next suggestion
            self.assertEqual(dialog._last_entry.get_text(), '126')
        finally:
            api.sysparam(self.store).update_parameter(u'SUGGEST_BATCH_NUMBER', u'0')
    def test_batch_number_suggestion_synchronized_mode(self):
        branch = api.get_current_branch(self.store)
        branch.acronym = u'AB'

        storable = self.create_storable(is_batch=True)
        storable2 = self.create_storable(is_batch=True)

        dialog = BatchIncreaseSelectionDialog(self.store, storable, 10)
        self.assertEqual(dialog._last_entry.get_text(), '')

        with self.sysparam(SUGGEST_BATCH_NUMBER=True, SYNCHRONIZED_MODE=True):
            storable.register_initial_stock(1, self.create_branch(), 0,
                                            batch_number=u'130')
            dialog = BatchIncreaseSelectionDialog(self.store, storable, 10)
            # Make sure it suggested right
            self.assertEqual(dialog._last_entry.get_text(), '131-AB')

            spinbutton = dialog.get_spin_by_entry(dialog._last_entry)
            spinbutton.update(5)
            # Updating the spinbutton should append a new entry with the suggestion
            self.assertEqual(dialog._last_entry.get_text(), '132-AB')
            self.click(dialog.main_dialog.ok_button)

            dialog = BatchIncreaseSelectionDialog(self.store, storable2, 10)
            # Since the dialog above was confirmed on the same store this one is,
            # it should consider it's batch numbers for the next suggestion
            self.assertEqual(dialog._last_entry.get_text(), '133-AB')

            branch.acronym = None
            spinbutton = dialog.get_spin_by_entry(dialog._last_entry)
    def test_get_batch_item(self):
        storable = self.create_storable()
        batch = self.create_storable_batch(storable, batch_number=u'123456')
        dialog = BatchIncreaseSelectionDialog(self.store, storable, 10)

        self.assertEqual(dialog.get_batch_item(batch), u'123456')
        self.assertEqual(dialog.get_batch_item(batch.batch_number), u'123456')
        self.assertEqual(dialog.get_batch_item(None), None)
    def test_get_batch_item(self):
        storable = self.create_storable()
        batch = self.create_storable_batch(storable, batch_number=u'123456')
        dialog = BatchIncreaseSelectionDialog(self.store, storable, 10)

        self.assertEqual(dialog.get_batch_item(batch), u'123456')
        self.assertEqual(dialog.get_batch_item(batch.batch_number), u'123456')
        self.assertEqual(dialog.get_batch_item(None), None)
    def test_batch_number_suggestion_synchronized_mode(self):
        branch = api.get_current_branch(self.store)
        branch.acronym = u'AB'

        storable = self.create_storable(is_batch=True)
        storable2 = self.create_storable(is_batch=True)

        dialog = BatchIncreaseSelectionDialog(self.store, storable, 10)
        self.assertEqual(dialog._last_entry.get_text(), '')

        with self.sysparam(SUGGEST_BATCH_NUMBER=True, SYNCHRONIZED_MODE=True):
            storable.register_initial_stock(1, self.create_branch(), 0,
                                            batch_number=u'130')
            dialog = BatchIncreaseSelectionDialog(self.store, storable, 10)
            # Make sure it suggested right
            self.assertEqual(dialog._last_entry.get_text(), '131-AB')

            spinbutton = dialog.get_spin_by_entry(dialog._last_entry)
            spinbutton.update(5)
            # Updating the spinbutton should append a new entry with the suggestion
            self.assertEqual(dialog._last_entry.get_text(), '132-AB')
            self.click(dialog.main_dialog.ok_button)

            dialog = BatchIncreaseSelectionDialog(self.store, storable2, 10)
            # Since the dialog above was confirmed on the same store this one is,
            # it should consider it's batch numbers for the next suggestion
            self.assertEqual(dialog._last_entry.get_text(), '133-AB')

            branch.acronym = None
            spinbutton = dialog.get_spin_by_entry(dialog._last_entry)
    def test_batch_number_suggestion_synchronized_mode(self):
        branch = api.get_current_branch(self.store)
        branch.acronym = u'AB'

        storable = self.create_storable(is_batch=True)
        storable2 = self.create_storable(is_batch=True)

        dialog = BatchIncreaseSelectionDialog(self.store, storable, 10)
        self.assertEqual(dialog._last_entry.get_text(), '')

        try:
            api.sysparam(self.store).update_parameter(u'SUGGEST_BATCH_NUMBER', u'1')
            # We need to do this by hand sincr update_parameter won't let us
            # update value for some parameters (SYNCHRONIZED_MODE is one of them)
            api.sysparam(self.store).SYNCHRONIZED_MODE = u'1'
            api.sysparam(self.store).rebuild_cache_for(u'SYNCHRONIZED_MODE')
            storable.register_initial_stock(1, self.create_branch(), 0,
                                            batch_number=u'130')
            dialog = BatchIncreaseSelectionDialog(self.store, storable, 10)
            # Make sure it suggested right
            self.assertEqual(dialog._last_entry.get_text(), '131-AB')

            spinbutton = dialog.get_spin_by_entry(dialog._last_entry)
            spinbutton.update(5)
            # Updating the spinbutton should append a new entry with the suggestion
            self.assertEqual(dialog._last_entry.get_text(), '132-AB')
            self.click(dialog.main_dialog.ok_button)

            dialog = BatchIncreaseSelectionDialog(self.store, storable2, 10)
            # Since the dialog above was confirmed on the same store this one is,
            # it should consider it's batch numbers for the next suggestion
            self.assertEqual(dialog._last_entry.get_text(), '133-AB')

            branch.acronym = None
            spinbutton = dialog.get_spin_by_entry(dialog._last_entry)
            # FIXME: Why is this not working? If i put a print before .update
            # and one after, I can see the traceback raises between the prints,
            # GUITest will say that there was an unhandled exception (this one)
            # and assertRaisesRegexp will say that ValueError wasn't raised. WTF???
            #with self.assertRaisesRegexp(
            #    ValueError,
            #    ("branch 'Moda Stoq' needs an acronym since we are on "
            #     "synchronized mode")):
            #    spinbutton.update(1)
        finally:
            api.sysparam(self.store).update_parameter(u'SUGGEST_BATCH_NUMBER', u'0')
            api.sysparam(self.store).SYNCHRONIZED_MODE = u'0'
            api.sysparam(self.store).rebuild_cache_for(u'SYNCHRONIZED_MODE')
    def test_batch_number_validation(self):
        storable = self.create_storable(is_batch=True)
        storable2 = self.create_storable(is_batch=True)
        self.create_storable_batch(storable=storable, batch_number=u'123')
        self.create_storable_batch(storable=storable, batch_number=u'124')

        dialog = BatchIncreaseSelectionDialog(self.store, storable2, 10)
        dialog._last_entry.update(u'123')
        self.assertInvalid(dialog, ['_last_entry'])
        dialog._last_entry.update(u'126')
        self.assertValid(dialog, ['_last_entry'])