Skip to content

Commit de1a16b

Browse files
committed
refactor iop test
1 parent dece47e commit de1a16b

File tree

8 files changed

+567
-975
lines changed

8 files changed

+567
-975
lines changed

src/tests/test_bench.py

Lines changed: 67 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,96 @@
11
from iop._director import _Director
22
from iop._utils import _Utils
3-
43
import timeit
54
import os
65

76
class TestBenchIoP:
7+
TEST_CASES = [
8+
{
9+
'name': 'Python BP to Python BO with Iris Message',
10+
'component': 'Python.BenchIoPProcess',
11+
'message_type': 'iris.Ens.StringRequest',
12+
'use_json': False
13+
},
14+
{
15+
'name': 'Python BP to ObjetScript BO with Iris Message',
16+
'component': 'Python.BenchIoPProcess.To.Cls',
17+
'message_type': 'iris.Ens.StringRequest',
18+
'use_json': False
19+
},
20+
{
21+
'name': 'Python BP to Python BO with Python Message',
22+
'component': 'Python.BenchIoPProcess',
23+
'message_type': 'msg.MyMessage',
24+
'use_json': True
25+
},
26+
{
27+
'name': 'Python BP to ObjetScript BO with Python Message',
28+
'component': 'Python.BenchIoPProcess.To.Cls',
29+
'message_type': 'msg.MyMessage',
30+
'use_json': True
31+
},
32+
{
33+
'name': 'ObjetScript BP to Python BO with Iris Message',
34+
'component': 'Bench.Process',
35+
'message_type': 'iris.Ens.StringRequest',
36+
'use_json': False
37+
},
38+
{
39+
'name': 'ObjetScript BP to ObjetScript BO with Iris Message',
40+
'component': 'Bench.Process.To.Cls',
41+
'message_type': 'iris.Ens.StringRequest',
42+
'use_json': False
43+
},
44+
{
45+
'name': 'ObjetScript BP to Python BO with Python Message',
46+
'component': 'Bench.Process',
47+
'message_type': 'msg.MyMessage',
48+
'use_json': True
49+
},
50+
{
51+
'name': 'ObjetScript BP to ObjetScript BO with Python Message',
52+
'component': 'Bench.Process.To.Cls',
53+
'message_type': 'msg.MyMessage',
54+
'use_json': True
55+
}
56+
]
857

9-
#before all tests
1058
@classmethod
1159
def setup_class(cls):
12-
# get abspath of 'src/tests/bench'
1360
path = os.path.abspath('src/tests/bench/settings.py')
14-
# migrate the production
1561
_Utils.migrate(path)
16-
# stop all productions
1762
_Director.stop_production()
18-
# set the default production
1963
_Director.set_default_production('Bench.Production')
20-
# start the production
2164
_Director.start_production()
22-
# create a list of results
2365
cls.results = []
2466

2567
def test_bench_iris(self):
26-
# this test is made to preload the production
2768
_Director.test_component('Python.BenchIoPProcess')
2869

