def test_get_next_invoice_number(self): main_branch = get_current_branch(self.store) sale = self.create_sale(branch=main_branch) sale.invoice_number = 1234 self.add_product(sale) self.add_payments(sale, u'money') sale.order() sale.confirm() # Test when the Invoice table is empty. self.store.execute(Update({Sale.invoice_id: None}, table=Sale)) self.store.execute( Update({TransferOrder.invoice_id: None}, table=TransferOrder)) self.clean_domain([Invoice]) last_invoice_number = Invoice.get_last_invoice_number(self.store) next_invoice_number = Invoice.get_next_invoice_number(self.store) self.assertEquals(last_invoice_number, 1234) self.assertEquals(next_invoice_number, 1235) # Creating a transfer order on same branch. transfer = self.create_transfer_order(source_branch=main_branch) transfer.invoice_number = next_invoice_number self.create_transfer_order_item(transfer) transfer.send() next_invoice_number = Invoice.get_next_invoice_number(self.store) self.assertEquals(transfer.invoice.invoice_number, 1235) self.assertEquals(next_invoice_number, 1236) # Creating a new sale and new tranfer on a different branch with mock.patch( 'stoqlib.domain.fiscal.get_current_branch') as get_branch: new_branch = self.create_branch() get_branch.return_value = new_branch new_sale = self.create_sale(branch=new_branch) new_sale.invoice_number = 1234 last_invoice_number = Invoice.get_last_invoice_number(self.store) next_invoice_number = Invoice.get_next_invoice_number(self.store) self.assertEquals(last_invoice_number, 1234) self.assertEquals(next_invoice_number, 1235) new_transfer = self.create_transfer_order(source_branch=new_branch) new_transfer.invoice_number = next_invoice_number self.create_transfer_order_item(new_transfer) new_transfer.send() next_invoice_number = Invoice.get_next_invoice_number(self.store) self.assertEquals(new_transfer.invoice.invoice_number, 1235) self.assertEquals(next_invoice_number, 1236)
def merge_with(self, other, skip=None, copy_empty_values=True): """Does automatic references updating when merging two objects. This will update all tables that reference the `other` object and make them reference `self` instead. After this it should be safe to remove the `other` object. Since there is no one referencing it anymore. :param skip: A set of (table, column) that should be skiped by the automatic update. This are normally tables that require a special treatment, like when there are constraints. :param copy_empty_values: If True, attributes that are either null or an empty string in self will be updated with the value from the other object (given that the other attribute is not empty as well) """ skip = skip or set() event_skip = DomainMergeEvent.emit(self, other) if event_skip: skip = skip.union(event_skip) if copy_empty_values: self.copy_empty_values(other) refs = self.store.list_references(type(self).id) for (table, column, other_table, other_column, u, d) in refs: if (table, column) in skip: continue clause = Field(table, column) == other.id self.store.execute(Update({column: self.id}, clause, table))
def test_returning_update(self): update = Update({column1: elem1}, table=table1, primary_columns=(column2, column3)) self.assertEquals( compile(Returning(update)), 'UPDATE "table 1" SET column1=elem1 ' 'RETURNING column2, column3')
def test_returning_update_with_columns(self): update = Update({column1: elem1}, table=table1, primary_columns=(column2, column3)) assert compile(Returning(update, columns=[ column3 ])) == ('UPDATE "table 1" SET column1=elem1 RETURNING column3')
def test_create_without_category(self): # Removing data from SellableCategory, so we can test the validation of # category_combo update when there is no category. self.store.execute(Update({Sellable.category_id: None}, table=Sellable)) self.clean_domain([CommissionSource, SellableCategory]) editor = ProductEditor(self.store) editor.code.update("12345") self.assertNotSensitive(editor, ['category_combo']) self.check_editor(editor, 'editor-product-create-no-category')
def test_execute_update_returning(self): if self.database._version < 80200: return # Can't run this test with old PostgreSQL versions. column1 = Column("id1", "returning_test") column2 = Column("id2", "returning_test") self.connection.execute("INSERT INTO returning_test VALUES (1, 2)") update = Update({"id2": 3}, column1 == 1, primary_columns=(column1, column2)) result = self.connection.execute(Returning(update)) self.assertEquals(result.get_one(), (1, 3))
def delete(cls, id, store): """Removes a device from the database. Since devices may be referenced by |cardcost| and |creditcarddata| objects, this method will also: * Remove all |cardcost| objects * Update all references to this device by |creditcarddata| objects to ``None``. """ CardOperationCost.delete_from_device(id, store) vals = {CreditCardData.device_id: None} clause = CreditCardData.device_id == id store.execute(Update(vals, clause, CreditCardData)) store.remove(store.get(cls, id))