def test_existing_batches(self):
        branch = api.get_current_branch(self.store)
        storable = self.create_storable(is_batch=True)
        batch1 = self.create_storable_batch(storable=storable,
                                            batch_number=u'1')
        batch2 = self.create_storable_batch(storable=storable,
                                            batch_number=u'2')
        self.create_storable_batch(storable=storable, batch_number=u'3')

        storable.increase_stock(10, branch, 0, None, batch=batch1)
        storable.increase_stock(10, branch, 0, None, batch=batch2)

        dialog = BatchSelectionDialog(self.store, storable, 5)
        # The last batch should not appear since it doesn't have a storable
        self.assertEqual(set([view.batch for view in dialog.existing_batches]),
                         set([batch1, batch2]))

        last_spin = dialog.get_spin_by_entry(dialog._last_entry)
        # The spin (at the moment, the only one appended) should be filled
        # with the total value (100, passed on the constructor)
        self.assertEqual(last_spin.read(), 5)
        view1 = self.store.find(StorableBatchView,
                                StorableBatchView.id == batch1.id).one()
        dialog.existing_batches.emit('row-activated', view1)
        self.assertEqual(last_spin.read(), 5)
    def test_existing_batches(self):
        branch = api.get_current_branch(self.store)
        storable = self.create_storable(is_batch=True)
        batch1 = self.create_storable_batch(storable=storable,
                                            batch_number=u'1')
        batch2 = self.create_storable_batch(storable=storable,
                                            batch_number=u'2')
        self.create_storable_batch(storable=storable, batch_number=u'3')

        storable.increase_stock(10, branch,
                                StockTransactionHistory.TYPE_INITIAL,
                                None, batch=batch1)
        storable.increase_stock(10, branch,
                                StockTransactionHistory.TYPE_INITIAL,
                                None, batch=batch2)

        dialog = BatchSelectionDialog(self.store, storable, 5)
        # The last batch should not appear since it doesn't have a storable
        self.assertEqual(set([view.batch for view in dialog.existing_batches]),
                         set([batch1, batch2]))

        last_spin = dialog.get_spin_by_entry(dialog._last_entry)
        # The spin (at the moment, the only one appended) should be filled
        # with the total value (100, passed on the constructor)
        self.assertEqual(last_spin.read(), 5)
        view1 = self.store.find(StorableBatchView,
                                StorableBatchView.id == batch1.id).one()
        dialog.existing_batches.emit('row-activated', view1)
        self.assertEqual(last_spin.read(), 5)
    def test_append_or_update_dumb_row(self):
        storable = self.create_storable(is_batch=True)
        dialog = BatchSelectionDialog(self.store, storable, 0)
        last_entry = dialog._last_entry
        self.assertEqual(len(dialog._entries), 1)

        # This should not append a new row, since there's no diff
        # quantity (quantity was passed as 0 on __init__)
        spinbutton = dialog.get_spin_by_entry(last_entry)
        spinbutton.update(10)
        self.assertIs(dialog._last_entry, last_entry)
        self.assertEqual(len(dialog._entries), 1)
    def test_append_or_update_dumb_row(self):
        storable = self.create_storable(is_batch=True)
        dialog = BatchSelectionDialog(self.store, storable, 0)
        last_entry = dialog._last_entry
        self.assertEqual(len(dialog._entries), 1)

        # This should not append a new row, since there's no diff
        # quantity (quantity was passed as 0 on __init__)
        spinbutton = dialog.get_spin_by_entry(last_entry)
        spinbutton.update(10)
        self.assertIs(dialog._last_entry, last_entry)
        self.assertEqual(len(dialog._entries), 1)
    def test_create(self):
        storable = self.create_storable(is_batch=True)
        batch = self.create_storable_batch(storable=storable,
                                           batch_number=u'1')
        batch.create_date = datetime.date(2010, 10, 10)
        batch = self.create_storable_batch(storable=storable,
                                           batch_number=u'2')
        batch.create_date = datetime.date(2011, 11, 11)
        batch = self.create_storable_batch(storable=storable,
                                           batch_number=u'3')
        batch.create_date = datetime.date(2012, 12, 12)

        storable.register_initial_stock(10, api.get_current_branch(self.store),
                                        1, u'1')
        storable.register_initial_stock(15, api.get_current_branch(self.store),
                                        1, u'2')
        storable.register_initial_stock(8, api.get_current_branch(self.store),
                                        1, u'3')

        dialog = BatchSelectionDialog(self.store, storable, 33)
        for entry in list(dialog._spins.keys()):
            entry.update(1)
            dialog._spins[entry].update(12)

        for entry in list(dialog._spins.keys())[1:]:
            entry.update(2)
            dialog._spins[entry].update(7)

        for entry in list(dialog._spins.keys())[2:]:
            entry.update(3)
            dialog._spins[entry].update(8)

        dialog.existing_batches_expander.set_expanded(True)

        self.check_dialog(dialog, 'dialog-batch-selection-dialog-create')