Skip to content

Commit 20cc621

Browse files
committed
fix structure test
1 parent 650c0c9 commit 20cc621

File tree

5 files changed

+37
-36
lines changed

5 files changed

+37
-36
lines changed

tests/PiperOperator.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@
1515
HEADERS = {"Content-Type": "application/json"}
1616
NER_RESPONSE_KEY = 'body'
1717

18+
1819
class PiperOperatorException(BaseException):
1920
def __init__(self, msg):
2021
pass
2122
# logger.exception(msg)
22-
2323

2424

2525
class FileLoadException(PiperOperatorException):
2626
def __init__(self, fn):
2727
self.fn = fn
28-
super().__init__(f'file {fn} can`t be loaded')
28+
super().__init__(f'file {fn} can`t be loaded')
2929

3030

3131
class JSONGetKeyException(PiperOperatorException):
3232
def __init__(self, key):
3333
self.key = key
34-
super().__init__(f'can`t get JSON key {key}')
34+
super().__init__(f'can`t get JSON key {key}')
3535

3636

3737
class NoAvailableModelsException(PiperOperatorException):
@@ -46,7 +46,8 @@ def get_data_by_key_from_response(cur_response, k):
4646
v = j.get(k)
4747
return v
4848

49-
def get_data_by_key_from_url(url, key, post=True, data=None, file_name=""):
49+
50+
def get_data_by_key_from_url(url, key, post=True, data=None, file_name=""):
5051
try:
5152
if post:
5253
if file_name:
@@ -68,7 +69,7 @@ def get_data_by_key_from_url(url, key, post=True, data=None, file_name=""):
6869
return val
6970
else:
7071
return cur_response
71-
72+
7273
else:
7374
cur_response = requests.get(url, headers=HEADERS, data=data)
7475
cur_response.raise_for_status()
@@ -83,7 +84,7 @@ def get_data_by_key_from_url(url, key, post=True, data=None, file_name=""):
8384
logger.exception(f'can`t get key from response: {cjke}')
8485

8586
except Exception as e:
86-
logger.exception(f'error while processing url {url}: {e}')
87+
logger.exception(f'error while processing url {url}: {e}')
8788

8889

8990
class PiperNLPWorker():
@@ -106,15 +107,14 @@ def __init__(self, base_url):
106107
# get named entitys from text url
107108
self.url_spacy_get_NE = f'{self.base_url}/extract_named_ents'
108109

109-
110110
def get_available_ner_models(self):
111111
return get_data_by_key_from_url(self.url_spacy_all_models, 'available_models', post=False)
112112

113113
def set_current_spacy_model(self, model):
114-
return get_data_by_key_from_url(self.url_spacy_set_model, '', post=True, data=json.dumps({'model_name':model}))
114+
return get_data_by_key_from_url(self.url_spacy_set_model, '', post=True, data=json.dumps({'model_name': model}))
115115

116116
def get_named_ent_from_text(self, txt):
117-
resp = get_data_by_key_from_url(self.url_spacy_get_NE, 'result', post=False, data=json.dumps({'txt':txt}))
117+
resp = get_data_by_key_from_url(self.url_spacy_get_NE, 'result', post=False, data=json.dumps({'txt': txt}))
118118
logger.debug(f'url is {resp}, response is {resp}')
119119
if NER_RESPONSE_KEY in resp.keys():
120120
named_ents = resp.get(NER_RESPONSE_KEY)
@@ -138,9 +138,9 @@ def get_text_from_file(self, fn):
138138
def set_tesseract_config(self, conf):
139139
return get_data_by_key_from_url(self.url_tsrct_cfg, '', post=True, data=json.dumps(conf))
140140

141+
141142
if __name__ == '__main__':
142143
piper_worker = PiperNLPWorker('http://localhost:8788')
143-
144144

145145
amodels = piper_worker.get_available_ner_models()
146146
print('all models', amodels)
@@ -162,16 +162,12 @@ def set_tesseract_config(self, conf):
162162
logger.exception(e)
163163
# pprint(resp)
164164

