def test_new_envelope_enforces_daily_limit(): factories.EnvelopeFactory.create(envelope_id="249999") with pytest.raises(ValueError) as e: Envelope.new_envelope() assert e.value.args == ( "Cannot create more than 9999 Envelopes on a single year.", )
def test_new_envelope_populates_envelope_id(): """Verify Envelope.new_envelope correctly populates envelope_id.""" # Create 3 envelopes: the first envelope in a year uses # different logic to subsequent years, # this verifies that ids increment in both cases. envelope1 = Envelope.new_envelope() assert envelope1.envelope_id == "300001" envelope2 = Envelope.new_envelope() assert envelope2.envelope_id == "300002" envelope3 = Envelope.new_envelope() assert envelope3.envelope_id == "300003"
def upload_and_create_envelopes( workbaskets: QuerySet, rendered_envelopes: Sequence[RenderedTransactions], first_envelope_id, ) -> UploadTaskResultData: """ Upload Envelope data to the the s3 and create artifacts in the database. Side effects on success: Create Envelope, EnvelopeTransaction and Upload objects in the database and upload envelope XML to an S3 object. :return: :class:`~exporter.util.UploadTaskResultData`. """ # upload_status holds data to pass to the next Task, including messages to the user. upload_status = UploadTaskResultData() current_envelope_id = first_envelope_id for rendered_envelope in rendered_envelopes: envelope = Envelope.new_envelope() if current_envelope_id != int(envelope.envelope_id): logger.error( "Envelope created out of sequence: %s != %i this may be due to simultaneous updates causing a race " "condition.", (current_envelope_id, int(envelope.envelope_id)), ) raise RaceCondition( f"Envelope out of sequence: {envelope.envelope_id} != {current_envelope_id}", ) current_envelope_id = int(envelope.envelope_id) envelope_transactions = [ EnvelopeTransaction(order=order, envelope=envelope, transaction=transaction) for order, transaction in enumerate(rendered_envelope.transactions) ] EnvelopeTransaction.objects.bulk_create(envelope_transactions) envelope.save() rendered_envelope.output.seek(0, os.SEEK_SET) content_file = ContentFile(rendered_envelope.output.read()) upload = Upload() upload.envelope = envelope upload.file = content_file rendered_envelope.output.seek(0, os.SEEK_SET) upload.checksum = md5(rendered_envelope.output.read()).hexdigest() upload.file.save(upload.filename, content_file) upload_status.add_upload_pk(upload.pk) logger.info("Workbasket saved to CDS S3 bucket") workbaskets.update(status=WorkflowStatus.SENT) logger.debug("Uploaded: %s", upload.filename) upload_status.add_envelope_messages( envelope.envelope_id, [f"Uploaded {upload.filename}"], ) return upload_status
def upload_and_create_envelopes( workbaskets: QuerySet, rendered_envelopes: Sequence[RenderedTransactions], first_envelope_id, ) -> Dict[Union[int, None], str]: # {envelope_id: message} User messages can be returned to the caller of the task. user_messages = {} current_envelope_id = first_envelope_id for rendered_envelope in rendered_envelopes: envelope = Envelope.new_envelope() if current_envelope_id != int(envelope.envelope_id): # TODO consider locking the table for writes instead logger.error( "Envelope created out of sequence: %s != %s this may due to simultaneous updates causing a race condition.", (current_envelope_id, int(envelope.envelope_id)), ) raise RaceCondition( f"Envelope out of sequence: {envelope.envelope_id} != {current_envelope_id}", ) current_envelope_id = int(envelope.envelope_id) envelope_transactions = [ EnvelopeTransaction(order=order, envelope=envelope, transaction=transaction) for order, transaction in enumerate(rendered_envelope.transactions) ] EnvelopeTransaction.objects.bulk_create(envelope_transactions) envelope.save() rendered_envelope.output.seek(0, os.SEEK_SET) content_file = ContentFile(rendered_envelope.output.read()) upload = Upload() upload.envelope = envelope upload.file = content_file rendered_envelope.output.seek(0, os.SEEK_SET) upload.checksum = md5(rendered_envelope.output.read()).hexdigest() upload.file.save(upload.filename, content_file) if settings.EXPORTER_DISABLE_NOTIFICATION: logger.info("HMRC notification disabled.") else: logger.info("Notify HMRC of upload, %s", upload.filename) upload.notify_hmrc() # sets notification_sent logger.info("Workbasket sent to CDS") workbaskets.update(status=WorkflowStatus.SENT_TO_CDS) logger.debug("Uploaded: %s", upload.filename) user_messages[envelope.envelope_id] = f"Uploaded {upload.filename}" return user_messages