75 lines
1.9 KiB
Python
75 lines
1.9 KiB
Python
from app.utils.pinyin_abbr import (
|
||
build_name_pinyin_abbr,
|
||
normalize_name_pinyin_abbr,
|
||
resolve_name_pinyin_abbr,
|
||
)
|
||
|
||
|
||
def test_empty_and_whitespace():
|
||
assert build_name_pinyin_abbr("") == ""
|
||
assert build_name_pinyin_abbr(" ") == ""
|
||
assert normalize_name_pinyin_abbr(None) == ""
|
||
assert normalize_name_pinyin_abbr(" AbC ") == "abc"
|
||
|
||
|
||
def test_chinese_and_alnum():
|
||
# pypinyin FIRST_LETTER: 工g业y硫l酸s(非设计稿中的 gylls)
|
||
assert build_name_pinyin_abbr("55% 工业硫酸") == "55gyls"
|
||
# pypinyin: 苯b骈p三s氮d唑z + BTA(非设计稿中的 bstnzbta)
|
||
assert build_name_pinyin_abbr("苯骈三氮唑BTA") == "bpsdzbta"
|
||
assert build_name_pinyin_abbr("PAC") == "pac"
|
||
|
||
|
||
def test_truncate_to_100():
|
||
long_name = "测" * 120
|
||
out = build_name_pinyin_abbr(long_name)
|
||
assert len(out) == 100
|
||
|
||
|
||
def test_resolve_submitted_provided_nonempty():
|
||
assert (
|
||
resolve_name_pinyin_abbr(
|
||
name="工业硫酸",
|
||
submitted=" CustomAbbr ",
|
||
existing="old",
|
||
submitted_provided=True,
|
||
)
|
||
== "customabbr"
|
||
)
|
||
|
||
|
||
def test_resolve_submitted_provided_empty_generates():
|
||
assert (
|
||
resolve_name_pinyin_abbr(
|
||
name="PAC",
|
||
submitted=" ",
|
||
existing="old",
|
||
submitted_provided=True,
|
||
)
|
||
== "pac"
|
||
)
|
||
|
||
|
||
def test_resolve_not_submitted_keeps_existing():
|
||
assert (
|
||
resolve_name_pinyin_abbr(
|
||
name="工业硫酸",
|
||
submitted=None,
|
||
existing=" KeepMe ",
|
||
submitted_provided=False,
|
||
)
|
||
== "keepme"
|
||
)
|
||
|
||
|
||
def test_resolve_not_submitted_empty_existing_generates():
|
||
assert (
|
||
resolve_name_pinyin_abbr(
|
||
name="PAC",
|
||
submitted=None,
|
||
existing="",
|
||
submitted_provided=False,
|
||
)
|
||
== "pac"
|
||
)
|