|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import unittest |
| 3 | + |
| 4 | +from nose.tools import eq_, ok_, assert_raises |
| 5 | + |
| 6 | +# project |
| 7 | +from ddtrace.ext import net |
| 8 | +from ddtrace.tracer import Tracer |
| 9 | +from ddtrace.contrib.flask_cache import get_traced_cache |
| 10 | + |
| 11 | +# 3rd party |
| 12 | +from flask import Flask |
| 13 | + |
| 14 | +# testing |
| 15 | +from ...test_tracer import DummyWriter |
| 16 | + |
| 17 | + |
| 18 | +class FlaskCacheWrapperTest(unittest.TestCase): |
| 19 | + SERVICE = "test-flask-cache" |
| 20 | + |
| 21 | + def test_cache_get_without_arguments(self): |
| 22 | + # initialize the dummy writer |
| 23 | + writer = DummyWriter() |
| 24 | + tracer = Tracer() |
| 25 | + tracer.writer = writer |
| 26 | + |
| 27 | + # create the TracedCache instance for a Flask app |
| 28 | + Cache = get_traced_cache(tracer, service=self.SERVICE) |
| 29 | + app = Flask(__name__) |
| 30 | + cache = Cache(app, config={"CACHE_TYPE": "simple"}) |
| 31 | + |
| 32 | + # make a wrong call |
| 33 | + with assert_raises(TypeError) as ex: |
| 34 | + cache.get() |
| 35 | + |
| 36 | + # ensure that the error is not caused by our tracer |
| 37 | + ok_("get()" in ex.exception.args[0]) |
| 38 | + ok_("argument" in ex.exception.args[0]) |
| 39 | + spans = writer.pop() |
| 40 | + # an error trace must be sent |
| 41 | + eq_(len(spans), 1) |
| 42 | + span = spans[0] |
| 43 | + eq_(span.service, self.SERVICE) |
| 44 | + eq_(span.resource, "get") |
| 45 | + eq_(span.name, "flask_cache.cmd") |
| 46 | + eq_(span.span_type, "cache") |
| 47 | + eq_(span.error, 1) |
| 48 | + |
| 49 | + def test_cache_set_without_arguments(self): |
| 50 | + # initialize the dummy writer |
| 51 | + writer = DummyWriter() |
| 52 | + tracer = Tracer() |
| 53 | + tracer.writer = writer |
| 54 | + |
| 55 | + # create the TracedCache instance for a Flask app |
| 56 | + Cache = get_traced_cache(tracer, service=self.SERVICE) |
| 57 | + app = Flask(__name__) |
| 58 | + cache = Cache(app, config={"CACHE_TYPE": "simple"}) |
| 59 | + |
| 60 | + # make a wrong call |
| 61 | + with assert_raises(TypeError) as ex: |
| 62 | + cache.set() |
| 63 | + |
| 64 | + # ensure that the error is not caused by our tracer |
| 65 | + ok_("set()" in ex.exception.args[0]) |
| 66 | + ok_("argument" in ex.exception.args[0]) |
| 67 | + spans = writer.pop() |
| 68 | + # an error trace must be sent |
| 69 | + eq_(len(spans), 1) |
| 70 | + span = spans[0] |
| 71 | + eq_(span.service, self.SERVICE) |
| 72 | + eq_(span.resource, "set") |
| 73 | + eq_(span.name, "flask_cache.cmd") |
| 74 | + eq_(span.span_type, "cache") |
| 75 | + eq_(span.error, 1) |
| 76 | + |
| 77 | + def test_cache_add_without_arguments(self): |
| 78 | + # initialize the dummy writer |
| 79 | + writer = DummyWriter() |
| 80 | + tracer = Tracer() |
| 81 | + tracer.writer = writer |
| 82 | + |
| 83 | + # create the TracedCache instance for a Flask app |
| 84 | + Cache = get_traced_cache(tracer, service=self.SERVICE) |
| 85 | + app = Flask(__name__) |
| 86 | + cache = Cache(app, config={"CACHE_TYPE": "simple"}) |
| 87 | + |
| 88 | + # make a wrong call |
| 89 | + with assert_raises(TypeError) as ex: |
| 90 | + cache.add() |
| 91 | + |
| 92 | + # ensure that the error is not caused by our tracer |
| 93 | + ok_("add()" in ex.exception.args[0]) |
| 94 | + ok_("argument" in ex.exception.args[0]) |
| 95 | + spans = writer.pop() |
| 96 | + # an error trace must be sent |
| 97 | + eq_(len(spans), 1) |
| 98 | + span = spans[0] |
| 99 | + eq_(span.service, self.SERVICE) |
| 100 | + eq_(span.resource, "add") |
| 101 | + eq_(span.name, "flask_cache.cmd") |
| 102 | + eq_(span.span_type, "cache") |
| 103 | + eq_(span.error, 1) |
| 104 | + |
| 105 | + def test_cache_delete_without_arguments(self): |
| 106 | + # initialize the dummy writer |
| 107 | + writer = DummyWriter() |
| 108 | + tracer = Tracer() |
| 109 | + tracer.writer = writer |
| 110 | + |
| 111 | + # create the TracedCache instance for a Flask app |
| 112 | + Cache = get_traced_cache(tracer, service=self.SERVICE) |
| 113 | + app = Flask(__name__) |
| 114 | + cache = Cache(app, config={"CACHE_TYPE": "simple"}) |
| 115 | + |
| 116 | + # make a wrong call |
| 117 | + with assert_raises(TypeError) as ex: |
| 118 | + cache.delete() |
| 119 | + |
| 120 | + # ensure that the error is not caused by our tracer |
| 121 | + ok_("delete()" in ex.exception.args[0]) |
| 122 | + ok_("argument" in ex.exception.args[0]) |
| 123 | + spans = writer.pop() |
| 124 | + # an error trace must be sent |
| 125 | + eq_(len(spans), 1) |
| 126 | + span = spans[0] |
| 127 | + eq_(span.service, self.SERVICE) |
| 128 | + eq_(span.resource, "delete") |
| 129 | + eq_(span.name, "flask_cache.cmd") |
| 130 | + eq_(span.span_type, "cache") |
| 131 | + eq_(span.error, 1) |
| 132 | + |
| 133 | + def test_cache_set_many_without_arguments(self): |
| 134 | + # initialize the dummy writer |
| 135 | + writer = DummyWriter() |
| 136 | + tracer = Tracer() |
| 137 | + tracer.writer = writer |
| 138 | + |
| 139 | + # create the TracedCache instance for a Flask app |
| 140 | + Cache = get_traced_cache(tracer, service=self.SERVICE) |
| 141 | + app = Flask(__name__) |
| 142 | + cache = Cache(app, config={"CACHE_TYPE": "simple"}) |
| 143 | + |
| 144 | + # make a wrong call |
| 145 | + with assert_raises(TypeError) as ex: |
| 146 | + cache.set_many() |
| 147 | + |
| 148 | + # ensure that the error is not caused by our tracer |
| 149 | + ok_("set_many()" in ex.exception.args[0]) |
| 150 | + ok_("argument" in ex.exception.args[0]) |
| 151 | + spans = writer.pop() |
| 152 | + # an error trace must be sent |
| 153 | + eq_(len(spans), 1) |
| 154 | + span = spans[0] |
| 155 | + eq_(span.service, self.SERVICE) |
| 156 | + eq_(span.resource, "set_many") |
| 157 | + eq_(span.name, "flask_cache.cmd") |
| 158 | + eq_(span.span_type, "cache") |
| 159 | + eq_(span.error, 1) |
0 commit comments