-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_fg.py
201 lines (165 loc) · 4.53 KB
/
test_fg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from flask import Flask, render_template
from flask_googletrans import translator
from googletrans import Translator
from os import path, mkdir, remove
from shutil import rmtree
from pytest import fixture
from atexit import register
from random import randint
from tempfile import TemporaryFile
from json import loads
from sys import version_info as V
text = 'something to say'
new_text = 'something new'
src = 'en'
dest = 'it'
dirs = ['templates', 'static']
translation = 'qualcosa da dire'
translation_es = 'algo que decir'
def toCleanUp():
for d in dirs:
if path.isdir(d):
rmtree(d)
if path.isfile('200'):
remove('200')
register(toCleanUp)
def toMimic(data):
for d in dirs:
if not path.isdir(d):
mkdir(d)
while True:
tFile = str(randint(1, 9999999)) + '.html'
if not path.isfile(tFile):
break
with open(path.join(dirs[0], tFile), 'w+') as file:
file.write(data)
return tFile
app = Flask(__name__)
eng = translator(app=app, route=True, cache=True)
@app.route('/translate')
def tran():
return render_template(
toMimic(
'{{ translate(text="%s", src="%s", dest=["%s"]) }}' % (
text, src, dest
)
)
)
@fixture
def client():
app.config['TESTING'] = True
app.config['SKIP'] = 'yes'
app.config['STATIC_FOLDER'] = 'static'
app.config['SERVER_NAME'] = 'localhost'
client = app.test_client()
yield client
def test_translate_template_initApp(client):
eng.init_app(app=app)
client.get('/translate').data
resp = client.get('/translate').data
assert resp.decode('utf8') == translation
def test__GtranRoute(client):
resp = loads(client.get(
'/gtran/%s/%s/%s' % (
src, dest, text
)).data
)['translation']
assert resp == translation
def test_translate_single_new_dest(client):
resp = eng.translate(
text=text,
src=src,
dest=['es']
)
assert resp == translation_es
def test_translate_multi(client):
def toDoRaise():
try:
resp = eng.translate(
text=text,
src=src,
dest=[
'en',
'it',
'es'
]
)
assert resp == {
'en': text,
'it': translation,
'es': translation_es
}
except Exception as e:
assert type(e) == AssertionError
toDoRaise()
eng.cache = False
eng.fail_safe = True
toDoRaise()
eng.cache = True
eng.fail_safe = False
def test_cache_translation(client):
with open(eng.file_name, 'w+') as f:
f.write('{"": {}}')
if path.isfile(eng.file_name):
remove(eng.file_name)
resp = client.get('/translate').data
assert resp.decode('utf8') == eng.STORAGE[text][dest]
def test_cache_tanslation_false(client):
try:
eng.file_name = '200'
eng.loadCache()
except Exception as e:
assert type(e) == IOError
def test_translate_multi_new_mod(client):
eng.STORAGE[text] = {'en': 'falsely modified'}
resp = eng.translate(
text=text,
dest=[dest, 'es'],
src=src
)
assert resp == {
dest: translation,
'es': translation_es
}
def test_translate_false_input(client):
try:
eng.translate(text=False)
except Exception as e:
assert type(e) == AttributeError
try:
eng.translate(text=text, src=False)
except Exception as e:
assert type(e) == TypeError
try:
eng.translate(text=text, src=src, dest=False)
except Exception as e:
assert type(e) == AttributeError
try:
eng.translate(text=text, src=src, dest=['200'])
except Exception as e:
assert type(e) == AttributeError
eng.fail_safe = True
try:
eng.translate(text=text, src=src, dest=['200'])
except Exception as e:
assert type(e) == AttributeError
def test_translator_false_input(client):
try:
translator(app=None)
except Exception as e:
assert type(e) == AttributeError
try:
translator(app=app, cache=200)
except Exception as e:
assert type(e) == AttributeError
remove(eng.file_name)
def test_translate_failsafe(client):
eng.fail_safe = True
eng.service_urls = ['fail_translation_test.not']
resp = eng.translate(
text=text,
src=src,
dest=['en']
)
assert eng.errors != []
assert resp == text