from flask import (Flask, request)
import opentracing
import sys
sys.path.append('../')
from flask_opentracing import FlaskTracer
import copy

app = Flask(__name__)
test_app = app.test_client()

empty_tracer = opentracing.Tracer()
tracer_with_attributes = FlaskTracer(empty_tracer, True, app, ['url', 'url_rule'])
tracer_without_attributes = FlaskTracer(empty_tracer, True, app)
tracer = FlaskTracer(empty_tracer)

@app.route('/test')
def check_test_works():
    return "Success"

@app.route('/another_test')
def check_two_tests_work():
    return "Success again"

def test_on():
    assert True

def test_span_creation():
    with app.test_request_context('/test'):
        app.preprocess_request()
        assert tracer_with_attributes.get_span(request)
        assert tracer_without_attributes.get_span(request)
        # clear current spans
        span = tracer_with_attributes.get_span(request)
        span2 = tracer_with_attributes.get_span(request)
        span.finish()
        span2.finish()
        tracer_with_attributes._current_spans = {}
        tracer_without_attributes._current_spans = {}

def test_span_deletion():
    assert not tracer_with_attributes._current_spans
    assert not tracer_without_attributes._current_spans
    test_app.get('/test')
    assert not tracer_with_attributes._current_spans
    assert not tracer_without_attributes._current_spans

def test_requests_distinct():
    with app.test_request_context('/test'):
        app.preprocess_request()
    with app.test_request_context('/test'):
        app.preprocess_request()
        span = tracer_with_attributes._current_spans.pop(request)
        span2 = tracer_without_attributes._current_spans.pop(request)
        assert span
        assert span2
        span.finish()
        span2.finish()
        assert not tracer_with_attributes.get_span(request)
        assert not tracer_without_attributes.get_span(request)
    # clear current spans
    for req in tracer_with_attributes._current_spans:
        tracer_with_attributes._current_spans[req].finish()
    tracer_with_attributes._current_spans = {}    
    for req in tracer_without_attributes._current_spans:
        tracer_without_attributes._current_spans[req].finish()
    tracer_without_attributes._current_spans = {}

# TODO: Tests for inject/extract; wait until new opentracing is on pypi
@@ -1189,168 +1189,56 @@
     
-assert not tracer_without_attributes._current_spans%0A    test_app.get('/test')%0A    assert not tracer_with_attributes._current_spans%0A    assert not tracer_without
+test_app.get('/test')%0A    assert not tracer_with
 _att
@@ -1458,78 +1458,18 @@
    s
-pan = tracer_with_attributes._current_spans.pop(request)%0A        span2
+econd_span
  = t
@@ -1470,35 +1470,32 @@
 an = tracer_with
-out
 _attributes._cur
@@ -1538,32 +1538,18 @@
 rt s
-pan%0A        assert 
+econd_
 span
-2
 %0A   
@@ -1558,34 +1558,18 @@
    s
-pan.finish()%0A        
+econd_
 span
-2
 .fin
@@ -1638,71 +1638,8 @@
 st)%0A
-        assert not tracer_without_attributes.get_span(request)%0A
     
@@ -1824,182 +1824,8 @@
 = %7B%7D
-    %0A    for req in tracer_without_attributes._current_spans:%0A        tracer_without_attributes._current_spans%5Breq%5D.finish()%0A    tracer_without_attributes._current_spans = %7B%7D
 %0A%0A# 

Edit tests