29-
30-
def test_bench_iris_message(self):
31-
body = "test"
32-
result = timeit.timeit(lambda: _Director.test_component('Python.BenchIoPProcess','','iris.Ens.StringRequest',body), number=1)
33-
# set the result in the list
34-
name = 'Python BP to Python BO with Iris Message'
35-
self.results.append((name,result))
36-
# assert the result
37-
assert result > 0
38-
39-
def test_bench_iris_message_to_cls(self):
40-
body = "test"
41-
result = timeit.timeit(lambda: _Director.test_component('Python.BenchIoPProcess.To.Cls','','iris.Ens.StringRequest',body), number=1)
42-
# set the result in the list
43-
name = 'Python BP to ObjetScript BO with Iris Message'
44-
self.results.append((name,result))
45-
# assert the result
46-
assert result > 0
47-
48-
def test_bench_python_message(self):
49-
body = "test"
50-
result = timeit.timeit(lambda: _Director.test_component('Python.BenchIoPProcess','','msg.MyMessage',f'{{"message":"{body}"}}'), number=1)
51-
# set the result in the list
52-
name = 'Python BP to Python BO with Python Message'
53-
self.results.append((name,result))
54-
# assert the result
55-
assert result > 0
56-
57-
def test_bench_python_message_to_cls(self):
70+
def run_benchmark(self, test_case):
5871
body = "test"
59-
result = timeit.timeit(lambda: _Director.test_component('Python.BenchIoPProcess.To.Cls','','msg.MyMessage',f'{{"message":"{body}"}}'), number=1)
60-
# set the result in the list
61-
name = 'Python BP to ObjetScript BO with Python Message'
62-
self.results.append((name,result))
63-
# assert the result
72+
message = f'{{"message":"{body}"}}' if test_case['use_json'] else body
73+
result = timeit.timeit(
74+
lambda: _Director.test_component(
75+
test_case['component'],
76+
'',
77+
test_case['message_type'],
78+
message
79+
),
80+
number=1
81+
)
82+
self.results.append((test_case['name'], result))
6483
assert result > 0
6584

66-
def test_bench_cls_iris_message(self):
67-
body = "test"
68-
result = timeit.timeit(lambda: _Director.test_component('Bench.Process','','iris.Ens.StringRequest',body), number=1)
69-
# set the result in the list
70-
name = 'ObjetScript BP to Python BO with Iris Message'
71-
self.results.append((name,result))
72-
# assert the result
73-
assert result > 0
74-
75-
def test_bench_cls_iris_message_to_cls(self):
76-
body = "test"
77-
result = timeit.timeit(lambda: _Director.test_component('Bench.Process.To.Cls','','iris.Ens.StringRequest',body), number=1)
78-
# set the result in the list
79-
name = 'ObjetScript BP to ObjetScript BO with Iris Message'
80-
self.results.append((name,result))
81-
# assert the result
82-
assert result > 0
83-
84-
def test_bench_cls_python_message(self):
85-
body = "test"
86-
result = timeit.timeit(lambda: _Director.test_component('Bench.Process','','msg.MyMessage',f'{{"message":"{body}"}}'), number=1)
87-
# set the result in the list
88-
name = 'ObjetScript BP to Python BO with Python Message'
89-
self.results.append((name,result))
90-
# assert the result
91-
assert result > 0
92-
93-
def test_bench_cls_python_message_to_cls(self):
94-
body = "test"
95-
result = timeit.timeit(lambda: _Director.test_component('Bench.Process.To.Cls','','msg.MyMessage',f'{{"message":"{body}"}}'), number=1)
96-
# set the result in the list
97-
name = 'ObjetScript BP to ObjetScript BO with Python Message'
98-
self.results.append((name,result))
99-
# assert the result
100-
assert result > 0
85+
def test_all_benchmarks(self):
86+
for test_case in self.TEST_CASES:
87+
self.run_benchmark(test_case)
10188

102-
#after all tests
10389
@classmethod
10490
def teardown_class(cls):
105-
# stop all productions
10691
_Director.stop_production()
107-
# set the default production
10892
_Director.set_default_production('test')
109-
# write the results in a file
11093
current_dir = os.path.dirname(os.path.abspath(__file__))
111-
with open(os.path.join(current_dir,'bench','result.txt'),'w') as f:
112-
for name,result in cls.results:
94+
with open(os.path.join(current_dir, 'bench', 'result.txt'), 'w') as f:
95+
for name, result in cls.results:
11396
f.write(f'{name}: {result}\n')

src/tests/test_cli.py

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import json
55
import os
66
from grongier.pex._cli import main
7+
from iop._director import _Director
78

89
def test_help():
910
# test help
@@ -19,7 +20,7 @@ def test_default_with_name():
1920
except SystemExit as e:
2021
assert e.code == 0
2122
# assert the output
22-
assert _cli._Director.get_default_production() == 'UnitTest.Production'
23+
assert _Director.get_default_production() == 'UnitTest.Production'
2324

