import unittest
import base64
import requests

'''
测试未通过的问题：
1.名字会识别错误（字形相近的字，例如：阎 and 阁）
2.上传90、270度正面图片,有时方向会识别错误（90度(1)识别为3;  270度(3)识别为2）
3.上传倾斜角度的图片（非0、90、180、360度）:名字和生日识别错误
4.模糊的图片
    1.太模糊的识别不出（101）
    2.较模糊的能识别出一部分(较清楚的一部分)，剩下模糊的部分字段为空
5.身份证地址无省的，会根据市自动识别出对应的省
'''

url = 'http://localhost:8080'


def send_request(image_path, image_type):
    with open(image_path, 'rb') as f:
        img_str: str = base64.encodebytes(f.read()).decode('utf-8')
        r = requests.post(f'{url}/ocr_system/idcard', json={'image': img_str, 'image_type': image_type})
        print(r.json())
        return r.json()


class TestIdCardOcr(unittest.TestCase):
    def test_it_works(self):
        self.assertEqual(1, 1, 'it not works')

    def test_test(self):
        image_path = './images/test.jpeg'
        r = send_request(image_path, '0')
        self.assertEqual(r['status'], '000', 'status case error')
        self.assertEqual(r['result']['orientation'], 0, 'orientation case error')
        self.assertEqual(r['result']['name']['text'], '阎妍', 'name case error')
        self.assertEqual(r['result']['id']['text'], '410802198701190046', 'id case error')

    def test_front_90(self):
        image_path = './images/front-90.jpg'
        r = send_request(image_path, '0')
        self.assertEqual(r['status'], '000', 'status case error')
        self.assertEqual(r['result']['orientation'], 1, 'orientation case error')
        self.assertEqual(r['result']['name']['text'], '刘新建', 'name case error')
        self.assertEqual(r['result']['id']['text'], '412902197906291716', 'id case error')
        self.assertEqual(r['result']['birthday']['text'], '1979年6月29日', 'birthday case error')
        self.assertEqual(r['result']['address_province']['text'], '河南省', 'address_province case error')
        self.assertEqual(r['result']['address_city']['text'], '邓州市', 'address_city case error')
        self.assertEqual(r['result']['address_region']['text'], '白牛乡', 'address_region case error')
        self.assertEqual(r['result']['address_detail']['text'], '故事村故事桥111号', 'address_detail case error')

    def test_front_0(self):
        image_path = './images/front-0.png'
        r = send_request(image_path, '0')
        self.assertEqual(r['status'], '000', 'status case error')

    def test_front_180(self):
        image_path = './images/front-180.png'
        r = send_request(image_path, '0')
        self.assertEqual(r['status'], '000', 'status case error')
        self.assertEqual(r['result']['orientation'], 2, 'orientation case error')
        self.assertEqual(r['result']['name']['text'], '阎妍', 'name case error')
        self.assertEqual(r['result']['id']['text'], '410802198701190046', 'id case error')

    def test_front_270(self):
        image_path = './images/front-270.png'
        r = send_request(image_path, '0')
        self.assertEqual(r['status'], '000', 'status case error')
        self.assertEqual(r['result']['orientation'], 3, 'orientation case error')
        self.assertEqual(r['result']['name']['text'], '阎妍', 'name case error')
        self.assertEqual(r['result']['id']['text'], '410802198701190046', 'id case error')

    def test_long_exp(self):
        image_path = './images/long-exp.png'
        r = send_request(image_path, '1')
        self.assertEqual(r['status'], '000', 'status case error')
        self.assertEqual(r['result']['expire_date']['text'], '20180227-长期', 'expire date case error')

    def test_tilt1(self):
        image_path = './images/tilt.jpg'
        r = send_request(image_path, '0')
        self.assertEqual(r['status'], '000', 'status case error')
        self.assertEqual(r['result']['orientation'], 0 or 1, 'orientation case error')

    def test_tilt2(self):
        image_path = './images/tilt.jpg'
        r = send_request(image_path, '0')
        self.assertEqual(r['result']['name']['text'], '奥巴马', 'name case error')
        self.assertEqual(r['result']['birthday']['text'], '1961年8月4日', 'birthday case error')

    def test_tilt3(self):
        image_path = './images/tilt.jpg'
        r = send_request(image_path, '0')
        self.assertEqual(r['result']['birthday']['text'], '1961年8月4日', 'birthday case error')

    def test_vague(self):
        image_path = './images/vague.jpg'
        r = send_request(image_path, '0')
        self.assertEqual(r['status'], '101', 'status case error')

    def test_bank(self):
        image_path = './images/bank.jpg'
        r = send_request(image_path, '0')
        self.assertEqual(r['status'], '101', 'status case error')

    def test_back(self):
        image_path = './images/back.jpeg'
        r = send_request(image_path, '1')
        self.assertEqual(r['status'], '000', 'status case error')
        self.assertEqual(r['result']['orientation'], 0, 'orientation case error')
        self.assertEqual(r['result']['expire_date']['text'], '20220114-20420114', 'expire_date case error')

    def test_back_270(self):
        image_path = './images/back-270.jpg'
        r = send_request(image_path, '1')
        self.assertEqual(r['status'], '000', 'status case error')
        self.assertEqual(r['result']['orientation'], 3, 'orientation case error')
        self.assertEqual(r['result']['expire_date']['text'], '20220114-20420114', 'expire_date case error')

    def test_back_180(self):
        image_path = './images/back-180.jpg'
        r = send_request(image_path, '1')
        self.assertEqual(r['status'], '000', 'status case error')
        self.assertEqual(r['result']['orientation'], 2, 'orientation case error')
        self.assertEqual(r['result']['expire_date']['text'], '20220114-20420114', 'expire_date case error')

    def test_back_90(self):
        image_path = './images/back-90.jpg'
        r = send_request(image_path, '1')
        self.assertEqual(r['status'], '000', 'status case error')
        self.assertEqual(r['result']['orientation'], 1, 'orientation case error')
        self.assertEqual(r['result']['expire_date']['text'], '20220114-20420114', 'expire_date case error')

    def test_back_vague(self):
        image_path = './images/back-vague.jpeg'
        r = send_request(image_path, '1')
        self.assertEqual(r['status'], '000', 'status case error')
        self.assertEqual(r['result']['orientation'], 0, 'orientation case error')
        self.assertEqual(r['result']['expire_date']['text'], '20120724-20220724', 'expire_date case error')

    def test_front_00(self):
        image_path = './images/front-00.png'
        r = send_request(image_path, '0')
        self.assertEqual(r['status'], '000', 'status case error')
        self.assertEqual(r['result']['orientation'], 0, 'orientation case error')
        self.assertEqual(r['result']['name']['text'], '张志学', 'name case error')
        self.assertEqual(r['result']['gender']['text'], '男', 'gender case error')
        self.assertEqual(r['result']['ethnicity']['text'], '汉', 'ethnicity case error')
        self.assertEqual(r['result']['id']['text'], '230103198602230916', 'id case error')
        self.assertEqual(r['result']['birthday']['text'], '1986年2月23日', 'birthday case error')
        self.assertEqual(r['result']['address_province']['text'], '', 'address_province case error')
        self.assertEqual(r['result']['address_city']['text'], '哈尔滨市', 'address_city case error')
        self.assertEqual(r['result']['address_region']['text'], '南岗区', 'address_region case error')
        self.assertEqual(r['result']['address_detail']['text'], '人和街75号5单元601户', 'address_detail case error')

    def test_front_900(self):
        image_path = './images/front-900.jpeg'
        r = send_request(image_path, '0')
        self.assertEqual(r['status'], '000', 'status case error')
        self.assertEqual(r['result']['orientation'], 1, 'orientation case error')

    def test_front_1800(self):
        image_path = './images/front-1800.jpeg'
        r = send_request(image_path, '0')
        self.assertEqual(r['status'], '000', 'status case error')
        self.assertEqual(r['result']['orientation'], 2, 'orientation case error')
        self.assertEqual(r['result']['name']['text'], '张志学', 'name case error')
        self.assertEqual(r['result']['gender']['text'], '男', 'gender case error')
        self.assertEqual(r['result']['ethnicity']['text'], '汉', 'ethnicity case error')
        self.assertEqual(r['result']['id']['text'], '230103198602230916', 'id case error')
        self.assertEqual(r['result']['birthday']['text'], '1986年2月23日', 'birthday case error')
        self.assertEqual(r['result']['address_province']['text'], '', 'address_province case error')
        self.assertEqual(r['result']['address_city']['text'], '哈尔滨市', 'address_city case error')
        self.assertEqual(r['result']['address_region']['text'], '南岗区', 'address_region case error')
        self.assertEqual(r['result']['address_detail']['text'], '人和街75号5单元601户', 'address_detail case error')

    def test_front_2700(self):
        image_path = './images/front-1800.jpeg'
        r = send_request(image_path, '0')
        self.assertEqual(r['status'], '000', 'status case error')
        self.assertEqual(r['result']['orientation'], 3, 'orientation case error')
        self.assertEqual(r['result']['name']['text'], '张志学', 'name case error')
        self.assertEqual(r['result']['gender']['text'], '男', 'gender case error')
        self.assertEqual(r['result']['ethnicity']['text'], '汉', 'ethnicity case error')
        self.assertEqual(r['result']['id']['text'], '230103198602230916', 'id case error')
        self.assertEqual(r['result']['birthday']['text'], '1986年2月23日', 'birthday case error')
        self.assertEqual(r['result']['address_province']['text'], '', 'address_province case error')
        self.assertEqual(r['result']['address_city']['text'], '哈尔滨市', 'address_city case error')
        self.assertEqual(r['result']['address_region']['text'], '南岗区', 'address_region case error')
        self.assertEqual(r['result']['address_detail']['text'], '人和街75号5单元601户', 'address_detail case error')

    def test_front_901(self):
        image_path = './images/front-901.jpeg'
        r = send_request(image_path, '0')
        self.assertEqual(r['status'], '000', 'status case error')
        self.assertEqual(r['result']['name']['text'], '李萌', 'name case error')
        self.assertEqual(r['result']['gender']['text'], '女', 'gender case error')
        self.assertEqual(r['result']['ethnicity']['text'], '汉', 'ethnicity case error')
        self.assertEqual(r['result']['id']['text'], '230623200210090646', 'id case error')
        self.assertEqual(r['result']['birthday']['text'], '2002年10月9日', 'birthday case error')
        self.assertEqual(r['result']['address_province']['text'], '黑龙江省', 'address_province case error')
        self.assertEqual(r['result']['address_city']['text'], '大庆市', 'address_city case error')
        self.assertEqual(r['result']['address_region']['text'], '萨尔图区', 'address_region case error')
        self.assertEqual(r['result']['address_detail']['text'], '刚军大街3-10号5门302室', 'address_detail case error')

    def test_long_name(self):
        image_path = './images/long_name.png'
        r = send_request(image_path, '0')
        self.assertEqual(r['status'], '000', 'status case error')
        self.assertEqual(r['result']['name']['text'], '叶干别尔地·坎吉巴依', 'name case error')
        self.assertEqual(r['result']['gender']['text'], '男', 'gender case error')
        self.assertEqual(r['result']['ethnicity']['text'], '柯尔克孜', 'ethnicity case error')
        self.assertEqual(r['result']['id']['text'], '653024199812031239', 'id case error')
        self.assertEqual(r['result']['birthday']['text'], '1998年12月3日', 'birthday case error')
        self.assertEqual(r['result']['address_province']['text'], '江苏省', 'address_province case error')
        self.assertEqual(r['result']['address_city']['text'], '南京市', 'address_city case error')
        self.assertEqual(r['result']['address_region']['text'], '栖霞区', 'address_region case error')
        self.assertEqual(r['result']['address_detail']['text'], '刚军大街3-文苑路1号', 'address_detail case error')

    def test_long_name_0(self):
        image_path = './images/birth.png'
        r = send_request(image_path, '0')
