Exemplo n.º 1
0
def test_extract_fails_with_invalid_date(ai_code: str, bad_value: str) -> None:
    ai = GS1ApplicationIdentifier.extract(ai_code)

    with pytest.raises(ParseError) as exc_info:
        GS1ElementString.extract(f"{ai_code}{bad_value}")

    assert (str(exc_info.value) ==
            f"Failed to parse GS1 AI {ai} date from {bad_value!r}.")
Exemplo n.º 2
0
def test_extract_fails_when_not_matching_pattern(ai_code: str,
                                                 bad_value: str) -> None:
    ai = GS1ApplicationIdentifier.extract(ai_code)

    with pytest.raises(ParseError) as exc_info:
        GS1ElementString.extract(bad_value)

    assert (
        str(exc_info.value) ==
        f"Failed to match {bad_value!r} with GS1 AI {ai} pattern '{ai.pattern}'."
    )
Exemplo n.º 3
0
def test_extract_amount_payable_and_currency(
        value: str, expected_currency: str, expected_decimal: Decimal) -> None:
    element_string = GS1ElementString.extract(value)

    assert element_string.decimal == expected_decimal

    # Optional: If py-moneyed is installed, create Money instances
    assert element_string.money is not None
    assert element_string.money.amount == expected_decimal
    assert element_string.money.currency.code == expected_currency
Exemplo n.º 4
0
    def parse(
        cls,
        value: str,
        *,
        rcn_region: Optional[RcnRegion] = None,
        separator_chars: Iterable[str] = DEFAULT_SEPARATOR_CHARS,
    ) -> GS1Message:
        """Parse a string from a barcode scan as a GS1 message with AIs.

        Args:
            value: The string to parse.
            rcn_region: The geographical region whose rules should be used to
                interpret Restricted Circulation Numbers (RCN).
                Needed to extract e.g. variable weight/price from GTIN.
            separator_chars: Characters used in place of the FNC1 symbol.
                Defaults to `<GS>` (ASCII value 29).
                If variable-length fields in the middle of the message are
                not terminated with a separator character, the parser might
                greedily consume the rest of the message.

        Returns:
            A message object with one or more element strings.

        Raises:
            ParseError: If a fixed-length field ends with a separator character.
        """
        value = value.strip()
        element_strings = []
        rest = value[:]

        while rest:
            element_string = GS1ElementString.extract(
                rest, rcn_region=rcn_region, separator_chars=separator_chars)
            element_strings.append(element_string)

            rest = rest[len(element_string):]

            if rest.startswith(tuple(separator_chars)):
                if element_string.ai.fnc1_required:
                    rest = rest[1:]
                else:
                    separator_char = rest[0]
                    raise ParseError(
                        f"Element String {element_string.as_hri()!r} has fixed length "
                        "and should not end with a separator character. "
                        f"Separator character {separator_char!r} found in {value!r}."
                    )

        return cls(value=value, element_strings=element_strings)
Exemplo n.º 5
0
def test_extract(value: str, expected: GS1ElementString) -> None:
    assert GS1ElementString.extract(value) == expected
Exemplo n.º 6
0
def test_as_hri(value: str, expected: str) -> None:
    assert GS1ElementString.extract(value).as_hri() == expected
Exemplo n.º 7
0
def test_extract_percentage_discount(value: str, expected: Decimal) -> None:
    assert GS1ElementString.extract(value).decimal == expected
Exemplo n.º 8
0
def test_extract_amount_payable(value: str, expected: Decimal) -> None:
    assert GS1ElementString.extract(value).decimal == expected
Exemplo n.º 9
0
def test_extract_variable_measures(value: str, expected: Decimal) -> None:
    assert GS1ElementString.extract(value).decimal == expected
Exemplo n.º 10
0
def test_extract_handles_zero_day_as_last_day_of_month(value: str,
                                                       expected: date) -> None:
    assert GS1ElementString.extract(value).date == expected
Exemplo n.º 11
0
def test_extract_handles_min_and_max_year_correctly(
        value: str, expected: GS1ElementString) -> None:
    assert GS1ElementString.extract(value) == expected
Exemplo n.º 12
0
from biip.gs1 import GS1ApplicationIdentifier, GS1ElementString, GS1Prefix
from biip.gtin import Gtin, GtinFormat
from biip.sscc import Sscc


@pytest.mark.parametrize(
    "value, expected",
    [
        (
            "00373400306809981733",
            GS1ElementString(
                ai=GS1ApplicationIdentifier.extract("00"),
                value="373400306809981733",
                pattern_groups=["373400306809981733"],
                sscc=Sscc(
                    value="373400306809981733",
                    prefix=GS1Prefix(value="734", usage="GS1 Sweden"),
                    extension_digit=3,
                    payload="37340030680998173",
                    check_digit=3,
                ),
            ),
        ),
        (
            "0107032069804988",
            GS1ElementString(
                ai=GS1ApplicationIdentifier.extract("01"),
                value="07032069804988",
                pattern_groups=["07032069804988"],
                gtin=Gtin(
                    value="07032069804988",
                    format=GtinFormat.GTIN_13,
Exemplo n.º 13
0
 [
     (
         "010703206980498815210526100329",
         GS1Message(
             value="010703206980498815210526100329",
             element_strings=[
                 GS1ElementString(
                     ai=GS1ApplicationIdentifier(
                         ai="01",
                         description="Global Trade Item Number (GTIN)",
                         data_title="GTIN",
                         fnc1_required=False,
                         format="N2+N14",
                         pattern="^01(\\d{14})$",
                     ),
                     value="07032069804988",
                     pattern_groups=["07032069804988"],
                     gtin=Gtin(
                         value="07032069804988",
                         format=GtinFormat.GTIN_13,
                         prefix=GS1Prefix(value="703", usage="GS1 Norway"),
                         payload="703206980498",
                         check_digit=8,
                     ),
                 ),
                 GS1ElementString(
                     ai=GS1ApplicationIdentifier(
                         ai="15",
                         description="Best before date (YYMMDD)",
                         data_title="BEST BEFORE or BEST BY",
                         fnc1_required=False,
Exemplo n.º 14
0
     "96385074",
     ParseResult(
         value="96385074",
         gtin=Gtin(
             value="96385074",
             format=GtinFormat.GTIN_8,
             prefix=GS1Prefix(value="00009", usage="GS1 US"),
             payload="9638507",
             check_digit=4,
         ),
         gs1_message=GS1Message(
             value="96385074",
             element_strings=[
                 GS1ElementString(
                     ai=GS1ApplicationIdentifier.extract("96"),
                     value="385074",
                     pattern_groups=["385074"],
                 )
             ],
         ),
         sscc_error=
         ("Failed to parse '96385074' as SSCC: Expected 18 digits, got 8."
          ),
     ),
 ),
 (
     # GTIN-12
     "123601057072",
     ParseResult(
         value="123601057072",
         gtin=Gtin(