forked from DataDog/datadog-lambda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tag_object.py
More file actions
196 lines (179 loc) · 7.02 KB
/
test_tag_object.py
File metadata and controls
196 lines (179 loc) · 7.02 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import unittest
from decimal import Decimal
try:
from unittest.mock import MagicMock, patch, call
except ImportError:
from mock import MagicMock, patch, call
from datadog_lambda.tag_object import tag_object
class TestTagObject(unittest.TestCase):
def test_tag_object(self):
payload = {
"hello": "world",
"anotherThing": {"foo": "bar", "blah": None, "nice": True},
"vals": [{"thingOne": 1}, {"thingTwo": 2}],
}
spanMock = MagicMock()
tag_object(spanMock, "function.request", payload)
spanMock.set_tag.assert_has_calls(
[
call("function.request.vals.0.thingOne", "1"),
call("function.request.vals.1.thingTwo", "2"),
call("function.request.hello", "world"),
call("function.request.anotherThing.blah", None),
call("function.request.anotherThing.foo", "bar"),
call("function.request.anotherThing.nice", "True"),
],
True,
)
def test_tag_object_max_depth(self):
payload = {
"hello": "world",
"level1": {
"level2_dict": {"level3": 3},
"level2_list": [None, True, "nice", {"l3": "v3"}],
"level2_bool": True,
"level2_int": 2,
},
"vals": [{"thingOne": 1}, {"thingTwo": 2}],
}
spanMock = MagicMock()
import datadog_lambda.tag_object as lib_ref
lib_ref.max_depth = 2 # setting up the test
tag_object(spanMock, "function.request", payload)
lib_ref.max_depth = 10 # revert the setup
spanMock.set_tag.assert_has_calls(
[
call("function.request.vals.0", "{'thingOne': 1}"),
call("function.request.vals.1", "{'thingTwo': 2}"),
call("function.request.hello", "world"),
call("function.request.level1.level2_dict", "{'level3': 3}"),
call(
"function.request.level1.level2_list",
"[None, True, 'nice', {'l3': 'v3'}]",
),
call("function.request.level1.level2_bool", "True"),
call("function.request.level1.level2_int", "2"),
],
True,
)
def test_tag_object_max_depth_0(self):
payload = {
"hello": "world",
"level1": {
"level2_dict": {"level3": 3},
"level2_list": [None, True, "nice", {"l3": "v3"}],
"level2_bool": True,
"level2_int": 2,
},
"vals": [{"thingOne": 1}, {"thingTwo": 2}],
}
spanMock = MagicMock()
import datadog_lambda.tag_object as lib_ref
lib_ref.max_depth = 0 # setting up the test
tag_object(spanMock, "function.request", payload)
lib_ref.max_depth = 10 # revert the setup
spanMock.set_tag.assert_has_calls(
[
call(
"function.request",
"{'hello': 'world', 'level1': {'level2_dict': {'level3': 3}, 'level2_list': [None, True, 'nice', {'l3': 'v3'}], 'level2_bool': True, 'level2_int': 2}, 'vals': [{'thingOne': 1}, {'thingTwo': 2}]}",
),
],
True,
)
def test_redacted_tag_object(self):
payload = {
"authorization": "world",
"anotherThing": {"password": "bar", "blah": None, "nice": True},
"vals": [{"thingOne": 1}, {"thingTwo": 2}],
}
spanMock = MagicMock()
tag_object(spanMock, "function.request", payload)
spanMock.set_tag.assert_has_calls(
[
call("function.request.vals.0.thingOne", "1"),
call("function.request.vals.1.thingTwo", "2"),
call("function.request.authorization", "redacted"),
call("function.request.anotherThing.blah", None),
call("function.request.anotherThing.password", "redacted"),
call("function.request.anotherThing.nice", "True"),
],
True,
)
def test_json_tag_object(self):
payload = {
"token": "world",
"jsonString": '{"stringifyThisJson":[{"here":"is","an":"object","number":1}]}',
}
spanMock = MagicMock()
tag_object(spanMock, "function.request", payload)
spanMock.set_tag.assert_has_calls(
[
call("function.request.token", "redacted"),
call("function.request.jsonString.stringifyThisJson.0.here", "is"),
call("function.request.jsonString.stringifyThisJson.0.an", "object"),
call("function.request.jsonString.stringifyThisJson.0.number", "1"),
],
True,
)
def test_unicode_tag_object(self):
payload = {
"token": "world",
"jsonString": '{"stringifyThisJson":[{"here":"is","an":"object","number":1}]}',
}
spanMock = MagicMock()
tag_object(spanMock, "function.request", payload)
spanMock.set_tag.assert_has_calls(
[
call("function.request.token", "redacted"),
call("function.request.jsonString.stringifyThisJson.0.here", "is"),
call("function.request.jsonString.stringifyThisJson.0.an", "object"),
call("function.request.jsonString.stringifyThisJson.0.number", "1"),
],
True,
)
def test_decimal_tag_object(self):
payload = {"myValue": Decimal(500.5)}
spanMock = MagicMock()
tag_object(spanMock, "function.request", payload)
spanMock.set_tag.assert_has_calls(
[
call("function.request.myValue", "500.5"),
],
True,
)
class CustomResponse(object):
"""
For example, chalice.app.Response class
"""
def __init__(self, body, headers=None, status_code: int = 200):
self.body = body
if headers is None:
headers = {}
self.headers = headers
self.status_code = status_code
def __str__(self):
return str(self.body)
class ResponseHasToDict(CustomResponse):
def to_dict(self):
return self.headers
def test_custom_response(self):
payload = self.CustomResponse({"hello": "world"}, {"key1": "val1"}, 200)
spanMock = MagicMock()
tag_object(spanMock, "function.response", payload)
spanMock.set_tag.assert_has_calls(
[
call("function.response", "{'hello': 'world'}"),
],
True,
)
def test_custom_response_to_dict(self):
payload = self.ResponseHasToDict({"hello": "world"}, {"key1": "val1"}, 200)
spanMock = MagicMock()
tag_object(spanMock, "function.response", payload)
spanMock.set_tag.assert_has_calls(
[
call("function.response.key1", "val1"),
],
True,
)