test_unit.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import pytest
  2. from magic_pdf.libs.boxbase import _is_in_or_part_overlap, _is_in_or_part_overlap_with_area_ratio, _is_in, \
  3. _is_part_overlap
  4. from magic_pdf.libs.commons import mymax, join_path, get_top_percent_list
  5. from magic_pdf.libs.path_utils import parse_s3path
  6. class Testpy():
  7. # 输入一个列表,如果列表空返回0,否则返回最大元素
  8. @pytest.mark.parametrize("list_input, target_num",
  9. [
  10. # ([0, 0, 0, 0], 0),
  11. # ([0], 0),
  12. # ([1, 2, 5, 8, 4], 8),
  13. # ([], 0),
  14. # ([1.1, 7.6, 1.009, 9.9], 9.9),
  15. ([1.0 * 10 ** 2, 3.5 * 10 ** 3, 0.9 * 10 ** 6], 0.9 * 10 ** 6),
  16. ])
  17. def test_list_max(self, list_input: list, target_num) -> None:
  18. """
  19. list_input: 输入列表元素,元素均为数字类型
  20. """
  21. assert target_num == mymax(list_input)
  22. # 连接多个参数生成路径信息,使用"/"作为连接符,生成的结果需要是一个合法路径
  23. @pytest.mark.parametrize("path_input, target_path", [
  24. # (['https:', '', 'www.baidu.com'], 'https://www.baidu.com'),
  25. # (['https:', 'www.baidu.com'], 'https:/www.baidu.com'),
  26. (['D:', 'file', 'pythonProject', 'demo' + '.py'], 'D:/file/pythonProject/demo.py'),
  27. ])
  28. def test_join_path(self, path_input: list, target_path: str) -> None:
  29. """
  30. path_input: 输入path的列表,列表元素均为字符串
  31. """
  32. assert target_path == join_path(*path_input)
  33. # 获取列表中前百分之多少的元素
  34. @pytest.mark.parametrize("num_list, percent, target_num_list", [
  35. # ([], 0.75, []),
  36. # ([-5, -10, 9, 3, 7, -7, 0, 23, -1, -11], 0.8, [23, 9, 7, 3, 0, -1, -5, -7]),
  37. # ([-5, -10, 9, 3, 7, -7, 0, 23, -1, -11], 0, []),
  38. ([-5, -10, 9, 3, 7, -7, 0, 23, -1, -11, 28], 0.8, [28, 23, 9, 7, 3, 0, -1, -5])
  39. ])
  40. def test_get_top_percent_list(self, num_list: list, percent: float, target_num_list: list) -> None:
  41. """
  42. num_list: 数字列表,列表元素为数字
  43. percent: 占比,float, 向下取证
  44. """
  45. assert target_num_list == get_top_percent_list(num_list, percent)
  46. # 输入一个s3路径,返回bucket名字和其余部分(key)
  47. @pytest.mark.parametrize("s3_path, target_data", [
  48. # ("s3://bucket/path/to/my/file.txt", "bucket"),
  49. # ("/path/to/my/file1.txt", "path"),
  50. ("bucket/path/to/my/file2.txt", "bucket"),
  51. # ("file2.txt", "False")
  52. ])
  53. def test_parse_s3path(self, s3_path: str, target_data: str):
  54. """
  55. s3_path: s3路径
  56. 如果为无效路径,则返回对应的bucket名字和其余部分
  57. 如果为异常路径 例如:file2.txt,则报异常
  58. """
  59. out_keys = parse_s3path(s3_path)
  60. assert target_data == out_keys[0]
  61. # 2个box是否处于包含或者部分重合关系。
  62. # 如果某边界重合算重合。
  63. # 部分边界重合,其他在内部也算包含
  64. @pytest.mark.parametrize("box1, box2, target_bool", [
  65. ((120, 133, 223, 248), (128, 168, 269, 295), True),
  66. # ((137, 53, 245, 157), (134, 11, 200, 147), True), # 部分重合
  67. # ((137, 56, 211, 116), (140, 66, 202, 199), True), # 部分重合
  68. # ((42, 34, 69, 65), (42, 34, 69, 65), True), # 部分重合
  69. # ((39, 63, 87, 106), (37, 66, 85, 109), True), # 部分重合
  70. # ((13, 37, 55, 66), (7, 46, 49, 75), True), # 部分重合
  71. # ((56, 83, 85, 104), (64, 85, 93, 106), True), # 部分重合
  72. # ((12, 53, 48, 94), (14, 53, 50, 94), True), # 部分重合
  73. # ((43, 54, 93, 131), (55, 82, 77, 106), True), # 包含
  74. # ((63, 2, 134, 71), (72, 43, 104, 78), True), # 包含
  75. # ((25, 57, 109, 127), (26, 73, 49, 95), True), # 包含
  76. # ((24, 47, 111, 115), (34, 81, 58, 106), True), # 包含
  77. # ((34, 8, 105, 83), (76, 20, 116, 45), True), # 包含
  78. ])
  79. def test_is_in_or_part_overlap(self, box1: tuple, box2: tuple, target_bool: bool) -> None:
  80. """
  81. box1: 坐标数组
  82. box2: 坐标数组
  83. """
  84. assert target_bool == _is_in_or_part_overlap(box1, box2)
  85. # 如果box1在box2内部,返回True
  86. # 如果是部分重合的,则重合面积占box1的比例大于阈值时候返回True
  87. @pytest.mark.parametrize("box1, box2, target_bool", [
  88. # ((35, 28, 108, 90), (47, 60, 83, 96), True), # 包含 box1 up box2, box2 多半,box1少半
  89. # ((65, 151, 92, 177), (49, 99, 105, 198), True), # 包含 box1 in box2
  90. # ((80, 62, 112, 84), (74, 40, 144, 111), True), # 包含 box1 in box2
  91. # ((65, 88, 127, 144), (92, 102, 131, 139), False), # 包含 box2 多半,box1约一半
  92. # ((92, 102, 131, 139), (65, 88, 127, 144), True), # 包含 box1 多半
  93. # ((100, 93, 199, 168), (169, 126, 198, 165), False), # 包含 box2 in box1
  94. # ((26, 75, 106, 172), (65, 108, 90, 128), False), # 包含 box2 in box1
  95. # ((28, 90, 77, 126), (35, 84, 84, 120), True), # 相交 box1多半,box2多半
  96. # ((37, 6, 69, 52), (28, 3, 60, 49), True), # 相交 box1多半,box2多半
  97. ((94, 29, 133, 60), (84, 30, 123, 61), True), # 相交 box1多半,box2多半
  98. ])
  99. def test_is_in_or_part_overlap_with_area_ratio(self, box1: tuple, box2: tuple, target_bool: bool) -> None:
  100. out_bool = _is_in_or_part_overlap_with_area_ratio(box1, box2)
  101. assert target_bool == out_bool
  102. # box1在box2内部或者box2在box1内部返回True。如果部分边界重合也算作包含。
  103. @pytest.mark.parametrize("box1, box2, target_bool", [
  104. # ((), (), False),
  105. # ((65, 151, 92, 177), (49, 99, 105, 198), True), # 包含 box1 in box2
  106. # ((80, 62, 112, 84), (74, 40, 144, 111), True), # 包含 box1 in box2
  107. # ((76, 140, 154, 277), (121, 326, 192, 384), False), # 分离
  108. # ((65, 88, 127, 144), (92, 102, 131, 139), False), # 包含 box2 多半,box1约一半
  109. # ((92, 102, 131, 139), (65, 88, 127, 144), False), # 包含 box1 多半
  110. # ((68, 94, 118, 120), (68, 90, 118, 122), True), # 包含,box1 in box2 两边x相切
  111. # ((69, 94, 118, 120), (68, 90, 118, 122), True), # 包含,box1 in box2 一边x相切
  112. ((69, 114, 118, 122), (68, 90, 118, 122), True), # 包含,box1 in box2 一边y相切
  113. # ((100, 93, 199, 168), (169, 126, 198, 165), True), # 包含 box2 in box1 Error
  114. # ((26, 75, 106, 172), (65, 108, 90, 128), True), # 包含 box2 in box1 Error
  115. # ((38, 94, 122, 120), (68, 94, 118, 120), True), # 包含,box2 in box1 两边y相切 Error
  116. # ((68, 34, 118, 158), (68, 94, 118, 120), True), # 包含,box2 in box1 两边x相切 Error
  117. # ((68, 34, 118, 158), (68, 94, 84, 120), True), # 包含,box2 in box1 一边x相切 Error
  118. # ((27, 94, 118, 158), (68, 94, 84, 120), True), # 包含,box2 in box1 一边y相切 Error
  119. ])
  120. def test_is_in(self, box1: tuple, box2: tuple, target_bool: bool) -> None:
  121. assert target_bool == _is_in(box1, box2)
  122. # 仅仅是部分包含关系,返回True,如果是完全包含关系则返回False
  123. @pytest.mark.parametrize("box1, box2, target_bool", [
  124. ((65, 151, 92, 177), (49, 99, 105, 198), False), # 包含 box1 in box2
  125. # ((80, 62, 112, 84), (74, 40, 144, 111), False), # 包含 box1 in box2
  126. # ((76, 140, 154, 277), (121, 326, 192, 384), False), # 分离 Error
  127. # ((65, 88, 127, 144), (92, 102, 131, 139), True), # 包含 box2 多半,box1约一半
  128. # ((92, 102, 131, 139), (65, 88, 127, 144), True), # 包含 box1 多半
  129. # ((68, 94, 118, 120), (68, 90, 118, 122), False), # 包含,box1 in box2 两边x相切
  130. # ((69, 94, 118, 120), (68, 90, 118, 122), False), # 包含,box1 in box2 一边x相切
  131. # ((69, 114, 118, 122), (68, 90, 118, 122), False), # 包含,box1 in box2 一边y相切
  132. # ((26, 75, 106, 172), (65, 108, 90, 128), False), # 包含 box2 in box1 Error
  133. # ((38, 94, 122, 120), (68, 94, 118, 120), False), # 包含,box2 in box1 两边y相切 Error
  134. # ((68, 34, 118, 158), (68, 94, 84, 120), False), # 包含,box2 in box1 一边x相切 Error
  135. ])
  136. def test_is_part_overlap(self, box1: tuple, box2: tuple, target_bool: bool) -> None:
  137. assert target_bool == _is_part_overlap(box1, box2)
  138. # python magicpdf.py --pdf C:\Users\renpengli\Desktop\test\testpdf.pdf