-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
106 lines (82 loc) · 3.55 KB
/
test.py
File metadata and controls
106 lines (82 loc) · 3.55 KB
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
import unittest
import os.path
import sys
from aiphilos.client import Client
from aiphilos.response import ResponseType
AIPHILOS_PY_SDK_TEST_USER = ""
AIPHILOS_PY_SDK_TEST_KEY = ""
class MainTests(unittest.TestCase):
def test_health(self):
client = Client()
client.set_auth_credentials(AIPHILOS_PY_SDK_TEST_USER, AIPHILOS_PY_SDK_TEST_KEY)
result = client.get_health()
self.assertEqual(result.status_code(), 200)
self.assertEqual(result.type(), ResponseType.HEALTH)
self.assertTrue("OK" in result.text())
def test_languages(self):
client = Client()
client.set_auth_credentials(AIPHILOS_PY_SDK_TEST_USER, AIPHILOS_PY_SDK_TEST_KEY)
result = client.get_languages()
self.assertEqual(result.status_code(), 200)
self.assertEqual(result.type(), ResponseType.LANGUAGES)
self.assertTrue("de-de" in result.text())
self.assertTrue("en-en" in result.text())
def test_parse_string_ok(self):
client = Client()
client.set_auth_credentials(AIPHILOS_PY_SDK_TEST_USER, AIPHILOS_PY_SDK_TEST_KEY)
result = client.parse_string("Ordner")
self.assertEqual(result.status_code(), 200)
self.assertEqual(result.type(), ResponseType.PARSE_STRING)
self.assertTrue("Schreibware" in result.text())
self.assertTrue("Verzeichnis" in result.text())
self.assertTrue("messagecode" in result.json())
self.assertEqual(result.json()["messagecode"], 0)
def test_parse_strings(self):
client = Client()
client.set_auth_credentials(AIPHILOS_PY_SDK_TEST_USER, AIPHILOS_PY_SDK_TEST_KEY)
result = client.parse_strings({"example_1": "Ordner leitz", "example_2": "tastatur"})
self.assertEqual(result.status_code(), 200)
self.assertTrue("example_1" in result.text())
self.assertTrue("Schreibware" in result.text())
self.assertTrue("example_2" in result.text())
self.assertTrue("Keyboard" in result.text())
def test_forbidden_without_creds(self):
client = Client()
result = client.parse_string("Ordner")
self.assertEqual(result.status_code(), 401)
def test_forbidden_invalid_creds(self):
client = Client()
client.set_auth_credentials("this-is-an-invalid-user@", "0")
result = client.parse_string("Ordner")
self.assertEqual(result.status_code(), 401)
def load_test_config():
global AIPHILOS_PY_SDK_TEST_USER
global AIPHILOS_PY_SDK_TEST_KEY
paths = [os.path.dirname(os.path.realpath(__file__)), "."]
files = ["test_creds", "test_creds.txt"]
for p in paths:
for f in files:
full = os.path.join(p, f)
if os.path.exists(full) and os.path.isfile(full):
with open(full, 'r') as creds:
user = creds.readline().strip()
if len(user) < 1:
continue
key = creds.readline().strip()
if len(key) < 1:
continue
AIPHILOS_PY_SDK_TEST_USER = user
AIPHILOS_PY_SDK_TEST_KEY = key
return True
return False
if __name__ == '__main__':
ok = load_test_config()
if ok:
unittest.main()
else:
hint = ("No API credentials for testing found.\n"
"Create a text file called 'test_creds.txt',\n"
"the first line containing the API user name,\n"
"the second line containing the key you wish to use.")
print(hint)
sys.exit(1)