def test_max_retries_reached(self): """ Test that if there are n max collisions, the function raises RuntimeError. """ model = mock.Mock() model.objects.select_for_update().filter.return_value = [] model.objects.filter().exists.side_effect = [True] * 10 with pytest.raises(RuntimeError): generate_datetime_based_reference(model)
def test_defaults(self): """Test the value with default params.""" model = mock.Mock() model.objects.select_for_update().filter.return_value = [] model.objects.filter().exists.return_value = False reference = generate_datetime_based_reference(model) assert reference == '201704180001'
def test_with_collision(self): """ Test that if there's already a record with that reference, the seq part is incremented. """ model = mock.Mock() model.objects.select_for_update().filter.return_value = [] model.objects.filter().exists.side_effect = [True, False] reference = generate_datetime_based_reference(model) assert reference == '201704180002'
def test_with_prefix(self): """ Test that if a prefix is specified, it will be used to generate the reference. """ model = mock.Mock() model.objects.select_for_update().filter.return_value = [] model.objects.filter().exists.return_value = False reference = generate_datetime_based_reference(model, prefix='pref/') assert reference == 'pref/201704180001'
def test_non_first_record_of_day(self): """ Test that if there are already some record for that day, the seq part starts counting from the next number. """ model = mock.Mock() model.objects.select_for_update().filter.return_value = [ mock.Mock(), mock.Mock() ] model.objects.filter().exists.return_value = False reference = generate_datetime_based_reference(model) assert reference == '201704180003'
def create_from_order(self, order, by, attrs): """ :param order: Order instance for this payment :param by: the Advisor who made the action :param attrs: attributes for the payment :returns: Payment object created """ return self.create( **attrs, reference=generate_datetime_based_reference(self.model), order=order, created_by=by, )
def test_first_invoice_number_of_the_day(self): """Test that the first invoice number of the day is generated as expected.""" # create some in different months/days dts = ( '2017-01-01 13:00:00', '2017-03-01 13:00:00', '2016-02-01 13:00:00', '2018-02-01 13:00:00', ) for dt in dts: with freeze_time(dt): OrderWithAcceptedQuoteFactory() assert Invoice.objects.count() == 4 with freeze_time('2017-02-01 13:00:00'): invoice_number = generate_datetime_based_reference(Invoice, field='invoice_number') assert invoice_number == '201702010001'
def create_from_order(self, order): """ :param order: Order instance for this invoice :returns: Invoice object generated from the order """ return self.create( order_reference=order.reference, invoice_number=generate_datetime_based_reference( self.model, field='invoice_number'), payment_due_date=calculate_payment_due_date(order), billing_company_name=order.billing_company_name, billing_address_1=order.billing_address_1, billing_address_2=order.billing_address_2, billing_address_town=order.billing_address_town, billing_address_county=order.billing_address_county, billing_address_postcode=order.billing_address_postcode, billing_address_country=order.billing_address_country, po_number=order.po_number, invoice_company_name=constants.DIT_COMPANY_NAME, invoice_address_1=constants.DIT_ADDRESS_1, invoice_address_2=constants.DIT_ADDRESS_2, invoice_address_town=constants.DIT_ADDRESS_TOWN, invoice_address_county=constants.DIT_ADDRESS_COUNTY, invoice_address_postcode=constants.DIT_ADDRESS_POSTCODE, invoice_address_country_id=constants.DIT_ADDRESS_COUNTRY_ID, invoice_vat_number=constants.DIT_VAT_NUMBER, contact_email=order.get_current_contact_email(), vat_status=order.vat_status, vat_number=order.vat_number, vat_verified=order.vat_verified, net_cost=order.net_cost, subtotal_cost=order.subtotal_cost, vat_cost=order.vat_cost, total_cost=order.total_cost, )
def save(self, *args, **kwargs): """Generate a reference if necessary.""" if not self.reference: self.reference = generate_datetime_based_reference(self.__class__, field='reference') super().save(*args, **kwargs)