all_test.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import base64
  2. import unittest
  3. from dataclasses import dataclass
  4. from pathlib import Path
  5. import requests
  6. url = 'http://192.168.199.249:18081'
  7. def send_request(image_path, image_type):
  8. with open(image_path, 'rb') as f:
  9. img_str: str = base64.encodebytes(f.read()).decode('utf-8')
  10. r = requests.post(f'{url}/ocr_system/idcard', json={'image': img_str, 'image_type': image_type})
  11. print(r.json())
  12. return r.json()
  13. @dataclass
  14. class ResultItem:
  15. status: str
  16. orientation: int
  17. name: str
  18. id: str
  19. ethnicity: str
  20. gender: str
  21. birthday: str
  22. address: str
  23. class TestIdCard0(unittest.TestCase):
  24. def _helper(self, image_path, item: ResultItem):
  25. root = Path(__file__).parent
  26. image_path = str(root / image_path)
  27. r = send_request(image_path, '0')
  28. self.assertEqual(item, ResultItem(status=r['status'],
  29. orientation=r['result']['orientation'],
  30. name=r['result']['name']['text'],
  31. id=r['result']['id']['text'],
  32. ethnicity=r['result']['ethnicity']['text'],
  33. gender=r['result']['gender']['text'],
  34. birthday=r['result']['birthday']['text'],
  35. address=r['result']['address']['text']
  36. ))
  37. def test_01_0(self):
  38. image_path = '../images/all/0/01_0.jpg'
  39. self._helper(image_path, ResultItem(status='000',
  40. orientation=0,
  41. name='左翔宇',
  42. id='220204199910123017',
  43. ethnicity='蒙古',
  44. gender='男',
  45. birthday='1999年10月12日',
  46. address='吉林省吉林市船营区鑫安小区2-6-60号'))
  47. def test_02_90(self):
  48. image_path = '../images/all/0/02_90.jpg'
  49. self._helper(image_path, ResultItem(status='000',
  50. orientation=1,
  51. name='张开天',
  52. id='622301199710247376',
  53. ethnicity='汉',
  54. gender='男',
  55. birthday='1997年10月24日',
  56. address='甘肃省武威市凉州区九墩乡平乐村四组23号'))
  57. def test_03_0(self):
  58. image_path = '../images/all/0/03_0(minor).jpg'
  59. self._helper(image_path, ResultItem(status='000',
  60. orientation=0,
  61. name='张荣军',
  62. id='654122199712254017',
  63. ethnicity='锡伯',
  64. gender='男',
  65. birthday='1997年12月25日',
  66. address='新疆察布查尔锡伯自治县堆齐牛录乡杜林拜北街西二巷29号'))
  67. def test_04_180(self):
  68. image_path = '../images/all/0/04_180.jpg'
  69. self._helper(image_path, ResultItem(status='000',
  70. orientation=2,
  71. name='方势文',
  72. id='360428199610220096',
  73. ethnicity='汉',
  74. gender='男',
  75. birthday='1996年10月22日',
  76. address='江西省九江市都昌县都昌镇沿湖路238号'))
  77. def test_addreess(self):
  78. image_path = '../images/all/0/address.png'
  79. self._helper(image_path, ResultItem(status='000',
  80. orientation=0,
  81. name='王磊',
  82. id='150203199308092110',
  83. ethnicity='汉',
  84. gender='男',
  85. birthday='1993年08月09日',
  86. address='内蒙古包头市昆都仑区昆河镇刘二圪梁村393号'))
  87. def test_long_name(self):
  88. image_path = '../images/all/0/long_name.png'
  89. self._helper(image_path, ResultItem(status='000',
  90. orientation=0,
  91. name='叶干别尔地.坎吉巴依',
  92. id='653024199812031239',
  93. ethnicity='柯尔克孜',
  94. gender='男',
  95. birthday='1998年12月03日',
  96. address='南京市栖霞区文苑路1号'))
  97. def test_mohu(self):
  98. image_path = '../images/all/0/mohu.jpg'
  99. self._helper(image_path, ResultItem(status='000',
  100. orientation=0,
  101. name='田浩',
  102. id='640221199702060618',
  103. ethnicity='汉',
  104. gender='男',
  105. birthday='1997年02月06日',
  106. address='宁夏平罗县黄渠桥镇侯家梁村二队29'))
  107. def test_mohu_90(self):
  108. image_path = '../images/all/0/mohu_90.jpg'
  109. self._helper(image_path, ResultItem(status='000',
  110. orientation=1,
  111. name='王睿',
  112. id='421202199506051096',
  113. ethnicity='汉',
  114. gender='男',
  115. birthday='1995年06月05日',
  116. address='湖北省咸宁市咸安区温泉淦河大道12号'))
  117. def test_small(self):
  118. image_path = '../images/all/0/small.jpg'
  119. self._helper(image_path, ResultItem(status='000',
  120. orientation=0,
  121. name='董建',
  122. id='654121199802042775',
  123. ethnicity='回',
  124. gender='男',
  125. birthday='1998年02月04日',
  126. address='新疆伊宁县愉群翁乡伊克翁村216号'))
  127. def test_special1(self):
  128. image_path = '../images/all/0/special1.jpg'
  129. self._helper(image_path, ResultItem(status='000',
  130. orientation=0,
  131. name='宋宝磊',
  132. id='150430199905051616',
  133. ethnicity='汉',
  134. gender='男',
  135. birthday='1999年05月05日',
  136. address='内蒙古赤峰市敖汉旗四家子镇林家地村唐坊沟七组'))
  137. def test_special2(self):
  138. image_path = '../images/all/0/special2.jpg'
  139. self._helper(image_path, ResultItem(status='000',
  140. orientation=0,
  141. name='彭贤端',
  142. id='450922199412083669',
  143. ethnicity='汉',
  144. gender='女',
  145. birthday='1994年12月08日',
  146. address='广西陆川县清湖镇塘寨村新屋队62号'))
  147. import pytest
  148. @pytest.mark.parametrize(
  149. "image_path, status, orientation, expire_date",
  150. [
  151. ('../images/all/1/ 01_0.jpg', '000', 0, '20220511-20410511'),
  152. # ('../images/all/1/ 02_0.jpg', '000', 0, '20180531-20280531'),
  153. # ('../images/all/1/03_180.jpg', '000', 2, '20170109-20270109'),
  154. # ('../images/all/1/04_90.jpg', '000', 1, '20190715-20390715'),
  155. # ('../images/all/1/05-270.jpg', '000', 0, '20140320-20240320'),
  156. # ('../images/all/1/small.png', '000', 0, '20190620-20290620'),
  157. # ('../images/all/1/special.jpg', '000', 0, '20190813-20290813'),
  158. ]
  159. )
  160. def test_back_side(image_path, status, orientation, expire_date):
  161. root = Path(__file__).parent
  162. image_path = str(root / image_path)
  163. r = send_request(image_path, '1')
  164. if r['status'] == '000':
  165. assert (r['result']['expire_date']['text'], r['result']['orientation']) == (expire_date, orientation)
  166. else:
  167. assert r['status'] == status
  168. if __name__ == '__main__':
  169. unittest.main()