コード例 #1
0
ファイル: test_serializers.py プロジェクト: uktrade/tamato
def test_serializer_throws_error_if_max_size_is_too_small():
    """If EnvelopeSerializer gets an max_envelope_size_in_mb is an int < 4k it
    will raise a ValueError otherwise it does not raises no exception."""
    EnvelopeSerializer(io.StringIO(), envelope_id=1, max_envelope_size=4096)

    with pytest.raises(ValueError) as e:
        EnvelopeSerializer(io.StringIO(), envelope_id=1, max_envelope_size=4095)
    assert e.value.args == (
        "Max envelope size 4095 is too small, it should be at least 4096.",
    )
コード例 #2
0
 def __init__(
     self,
     output_constructor: callable,
     envelope_id=1,
     *args,
     **kwargs,
 ) -> None:
     self.output_constructor = output_constructor
     EnvelopeSerializer.__init__(self,
                                 self.output_constructor(),
                                 envelope_id=envelope_id,
                                 *args,
                                 **kwargs)
コード例 #3
0
ファイル: test_serializers.py プロジェクト: uktrade/tamato
def test_serializer_min_envelope_size_is_large_enough():
    """MIN_ENVELOPE_SIZE needs to be large enough to hold the templates the
    templates for the envelope and some content."""
    serializer = EnvelopeSerializer(io.StringIO(), 1)

    min_content_size = 1024  # minimum size for content.
    templates_size = len(
        (
            serializer.render_file_header()
            + serializer.render_envelope_start()
            + serializer.render_envelope_end()
        ).encode(),
    )

    assert EnvelopeSerializer.MIN_ENVELOPE_SIZE > templates_size + min_content_size
コード例 #4
0
ファイル: serializers.py プロジェクト: uktrade/tamato
 def __init__(
     self,
     output_constructor: callable,
     envelope_id=1,
     benchmark=False,
     *args,
     **kwargs,
 ) -> None:
     """
     :param output_constructor: callable that returns a file like object to write to, called each time a new envelope is started.
     :param envelope_id: Envelope ID, to use later, when creating Envelope objects in the database.
     :param args: Passed through to EnvelopeSerializer.
     :param kwargs: Passed through to EnvelopeSerializer.
     """
     self.output_constructor = output_constructor
     EnvelopeSerializer.__init__(self,
                                 self.output_constructor(),
                                 envelope_id=envelope_id,
                                 **kwargs)
コード例 #5
0
ファイル: test_serializers.py プロジェクト: uktrade/tamato
def test_envelope_serializer_outputs_expected_items(approved_workbasket):
    """EnvelopeSerializer should output all the models passed to it and
    generated records for descriptions."""
    # Transaction context manager is not used to create transactions,
    # as it creates phantom workbaskets and transactions which cause XSD failures later.
    tx = ApprovedTransactionFactory.create(workbasket=approved_workbasket)
    regulation = factories.RegulationFactory.create(
        transaction=tx,
        regulation_group=None,
    )
    footnote = factories.FootnoteFactory.create(
        transaction=tx,
    )  # FootnoteFactory also creates a FootnoteType and FootnoteDescription

    expected_items = [
        regulation,
        footnote.footnote_type,
        footnote,
        *footnote.descriptions.all(),
    ]  # < Note: These are sorted by record_code, subrecord_code
    expected_item_record_codes = {
        (o.record_code, o.subrecord_code) for o in expected_items
    }

    assertQuerysetEqual(
        expected_items,
        approved_workbasket.tracked_models.all(),
        transform=lambda o: o,
        ordered=False,
    )  # Some of the factories create other data so sanity check expected items.

    output = io.BytesIO()
    with EnvelopeSerializer(output, random.randint(2, 9999)) as env:
        env.render_transaction(
            models=approved_workbasket.tracked_models.all(),
            transaction_id=tx.order,
        )

    output_xml = etree.XML(output.getvalue())
    output_record_codes = {*taric_xml_record_codes(output_xml)}

    # More items than models are output, as models for descriptions aren't kept.
    assert (
        output_record_codes >= expected_item_record_codes
    ), f"Output ({output_record_codes}) missing some expected record codes"