165-
166165
txt = piper_worker.get_text_from_file('/home/pavel/repo/piper_new/piper/tests/ocr_data.pdf')
167166
logger.info(f'txt {txt}')
168167

169-
170168
ts_conf = dict()
171169
ts_conf['ts_lang'] = 'eng'
172170
ts_conf['ts_config_row'] = rf'--oem 1 --psm 6'
173171

174172
resp = piper_worker.set_tesseract_config(ts_conf)
175173
logger.info(resp)
176-
177-

tests/__init__.py

Whitespace-only changes.

tests/running_piper_test.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
# pytest -vs tests/running_piper_test.py::TestDocker
22
import requests
33

4+
from piper.envs import VirtualEnv
5+
46
main_app_url = f'http://localhost:8788'
57

6-
class TestDocker():
7-
'''
8+
9+
class TestDocker:
10+
"""
811
Docker container API test. Methods:
912
health_check
1013
run
11-
'''
14+
"""
15+
1216
def test_health_check(self):
1317
url = f'{main_app_url}/health_check'
1418
print(url)
@@ -29,10 +33,13 @@ def test_run(self):
2933
assert need_result == result.get('value')
3034

3135

32-
class TestVenv():
33-
'''
36+
class TestVenv:
37+
"""
3438
venv container API test. Methods:
3539
dummy
36-
'''
37-
def test_dummy(self):
38-
assert 1 / 0
40+
"""
41+
42+
with VirtualEnv() as env:
43+
env.copy_struct_project()
44+
env.create_files_for_venv()
45+
env.create_files_for_tests()

tests/tsrct_test.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,20 @@
2121
main_app_url = f'http://localhost:8788'
2222
file_path = Path(__file__).parent
2323

24+
2425
# pytest -vs tests/tsrct_test.py::TestTesseract::test_recognizer
25-
class TestTesseract():
26-
'''
26+
class TestTesseract:
27+
"""
2728
Docker container API test. Methods:
2829
test_recognizer_jpg
2930
test_recognizer_pdf
3031
health_check
31-
'''
32+
"""
3233

3334
def test_recognizer_jpg(self):
34-
'''
35+
"""
3536
jpg file recognize test
36-
'''
37+
"""
3738
fn = file_path.joinpath('ocr_data.jpg')
3839
url = f'{main_app_url}/recognize'
3940

@@ -49,29 +50,27 @@ def test_recognizer_jpg(self):
4950
except Exception as e:
5051
pytest.raises(Exception)
5152

52-
5353
def test_recognizer_pdf(self):
54-
'''
54+
"""
5555
pdf file recognize test
56-
'''
56+
"""
5757
fn = file_path.joinpath('ocr_data.pdf')
5858
url = f'{main_app_url}/recognize'
5959

6060
result = tu.send_file_to_service(url, fn)
6161
print(result.status_code)
62-
assert result.status_code == 200
62+
assert result.status_code == 200
6363
try:
6464
data = result.json()
6565
print('data', data)
6666
assert len(data) != 0
6767
except Exception as e:
6868
pytest.raises(Exception)
6969

70-
7170
def test_health_check(self):
72-
'''
71+
"""
7372
health check test
74-
'''
73+
"""
7574
url = f'{main_app_url}/health_check'
7675
print(url)
7776
result = requests.post(url)

tests/use_case_folder_processing.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
SOURCE_FOLDER = file_path
3131
OUTPUT_FOLDER = file_path.joinpath('out')
3232

33-
3433
if __name__ == '__main__':
3534
cfg = get_configuration()
3635

@@ -43,7 +42,7 @@
4342
ts_conf = dict()
4443
ts_conf['ts_lang'] = 'eng'
4544

46-
for oem in cfg.available_OEM:
45+
for oem in cfg.available_OEM:
4746
for psm in cfg.available_PSM:
4847
# change tesseract config
4948
ts_conf['ts_config_row'] = rf'--oem {oem} --psm {psm}'

0 commit comments

Comments
 (0)