2425
def test_default_without_name():
2526
# test default
@@ -36,50 +37,81 @@ def test_cli_namespace():
3637

3738
def test_start():
3839
with patch('grongier.pex._director._Director.start_production_with_log') as mock_start:
39-
main(['-s', 'my_production'])
40+
try:
41+
main(['-s', 'my_production'])
42+
except SystemExit as e:
43+
assert e.code == 0
4044
mock_start.assert_called_once_with('my_production')
4145
with patch('grongier.pex._director._Director.start_production') as mock_start:
42-
main(['-s', 'my_production', '-D'])
46+
try:
47+
main(['-s', 'my_production', '-D'])
48+
except SystemExit as e:
49+
assert e.code == 0
4350
mock_start.assert_called_once_with('my_production')
4451

4552
def test_init():
4653
with patch('grongier.pex._utils._Utils.setup') as mock_setup:
47-
main(['-i'])
54+
try:
55+
main(['-i'])
56+
except SystemExit as e:
57+
assert e.code == 0
4858
mock_setup.assert_called_once_with(None)
4959

5060
def test_kill():
5161
with patch('grongier.pex._director._Director.shutdown_production') as mock_shutdown:
52-
main(['-k'])
62+
try:
63+
main(['-k'])
64+
except SystemExit as e:
65+
assert e.code == 0
5366
mock_shutdown.assert_called_once()
5467

5568
def test_restart():
5669
with patch('grongier.pex._director._Director.restart_production') as mock_restart:
57-
main(['-r'])
70+
try:
71+
main(['-r'])
72+
except SystemExit as e:
73+
assert e.code == 0
5874
mock_restart.assert_called_once()
5975

6076
def test_migrate_relative():
6177
with patch('grongier.pex._utils._Utils.migrate') as mock_migrate:
62-
main(['-m', 'settings.json'])
78+
try:
79+
main(['-m', 'settings.json'])
80+
except SystemExit as e:
81+
assert e.code == 0
6382
mock_migrate.assert_called_once_with(os.path.join(os.getcwd(), 'settings.json'))
6483

6584
def test_migrate_absolute():
6685
with patch('grongier.pex._utils._Utils.migrate') as mock_migrate:
67-
main(['-m', '/tmp/settings.json'])
86+
try:
87+
main(['-m', '/tmp/settings.json'])
88+
except SystemExit as e:
89+
assert e.code == 0
6890
mock_migrate.assert_called_once_with('/tmp/settings.json')
6991

7092
def test_stop():
7193
with patch('grongier.pex._director._Director.stop_production') as mock_stop:
7294
with patch('sys.stdout', new=StringIO()) as fake_out:
73-
main(['-S'])
95+
try:
96+
main(['-S'])
97+
except SystemExit as e:
98+
assert e.code == 0
7499
mock_stop.assert_called_once()
75100
assert fake_out.getvalue().strip() == 'Production UnitTest.Production stopped'
76101

77102
def test_test():
78103
with patch('grongier.pex._director._Director.test_component') as mock_test:
79-
main(['-t', 'my_test', '-C', 'MyClass', '-B', 'my_body'])
104+
try:
105+
main(['-t', 'my_test', '-C', 'MyClass', '-B', 'my_body'])
106+
except SystemExit as e:
107+
assert e.code == 0
80108
mock_test.assert_called_once_with('my_test', classname='MyClass', body='my_body')
81109

82110
def test_test_japanese():
83111
with patch('grongier.pex._director._Director.test_component') as mock_test:
84-
main(['-t', 'my_test', '-C', 'MyClass', '-B', 'あいうえお'])
112+
try:
113+
main(['-t', 'my_test', '-C', 'MyClass', '-B', 'あいうえお'])
114+
except SystemExit as e:
115+
assert e.code == 0
85116
mock_test.assert_called_once_with('my_test', classname='MyClass', body='あいうえお')
117+

0 commit comments

Comments
 (0)