1717import pytest
1818
1919from price_parser import Price
20+ from price_parser .parser import extract_currency_symbol
2021
2122
2223class Example (Price ):
@@ -625,8 +626,6 @@ def __eq__(self, other):
625626 'Р' , '30' , 30 ),
626627 Example ('€' , '€ 139.00' ,
627628 '€' , '139.00' , 139 ),
628- Example ('There are 163 products.' , 'From 26 to 50 €' ,
629- '€' , '26' , 26 ),
630629 Example ('Pris NOK 1 999,00' , '139,00' ,
631630 'NOK' , '139,00' , 139 ),
632631 Example ('/sqft' , '1.52' ,
@@ -1909,15 +1908,55 @@ def __eq__(self, other):
19091908 'CHF' , '19.90' , 19.90 ),
19101909 Example ('' , '530,42 Zł' ,
19111910 'Zł' , '530,42' , 530.42 ),
1911+
1912+ # Prefer values next to currency symbols
1913+ Example ('3 Ausgaben für nur 14,85 EUR' , '3 Ausgaben für nur 14,85 EUR' ,
1914+ 'EUR' , '14,85' , 14.85 ),
1915+ Example (None , '2 items at 24,00€' ,
1916+ '€' , '24,00' , 24.00 ),
1917+ Example (None , '2 items at 24,00 €' ,
1918+ '€' , '24,00' , 24.00 ),
1919+ Example (None , '2 items at €24,00' ,
1920+ '€' , '24,00' , 24.00 ),
1921+ Example (None , '2 items at € 24,00' ,
1922+ '€' , '24,00' , 24.00 ),
1923+ Example (None , '2 items at 24,00€ or 30,00€' ,
1924+ '€' , '24,00' , 24.00 ),
1925+ Example (None , '2 items at 24,00€ or 30,00 €' ,
1926+ '€' , '24,00' , 24.00 ),
1927+ Example (None , '2 items at 24,00€ or €30,00' ,
1928+ '€' , '24,00' , 24.00 ),
1929+ Example (None , '2 items at 24,00€ or € 30,00' ,
1930+ '€' , '24,00' , 24.00 ),
1931+ Example (None , '2 items at 24,00 € or 30,00€' ,
1932+ '€' , '24,00' , 24.00 ),
1933+ Example (None , '2 items at 24,00 € or 30,00 €' ,
1934+ '€' , '24,00' , 24.00 ),
1935+ Example (None , '2 items at 24,00 € or €30,00' ,
1936+ '€' , '24,00' , 24.00 ),
1937+ Example (None , '2 items at 24,00 € or € 30,00' ,
1938+ '€' , '24,00' , 24.00 ),
1939+ Example (None , '2 items at €24,00 or 30,00€' ,
1940+ '€' , '24,00' , 24.00 ),
1941+ Example (None , '2 items at €24,00 or 30,00 €' ,
1942+ '€' , '24,00' , 24.00 ),
1943+ Example (None , '2 items at €24,00 or €30,00' ,
1944+ '€' , '24,00' , 24.00 ),
1945+ Example (None , '2 items at €24,00 or € 30,00' ,
1946+ '€' , '24,00' , 24.00 ),
1947+ Example (None , '2 items at € 24,00 or 30,00€' ,
1948+ '€' , '24,00' , 24.00 ),
1949+ Example (None , '2 items at € 24,00 or 30,00 €' ,
1950+ '€' , '24,00' , 24.00 ),
1951+ Example (None , '2 items at € 24,00 or €30,00' ,
1952+ '€' , '24,00' , 24.00 ),
1953+ Example (None , '2 items at € 24,00 or € 30,00' ,
1954+ '€' , '24,00' , 24.00 ),
19121955]
19131956
19141957
19151958PRICE_PARSING_EXAMPLES_XFAIL = [
19161959 # amount is picked as a price
1917- Example ('3 Ausgaben für nur 14,85 EUR' , '3 Ausgaben für nur 14,85 EUR' ,
1918- 'EUR' , '14,85' , 14.85 ),
1919- Example (None , 'Buy Now - 2 Litre Was $120.00 Now $60.00' ,
1920- '$' , '60.00' , 60 ),
19211960 Example ('Цена: уточняйте (мин. заказ: 1 )' , 'Цена: уточняйте (мин. заказ: 1 )' ,
19221961 None , None , None ),
19231962 Example (None , '50 - $2.00 100 - $2.75 400 - $4.50 1,000 - $9.00 2,000 - $17.00 3,000 - $24.00 10,000 - $75.00' ,
@@ -1931,6 +1970,14 @@ def __eq__(self, other):
19311970 Example ('Cuneo' , '61.858 L' , # Romanian New Leu
19321971 'L' , '61.858' , 61858 ),
19331972
1973+ # no handling of price ranges
1974+ Example ('There are 163 products.' , 'From 26 to 50 €' ,
1975+ '€' , '26' , 26 ),
1976+
1977+ # no handling of old-vs-new prices
1978+ Example (None , 'Buy Now - 2 Litre Was $120.00 Now $60.00' ,
1979+ '$' , '60.00' , 60 ),
1980+
19341981 # "р" / "руб" is detected as currency
19351982 Example ('>' , 'См. цену в прайсе' ,
19361983 None , None , None ),
@@ -1977,6 +2024,30 @@ def test_parsing(example: Example):
19772024 assert parsed == example
19782025
19792026
2027+ @pytest .mark .parametrize (
2028+ "input_string,symbol" ,
2029+ (
2030+ # no currency
2031+ ('' , None ),
2032+ ('1' , None ),
2033+
2034+ # fictional currency
2035+ ('10 eddies' , None ),
2036+
2037+ # cyrrency code
2038+ ('5 CNY' , 'CNY' ),
2039+
2040+ # cyrrency name
2041+ ('5 euros' , 'euro' ),
2042+
2043+ # currency symbol
2044+ ('$4' , '$' ),
2045+ )
2046+ )
2047+ def test_extract_currency_symbol (input_string , symbol ):
2048+ assert extract_currency_symbol (input_string , None ) == symbol
2049+
2050+
19802051@pytest .mark .parametrize (
19812052 "amount,amount_float" ,
19822053 (
0 commit comments