From 14e0fe5e85fcb0b000f801e0eaabd729dda2c5c9 Mon Sep 17 00:00:00 2001 From: HitBlast Date: Mon, 9 Dec 2024 19:59:11 +0600 Subject: [PATCH] added test for synchronous versions --- tests/test_main.py | 95 ++++++-------- tests/test_main_async.py | 267 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 307 insertions(+), 55 deletions(-) create mode 100755 tests/test_main_async.py diff --git a/tests/test_main.py b/tests/test_main.py index 8f7e31c..ef6b542 100755 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -12,32 +12,28 @@ ) # Import local modules. -import pytest - import avro from avro.core import config # Test functions for this file. -@pytest.mark.asyncio -async def test_patterns_without_rules_from_config() -> NoReturn: +def test_patterns_without_rules_from_config() -> NoReturn: """ Tests all patterns from config that don't have rules. """ for pattern in config.DICT["avro"]["patterns"]: if "rules" not in pattern and pattern.get("find", None): - assert pattern["replace"] == await avro.parse(pattern["find"]) + assert pattern["replace"] == avro.parse_sync(pattern["find"]) -@pytest.mark.asyncio -async def test_patterns_without_rules_not_from_config() -> NoReturn: +def test_patterns_without_rules_not_from_config() -> NoReturn: """ Tests all patterns not from config that don't have rules. This test is done in addition to test_patterns_without_rules_from_config() to ensure that text - passed manually to avro.parse are properly parsed when they + passed manually to avro.parse_sync are properly parsed when they don't exact match a pattern that has no rules specified. """ @@ -49,11 +45,10 @@ async def test_patterns_without_rules_not_from_config() -> NoReturn: } for key, value in conjunctions.items(): - assert key == await avro.parse(value) + assert key == avro.parse_sync(value) -@pytest.mark.asyncio -async def test_patterns_numbers() -> NoReturn: +def test_patterns_numbers() -> NoReturn: """ Test patterns - numbers """ @@ -73,11 +68,10 @@ async def test_patterns_numbers() -> NoReturn: } for key, value in numbers.items(): - assert key == await avro.parse(value) + assert key == avro.parse_sync(value) -@pytest.mark.asyncio -async def test_patterns_punctuations() -> NoReturn: +def test_patterns_punctuations() -> NoReturn: """ Tests patterns - punctuations """ @@ -89,12 +83,11 @@ async def test_patterns_punctuations() -> NoReturn: } for key, value in punctuations.items(): - assert key == await avro.parse(value) - assert value == await avro.reverse(key) + assert key == avro.parse_sync(value) + assert value == avro.reverse_sync(key) -@pytest.mark.asyncio -async def test_patterns_with_rules_svaravarna() -> NoReturn: +def test_patterns_with_rules_svaravarna() -> NoReturn: """ Test patterns - with rules - svaravarna / shoroborno (derived from Bengali) """ @@ -106,7 +99,7 @@ async def test_patterns_with_rules_svaravarna() -> NoReturn: "ঈ": "I", "উ": "u", "ঊ": "oo", - "ঊ": "U", + "ঊ": "U", # noqa: F601 "এ": "e", "ঐ": "OI", "ও": "O", @@ -114,11 +107,10 @@ async def test_patterns_with_rules_svaravarna() -> NoReturn: } for key, value in svaravarna.items(): - assert key == await avro.parse(value) + assert key == avro.parse_sync(value) -@pytest.mark.asyncio -async def test_non_ascii() -> NoReturn: +def test_non_ascii() -> NoReturn: """ Test processor response for non-ASCII characters. Parse function should return any non-ASCII characters that is passed to it. @@ -133,25 +125,23 @@ async def test_non_ascii() -> NoReturn: } for key, value in non_ascii.items(): - assert key == await avro.parse(value) + assert key == avro.parse_sync(value) -@pytest.mark.asyncio -async def test_ascii() -> NoReturn: +def test_ascii() -> NoReturn: """ Test processor response for ASCII characters. Reverse function should return any ASCII characters that is passed to it. """ - assert "Avwg evsjvi gan MvB|" == await avro.reverse("Avwg evsjvi গান MvB|") + assert "Avwg evsjvi gan MvB|" == avro.reverse_sync("Avwg evsjvi গান MvB|") assert ( "Avwg amar Avwg‡K wPiw`b GB banglay Lyu‡R cvB!" - == await avro.reverse("Avwg আমার Avwg‡K wPiw`b GB বাংলায় Lyu‡R cvB!") + == avro.reverse_sync("Avwg আমার Avwg‡K wPiw`b GB বাংলায় Lyu‡R cvB!") ) -@pytest.mark.asyncio -async def test_words_with_punctuations() -> NoReturn: +def test_words_with_punctuations() -> NoReturn: """ Test parsing and reversing of words with punctuations. """ @@ -164,104 +154,99 @@ async def test_words_with_punctuations() -> NoReturn: } for key, value in test_words.items(): - assert key == await avro.parse(value) - assert value.lower() == await avro.reverse(key) + assert key == avro.parse_sync(value) + assert value.lower() == avro.reverse_sync(key) -@pytest.mark.asyncio -async def test_exceptions() -> NoReturn: +def test_exceptions() -> NoReturn: """ Test parsing and reversing of exceptions. """ - assert "আমি উইকিপিডিয়া আর ফেসবুক চালাই।" == await avro.parse( + assert "আমি উইকিপিডিয়া আর ফেসবুক চালাই।" == avro.parse_sync( "ami Wikipedia ar Facebook calai." ) - assert "ami Wikipedia ar Facebook chalai." == await avro.reverse( + assert "ami Wikipedia ar Facebook chalai." == avro.reverse_sync( "আমি উইকিপিডিয়া আর ফেসবুক চালাই।" ) -@pytest.mark.asyncio -async def test_conversion_bijoy_func() -> NoReturn: +def test_conversion_bijoy_func() -> NoReturn: """ Test conversion to Bijoy directly. """ # Regular Conversion. - assert "Avwg evsjvq Mvb MvB;" == await avro.to_bijoy("আমি বাংলায় গান গাই;") + assert "Avwg evsjvq Mvb MvB;" == avro.to_bijoy_sync("আমি বাংলায় গান গাই;") assert [ "Avwg evsjvi Mvb MvB|", "Avwg Avgvi Avwg‡K wPiw`b GB evsjvq Lyu‡R cvB!", - ] == await avro.to_bijoy( + ] == avro.to_bijoy_sync( "আমি বাংলার গান গাই।", "আমি আমার আমিকে চিরদিন এই বাংলায় খুঁজে পাই!" ) # Fail-safe Conversion. - assert "Hello, World!" == await avro.to_bijoy("Hello, World!") + assert "Hello, World!" == avro.to_bijoy_sync("Hello, World!") -@pytest.mark.asyncio -async def test_conversion_unicode_func() -> NoReturn: +def test_conversion_unicode_func() -> NoReturn: """ Test conversion to Unicode directly. """ # Regular Conversion. - assert "আমি বাংলায় গান গাই;" == await avro.to_unicode("Avwg evsjvh় Mvb MvB;") + assert "আমি বাংলায় গান গাই;" == avro.to_unicode_sync("Avwg evsjvh় Mvb MvB;") assert [ "আমি বাংলার গান গাই।", "আমি আমার আমিকে চিরদিন এই বাংলায় খুঁজে পাই!", - ] == await avro.to_unicode( + ] == avro.to_unicode_sync( "Avwg evsjvi Mvb MvB|", "Avwg Avgvi Avwg‡K wPiw`b GB evsjvq Lyu‡R cvB!" ) -@pytest.mark.asyncio -async def test_parse_sentences() -> NoReturn: +def test_parse_sentences() -> NoReturn: """ Test parsing of sentences (Unicode). """ # Default parsing. - assert "আমি বাংলায় গান গাই;" == await avro.parse("ami banglay gan gai;") + assert "আমি বাংলায় গান গাই;" == avro.parse_sync("ami banglay gan gai;") assert [ "আমি বাংলার গান গাই।", "আমি আমার আমিকে চিরদিন এই বাংলায় খুঁজে পাই।", - ] == await avro.parse( + ] == avro.parse_sync( "ami banglar gan gai.", "ami amar amike cirodin ei banglay khu^je pai." ) # Bijoy parsing. - assert "Avwg evsjvq Mvb MvB;" == await avro.parse( + assert "Avwg evsjvq Mvb MvB;" == avro.parse_sync( "ami banglay gan gai;", bijoy=True ) assert [ "Avwg evsjvi Mvb MvB|", "Avwg Avgvi Avwg‡K wPiw`b GB evsjvq Lyu‡R cvB!", - ] == await avro.parse( + ] == avro.parse_sync( "ami banglar gan gai.", "ami amar amike cirodin ei banglay khu^je pai!", bijoy=True, ) -@pytest.mark.asyncio -async def test_reverse_sentences() -> NoReturn: +def test_reverse_sentences() -> NoReturn: """ Test reversing of sentences (Unicode). """ # Default reversing. - assert "ami banglay gan gai." == await avro.reverse("আমি বাংলায় গান গাই।") + assert "ami banglay gan gai." == avro.reverse_sync("আমি বাংলায় গান গাই।") assert [ "rohim, tomake korim dakche. ekhon ki rowna debe?", "rowna dile amake bole zew.", - ] == await avro.reverse( + ] == avro.reverse_sync( "রহিম, তোমাকে করিম ডাকছে। এখন কি রওনা দেবে?", "রওনা দিলে আমাকে বলে যেও।" ) # Bijoy reversing. - assert "ami banglar gan gai." == await avro.reverse( + assert "ami banglar gan gai." == avro.reverse_sync( "Avwg evsjvi Mvb MvB|", from_bijoy=True ) diff --git a/tests/test_main_async.py b/tests/test_main_async.py new file mode 100755 index 0000000..db1a972 --- /dev/null +++ b/tests/test_main_async.py @@ -0,0 +1,267 @@ +# SPDX-License-Identifier: MIT + + +# Import first-party Python modules. +import os +import sys +from typing import NoReturn + +# Add support layer for accessing the primary package. +sys.path.append( + os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)) +) + +# Import local modules. +import pytest + +import avro +from avro.core import config + + +# Test functions for this file. +@pytest.mark.asyncio +async def test_patterns_without_rules_from_config() -> NoReturn: + """ + Tests all patterns from config that don't have rules. + """ + + for pattern in config.DICT["avro"]["patterns"]: + if "rules" not in pattern and pattern.get("find", None): + assert pattern["replace"] == await avro.parse(pattern["find"]) + + +@pytest.mark.asyncio +async def test_patterns_without_rules_not_from_config() -> NoReturn: + """ + Tests all patterns not from config that don't have rules. + + This test is done in addition to + test_patterns_without_rules_from_config() to ensure that text + passed manually to avro.parse are properly parsed when they + don't exact match a pattern that has no rules specified. + """ + + conjunctions = { + "ভ্ল": "bhl", + "ব্ধ": "bdh", + "ড্ড": "DD", + "স্তব্ধ বক": "stbdh bk", # Stunned stork! + } + + for key, value in conjunctions.items(): + assert key == await avro.parse(value) + + +@pytest.mark.asyncio +async def test_patterns_numbers() -> NoReturn: + """ + Test patterns - numbers + """ + + numbers = { + "০": "0", + "১": "1", + "২": "2", + "৩": "3", + "৪": "4", + "৫": "5", + "৬": "6", + "৭": "7", + "৮": "8", + "৯": "9", + "১১২": "112", + } + + for key, value in numbers.items(): + assert key == await avro.parse(value) + + +@pytest.mark.asyncio +async def test_patterns_punctuations() -> NoReturn: + """ + Tests patterns - punctuations + """ + + punctuations = { + "।": ".", + "।।": "..", + "...": "...", + } + + for key, value in punctuations.items(): + assert key == await avro.parse(value) + assert value == await avro.reverse(key) + + +@pytest.mark.asyncio +async def test_patterns_with_rules_svaravarna() -> NoReturn: + """ + Test patterns - with rules - svaravarna / shoroborno (derived from Bengali) + """ + + svaravarna = { + "অ": "o", + "আ": "a", + "ই": "i", + "ঈ": "I", + "উ": "u", + "ঊ": "oo", + "ঊ": "U", # noqa: F601 + "এ": "e", + "ঐ": "OI", + "ও": "O", + "ঔ": "OU", + } + + for key, value in svaravarna.items(): + assert key == await avro.parse(value) + + +@pytest.mark.asyncio +async def test_non_ascii() -> NoReturn: + """ + Test processor response for non-ASCII characters. + Parse function should return any non-ASCII characters that is passed to it. + """ + + # Mixed strings. + non_ascii = { + "ব": "ব", + "অভ্র": "অভ্র", + "বআবা গো": "বaba gO", + "আমি বাংলায় গান গাই": "aমি বাংলায় gaন গাi", + } + + for key, value in non_ascii.items(): + assert key == await avro.parse(value) + + +@pytest.mark.asyncio +async def test_ascii() -> NoReturn: + """ + Test processor response for ASCII characters. + Reverse function should return any ASCII characters that is passed to it. + """ + + assert "Avwg evsjvi gan MvB|" == await avro.reverse("Avwg evsjvi গান MvB|") + assert ( + "Avwg amar Avwg‡K wPiw`b GB banglay Lyu‡R cvB!" + == await avro.reverse("Avwg আমার Avwg‡K wPiw`b GB বাংলায় Lyu‡R cvB!") + ) + + +@pytest.mark.asyncio +async def test_words_with_punctuations() -> NoReturn: + """ + Test parsing and reversing of words with punctuations. + """ + + test_words = { + "আয়রে,": "ayre,", + "ভোলা;": "bhOla;", + "/খেয়াল": "/kheyal", + "খোলা|": "khOla|", + } + + for key, value in test_words.items(): + assert key == await avro.parse(value) + assert value.lower() == await avro.reverse(key) + + +@pytest.mark.asyncio +async def test_exceptions() -> NoReturn: + """ + Test parsing and reversing of exceptions. + """ + + assert "আমি উইকিপিডিয়া আর ফেসবুক চালাই।" == await avro.parse( + "ami Wikipedia ar Facebook calai." + ) + assert "ami Wikipedia ar Facebook chalai." == await avro.reverse( + "আমি উইকিপিডিয়া আর ফেসবুক চালাই।" + ) + + +@pytest.mark.asyncio +async def test_conversion_bijoy_func() -> NoReturn: + """ + Test conversion to Bijoy directly. + """ + + # Regular Conversion. + assert "Avwg evsjvq Mvb MvB;" == await avro.to_bijoy("আমি বাংলায় গান গাই;") + assert [ + "Avwg evsjvi Mvb MvB|", + "Avwg Avgvi Avwg‡K wPiw`b GB evsjvq Lyu‡R cvB!", + ] == await avro.to_bijoy( + "আমি বাংলার গান গাই।", "আমি আমার আমিকে চিরদিন এই বাংলায় খুঁজে পাই!" + ) + + # Fail-safe Conversion. + assert "Hello, World!" == await avro.to_bijoy("Hello, World!") + + +@pytest.mark.asyncio +async def test_conversion_unicode_func() -> NoReturn: + """ + Test conversion to Unicode directly. + """ + + # Regular Conversion. + assert "আমি বাংলায় গান গাই;" == await avro.to_unicode("Avwg evsjvh় Mvb MvB;") + assert [ + "আমি বাংলার গান গাই।", + "আমি আমার আমিকে চিরদিন এই বাংলায় খুঁজে পাই!", + ] == await avro.to_unicode( + "Avwg evsjvi Mvb MvB|", "Avwg Avgvi Avwg‡K wPiw`b GB evsjvq Lyu‡R cvB!" + ) + + +@pytest.mark.asyncio +async def test_parse_sentences() -> NoReturn: + """ + Test parsing of sentences (Unicode). + """ + + # Default parsing. + assert "আমি বাংলায় গান গাই;" == await avro.parse("ami banglay gan gai;") + assert [ + "আমি বাংলার গান গাই।", + "আমি আমার আমিকে চিরদিন এই বাংলায় খুঁজে পাই।", + ] == await avro.parse( + "ami banglar gan gai.", "ami amar amike cirodin ei banglay khu^je pai." + ) + + # Bijoy parsing. + assert "Avwg evsjvq Mvb MvB;" == await avro.parse( + "ami banglay gan gai;", bijoy=True + ) + assert [ + "Avwg evsjvi Mvb MvB|", + "Avwg Avgvi Avwg‡K wPiw`b GB evsjvq Lyu‡R cvB!", + ] == await avro.parse( + "ami banglar gan gai.", + "ami amar amike cirodin ei banglay khu^je pai!", + bijoy=True, + ) + + +@pytest.mark.asyncio +async def test_reverse_sentences() -> NoReturn: + """ + Test reversing of sentences (Unicode). + """ + + # Default reversing. + assert "ami banglay gan gai." == await avro.reverse("আমি বাংলায় গান গাই।") + assert [ + "rohim, tomake korim dakche. ekhon ki rowna debe?", + "rowna dile amake bole zew.", + ] == await avro.reverse( + "রহিম, তোমাকে করিম ডাকছে। এখন কি রওনা দেবে?", "রওনা দিলে আমাকে বলে যেও।" + ) + + # Bijoy reversing. + assert "ami banglar gan gai." == await avro.reverse( + "Avwg evsjvi Mvb MvB|", from_bijoy=True + )