This repository has been archived by the owner on Apr 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
run_tests.py
executable file
·81 lines (56 loc) · 2.27 KB
/
run_tests.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Script to verify the behavior of a running jp_tokenizer server.
"""
from json import dumps
from json import loads
from typing import ByteString
from typing import Dict
from typing import Text
from unittest import TestCase
from urllib.request import Request
from urllib.request import urlopen
def _post(url: Text, body: ByteString, headers: Dict) -> Text:
request = Request(url, body, headers)
with urlopen(request) as connection:
response = connection.read().decode('utf-8')
return response
def _post_text(url: Text, payload: Text) -> Text:
headers = {'Content-Type': 'text/plain; charset=utf-8'}
body = payload.encode('utf-8')
return _post(url, body, headers)
def _post_json(url: Text, payload: Dict) -> Text:
headers = {'Content-Type': 'application/json; charset=utf-8'}
body = dumps(payload).encode('utf-8')
return _post(url, body, headers)
class ServerTests(TestCase):
url_base = 'http://localhost'
def test_tokenize(self):
url = self.url_base + '/tokenize'
sentence = 'サザエさんは走った'
response = _post_text(url, sentence)
self.assertEqual(len(response.split(' ')), 4)
def test_lemmatize(self):
url = self.url_base + '/lemmatize'
sentence = 'サザエさんは走った'
response = _post_text(url, sentence)
self.assertEqual(len(response.split(' ')), 4)
def test_tokenize_batch(self):
url = self.url_base + '/batch/tokenize'
sentences = {'sentences': [{'jp': 'サザエさんは走った'}]}
response = loads(_post_json(url, sentences))
self.assertEqual(len(response['sentences']), 1)
self.assertEqual(len(response['sentences'][0]['tokens']), 4)
def test_lemmatize_batch(self):
url = self.url_base + '/batch/lemmatize'
sentences = {'sentences': [{'jp': 'サザエさんは走った'}]}
response = loads(_post_json(url, sentences))
self.assertEqual(len(response['sentences']), 1)
self.assertEqual(len(response['sentences'][0]['lemmas']), 4)
if __name__ == '__main__':
from sys import argv
from unittest import main
if len(argv) > 1:
url_base = argv.pop(1)
ServerTests.url_base = url_base
main()