File tree Expand file tree Collapse file tree 4 files changed +67
-0
lines changed
python_test_example/flask_app Expand file tree Collapse file tree 4 files changed +67
-0
lines changed Original file line number Diff line number Diff line change
1
+ import json
2
+
3
+ from flask import Flask , jsonify
4
+
5
+ from .service import Service
6
+
7
+ app = Flask (__name__ )
8
+
9
+ print ('Instantiate Service in app' )
10
+ service = Service ()
11
+
12
+ @app .route ('/' , methods = ['GET' ])
13
+ def index ():
14
+ message = {'Message' : 'ok' }
15
+ return jsonify (message )
16
+
17
+ @app .route ('/predict' , methods = ['POST' ])
18
+ def predict ():
19
+ print ('Call predict in app' )
20
+ target = 'A'
21
+ message = {'result' : service .predict (target )}
22
+ return jsonify (message )
Original file line number Diff line number Diff line change
1
+ import json
2
+ from unittest import TestCase , main
3
+
4
+ from flask_app .app import app
5
+
6
+ print ('In app_test' )
7
+
8
+ class AppTestCase (TestCase ):
9
+ def setUp (self ):
10
+ print ('Call setUp in AppTestCase' )
11
+ self .client = app .test_client ()
12
+
13
+ def test_index (self ):
14
+ response = self .client .get ('/' )
15
+ self .assertEqual (response .status_code , 200 )
16
+
17
+ def test_index_content (self ):
18
+ response = self .client .get ('/' )
19
+ self .assertEqual (response .content_type , 'application/json' )
20
+
21
+ def test_predict (self ):
22
+ response = self .client .get ('/predict' )
23
+ self .assertEqual (response .status_code , 200 )
24
+
25
+ def test_predict_content (self ):
26
+ response = self .client .get ('/predict' )
27
+ self .assertEqual (response .content_type , 'application/json' )
28
+
29
+ def test_predict_data (self ):
30
+ response = self .client .get ('/predict' )
31
+ self .assertIsInstance (json .loads (response .data )['result' ], float )
32
+
33
+ if __name__ == '__main__' :
34
+ main ()
Original file line number Diff line number Diff line change
1
+ from random import randint
2
+
3
+ print ('In service' )
4
+
5
+ class Service :
6
+ def __init__ (self ):
7
+ print ('Init Service' )
8
+
9
+ def predict (self , target : str ):
10
+ print ('Call predict in service' )
11
+ return randint (0 , 1000 ) / 1000.0
You can’t perform that action at this time.
0 commit comments