forked from qiajigou/flask-zipkin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_flask_zipkin.py
68 lines (49 loc) · 1.79 KB
/
test_flask_zipkin.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
from __future__ import with_statement
# import sys
import unittest
import mock
import flask
from flask_zipkin import Zipkin
def handle_transport(*kargs, **kwargs):
return True
class FlaskZipkinTestCase(unittest.TestCase):
def setUp(self):
app = flask.Flask(__name__)
app.testing = True
app.config['ZIPKIN_DISABLE'] = False
app.config['ZIPKIN_DSN'] = 'whatever'
bp = flask.Blueprint('bp', __name__, url_prefix='/bp')
@app.route('/foo')
def foo():
return 'bar'
@bp.route('/bar')
def bar():
return 'foo'
z = Zipkin()
z._transport_handler = handle_transport
self.z = z
self.app = app
self.app.register_blueprint(bp)
z.init_app(self.app)
@mock.patch('py_zipkin.zipkin.create_endpoint')
def test_normal_get(self, create_endpoint_mock):
rv = self.app.test_client().get('/foo')
assert rv.status_code == 200
@mock.patch('py_zipkin.zipkin.create_endpoint')
def test_flask_request_context_with_app(self, create_endpoint_mock):
with self.app.test_request_context('/foo'):
self.app.preprocess_request()
assert flask.g._zipkin_span
assert flask.g._zipkin_span.span_name == 'foo.GET'
@mock.patch('py_zipkin.zipkin.create_endpoint')
def test_flask_request_context_with_bp(self, create_endpoint_mock):
with self.app.test_request_context('/bp/bar'):
self.app.preprocess_request()
assert flask.g._zipkin_span
assert flask.g._zipkin_span.span_name == 'bp.bar.GET'
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(FlaskZipkinTestCase))
return suite
if __name__ == '__main__':
unittest.main(defaultTest='suite')